annotate audioio/PhaseVocoderTimeStretcher.cpp @ 16:3715efc38f95

* substantial enhancements to time stretcher: -- use putInput/getOutput methods to ensure the audio source always feeds it enough input, avoiding underruns due to rounding error -- add a percussion detector and an optional "Sharpen" toggle to the main window, which invokes a very basic variable speed timestretcher
author Chris Cannam
date Wed, 13 Sep 2006 17:17:42 +0000
parents cc566264c935
children 67d54627efd3
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@14 16 #include "PhaseVocoderTimeStretcher.h"
Chris@0 17
Chris@0 18 #include <iostream>
Chris@0 19 #include <cassert>
Chris@0 20
Chris@14 21 //#define DEBUG_PHASE_VOCODER_TIME_STRETCHER 1
Chris@0 22
Chris@16 23 PhaseVocoderTimeStretcher::PhaseVocoderTimeStretcher(size_t channels,
Chris@16 24 float ratio,
Chris@16 25 bool sharpen,
Chris@15 26 size_t maxProcessInputBlockSize) :
Chris@16 27 m_channels(channels),
Chris@16 28 m_ratio(ratio),
Chris@16 29 m_sharpen(sharpen)
Chris@0 30 {
Chris@16 31 m_wlen = 1024;
Chris@16 32
Chris@15 33 if (ratio < 1) {
Chris@16 34 if (ratio < 0.4) {
Chris@16 35 m_n1 = 1024;
Chris@16 36 m_wlen = 2048;
Chris@16 37 } else if (ratio < 0.8) {
Chris@16 38 m_n1 = 512;
Chris@16 39 } else {
Chris@16 40 m_n1 = 256;
Chris@16 41 }
Chris@16 42 if (m_sharpen) {
Chris@16 43 m_n1 /= 2;
Chris@16 44 m_wlen = 2048;
Chris@16 45 }
Chris@15 46 m_n2 = m_n1 * ratio;
Chris@15 47 } else {
Chris@16 48 if (ratio > 2) {
Chris@16 49 m_n2 = 512;
Chris@16 50 m_wlen = 4096;
Chris@16 51 } else if (ratio > 1.6) {
Chris@16 52 m_n2 = 384;
Chris@16 53 m_wlen = 2048;
Chris@16 54 } else {
Chris@16 55 m_n2 = 256;
Chris@16 56 }
Chris@16 57 if (m_sharpen) {
Chris@16 58 m_n2 /= 2;
Chris@16 59 if (m_wlen < 2048) m_wlen = 2048;
Chris@16 60 }
Chris@15 61 m_n1 = m_n2 / ratio;
Chris@15 62 }
Chris@16 63
Chris@16 64 m_window = new Window<float>(HanningWindow, m_wlen);
Chris@15 65
Chris@16 66 m_prevPhase = new float *[m_channels];
Chris@16 67 m_prevAdjustedPhase = new float *[m_channels];
Chris@16 68 if (m_sharpen) m_prevMag = new float *[m_channels];
Chris@16 69 else m_prevMag = 0;
Chris@16 70 m_prevPercussiveCount = new int[m_channels];
Chris@15 71
Chris@16 72 m_dbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@0 73 m_time = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * m_wlen);
Chris@0 74 m_freq = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * m_wlen);
Chris@16 75
Chris@0 76 m_plan = fftwf_plan_dft_1d(m_wlen, m_time, m_freq, FFTW_FORWARD, FFTW_ESTIMATE);
Chris@0 77 m_iplan = fftwf_plan_dft_c2r_1d(m_wlen, m_freq, m_dbuf, FFTW_ESTIMATE);
Chris@0 78
Chris@16 79 m_inbuf = new RingBuffer<float> *[m_channels];
Chris@16 80 m_outbuf = new RingBuffer<float> *[m_channels];
Chris@16 81 m_mashbuf = new float *[m_channels];
Chris@16 82
Chris@16 83 m_modulationbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@16 84
Chris@16 85 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 86
Chris@16 87 m_prevPhase[c] = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@16 88 m_prevAdjustedPhase[c] = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@16 89
Chris@16 90 if (m_sharpen) {
Chris@16 91 m_prevMag[c] = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@16 92 }
Chris@16 93
Chris@16 94 m_inbuf[c] = new RingBuffer<float>(m_wlen);
Chris@16 95 m_outbuf[c] = new RingBuffer<float>
Chris@16 96 (lrintf((maxProcessInputBlockSize + m_wlen) * ratio));
Chris@16 97
Chris@16 98 m_mashbuf[c] = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@16 99
Chris@16 100 for (int i = 0; i < m_wlen; ++i) {
Chris@16 101 m_mashbuf[c][i] = 0.0;
Chris@16 102 m_prevPhase[c][i] = 0.0;
Chris@16 103 m_prevAdjustedPhase[c][i] = 0.0;
Chris@16 104 if (m_sharpen) m_prevMag[c][i] = 0.0;
Chris@16 105 }
Chris@16 106
Chris@16 107 m_prevPercussiveCount[c] = 0;
Chris@16 108 }
Chris@16 109
Chris@0 110 for (int i = 0; i < m_wlen; ++i) {
Chris@16 111 m_modulationbuf[i] = 0.0;
Chris@0 112 }
Chris@16 113
Chris@16 114 std::cerr << "PhaseVocoderTimeStretcher: channels = " << channels
Chris@16 115 << ", ratio = " << ratio
Chris@16 116 << ", n1 = " << m_n1 << ", n2 = " << m_n2 << ", wlen = "
Chris@16 117 << m_wlen << ", max = " << maxProcessInputBlockSize
Chris@16 118 << ", outbuflen = " << m_outbuf[0]->getSize() << std::endl;
Chris@0 119 }
Chris@0 120
Chris@14 121 PhaseVocoderTimeStretcher::~PhaseVocoderTimeStretcher()
Chris@0 122 {
Chris@14 123 std::cerr << "PhaseVocoderTimeStretcher::~PhaseVocoderTimeStretcher" << std::endl;
Chris@0 124
Chris@0 125 fftwf_destroy_plan(m_plan);
Chris@0 126 fftwf_destroy_plan(m_iplan);
Chris@0 127
Chris@0 128 fftwf_free(m_time);
Chris@0 129 fftwf_free(m_freq);
Chris@0 130 fftwf_free(m_dbuf);
Chris@16 131
Chris@16 132 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 133
Chris@16 134 fftwf_free(m_mashbuf[c]);
Chris@16 135 fftwf_free(m_prevPhase[c]);
Chris@16 136 fftwf_free(m_prevAdjustedPhase[c]);
Chris@16 137 if (m_sharpen) fftwf_free(m_prevMag[c]);
Chris@16 138
Chris@16 139 delete m_inbuf[c];
Chris@16 140 delete m_outbuf[c];
Chris@16 141 }
Chris@16 142
Chris@13 143 fftwf_free(m_modulationbuf);
Chris@0 144
Chris@16 145 delete[] m_prevPhase;
Chris@16 146 delete[] m_prevAdjustedPhase;
Chris@16 147 if (m_sharpen) delete[] m_prevMag;
Chris@16 148 delete[] m_prevPercussiveCount;
Chris@16 149 delete[] m_inbuf;
Chris@16 150 delete[] m_outbuf;
Chris@16 151 delete[] m_mashbuf;
Chris@15 152
Chris@0 153 delete m_window;
Chris@0 154 }
Chris@0 155
Chris@0 156 size_t
Chris@14 157 PhaseVocoderTimeStretcher::getProcessingLatency() const
Chris@0 158 {
Chris@0 159 return getWindowSize() - getInputIncrement();
Chris@0 160 }
Chris@0 161
Chris@0 162 void
Chris@16 163 PhaseVocoderTimeStretcher::process(float **input, float **output, size_t samples)
Chris@16 164 {
Chris@16 165 putInput(input, samples);
Chris@16 166 getOutput(output, lrintf(samples * m_ratio));
Chris@16 167 }
Chris@16 168
Chris@16 169 size_t
Chris@16 170 PhaseVocoderTimeStretcher::getRequiredInputSamples() const
Chris@16 171 {
Chris@16 172 if (m_inbuf[0]->getReadSpace() >= m_wlen) return 0;
Chris@16 173 return m_wlen - m_inbuf[0]->getReadSpace();
Chris@16 174 }
Chris@16 175
Chris@16 176 void
Chris@16 177 PhaseVocoderTimeStretcher::putInput(float **input, size_t samples)
Chris@0 178 {
Chris@0 179 // We need to add samples from input to our internal buffer. When
Chris@0 180 // we have m_windowSize samples in the buffer, we can process it,
Chris@0 181 // move the samples back by m_n1 and write the output onto our
Chris@0 182 // internal output buffer. If we have (samples * ratio) samples
Chris@0 183 // in that, we can write m_n2 of them back to output and return
Chris@0 184 // (otherwise we have to write zeroes).
Chris@0 185
Chris@0 186 // When we process, we write m_wlen to our fixed output buffer
Chris@0 187 // (m_mashbuf). We then pull out the first m_n2 samples from that
Chris@0 188 // buffer, push them into the output ring buffer, and shift
Chris@0 189 // m_mashbuf left by that amount.
Chris@0 190
Chris@0 191 // The processing latency is then m_wlen - m_n2.
Chris@0 192
Chris@0 193 size_t consumed = 0;
Chris@0 194
Chris@0 195 while (consumed < samples) {
Chris@0 196
Chris@16 197 size_t writable = m_inbuf[0]->getWriteSpace();
Chris@0 198 writable = std::min(writable, samples - consumed);
Chris@0 199
Chris@0 200 if (writable == 0) {
Chris@0 201 //!!! then what? I don't think this should happen, but
Chris@16 202 std::cerr << "WARNING: PhaseVocoderTimeStretcher::putInput: writable == 0" << std::endl;
Chris@0 203 break;
Chris@0 204 }
Chris@0 205
Chris@14 206 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@0 207 std::cerr << "writing " << writable << " from index " << consumed << " to inbuf, consumed will be " << consumed + writable << std::endl;
Chris@0 208 #endif
Chris@16 209
Chris@16 210 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 211 m_inbuf[c]->write(input[c] + consumed, writable);
Chris@16 212 }
Chris@0 213 consumed += writable;
Chris@0 214
Chris@16 215 while (m_inbuf[0]->getReadSpace() >= m_wlen &&
Chris@16 216 m_outbuf[0]->getWriteSpace() >= m_n2) {
Chris@0 217
Chris@0 218 // We know we have at least m_wlen samples available
Chris@16 219 // in m_inbuf. We need to peek m_wlen of them for
Chris@0 220 // processing, and then read m_n1 to advance the read
Chris@0 221 // pointer.
Chris@16 222
Chris@16 223 size_t n2 = m_n2;
Chris@16 224 bool isPercussive = false;
Chris@0 225
Chris@16 226 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 227
Chris@16 228 size_t got = m_inbuf[c]->peek(m_dbuf, m_wlen);
Chris@16 229 assert(got == m_wlen);
Chris@0 230
Chris@16 231 bool thisChannelPercussive =
Chris@16 232 processBlock(c, m_dbuf, m_mashbuf[c],
Chris@16 233 c == 0 ? m_modulationbuf : 0,
Chris@16 234 isPercussive);
Chris@16 235
Chris@16 236 if (thisChannelPercussive && c == 0) {
Chris@16 237 isPercussive = true;
Chris@16 238 }
Chris@16 239
Chris@16 240 if (isPercussive) {
Chris@16 241 n2 = m_n1;
Chris@16 242 }
Chris@0 243
Chris@14 244 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 245 std::cerr << "writing first " << m_n2 << " from mashbuf, skipping " << m_n1 << " on inbuf " << std::endl;
Chris@0 246 #endif
Chris@16 247 m_inbuf[c]->skip(m_n1);
Chris@13 248
Chris@16 249 for (size_t i = 0; i < n2; ++i) {
Chris@16 250 if (m_modulationbuf[i] > 0.f) {
Chris@16 251 m_mashbuf[c][i] /= m_modulationbuf[i];
Chris@16 252 }
Chris@16 253 }
Chris@16 254
Chris@16 255 m_outbuf[c]->write(m_mashbuf[c], n2);
Chris@16 256
Chris@16 257 for (size_t i = 0; i < m_wlen - n2; ++i) {
Chris@16 258 m_mashbuf[c][i] = m_mashbuf[c][i + n2];
Chris@16 259 }
Chris@16 260
Chris@16 261 for (size_t i = m_wlen - n2; i < m_wlen; ++i) {
Chris@16 262 m_mashbuf[c][i] = 0.0f;
Chris@13 263 }
Chris@13 264 }
Chris@13 265
Chris@16 266 for (size_t i = 0; i < m_wlen - n2; ++i) {
Chris@16 267 m_modulationbuf[i] = m_modulationbuf[i + n2];
Chris@0 268 }
Chris@13 269
Chris@16 270 for (size_t i = m_wlen - n2; i < m_wlen; ++i) {
Chris@13 271 m_modulationbuf[i] = 0.0f;
Chris@0 272 }
Chris@0 273 }
Chris@0 274
Chris@0 275
Chris@14 276 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 277 std::cerr << "loop ended: inbuf read space " << m_inbuf[0]->getReadSpace() << ", outbuf write space " << m_outbuf[0]->getWriteSpace() << std::endl;
Chris@0 278 #endif
Chris@0 279 }
Chris@0 280
Chris@16 281 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 282 std::cerr << "PhaseVocoderTimeStretcher::putInput returning" << std::endl;
Chris@16 283 #endif
Chris@16 284 }
Chris@12 285
Chris@16 286 size_t
Chris@16 287 PhaseVocoderTimeStretcher::getAvailableOutputSamples() const
Chris@16 288 {
Chris@16 289 return m_outbuf[0]->getReadSpace();
Chris@16 290 }
Chris@16 291
Chris@16 292 void
Chris@16 293 PhaseVocoderTimeStretcher::getOutput(float **output, size_t samples)
Chris@16 294 {
Chris@16 295 if (m_outbuf[0]->getReadSpace() < samples) {
Chris@16 296 std::cerr << "WARNING: PhaseVocoderTimeStretcher::getOutput: not enough data (yet?) (" << m_outbuf[0]->getReadSpace() << " < " << samples << ")" << std::endl;
Chris@16 297 size_t fill = samples - m_outbuf[0]->getReadSpace();
Chris@16 298 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 299 for (size_t i = 0; i < fill; ++i) {
Chris@16 300 output[c][i] = 0.0;
Chris@16 301 }
Chris@16 302 m_outbuf[c]->read(output[c] + fill, m_outbuf[c]->getReadSpace());
Chris@16 303 }
Chris@0 304 } else {
Chris@14 305 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 306 std::cerr << "enough data - writing " << samples << " from outbuf" << std::endl;
Chris@0 307 #endif
Chris@16 308 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 309 m_outbuf[c]->read(output[c], samples);
Chris@16 310 }
Chris@0 311 }
Chris@0 312
Chris@14 313 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 314 std::cerr << "PhaseVocoderTimeStretcher::getOutput returning" << std::endl;
Chris@0 315 #endif
Chris@0 316 }
Chris@0 317
Chris@16 318 bool
Chris@16 319 PhaseVocoderTimeStretcher::processBlock(size_t c,
Chris@16 320 float *buf, float *out,
Chris@16 321 float *modulation,
Chris@16 322 bool knownPercussive)
Chris@0 323 {
Chris@0 324 size_t i;
Chris@16 325 bool isPercussive = knownPercussive;
Chris@0 326
Chris@0 327 // buf contains m_wlen samples; out contains enough space for
Chris@0 328 // m_wlen * ratio samples (we mix into out, rather than replacing)
Chris@0 329
Chris@14 330 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 331 std::cerr << "PhaseVocoderTimeStretcher::processBlock (channel " << c << ")" << std::endl;
Chris@0 332 #endif
Chris@0 333
Chris@0 334 m_window->cut(buf);
Chris@0 335
Chris@0 336 for (i = 0; i < m_wlen/2; ++i) {
Chris@0 337 float temp = buf[i];
Chris@0 338 buf[i] = buf[i + m_wlen/2];
Chris@0 339 buf[i + m_wlen/2] = temp;
Chris@0 340 }
Chris@0 341
Chris@0 342 for (i = 0; i < m_wlen; ++i) {
Chris@0 343 m_time[i][0] = buf[i];
Chris@0 344 m_time[i][1] = 0.0;
Chris@0 345 }
Chris@0 346
Chris@0 347 fftwf_execute(m_plan); // m_time -> m_freq
Chris@0 348
Chris@16 349 if (m_sharpen && c == 0) { //!!!
Chris@16 350
Chris@16 351 int count = 0;
Chris@16 352
Chris@16 353 for (i = 0; i < m_wlen; ++i) {
Chris@16 354
Chris@16 355 float mag = sqrtf(m_freq[i][0] * m_freq[i][0] +
Chris@16 356 m_freq[i][1] * m_freq[i][1]);
Chris@16 357
Chris@16 358 if (m_prevMag[c][i] > 0) {
Chris@16 359 float magdiff = 20.f * log10f(mag / m_prevMag[c][i]);
Chris@16 360 if (magdiff > 3.f) ++count;
Chris@16 361 }
Chris@16 362
Chris@16 363 m_prevMag[c][i] = mag;
Chris@16 364 }
Chris@16 365
Chris@16 366 if (count > m_wlen / 6 &&
Chris@16 367 count > m_prevPercussiveCount[c] * 1.2) {
Chris@16 368 isPercussive = true;
Chris@16 369 std::cerr << "isPercussive (count = " << count << ", prev = " << m_prevPercussiveCount[c] << ")" << std::endl;
Chris@16 370 }
Chris@16 371
Chris@16 372 m_prevPercussiveCount[c] = count;
Chris@16 373 }
Chris@16 374
Chris@16 375 size_t n2 = m_n2;
Chris@16 376 if (isPercussive) n2 = m_n1;
Chris@16 377
Chris@0 378 for (i = 0; i < m_wlen; ++i) {
Chris@16 379
Chris@16 380 float mag;
Chris@16 381
Chris@16 382 if (m_sharpen && c == 0) {
Chris@16 383 mag = m_prevMag[c][i]; // can reuse this
Chris@16 384 } else {
Chris@16 385 mag = sqrtf(m_freq[i][0] * m_freq[i][0] +
Chris@16 386 m_freq[i][1] * m_freq[i][1]);
Chris@16 387 }
Chris@0 388
Chris@12 389 float phase = princargf(atan2f(m_freq[i][1], m_freq[i][0]));
Chris@12 390
Chris@12 391 float omega = (2 * M_PI * m_n1 * i) / m_wlen;
Chris@0 392
Chris@16 393 float expectedPhase = m_prevPhase[c][i] + omega;
Chris@12 394
Chris@12 395 float phaseError = princargf(phase - expectedPhase);
Chris@12 396
Chris@12 397 float phaseIncrement = (omega + phaseError) / m_n1;
Chris@12 398
Chris@16 399 float adjustedPhase = m_prevAdjustedPhase[c][i] + n2 * phaseIncrement;
Chris@16 400
Chris@16 401 if (isPercussive) adjustedPhase = phase;
Chris@0 402
Chris@12 403 float real = mag * cosf(adjustedPhase);
Chris@12 404 float imag = mag * sinf(adjustedPhase);
Chris@0 405 m_freq[i][0] = real;
Chris@0 406 m_freq[i][1] = imag;
Chris@12 407
Chris@16 408 m_prevPhase[c][i] = phase;
Chris@16 409 m_prevAdjustedPhase[c][i] = adjustedPhase;
Chris@0 410 }
Chris@0 411
Chris@0 412 fftwf_execute(m_iplan); // m_freq -> in, inverse fft
Chris@0 413
Chris@0 414 for (i = 0; i < m_wlen/2; ++i) {
Chris@0 415 float temp = buf[i] / m_wlen;
Chris@0 416 buf[i] = buf[i + m_wlen/2] / m_wlen;
Chris@0 417 buf[i + m_wlen/2] = temp;
Chris@0 418 }
Chris@0 419
Chris@0 420 m_window->cut(buf);
Chris@15 421
Chris@0 422 for (i = 0; i < m_wlen; ++i) {
Chris@16 423 out[i] += buf[i];
Chris@0 424 }
Chris@16 425
Chris@16 426 if (modulation) {
Chris@16 427
Chris@16 428 float area = m_window->getArea();
Chris@16 429
Chris@16 430 for (i = 0; i < m_wlen; ++i) {
Chris@16 431 float val = m_window->getValue(i);
Chris@16 432 modulation[i] += val * area;
Chris@16 433 }
Chris@16 434 }
Chris@16 435
Chris@16 436 return isPercussive;
Chris@0 437 }
Chris@15 438