annotate audioio/IntegerTimeStretcher.cpp @ 3:e764bbf2b090 last-cc-copyright

* Move the current DenseThreeDimensionalModel to EditableDenseThreeDimensionalModel (wow!), and make DTDM an abstract base * Move FFTFuzzyAdapter to FFTModel as a new subclass of DTDM
author Chris Cannam
date Mon, 31 Jul 2006 17:05:18 +0000
parents cd5d7ff8ef38
children ee967635c728
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 Sonic Visualiser
Chris@0 5 An audio file viewer and annotation editor.
Chris@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@0 9 This program is free software; you can redistribute it and/or
Chris@0 10 modify it under the terms of the GNU General Public License as
Chris@0 11 published by the Free Software Foundation; either version 2 of the
Chris@0 12 License, or (at your option) any later version. See the file
Chris@0 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "IntegerTimeStretcher.h"
Chris@0 17
Chris@0 18 #include <iostream>
Chris@0 19 #include <cassert>
Chris@0 20
Chris@0 21 //#define DEBUG_INTEGER_TIME_STRETCHER 1
Chris@0 22
Chris@0 23 IntegerTimeStretcher::IntegerTimeStretcher(size_t ratio,
Chris@0 24 size_t maxProcessInputBlockSize,
Chris@0 25 size_t inputIncrement,
Chris@0 26 size_t windowSize,
Chris@0 27 WindowType windowType) :
Chris@0 28 m_ratio(ratio),
Chris@0 29 m_n1(inputIncrement),
Chris@0 30 m_n2(m_n1 * ratio),
Chris@0 31 m_wlen(std::max(windowSize, m_n2 * 2)),
Chris@0 32 m_inbuf(m_wlen),
Chris@0 33 m_outbuf(maxProcessInputBlockSize * ratio)
Chris@0 34 {
Chris@0 35 m_window = new Window<float>(windowType, m_wlen),
Chris@0 36
Chris@0 37 m_time = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * m_wlen);
Chris@0 38 m_freq = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * m_wlen);
Chris@0 39 m_dbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@0 40
Chris@0 41 m_plan = fftwf_plan_dft_1d(m_wlen, m_time, m_freq, FFTW_FORWARD, FFTW_ESTIMATE);
Chris@0 42 m_iplan = fftwf_plan_dft_c2r_1d(m_wlen, m_freq, m_dbuf, FFTW_ESTIMATE);
Chris@0 43
Chris@0 44 m_mashbuf = new float[m_wlen];
Chris@0 45 for (int i = 0; i < m_wlen; ++i) {
Chris@0 46 m_mashbuf[i] = 0.0;
Chris@0 47 }
Chris@0 48 }
Chris@0 49
Chris@0 50 IntegerTimeStretcher::~IntegerTimeStretcher()
Chris@0 51 {
Chris@0 52 std::cerr << "IntegerTimeStretcher::~IntegerTimeStretcher" << std::endl;
Chris@0 53
Chris@0 54 fftwf_destroy_plan(m_plan);
Chris@0 55 fftwf_destroy_plan(m_iplan);
Chris@0 56
Chris@0 57 fftwf_free(m_time);
Chris@0 58 fftwf_free(m_freq);
Chris@0 59 fftwf_free(m_dbuf);
Chris@0 60
Chris@0 61 delete m_window;
Chris@0 62 delete m_mashbuf;
Chris@0 63 }
Chris@0 64
Chris@0 65 size_t
Chris@0 66 IntegerTimeStretcher::getProcessingLatency() const
Chris@0 67 {
Chris@0 68 return getWindowSize() - getInputIncrement();
Chris@0 69 }
Chris@0 70
Chris@0 71 void
Chris@0 72 IntegerTimeStretcher::process(float *input, float *output, size_t samples)
Chris@0 73 {
Chris@0 74 // We need to add samples from input to our internal buffer. When
Chris@0 75 // we have m_windowSize samples in the buffer, we can process it,
Chris@0 76 // move the samples back by m_n1 and write the output onto our
Chris@0 77 // internal output buffer. If we have (samples * ratio) samples
Chris@0 78 // in that, we can write m_n2 of them back to output and return
Chris@0 79 // (otherwise we have to write zeroes).
Chris@0 80
Chris@0 81 // When we process, we write m_wlen to our fixed output buffer
Chris@0 82 // (m_mashbuf). We then pull out the first m_n2 samples from that
Chris@0 83 // buffer, push them into the output ring buffer, and shift
Chris@0 84 // m_mashbuf left by that amount.
Chris@0 85
Chris@0 86 // The processing latency is then m_wlen - m_n2.
Chris@0 87
Chris@0 88 size_t consumed = 0;
Chris@0 89
Chris@0 90 #ifdef DEBUG_INTEGER_TIME_STRETCHER
Chris@0 91 std::cerr << "IntegerTimeStretcher::process(" << samples << ", consumed = " << consumed << "), writable " << m_inbuf.getWriteSpace() <<", readable "<< m_outbuf.getReadSpace() << std::endl;
Chris@0 92 #endif
Chris@0 93
Chris@0 94 while (consumed < samples) {
Chris@0 95
Chris@0 96 size_t writable = m_inbuf.getWriteSpace();
Chris@0 97 writable = std::min(writable, samples - consumed);
Chris@0 98
Chris@0 99 if (writable == 0) {
Chris@0 100 //!!! then what? I don't think this should happen, but
Chris@0 101 std::cerr << "WARNING: IntegerTimeStretcher::process: writable == 0" << std::endl;
Chris@0 102 break;
Chris@0 103 }
Chris@0 104
Chris@0 105 #ifdef DEBUG_INTEGER_TIME_STRETCHER
Chris@0 106 std::cerr << "writing " << writable << " from index " << consumed << " to inbuf, consumed will be " << consumed + writable << std::endl;
Chris@0 107 #endif
Chris@0 108 m_inbuf.write(input + consumed, writable);
Chris@0 109 consumed += writable;
Chris@0 110
Chris@0 111 while (m_inbuf.getReadSpace() >= m_wlen &&
Chris@0 112 m_outbuf.getWriteSpace() >= m_n2) {
Chris@0 113
Chris@0 114 // We know we have at least m_wlen samples available
Chris@0 115 // in m_inbuf. We need to peek m_wlen of them for
Chris@0 116 // processing, and then read m_n1 to advance the read
Chris@0 117 // pointer.
Chris@0 118
Chris@0 119 size_t got = m_inbuf.peek(m_dbuf, m_wlen);
Chris@0 120 assert(got == m_wlen);
Chris@0 121
Chris@0 122 processBlock(m_dbuf, m_mashbuf);
Chris@0 123
Chris@0 124 #ifdef DEBUG_INTEGER_TIME_STRETCHER
Chris@0 125 std::cerr << "writing first " << m_n2 << " from mashbuf, skipping " << m_n1 << " on inbuf " << std::endl;
Chris@0 126 #endif
Chris@0 127 m_inbuf.skip(m_n1);
Chris@0 128 m_outbuf.write(m_mashbuf, m_n2);
Chris@0 129
Chris@0 130 for (size_t i = 0; i < m_wlen - m_n2; ++i) {
Chris@0 131 m_mashbuf[i] = m_mashbuf[i + m_n2];
Chris@0 132 }
Chris@0 133 for (size_t i = m_wlen - m_n2; i < m_wlen; ++i) {
Chris@0 134 m_mashbuf[i] = 0.0f;
Chris@0 135 }
Chris@0 136 }
Chris@0 137
Chris@0 138 // std::cerr << "WARNING: IntegerTimeStretcher::process: writespace not enough for output increment (" << m_outbuf.getWriteSpace() << " < " << m_n2 << ")" << std::endl;
Chris@0 139 // }
Chris@0 140
Chris@0 141 #ifdef DEBUG_INTEGER_TIME_STRETCHER
Chris@0 142 std::cerr << "loop ended: inbuf read space " << m_inbuf.getReadSpace() << ", outbuf write space " << m_outbuf.getWriteSpace() << std::endl;
Chris@0 143 #endif
Chris@0 144 }
Chris@0 145
Chris@0 146 if (m_outbuf.getReadSpace() < samples * m_ratio) {
Chris@0 147 std::cerr << "WARNING: IntegerTimeStretcher::process: not enough data (yet?) (" << m_outbuf.getReadSpace() << " < " << (samples * m_ratio) << ")" << std::endl;
Chris@0 148 size_t fill = samples * m_ratio - m_outbuf.getReadSpace();
Chris@0 149 for (size_t i = 0; i < fill; ++i) {
Chris@0 150 output[i] = 0.0;
Chris@0 151 }
Chris@0 152 m_outbuf.read(output + fill, m_outbuf.getReadSpace());
Chris@0 153 } else {
Chris@0 154 #ifdef DEBUG_INTEGER_TIME_STRETCHER
Chris@0 155 std::cerr << "enough data - writing " << samples * m_ratio << " from outbuf" << std::endl;
Chris@0 156 #endif
Chris@0 157 m_outbuf.read(output, samples * m_ratio);
Chris@0 158 }
Chris@0 159
Chris@0 160 #ifdef DEBUG_INTEGER_TIME_STRETCHER
Chris@0 161 std::cerr << "IntegerTimeStretcher::process returning" << std::endl;
Chris@0 162 #endif
Chris@0 163 }
Chris@0 164
Chris@0 165 void
Chris@0 166 IntegerTimeStretcher::processBlock(float *buf, float *out)
Chris@0 167 {
Chris@0 168 size_t i;
Chris@0 169
Chris@0 170 // buf contains m_wlen samples; out contains enough space for
Chris@0 171 // m_wlen * ratio samples (we mix into out, rather than replacing)
Chris@0 172
Chris@0 173 #ifdef DEBUG_INTEGER_TIME_STRETCHER
Chris@0 174 std::cerr << "IntegerTimeStretcher::processBlock" << std::endl;
Chris@0 175 #endif
Chris@0 176
Chris@0 177 m_window->cut(buf);
Chris@0 178
Chris@0 179 for (i = 0; i < m_wlen/2; ++i) {
Chris@0 180 float temp = buf[i];
Chris@0 181 buf[i] = buf[i + m_wlen/2];
Chris@0 182 buf[i + m_wlen/2] = temp;
Chris@0 183 }
Chris@0 184
Chris@0 185 for (i = 0; i < m_wlen; ++i) {
Chris@0 186 m_time[i][0] = buf[i];
Chris@0 187 m_time[i][1] = 0.0;
Chris@0 188 }
Chris@0 189
Chris@0 190 fftwf_execute(m_plan); // m_time -> m_freq
Chris@0 191
Chris@0 192 for (i = 0; i < m_wlen; ++i) {
Chris@0 193
Chris@0 194 float mag = sqrtf(m_freq[i][0] * m_freq[i][0] +
Chris@0 195 m_freq[i][1] * m_freq[i][1]);
Chris@0 196
Chris@0 197 float phase = atan2f(m_freq[i][1], m_freq[i][0]);
Chris@0 198
Chris@0 199 phase = phase * m_ratio;
Chris@0 200
Chris@0 201 float real = mag * cosf(phase);
Chris@0 202 float imag = mag * sinf(phase);
Chris@0 203 m_freq[i][0] = real;
Chris@0 204 m_freq[i][1] = imag;
Chris@0 205 }
Chris@0 206
Chris@0 207 fftwf_execute(m_iplan); // m_freq -> in, inverse fft
Chris@0 208
Chris@0 209 for (i = 0; i < m_wlen/2; ++i) {
Chris@0 210 float temp = buf[i] / m_wlen;
Chris@0 211 buf[i] = buf[i + m_wlen/2] / m_wlen;
Chris@0 212 buf[i + m_wlen/2] = temp;
Chris@0 213 }
Chris@0 214
Chris@0 215 m_window->cut(buf);
Chris@0 216
Chris@0 217 int div = m_wlen / m_n2;
Chris@0 218 if (div > 1) div /= 2;
Chris@0 219 for (i = 0; i < m_wlen; ++i) {
Chris@0 220 buf[i] /= div;
Chris@0 221 }
Chris@0 222
Chris@0 223 for (i = 0; i < m_wlen; ++i) {
Chris@0 224 out[i] += buf[i];
Chris@0 225 }
Chris@0 226 }