Chris@43: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@43: Chris@43: /* Chris@43: Sonic Visualiser Chris@43: An audio file viewer and annotation editor. Chris@43: Centre for Digital Music, Queen Mary, University of London. Chris@43: This file copyright 2006 Chris Cannam and QMUL. Chris@43: Chris@43: This program is free software; you can redistribute it and/or Chris@43: modify it under the terms of the GNU General Public License as Chris@43: published by the Free Software Foundation; either version 2 of the Chris@43: License, or (at your option) any later version. See the file Chris@43: COPYING included with this distribution for more information. Chris@43: */ Chris@43: Chris@63: #ifndef HAVE_RUBBERBAND Chris@63: Chris@43: #include "PhaseVocoderTimeStretcher.h" Chris@43: Chris@43: #include Chris@43: #include Chris@43: Chris@43: #include Chris@43: Chris@43: //#define DEBUG_PHASE_VOCODER_TIME_STRETCHER 1 Chris@43: Chris@43: PhaseVocoderTimeStretcher::PhaseVocoderTimeStretcher(size_t sampleRate, Chris@43: size_t channels, Chris@43: float ratio, Chris@43: bool sharpen, Chris@43: size_t maxOutputBlockSize) : Chris@43: m_sampleRate(sampleRate), Chris@43: m_channels(channels), Chris@43: m_maxOutputBlockSize(maxOutputBlockSize), Chris@43: m_ratio(ratio), Chris@43: m_sharpen(sharpen), Chris@43: m_totalCount(0), Chris@43: m_transientCount(0), Chris@43: m_n2sum(0), Chris@43: m_mutex(new QMutex()) Chris@43: { Chris@43: initialise(); Chris@43: } Chris@43: Chris@43: PhaseVocoderTimeStretcher::~PhaseVocoderTimeStretcher() Chris@43: { Chris@43: std::cerr << "PhaseVocoderTimeStretcher::~PhaseVocoderTimeStretcher" << std::endl; Chris@43: Chris@43: cleanup(); Chris@43: Chris@43: delete m_mutex; Chris@43: } Chris@43: Chris@43: void Chris@43: PhaseVocoderTimeStretcher::initialise() Chris@43: { Chris@43: std::cerr << "PhaseVocoderTimeStretcher::initialise" << std::endl; Chris@43: Chris@43: calculateParameters(); Chris@43: Chris@43: m_analysisWindow = new Window(HanningWindow, m_wlen); Chris@43: m_synthesisWindow = new Window(HanningWindow, m_wlen); Chris@43: Chris@43: m_prevPhase = new float *[m_channels]; Chris@43: m_prevAdjustedPhase = new float *[m_channels]; Chris@43: Chris@43: m_prevTransientMag = (float *)fftf_malloc(sizeof(float) * (m_wlen / 2 + 1)); Chris@43: m_prevTransientScore = 0; Chris@43: m_prevTransient = false; Chris@43: Chris@43: m_tempbuf = (float *)fftf_malloc(sizeof(float) * m_wlen); Chris@43: Chris@43: m_time = new float *[m_channels]; Chris@43: m_freq = new fftf_complex *[m_channels]; Chris@43: m_plan = new fftf_plan[m_channels]; Chris@43: m_iplan = new fftf_plan[m_channels]; Chris@43: Chris@43: m_inbuf = new RingBuffer *[m_channels]; Chris@43: m_outbuf = new RingBuffer *[m_channels]; Chris@43: m_mashbuf = new float *[m_channels]; Chris@43: Chris@43: m_modulationbuf = (float *)fftf_malloc(sizeof(float) * m_wlen); Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: Chris@43: m_prevPhase[c] = (float *)fftf_malloc(sizeof(float) * (m_wlen / 2 + 1)); Chris@43: m_prevAdjustedPhase[c] = (float *)fftf_malloc(sizeof(float) * (m_wlen / 2 + 1)); Chris@43: Chris@43: m_time[c] = (float *)fftf_malloc(sizeof(float) * m_wlen); Chris@43: m_freq[c] = (fftf_complex *)fftf_malloc(sizeof(fftf_complex) * Chris@43: (m_wlen / 2 + 1)); Chris@43: Chris@43: m_plan[c] = fftf_plan_dft_r2c_1d(m_wlen, m_time[c], m_freq[c], FFTW_MEASURE); Chris@43: m_iplan[c] = fftf_plan_dft_c2r_1d(m_wlen, m_freq[c], m_time[c], FFTW_MEASURE); Chris@43: Chris@43: m_outbuf[c] = new RingBuffer Chris@43: ((m_maxOutputBlockSize + m_wlen) * 2); Chris@43: m_inbuf[c] = new RingBuffer Chris@43: (lrintf(m_outbuf[c]->getSize() / m_ratio) + m_wlen); Chris@43: Chris@43: std::cerr << "making inbuf size " << m_inbuf[c]->getSize() << " (outbuf size is " << m_outbuf[c]->getSize() << ", ratio " << m_ratio << ")" << std::endl; Chris@43: Chris@43: Chris@43: m_mashbuf[c] = (float *)fftf_malloc(sizeof(float) * m_wlen); Chris@43: Chris@43: for (size_t i = 0; i < m_wlen; ++i) { Chris@43: m_mashbuf[c][i] = 0.0; Chris@43: } Chris@43: Chris@43: for (size_t i = 0; i <= m_wlen/2; ++i) { Chris@43: m_prevPhase[c][i] = 0.0; Chris@43: m_prevAdjustedPhase[c][i] = 0.0; Chris@43: } Chris@43: } Chris@43: Chris@43: for (size_t i = 0; i < m_wlen; ++i) { Chris@43: m_modulationbuf[i] = 0.0; Chris@43: } Chris@43: Chris@43: for (size_t i = 0; i <= m_wlen/2; ++i) { Chris@43: m_prevTransientMag[i] = 0.0; Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@43: PhaseVocoderTimeStretcher::calculateParameters() Chris@43: { Chris@43: std::cerr << "PhaseVocoderTimeStretcher::calculateParameters" << std::endl; Chris@43: Chris@43: m_wlen = 1024; Chris@43: Chris@43: //!!! In transient sharpening mode, we need to pick the window Chris@43: //length so as to be more or less fixed in audio duration (i.e. we Chris@43: //need to exploit the sample rate) Chris@43: Chris@43: //!!! have to work out the relationship between wlen and transient Chris@43: //threshold Chris@43: Chris@43: if (m_ratio < 1) { Chris@43: if (m_ratio < 0.4) { Chris@43: m_n1 = 1024; Chris@43: m_wlen = 2048; Chris@43: } else if (m_ratio < 0.8) { Chris@43: m_n1 = 512; Chris@43: } else { Chris@43: m_n1 = 256; Chris@43: } Chris@43: if (shouldSharpen()) { Chris@43: m_wlen = 2048; Chris@43: } Chris@43: m_n2 = lrintf(m_n1 * m_ratio); Chris@43: } else { Chris@43: if (m_ratio > 2) { Chris@43: m_n2 = 512; Chris@43: m_wlen = 4096; Chris@43: } else if (m_ratio > 1.6) { Chris@43: m_n2 = 384; Chris@43: m_wlen = 2048; Chris@43: } else { Chris@43: m_n2 = 256; Chris@43: } Chris@43: if (shouldSharpen()) { Chris@43: if (m_wlen < 2048) m_wlen = 2048; Chris@43: } Chris@43: m_n1 = lrintf(m_n2 / m_ratio); Chris@43: if (m_n1 == 0) { Chris@43: m_n1 = 1; Chris@43: m_n2 = lrintf(m_ratio); Chris@43: } Chris@43: } Chris@43: Chris@43: m_transientThreshold = lrintf(m_wlen / 4.5); Chris@43: Chris@43: m_totalCount = 0; Chris@43: m_transientCount = 0; Chris@43: m_n2sum = 0; Chris@43: Chris@43: Chris@43: std::cerr << "PhaseVocoderTimeStretcher: channels = " << m_channels Chris@43: << ", ratio = " << m_ratio Chris@43: << ", n1 = " << m_n1 << ", n2 = " << m_n2 << ", wlen = " Chris@43: << m_wlen << ", max = " << m_maxOutputBlockSize << std::endl; Chris@43: // << ", outbuflen = " << m_outbuf[0]->getSize() << std::endl; Chris@43: } Chris@43: Chris@43: void Chris@43: PhaseVocoderTimeStretcher::cleanup() Chris@43: { Chris@43: std::cerr << "PhaseVocoderTimeStretcher::cleanup" << std::endl; Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: Chris@43: fftf_destroy_plan(m_plan[c]); Chris@43: fftf_destroy_plan(m_iplan[c]); Chris@43: Chris@43: fftf_free(m_time[c]); Chris@43: fftf_free(m_freq[c]); Chris@43: Chris@43: fftf_free(m_mashbuf[c]); Chris@43: fftf_free(m_prevPhase[c]); Chris@43: fftf_free(m_prevAdjustedPhase[c]); Chris@43: Chris@43: delete m_inbuf[c]; Chris@43: delete m_outbuf[c]; Chris@43: } Chris@43: Chris@43: fftf_free(m_tempbuf); Chris@43: fftf_free(m_modulationbuf); Chris@43: fftf_free(m_prevTransientMag); Chris@43: Chris@43: delete[] m_prevPhase; Chris@43: delete[] m_prevAdjustedPhase; Chris@43: delete[] m_inbuf; Chris@43: delete[] m_outbuf; Chris@43: delete[] m_mashbuf; Chris@43: delete[] m_time; Chris@43: delete[] m_freq; Chris@43: delete[] m_plan; Chris@43: delete[] m_iplan; Chris@43: Chris@43: delete m_analysisWindow; Chris@43: delete m_synthesisWindow; Chris@43: } Chris@43: Chris@43: void Chris@43: PhaseVocoderTimeStretcher::setRatio(float ratio) Chris@43: { Chris@43: QMutexLocker locker(m_mutex); Chris@43: Chris@43: size_t formerWlen = m_wlen; Chris@43: m_ratio = ratio; Chris@43: Chris@43: std::cerr << "PhaseVocoderTimeStretcher::setRatio: new ratio " << ratio Chris@43: << std::endl; Chris@43: Chris@43: calculateParameters(); Chris@43: Chris@43: if (m_wlen == formerWlen) { Chris@43: Chris@43: // This is the only container whose size depends on m_ratio Chris@43: Chris@43: RingBuffer **newin = new RingBuffer *[m_channels]; Chris@43: Chris@43: size_t formerSize = m_inbuf[0]->getSize(); Chris@43: size_t newSize = lrintf(m_outbuf[0]->getSize() / m_ratio) + m_wlen; Chris@43: Chris@43: std::cerr << "resizing inbuf from " << formerSize << " to " Chris@43: << newSize << " (outbuf size is " << m_outbuf[0]->getSize() << ", ratio " << m_ratio << ")" << std::endl; Chris@43: Chris@43: if (formerSize != newSize) { Chris@43: Chris@43: size_t ready = m_inbuf[0]->getReadSpace(); Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: newin[c] = new RingBuffer(newSize); Chris@43: } Chris@43: Chris@43: if (ready > 0) { Chris@43: Chris@43: size_t copy = std::min(ready, newSize); Chris@43: float *tmp = new float[ready]; Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: m_inbuf[c]->read(tmp, ready); Chris@43: newin[c]->write(tmp + ready - copy, copy); Chris@43: } Chris@43: Chris@43: delete[] tmp; Chris@43: } Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: delete m_inbuf[c]; Chris@43: } Chris@43: Chris@43: delete[] m_inbuf; Chris@43: m_inbuf = newin; Chris@43: } Chris@43: Chris@43: } else { Chris@43: Chris@43: std::cerr << "wlen changed" << std::endl; Chris@43: cleanup(); Chris@43: initialise(); Chris@43: } Chris@43: } Chris@43: Chris@43: size_t Chris@43: PhaseVocoderTimeStretcher::getProcessingLatency() const Chris@43: { Chris@43: return getWindowSize() - getInputIncrement(); Chris@43: } Chris@43: Chris@43: size_t Chris@43: PhaseVocoderTimeStretcher::getRequiredInputSamples() const Chris@43: { Chris@43: QMutexLocker locker(m_mutex); Chris@43: Chris@43: if (m_inbuf[0]->getReadSpace() >= m_wlen) return 0; Chris@43: return m_wlen - m_inbuf[0]->getReadSpace(); Chris@43: } Chris@43: Chris@43: void Chris@43: PhaseVocoderTimeStretcher::putInput(float **input, size_t samples) Chris@43: { Chris@43: QMutexLocker locker(m_mutex); Chris@43: Chris@43: // We need to add samples from input to our internal buffer. When Chris@43: // we have m_windowSize samples in the buffer, we can process it, Chris@43: // move the samples back by m_n1 and write the output onto our Chris@43: // internal output buffer. If we have (samples * ratio) samples Chris@43: // in that, we can write m_n2 of them back to output and return Chris@43: // (otherwise we have to write zeroes). Chris@43: Chris@43: // When we process, we write m_wlen to our fixed output buffer Chris@43: // (m_mashbuf). We then pull out the first m_n2 samples from that Chris@43: // buffer, push them into the output ring buffer, and shift Chris@43: // m_mashbuf left by that amount. Chris@43: Chris@43: // The processing latency is then m_wlen - m_n2. Chris@43: Chris@43: size_t consumed = 0; Chris@43: Chris@43: while (consumed < samples) { Chris@43: Chris@43: size_t writable = m_inbuf[0]->getWriteSpace(); Chris@43: writable = std::min(writable, samples - consumed); Chris@43: Chris@43: if (writable == 0) { Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: 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@43: #endif Chris@43: if (m_inbuf[0]->getReadSpace() < m_wlen || Chris@43: m_outbuf[0]->getWriteSpace() < m_n2) { Chris@43: std::cerr << "WARNING: PhaseVocoderTimeStretcher::putInput: Inbuf has " << m_inbuf[0]->getReadSpace() << ", outbuf has space for " << m_outbuf[0]->getWriteSpace() << " (n2 = " << m_n2 << ", wlen = " << m_wlen << "), won't be able to process" << std::endl; Chris@43: break; Chris@43: } Chris@43: } else { Chris@43: Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: std::cerr << "writing " << writable << " from index " << consumed << " to inbuf, consumed will be " << consumed + writable << std::endl; Chris@43: #endif Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: m_inbuf[c]->write(input[c] + consumed, writable); Chris@43: } Chris@43: consumed += writable; Chris@43: } Chris@43: Chris@43: while (m_inbuf[0]->getReadSpace() >= m_wlen && Chris@43: m_outbuf[0]->getWriteSpace() >= m_n2) { Chris@43: Chris@43: // We know we have at least m_wlen samples available Chris@43: // in m_inbuf. We need to peek m_wlen of them for Chris@43: // processing, and then read m_n1 to advance the read Chris@43: // pointer. Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: Chris@43: size_t got = m_inbuf[c]->peek(m_tempbuf, m_wlen); Chris@43: assert(got == m_wlen); Chris@43: Chris@43: analyseBlock(c, m_tempbuf); Chris@43: } Chris@43: Chris@43: bool transient = false; Chris@43: if (shouldSharpen()) transient = isTransient(); Chris@43: Chris@43: size_t n2 = m_n2; Chris@43: Chris@43: if (transient) { Chris@43: n2 = m_n1; Chris@43: } Chris@43: Chris@43: ++m_totalCount; Chris@43: if (transient) ++m_transientCount; Chris@43: m_n2sum += n2; Chris@43: Chris@43: // std::cerr << "ratio for last 10: " < 50 && m_transientCount < m_totalCount) { Chris@43: Chris@43: int fixed = lrintf(m_transientCount * m_n1); Chris@43: Chris@43: int idealTotal = lrintf(m_totalCount * m_n1 * m_ratio); Chris@43: int idealSquashy = idealTotal - fixed; Chris@43: Chris@43: int squashyCount = m_totalCount - m_transientCount; Chris@43: Chris@43: n2 = lrintf(idealSquashy / squashyCount); Chris@43: Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: if (n2 != m_n2) { Chris@43: std::cerr << m_n2 << " -> " << n2 << std::endl; Chris@43: } Chris@43: #endif Chris@43: } Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: Chris@43: synthesiseBlock(c, m_mashbuf[c], Chris@43: c == 0 ? m_modulationbuf : 0, Chris@43: m_prevTransient ? m_n1 : m_n2); Chris@43: Chris@43: Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: std::cerr << "writing first " << m_n2 << " from mashbuf, skipping " << m_n1 << " on inbuf " << std::endl; Chris@43: #endif Chris@43: m_inbuf[c]->skip(m_n1); Chris@43: Chris@43: for (size_t i = 0; i < n2; ++i) { Chris@43: if (m_modulationbuf[i] > 0.f) { Chris@43: m_mashbuf[c][i] /= m_modulationbuf[i]; Chris@43: } Chris@43: } Chris@43: Chris@43: m_outbuf[c]->write(m_mashbuf[c], n2); Chris@43: Chris@43: for (size_t i = 0; i < m_wlen - n2; ++i) { Chris@43: m_mashbuf[c][i] = m_mashbuf[c][i + n2]; Chris@43: } Chris@43: Chris@43: for (size_t i = m_wlen - n2; i < m_wlen; ++i) { Chris@43: m_mashbuf[c][i] = 0.0f; Chris@43: } Chris@43: } Chris@43: Chris@43: m_prevTransient = transient; Chris@43: Chris@43: for (size_t i = 0; i < m_wlen - n2; ++i) { Chris@43: m_modulationbuf[i] = m_modulationbuf[i + n2]; Chris@43: } Chris@43: Chris@43: for (size_t i = m_wlen - n2; i < m_wlen; ++i) { Chris@43: m_modulationbuf[i] = 0.0f; Chris@43: } Chris@43: Chris@43: if (!transient) m_n2 = n2; Chris@43: } Chris@43: Chris@43: Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: std::cerr << "loop ended: inbuf read space " << m_inbuf[0]->getReadSpace() << ", outbuf write space " << m_outbuf[0]->getWriteSpace() << std::endl; Chris@43: #endif Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: std::cerr << "PhaseVocoderTimeStretcher::putInput returning" << std::endl; Chris@43: #endif Chris@43: Chris@43: // std::cerr << "ratio: nominal: " << getRatio() << " actual: " Chris@43: // << m_total2 << "/" << m_total1 << " = " << float(m_total2) / float(m_total1) << " ideal: " << m_ratio << std::endl; Chris@43: } Chris@43: Chris@43: size_t Chris@43: PhaseVocoderTimeStretcher::getAvailableOutputSamples() const Chris@43: { Chris@43: QMutexLocker locker(m_mutex); Chris@43: Chris@43: return m_outbuf[0]->getReadSpace(); Chris@43: } Chris@43: Chris@43: void Chris@43: PhaseVocoderTimeStretcher::getOutput(float **output, size_t samples) Chris@43: { Chris@43: QMutexLocker locker(m_mutex); Chris@43: Chris@43: if (m_outbuf[0]->getReadSpace() < samples) { Chris@43: std::cerr << "WARNING: PhaseVocoderTimeStretcher::getOutput: not enough data (yet?) (" << m_outbuf[0]->getReadSpace() << " < " << samples << ")" << std::endl; Chris@43: size_t fill = samples - m_outbuf[0]->getReadSpace(); Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: for (size_t i = 0; i < fill; ++i) { Chris@43: output[c][i] = 0.0; Chris@43: } Chris@43: m_outbuf[c]->read(output[c] + fill, m_outbuf[c]->getReadSpace()); Chris@43: } Chris@43: } else { Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: std::cerr << "enough data - writing " << samples << " from outbuf" << std::endl; Chris@43: #endif Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: m_outbuf[c]->read(output[c], samples); Chris@43: } Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: std::cerr << "PhaseVocoderTimeStretcher::getOutput returning" << std::endl; Chris@43: #endif Chris@43: } Chris@43: Chris@43: void Chris@43: PhaseVocoderTimeStretcher::analyseBlock(size_t c, float *buf) Chris@43: { Chris@43: size_t i; Chris@43: Chris@43: // buf contains m_wlen samples Chris@43: Chris@43: #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER Chris@43: std::cerr << "PhaseVocoderTimeStretcher::analyseBlock (channel " << c << ")" << std::endl; Chris@43: #endif Chris@43: Chris@43: m_analysisWindow->cut(buf); Chris@43: Chris@43: for (i = 0; i < m_wlen/2; ++i) { Chris@43: float temp = buf[i]; Chris@43: buf[i] = buf[i + m_wlen/2]; Chris@43: buf[i + m_wlen/2] = temp; Chris@43: } Chris@43: Chris@43: for (i = 0; i < m_wlen; ++i) { Chris@43: m_time[c][i] = buf[i]; Chris@43: } Chris@43: Chris@43: fftf_execute(m_plan[c]); // m_time -> m_freq Chris@43: } Chris@43: Chris@43: bool Chris@43: PhaseVocoderTimeStretcher::isTransient() Chris@43: { Chris@43: int count = 0; Chris@43: Chris@43: for (size_t i = 0; i <= m_wlen/2; ++i) { Chris@43: Chris@43: float real = 0.f, imag = 0.f; Chris@43: Chris@43: for (size_t c = 0; c < m_channels; ++c) { Chris@43: real += m_freq[c][i][0]; Chris@43: imag += m_freq[c][i][1]; Chris@43: } Chris@43: Chris@43: float sqrmag = (real * real + imag * imag); Chris@43: Chris@43: if (m_prevTransientMag[i] > 0.f) { Chris@43: float diff = 10.f * log10f(sqrmag / m_prevTransientMag[i]); Chris@43: if (diff > 3.f) ++count; Chris@43: } Chris@43: Chris@43: m_prevTransientMag[i] = sqrmag; Chris@43: } Chris@43: Chris@43: bool isTransient = false; Chris@43: Chris@43: // if (count > m_transientThreshold && Chris@43: // count > m_prevTransientScore * 1.2) { Chris@43: if (count > m_prevTransientScore && Chris@43: count > m_transientThreshold && Chris@43: count - m_prevTransientScore > int(m_wlen) / 20) { Chris@43: isTransient = true; Chris@43: Chris@43: Chris@43: // 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@43: // } else { Chris@43: // std::cerr << " !transient (count = " << count << ", prev = " << m_prevTransientScore << ", diff = " << count - m_prevTransientScore << ")" << std::endl; Chris@43: } Chris@43: Chris@43: m_prevTransientScore = count; Chris@43: Chris@43: return isTransient; Chris@43: } Chris@43: Chris@43: void Chris@43: PhaseVocoderTimeStretcher::synthesiseBlock(size_t c, Chris@43: float *out, Chris@43: float *modulation, Chris@43: size_t lastStep) Chris@43: { Chris@43: bool unchanged = (lastStep == m_n1); Chris@43: Chris@43: for (size_t i = 0; i <= m_wlen/2; ++i) { Chris@43: Chris@43: float phase = princargf(atan2f(m_freq[c][i][1], m_freq[c][i][0])); Chris@43: float adjustedPhase = phase; Chris@43: Chris@43: if (!unchanged) { Chris@43: Chris@43: float omega = (2 * M_PI * m_n1 * i) / m_wlen; Chris@43: Chris@43: float expectedPhase = m_prevPhase[c][i] + omega; Chris@43: Chris@43: float phaseError = princargf(phase - expectedPhase); Chris@43: Chris@43: float phaseIncrement = (omega + phaseError) / m_n1; Chris@43: Chris@43: adjustedPhase = m_prevAdjustedPhase[c][i] + Chris@43: lastStep * phaseIncrement; Chris@43: Chris@43: float mag = sqrtf(m_freq[c][i][0] * m_freq[c][i][0] + Chris@43: m_freq[c][i][1] * m_freq[c][i][1]); Chris@43: Chris@43: float real = mag * cosf(adjustedPhase); Chris@43: float imag = mag * sinf(adjustedPhase); Chris@43: m_freq[c][i][0] = real; Chris@43: m_freq[c][i][1] = imag; Chris@43: } Chris@43: Chris@43: m_prevPhase[c][i] = phase; Chris@43: m_prevAdjustedPhase[c][i] = adjustedPhase; Chris@43: } Chris@43: Chris@43: fftf_execute(m_iplan[c]); // m_freq -> m_time, inverse fft Chris@43: Chris@43: for (size_t i = 0; i < m_wlen/2; ++i) { Chris@43: float temp = m_time[c][i]; Chris@43: m_time[c][i] = m_time[c][i + m_wlen/2]; Chris@43: m_time[c][i + m_wlen/2] = temp; Chris@43: } Chris@43: Chris@43: for (size_t i = 0; i < m_wlen; ++i) { Chris@43: m_time[c][i] = m_time[c][i] / m_wlen; Chris@43: } Chris@43: Chris@43: m_synthesisWindow->cut(m_time[c]); Chris@43: Chris@43: for (size_t i = 0; i < m_wlen; ++i) { Chris@43: out[i] += m_time[c][i]; Chris@43: } Chris@43: Chris@43: if (modulation) { Chris@43: Chris@43: float area = m_analysisWindow->getArea(); Chris@43: Chris@43: for (size_t i = 0; i < m_wlen; ++i) { Chris@43: float val = m_synthesisWindow->getValue(i); Chris@43: modulation[i] += val * area; Chris@43: } Chris@43: } Chris@43: } Chris@43: Chris@43: Chris@63: #endif