annotate audioio/PhaseVocoderTimeStretcher.cpp @ 31:37af203dbd15

* Buffer size fixes in the time stretcher, to avoid running out of input data for large or small ratios
author Chris Cannam
date Thu, 21 Sep 2006 09:43:41 +0000
parents d88d117e0c34
children e3b32dc5180b
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@25 21 #include <QMutexLocker>
Chris@25 22
Chris@14 23 //#define DEBUG_PHASE_VOCODER_TIME_STRETCHER 1
Chris@0 24
Chris@22 25 PhaseVocoderTimeStretcher::PhaseVocoderTimeStretcher(size_t sampleRate,
Chris@22 26 size_t channels,
Chris@16 27 float ratio,
Chris@16 28 bool sharpen,
Chris@31 29 size_t maxOutputBlockSize) :
Chris@22 30 m_sampleRate(sampleRate),
Chris@16 31 m_channels(channels),
Chris@31 32 m_maxOutputBlockSize(maxOutputBlockSize),
Chris@16 33 m_ratio(ratio),
Chris@21 34 m_sharpen(sharpen),
Chris@21 35 m_totalCount(0),
Chris@21 36 m_transientCount(0),
Chris@25 37 m_n2sum(0),
Chris@25 38 m_mutex(new QMutex())
Chris@0 39 {
Chris@25 40 initialise();
Chris@16 41
Chris@25 42 }
Chris@22 43
Chris@25 44 PhaseVocoderTimeStretcher::~PhaseVocoderTimeStretcher()
Chris@25 45 {
Chris@25 46 std::cerr << "PhaseVocoderTimeStretcher::~PhaseVocoderTimeStretcher" << std::endl;
Chris@21 47
Chris@25 48 cleanup();
Chris@25 49
Chris@25 50 delete m_mutex;
Chris@25 51 }
Chris@22 52
Chris@25 53 void
Chris@25 54 PhaseVocoderTimeStretcher::initialise()
Chris@25 55 {
Chris@25 56 std::cerr << "PhaseVocoderTimeStretcher::initialise" << std::endl;
Chris@25 57
Chris@25 58 calculateParameters();
Chris@16 59
Chris@20 60 m_analysisWindow = new Window<float>(HanningWindow, m_wlen);
Chris@20 61 m_synthesisWindow = new Window<float>(HanningWindow, m_wlen);
Chris@15 62
Chris@16 63 m_prevPhase = new float *[m_channels];
Chris@16 64 m_prevAdjustedPhase = new float *[m_channels];
Chris@15 65
Chris@20 66 m_prevTransientMag = (float *)fftwf_malloc(sizeof(float) * (m_wlen / 2 + 1));
Chris@21 67 m_prevTransientScore = 0;
Chris@20 68 m_prevTransient = false;
Chris@20 69
Chris@20 70 m_tempbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@20 71
Chris@20 72 m_time = new float *[m_channels];
Chris@20 73 m_freq = new fftwf_complex *[m_channels];
Chris@20 74 m_plan = new fftwf_plan[m_channels];
Chris@20 75 m_iplan = new fftwf_plan[m_channels];
Chris@0 76
Chris@16 77 m_inbuf = new RingBuffer<float> *[m_channels];
Chris@16 78 m_outbuf = new RingBuffer<float> *[m_channels];
Chris@16 79 m_mashbuf = new float *[m_channels];
Chris@16 80
Chris@16 81 m_modulationbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@16 82
Chris@16 83 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 84
Chris@20 85 m_prevPhase[c] = (float *)fftwf_malloc(sizeof(float) * (m_wlen / 2 + 1));
Chris@20 86 m_prevAdjustedPhase[c] = (float *)fftwf_malloc(sizeof(float) * (m_wlen / 2 + 1));
Chris@16 87
Chris@20 88 m_time[c] = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@20 89 m_freq[c] = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) *
Chris@20 90 (m_wlen / 2 + 1));
Chris@20 91
Chris@20 92 m_plan[c] = fftwf_plan_dft_r2c_1d(m_wlen, m_time[c], m_freq[c], FFTW_ESTIMATE);
Chris@20 93 m_iplan[c] = fftwf_plan_dft_c2r_1d(m_wlen, m_freq[c], m_time[c], FFTW_ESTIMATE);
Chris@16 94
Chris@16 95 m_outbuf[c] = new RingBuffer<float>
Chris@31 96 ((m_maxOutputBlockSize + m_wlen) * 2);
Chris@31 97 m_inbuf[c] = new RingBuffer<float>
Chris@31 98 (lrintf(m_outbuf[c]->getSize() / m_ratio) + m_wlen);
Chris@31 99
Chris@31 100 std::cerr << "making inbuf size " << m_inbuf[c]->getSize() << " (outbuf size is " << m_outbuf[c]->getSize() << ", ratio " << m_ratio << ")" << std::endl;
Chris@31 101
Chris@31 102
Chris@16 103 m_mashbuf[c] = (float *)fftwf_malloc(sizeof(float) * m_wlen);
Chris@16 104
Chris@31 105 for (size_t i = 0; i < m_wlen; ++i) {
Chris@16 106 m_mashbuf[c][i] = 0.0;
Chris@20 107 }
Chris@20 108
Chris@31 109 for (size_t i = 0; i <= m_wlen/2; ++i) {
Chris@16 110 m_prevPhase[c][i] = 0.0;
Chris@16 111 m_prevAdjustedPhase[c][i] = 0.0;
Chris@16 112 }
Chris@16 113 }
Chris@16 114
Chris@31 115 for (size_t i = 0; i < m_wlen; ++i) {
Chris@16 116 m_modulationbuf[i] = 0.0;
Chris@0 117 }
Chris@16 118
Chris@31 119 for (size_t i = 0; i <= m_wlen/2; ++i) {
Chris@20 120 m_prevTransientMag[i] = 0.0;
Chris@20 121 }
Chris@0 122 }
Chris@0 123
Chris@25 124 void
Chris@25 125 PhaseVocoderTimeStretcher::calculateParameters()
Chris@0 126 {
Chris@25 127 std::cerr << "PhaseVocoderTimeStretcher::calculateParameters" << std::endl;
Chris@25 128
Chris@25 129 m_wlen = 1024;
Chris@25 130
Chris@25 131 //!!! In transient sharpening mode, we need to pick the window
Chris@25 132 //length so as to be more or less fixed in audio duration (i.e. we
Chris@25 133 //need to exploit the sample rate)
Chris@25 134
Chris@25 135 //!!! have to work out the relationship between wlen and transient
Chris@25 136 //threshold
Chris@25 137
Chris@25 138 if (m_ratio < 1) {
Chris@25 139 if (m_ratio < 0.4) {
Chris@25 140 m_n1 = 1024;
Chris@25 141 m_wlen = 2048;
Chris@25 142 } else if (m_ratio < 0.8) {
Chris@25 143 m_n1 = 512;
Chris@25 144 } else {
Chris@25 145 m_n1 = 256;
Chris@25 146 }
Chris@25 147 if (m_sharpen) {
Chris@25 148 m_wlen = 2048;
Chris@25 149 }
Chris@31 150 m_n2 = lrintf(m_n1 * m_ratio);
Chris@25 151 } else {
Chris@25 152 if (m_ratio > 2) {
Chris@25 153 m_n2 = 512;
Chris@25 154 m_wlen = 4096;
Chris@25 155 } else if (m_ratio > 1.6) {
Chris@25 156 m_n2 = 384;
Chris@25 157 m_wlen = 2048;
Chris@25 158 } else {
Chris@25 159 m_n2 = 256;
Chris@25 160 }
Chris@25 161 if (m_sharpen) {
Chris@25 162 if (m_wlen < 2048) m_wlen = 2048;
Chris@25 163 }
Chris@31 164 m_n1 = lrintf(m_n2 / m_ratio);
Chris@25 165 }
Chris@25 166
Chris@31 167 m_transientThreshold = lrintf(m_wlen / 4.5);
Chris@26 168
Chris@26 169 m_totalCount = 0;
Chris@26 170 m_transientCount = 0;
Chris@26 171 m_n2sum = 0;
Chris@26 172
Chris@26 173
Chris@26 174 std::cerr << "PhaseVocoderTimeStretcher: channels = " << m_channels
Chris@26 175 << ", ratio = " << m_ratio
Chris@26 176 << ", n1 = " << m_n1 << ", n2 = " << m_n2 << ", wlen = "
Chris@31 177 << m_wlen << ", max = " << m_maxOutputBlockSize << std::endl;
Chris@26 178 // << ", outbuflen = " << m_outbuf[0]->getSize() << std::endl;
Chris@25 179 }
Chris@25 180
Chris@25 181 void
Chris@25 182 PhaseVocoderTimeStretcher::cleanup()
Chris@25 183 {
Chris@25 184 std::cerr << "PhaseVocoderTimeStretcher::cleanup" << std::endl;
Chris@0 185
Chris@20 186 for (size_t c = 0; c < m_channels; ++c) {
Chris@0 187
Chris@20 188 fftwf_destroy_plan(m_plan[c]);
Chris@20 189 fftwf_destroy_plan(m_iplan[c]);
Chris@16 190
Chris@20 191 fftwf_free(m_time[c]);
Chris@20 192 fftwf_free(m_freq[c]);
Chris@16 193
Chris@16 194 fftwf_free(m_mashbuf[c]);
Chris@16 195 fftwf_free(m_prevPhase[c]);
Chris@16 196 fftwf_free(m_prevAdjustedPhase[c]);
Chris@16 197
Chris@16 198 delete m_inbuf[c];
Chris@16 199 delete m_outbuf[c];
Chris@16 200 }
Chris@16 201
Chris@20 202 fftwf_free(m_tempbuf);
Chris@13 203 fftwf_free(m_modulationbuf);
Chris@20 204 fftwf_free(m_prevTransientMag);
Chris@0 205
Chris@16 206 delete[] m_prevPhase;
Chris@16 207 delete[] m_prevAdjustedPhase;
Chris@16 208 delete[] m_inbuf;
Chris@16 209 delete[] m_outbuf;
Chris@16 210 delete[] m_mashbuf;
Chris@20 211 delete[] m_time;
Chris@20 212 delete[] m_freq;
Chris@20 213 delete[] m_plan;
Chris@20 214 delete[] m_iplan;
Chris@15 215
Chris@20 216 delete m_analysisWindow;
Chris@20 217 delete m_synthesisWindow;
Chris@0 218 }
Chris@0 219
Chris@25 220 void
Chris@25 221 PhaseVocoderTimeStretcher::setRatio(float ratio)
Chris@25 222 {
Chris@25 223 QMutexLocker locker(m_mutex);
Chris@25 224
Chris@25 225 size_t formerWlen = m_wlen;
Chris@25 226 m_ratio = ratio;
Chris@25 227
Chris@25 228 calculateParameters();
Chris@25 229
Chris@25 230 if (m_wlen == formerWlen) {
Chris@25 231
Chris@25 232 // This is the only container whose size depends on m_ratio
Chris@25 233
Chris@31 234 RingBuffer<float> **newin = new RingBuffer<float> *[m_channels];
Chris@25 235
Chris@31 236 size_t formerSize = m_inbuf[0]->getSize();
Chris@31 237 size_t newSize = lrintf(m_outbuf[0]->getSize() / m_ratio) + m_wlen;
Chris@25 238
Chris@31 239 std::cerr << "resizing inbuf from " << formerSize << " to "
Chris@31 240 << newSize << " (outbuf size is " << m_outbuf[0]->getSize() << ", ratio " << m_ratio << ")" << std::endl;
Chris@25 241
Chris@31 242 if (formerSize != newSize) {
Chris@25 243
Chris@31 244 size_t ready = m_inbuf[0]->getReadSpace();
Chris@25 245
Chris@25 246 for (size_t c = 0; c < m_channels; ++c) {
Chris@31 247 newin[c] = new RingBuffer<float>(newSize);
Chris@25 248 }
Chris@25 249
Chris@31 250 if (ready > 0) {
Chris@31 251
Chris@31 252 size_t copy = std::min(ready, newSize);
Chris@31 253 float *tmp = new float[ready];
Chris@31 254
Chris@31 255 for (size_t c = 0; c < m_channels; ++c) {
Chris@31 256 m_inbuf[c]->read(tmp, ready);
Chris@31 257 newin[c]->write(tmp + ready - copy, copy);
Chris@31 258 }
Chris@31 259
Chris@31 260 delete[] tmp;
Chris@31 261 }
Chris@31 262
Chris@31 263 for (size_t c = 0; c < m_channels; ++c) {
Chris@31 264 delete m_inbuf[c];
Chris@31 265 }
Chris@31 266
Chris@31 267 delete[] m_inbuf;
Chris@31 268 m_inbuf = newin;
Chris@25 269 }
Chris@25 270
Chris@25 271 } else {
Chris@25 272
Chris@25 273 std::cerr << "wlen changed" << std::endl;
Chris@25 274 cleanup();
Chris@25 275 initialise();
Chris@25 276 }
Chris@25 277 }
Chris@25 278
Chris@0 279 size_t
Chris@14 280 PhaseVocoderTimeStretcher::getProcessingLatency() const
Chris@0 281 {
Chris@0 282 return getWindowSize() - getInputIncrement();
Chris@0 283 }
Chris@0 284
Chris@16 285 size_t
Chris@16 286 PhaseVocoderTimeStretcher::getRequiredInputSamples() const
Chris@16 287 {
Chris@25 288 QMutexLocker locker(m_mutex);
Chris@25 289
Chris@16 290 if (m_inbuf[0]->getReadSpace() >= m_wlen) return 0;
Chris@16 291 return m_wlen - m_inbuf[0]->getReadSpace();
Chris@16 292 }
Chris@16 293
Chris@16 294 void
Chris@16 295 PhaseVocoderTimeStretcher::putInput(float **input, size_t samples)
Chris@0 296 {
Chris@25 297 QMutexLocker locker(m_mutex);
Chris@25 298
Chris@0 299 // We need to add samples from input to our internal buffer. When
Chris@0 300 // we have m_windowSize samples in the buffer, we can process it,
Chris@0 301 // move the samples back by m_n1 and write the output onto our
Chris@0 302 // internal output buffer. If we have (samples * ratio) samples
Chris@0 303 // in that, we can write m_n2 of them back to output and return
Chris@0 304 // (otherwise we have to write zeroes).
Chris@0 305
Chris@0 306 // When we process, we write m_wlen to our fixed output buffer
Chris@0 307 // (m_mashbuf). We then pull out the first m_n2 samples from that
Chris@0 308 // buffer, push them into the output ring buffer, and shift
Chris@0 309 // m_mashbuf left by that amount.
Chris@0 310
Chris@0 311 // The processing latency is then m_wlen - m_n2.
Chris@0 312
Chris@0 313 size_t consumed = 0;
Chris@0 314
Chris@0 315 while (consumed < samples) {
Chris@0 316
Chris@16 317 size_t writable = m_inbuf[0]->getWriteSpace();
Chris@0 318 writable = std::min(writable, samples - consumed);
Chris@0 319
Chris@0 320 if (writable == 0) {
Chris@0 321 //!!! then what? I don't think this should happen, but
Chris@31 322 std::cerr << "WARNING: PhaseVocoderTimeStretcher::putInput: writable == 0 (inbuf has " << m_inbuf[0]->getReadSpace() << " samples available for reading, space for " << m_inbuf[0]->getWriteSpace() << " more)" << std::endl;
Chris@31 323 if (m_inbuf[0]->getReadSpace() < m_wlen ||
Chris@31 324 m_outbuf[0]->getWriteSpace() < m_n2) {
Chris@31 325 std::cerr << "Outbuf has space for " << m_outbuf[0]->getWriteSpace() << " (n2 = " << m_n2 << "), won't be able to process" << std::endl;
Chris@31 326 break;
Chris@31 327 }
Chris@31 328 } else {
Chris@0 329
Chris@14 330 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@31 331 std::cerr << "writing " << writable << " from index " << consumed << " to inbuf, consumed will be " << consumed + writable << std::endl;
Chris@0 332 #endif
Chris@16 333
Chris@31 334 for (size_t c = 0; c < m_channels; ++c) {
Chris@31 335 m_inbuf[c]->write(input[c] + consumed, writable);
Chris@31 336 }
Chris@31 337 consumed += writable;
Chris@16 338 }
Chris@0 339
Chris@16 340 while (m_inbuf[0]->getReadSpace() >= m_wlen &&
Chris@16 341 m_outbuf[0]->getWriteSpace() >= m_n2) {
Chris@0 342
Chris@0 343 // We know we have at least m_wlen samples available
Chris@16 344 // in m_inbuf. We need to peek m_wlen of them for
Chris@0 345 // processing, and then read m_n1 to advance the read
Chris@0 346 // pointer.
Chris@16 347
Chris@20 348 for (size_t c = 0; c < m_channels; ++c) {
Chris@20 349
Chris@20 350 size_t got = m_inbuf[c]->peek(m_tempbuf, m_wlen);
Chris@20 351 assert(got == m_wlen);
Chris@20 352
Chris@20 353 analyseBlock(c, m_tempbuf);
Chris@20 354 }
Chris@20 355
Chris@20 356 bool transient = false;
Chris@20 357 if (m_sharpen) transient = isTransient();
Chris@20 358
Chris@16 359 size_t n2 = m_n2;
Chris@20 360
Chris@20 361 if (transient) {
Chris@20 362 n2 = m_n1;
Chris@20 363 }
Chris@0 364
Chris@21 365 ++m_totalCount;
Chris@21 366 if (transient) ++m_transientCount;
Chris@21 367 m_n2sum += n2;
Chris@21 368
Chris@21 369 // std::cerr << "ratio for last 10: " <<last10num << "/" << (10 * m_n1) << " = " << float(last10num) / float(10 * m_n1) << " (should be " << m_ratio << ")" << std::endl;
Chris@21 370
Chris@21 371 if (m_totalCount > 50 && m_transientCount < m_totalCount) {
Chris@21 372
Chris@21 373 int fixed = lrintf(m_transientCount * m_n1);
Chris@21 374 int squashy = m_n2sum - fixed;
Chris@21 375
Chris@21 376 int idealTotal = lrintf(m_totalCount * m_n1 * m_ratio);
Chris@21 377 int idealSquashy = idealTotal - fixed;
Chris@21 378
Chris@21 379 int squashyCount = m_totalCount - m_transientCount;
Chris@21 380
Chris@21 381 n2 = lrintf(idealSquashy / squashyCount);
Chris@21 382
Chris@21 383 if (n2 != m_n2) {
Chris@21 384 std::cerr << m_n2 << " -> " << n2 << std::endl;
Chris@21 385 }
Chris@21 386 }
Chris@21 387
Chris@16 388 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 389
Chris@20 390 synthesiseBlock(c, m_mashbuf[c],
Chris@20 391 c == 0 ? m_modulationbuf : 0,
Chris@20 392 m_prevTransient ? m_n1 : m_n2);
Chris@16 393
Chris@0 394
Chris@14 395 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 396 std::cerr << "writing first " << m_n2 << " from mashbuf, skipping " << m_n1 << " on inbuf " << std::endl;
Chris@0 397 #endif
Chris@16 398 m_inbuf[c]->skip(m_n1);
Chris@13 399
Chris@16 400 for (size_t i = 0; i < n2; ++i) {
Chris@16 401 if (m_modulationbuf[i] > 0.f) {
Chris@16 402 m_mashbuf[c][i] /= m_modulationbuf[i];
Chris@16 403 }
Chris@16 404 }
Chris@16 405
Chris@16 406 m_outbuf[c]->write(m_mashbuf[c], n2);
Chris@16 407
Chris@16 408 for (size_t i = 0; i < m_wlen - n2; ++i) {
Chris@16 409 m_mashbuf[c][i] = m_mashbuf[c][i + n2];
Chris@16 410 }
Chris@16 411
Chris@16 412 for (size_t i = m_wlen - n2; i < m_wlen; ++i) {
Chris@16 413 m_mashbuf[c][i] = 0.0f;
Chris@13 414 }
Chris@13 415 }
Chris@13 416
Chris@20 417 m_prevTransient = transient;
Chris@17 418
Chris@16 419 for (size_t i = 0; i < m_wlen - n2; ++i) {
Chris@16 420 m_modulationbuf[i] = m_modulationbuf[i + n2];
Chris@0 421 }
Chris@13 422
Chris@16 423 for (size_t i = m_wlen - n2; i < m_wlen; ++i) {
Chris@13 424 m_modulationbuf[i] = 0.0f;
Chris@0 425 }
Chris@21 426
Chris@21 427 if (!transient) m_n2 = n2;
Chris@0 428 }
Chris@0 429
Chris@0 430
Chris@14 431 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 432 std::cerr << "loop ended: inbuf read space " << m_inbuf[0]->getReadSpace() << ", outbuf write space " << m_outbuf[0]->getWriteSpace() << std::endl;
Chris@0 433 #endif
Chris@0 434 }
Chris@0 435
Chris@16 436 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 437 std::cerr << "PhaseVocoderTimeStretcher::putInput returning" << std::endl;
Chris@16 438 #endif
Chris@21 439
Chris@21 440 // std::cerr << "ratio: nominal: " << getRatio() << " actual: "
Chris@21 441 // << m_total2 << "/" << m_total1 << " = " << float(m_total2) / float(m_total1) << " ideal: " << m_ratio << std::endl;
Chris@16 442 }
Chris@12 443
Chris@16 444 size_t
Chris@16 445 PhaseVocoderTimeStretcher::getAvailableOutputSamples() const
Chris@16 446 {
Chris@25 447 QMutexLocker locker(m_mutex);
Chris@25 448
Chris@16 449 return m_outbuf[0]->getReadSpace();
Chris@16 450 }
Chris@16 451
Chris@16 452 void
Chris@16 453 PhaseVocoderTimeStretcher::getOutput(float **output, size_t samples)
Chris@16 454 {
Chris@25 455 QMutexLocker locker(m_mutex);
Chris@25 456
Chris@16 457 if (m_outbuf[0]->getReadSpace() < samples) {
Chris@16 458 std::cerr << "WARNING: PhaseVocoderTimeStretcher::getOutput: not enough data (yet?) (" << m_outbuf[0]->getReadSpace() << " < " << samples << ")" << std::endl;
Chris@16 459 size_t fill = samples - m_outbuf[0]->getReadSpace();
Chris@16 460 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 461 for (size_t i = 0; i < fill; ++i) {
Chris@16 462 output[c][i] = 0.0;
Chris@16 463 }
Chris@16 464 m_outbuf[c]->read(output[c] + fill, m_outbuf[c]->getReadSpace());
Chris@16 465 }
Chris@0 466 } else {
Chris@14 467 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 468 std::cerr << "enough data - writing " << samples << " from outbuf" << std::endl;
Chris@0 469 #endif
Chris@16 470 for (size_t c = 0; c < m_channels; ++c) {
Chris@16 471 m_outbuf[c]->read(output[c], samples);
Chris@16 472 }
Chris@0 473 }
Chris@0 474
Chris@14 475 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@16 476 std::cerr << "PhaseVocoderTimeStretcher::getOutput returning" << std::endl;
Chris@0 477 #endif
Chris@0 478 }
Chris@0 479
Chris@20 480 void
Chris@20 481 PhaseVocoderTimeStretcher::analyseBlock(size_t c, float *buf)
Chris@0 482 {
Chris@0 483 size_t i;
Chris@0 484
Chris@20 485 // buf contains m_wlen samples
Chris@0 486
Chris@14 487 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
Chris@20 488 std::cerr << "PhaseVocoderTimeStretcher::analyseBlock (channel " << c << ")" << std::endl;
Chris@0 489 #endif
Chris@0 490
Chris@20 491 m_analysisWindow->cut(buf);
Chris@0 492
Chris@0 493 for (i = 0; i < m_wlen/2; ++i) {
Chris@0 494 float temp = buf[i];
Chris@0 495 buf[i] = buf[i + m_wlen/2];
Chris@0 496 buf[i + m_wlen/2] = temp;
Chris@0 497 }
Chris@19 498
Chris@0 499 for (i = 0; i < m_wlen; ++i) {
Chris@20 500 m_time[c][i] = buf[i];
Chris@0 501 }
Chris@0 502
Chris@20 503 fftwf_execute(m_plan[c]); // m_time -> m_freq
Chris@20 504 }
Chris@0 505
Chris@20 506 bool
Chris@20 507 PhaseVocoderTimeStretcher::isTransient()
Chris@20 508 {
Chris@20 509 int count = 0;
Chris@16 510
Chris@31 511 for (size_t i = 0; i <= m_wlen/2; ++i) {
Chris@16 512
Chris@20 513 float real = 0.f, imag = 0.f;
Chris@20 514
Chris@20 515 for (size_t c = 0; c < m_channels; ++c) {
Chris@20 516 real += m_freq[c][i][0];
Chris@20 517 imag += m_freq[c][i][1];
Chris@16 518 }
Chris@16 519
Chris@20 520 float sqrmag = (real * real + imag * imag);
Chris@20 521
Chris@20 522 if (m_prevTransientMag[i] > 0.f) {
Chris@20 523 float diff = 10.f * log10f(sqrmag / m_prevTransientMag[i]);
Chris@20 524 if (diff > 3.f) ++count;
Chris@20 525 }
Chris@20 526
Chris@20 527 m_prevTransientMag[i] = sqrmag;
Chris@16 528 }
Chris@16 529
Chris@20 530 bool isTransient = false;
Chris@16 531
Chris@26 532 // if (count > m_transientThreshold &&
Chris@26 533 // count > m_prevTransientScore * 1.2) {
Chris@26 534 if (count > m_prevTransientScore &&
Chris@26 535 count > m_transientThreshold &&
Chris@26 536 count - m_prevTransientScore > m_wlen / 20) {
Chris@20 537 isTransient = true;
Chris@26 538
Chris@26 539
Chris@26 540 std::cerr << "isTransient (count = " << count << ", prev = " << m_prevTransientScore << ", diff = " << count - m_prevTransientScore << ", ratio = " << (m_totalCount > 0 ? (float (m_n2sum) / float(m_totalCount * m_n1)) : 1.f) << ", ideal = " << m_ratio << ")" << std::endl;
Chris@26 541 // } else {
Chris@26 542 // std::cerr << " !transient (count = " << count << ", prev = " << m_prevTransientScore << ", diff = " << count - m_prevTransientScore << ")" << std::endl;
Chris@20 543 }
Chris@16 544
Chris@21 545 m_prevTransientScore = count;
Chris@20 546
Chris@20 547 return isTransient;
Chris@20 548 }
Chris@20 549
Chris@20 550 void
Chris@20 551 PhaseVocoderTimeStretcher::synthesiseBlock(size_t c,
Chris@20 552 float *out,
Chris@20 553 float *modulation,
Chris@20 554 size_t lastStep)
Chris@20 555 {
Chris@20 556 bool unchanged = (lastStep == m_n1);
Chris@20 557
Chris@31 558 for (size_t i = 0; i <= m_wlen/2; ++i) {
Chris@0 559
Chris@20 560 float phase = princargf(atan2f(m_freq[c][i][1], m_freq[c][i][0]));
Chris@19 561 float adjustedPhase = phase;
Chris@12 562
Chris@20 563 if (!unchanged) {
Chris@16 564
Chris@20 565 float mag = sqrtf(m_freq[c][i][0] * m_freq[c][i][0] +
Chris@20 566 m_freq[c][i][1] * m_freq[c][i][1]);
Chris@19 567
Chris@20 568 float omega = (2 * M_PI * m_n1 * i) / m_wlen;
Chris@20 569
Chris@20 570 float expectedPhase = m_prevPhase[c][i] + omega;
Chris@20 571
Chris@20 572 float phaseError = princargf(phase - expectedPhase);
Chris@20 573
Chris@20 574 float phaseIncrement = (omega + phaseError) / m_n1;
Chris@20 575
Chris@20 576 adjustedPhase = m_prevAdjustedPhase[c][i] +
Chris@20 577 lastStep * phaseIncrement;
Chris@20 578
Chris@20 579 float real = mag * cosf(adjustedPhase);
Chris@20 580 float imag = mag * sinf(adjustedPhase);
Chris@20 581 m_freq[c][i][0] = real;
Chris@20 582 m_freq[c][i][1] = imag;
Chris@19 583 }
Chris@19 584
Chris@16 585 m_prevPhase[c][i] = phase;
Chris@16 586 m_prevAdjustedPhase[c][i] = adjustedPhase;
Chris@0 587 }
Chris@20 588
Chris@20 589 fftwf_execute(m_iplan[c]); // m_freq -> m_time, inverse fft
Chris@19 590
Chris@31 591 for (size_t i = 0; i < m_wlen/2; ++i) {
Chris@20 592 float temp = m_time[c][i];
Chris@20 593 m_time[c][i] = m_time[c][i + m_wlen/2];
Chris@20 594 m_time[c][i + m_wlen/2] = temp;
Chris@20 595 }
Chris@20 596
Chris@31 597 for (size_t i = 0; i < m_wlen; ++i) {
Chris@20 598 m_time[c][i] = m_time[c][i] / m_wlen;
Chris@0 599 }
Chris@15 600
Chris@20 601 m_synthesisWindow->cut(m_time[c]);
Chris@19 602
Chris@31 603 for (size_t i = 0; i < m_wlen; ++i) {
Chris@20 604 out[i] += m_time[c][i];
Chris@0 605 }
Chris@16 606
Chris@16 607 if (modulation) {
Chris@16 608
Chris@20 609 float area = m_analysisWindow->getArea();
Chris@16 610
Chris@31 611 for (size_t i = 0; i < m_wlen; ++i) {
Chris@20 612 float val = m_synthesisWindow->getValue(i);
Chris@16 613 modulation[i] += val * area;
Chris@16 614 }
Chris@16 615 }
Chris@0 616 }
Chris@15 617
Chris@20 618