annotate audioio/PhaseVocoderTimeStretcher.cpp @ 26:d88d117e0c34

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