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@14
|
23 PhaseVocoderTimeStretcher::PhaseVocoderTimeStretcher(float 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@12
|
30 m_n2(lrintf(m_n1 * ratio)),
|
Chris@0
|
31 m_wlen(std::max(windowSize, m_n2 * 2)),
|
Chris@0
|
32 m_inbuf(m_wlen),
|
Chris@12
|
33 m_outbuf(maxProcessInputBlockSize * ratio + 1024) //!!!
|
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@12
|
40 m_mashbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
|
Chris@13
|
41 m_modulationbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
|
Chris@12
|
42 m_prevPhase = (float *)fftwf_malloc(sizeof(float) * m_wlen);
|
Chris@12
|
43 m_prevAdjustedPhase = (float *)fftwf_malloc(sizeof(float) * m_wlen);
|
Chris@0
|
44
|
Chris@0
|
45 m_plan = fftwf_plan_dft_1d(m_wlen, m_time, m_freq, FFTW_FORWARD, FFTW_ESTIMATE);
|
Chris@0
|
46 m_iplan = fftwf_plan_dft_c2r_1d(m_wlen, m_freq, m_dbuf, FFTW_ESTIMATE);
|
Chris@0
|
47
|
Chris@0
|
48 for (int i = 0; i < m_wlen; ++i) {
|
Chris@0
|
49 m_mashbuf[i] = 0.0;
|
Chris@13
|
50 m_modulationbuf[i] = 0.0;
|
Chris@12
|
51 m_prevPhase[i] = 0.0;
|
Chris@12
|
52 m_prevAdjustedPhase[i] = 0.0;
|
Chris@0
|
53 }
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@14
|
56 PhaseVocoderTimeStretcher::~PhaseVocoderTimeStretcher()
|
Chris@0
|
57 {
|
Chris@14
|
58 std::cerr << "PhaseVocoderTimeStretcher::~PhaseVocoderTimeStretcher" << std::endl;
|
Chris@0
|
59
|
Chris@0
|
60 fftwf_destroy_plan(m_plan);
|
Chris@0
|
61 fftwf_destroy_plan(m_iplan);
|
Chris@0
|
62
|
Chris@0
|
63 fftwf_free(m_time);
|
Chris@0
|
64 fftwf_free(m_freq);
|
Chris@0
|
65 fftwf_free(m_dbuf);
|
Chris@12
|
66 fftwf_free(m_mashbuf);
|
Chris@13
|
67 fftwf_free(m_modulationbuf);
|
Chris@12
|
68 fftwf_free(m_prevPhase);
|
Chris@12
|
69 fftwf_free(m_prevAdjustedPhase);
|
Chris@0
|
70
|
Chris@0
|
71 delete m_window;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 size_t
|
Chris@14
|
75 PhaseVocoderTimeStretcher::getProcessingLatency() const
|
Chris@0
|
76 {
|
Chris@0
|
77 return getWindowSize() - getInputIncrement();
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 void
|
Chris@14
|
81 PhaseVocoderTimeStretcher::process(float *input, float *output, size_t samples)
|
Chris@0
|
82 {
|
Chris@0
|
83 // We need to add samples from input to our internal buffer. When
|
Chris@0
|
84 // we have m_windowSize samples in the buffer, we can process it,
|
Chris@0
|
85 // move the samples back by m_n1 and write the output onto our
|
Chris@0
|
86 // internal output buffer. If we have (samples * ratio) samples
|
Chris@0
|
87 // in that, we can write m_n2 of them back to output and return
|
Chris@0
|
88 // (otherwise we have to write zeroes).
|
Chris@0
|
89
|
Chris@0
|
90 // When we process, we write m_wlen to our fixed output buffer
|
Chris@0
|
91 // (m_mashbuf). We then pull out the first m_n2 samples from that
|
Chris@0
|
92 // buffer, push them into the output ring buffer, and shift
|
Chris@0
|
93 // m_mashbuf left by that amount.
|
Chris@0
|
94
|
Chris@0
|
95 // The processing latency is then m_wlen - m_n2.
|
Chris@0
|
96
|
Chris@0
|
97 size_t consumed = 0;
|
Chris@0
|
98
|
Chris@14
|
99 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
|
Chris@14
|
100 std::cerr << "PhaseVocoderTimeStretcher::process(" << samples << ", consumed = " << consumed << "), writable " << m_inbuf.getWriteSpace() <<", readable "<< m_outbuf.getReadSpace() << std::endl;
|
Chris@0
|
101 #endif
|
Chris@0
|
102
|
Chris@0
|
103 while (consumed < samples) {
|
Chris@0
|
104
|
Chris@0
|
105 size_t writable = m_inbuf.getWriteSpace();
|
Chris@0
|
106 writable = std::min(writable, samples - consumed);
|
Chris@0
|
107
|
Chris@0
|
108 if (writable == 0) {
|
Chris@0
|
109 //!!! then what? I don't think this should happen, but
|
Chris@14
|
110 std::cerr << "WARNING: PhaseVocoderTimeStretcher::process: writable == 0" << std::endl;
|
Chris@0
|
111 break;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@14
|
114 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
|
Chris@0
|
115 std::cerr << "writing " << writable << " from index " << consumed << " to inbuf, consumed will be " << consumed + writable << std::endl;
|
Chris@0
|
116 #endif
|
Chris@0
|
117 m_inbuf.write(input + consumed, writable);
|
Chris@0
|
118 consumed += writable;
|
Chris@0
|
119
|
Chris@0
|
120 while (m_inbuf.getReadSpace() >= m_wlen &&
|
Chris@0
|
121 m_outbuf.getWriteSpace() >= m_n2) {
|
Chris@0
|
122
|
Chris@0
|
123 // We know we have at least m_wlen samples available
|
Chris@0
|
124 // in m_inbuf. We need to peek m_wlen of them for
|
Chris@0
|
125 // processing, and then read m_n1 to advance the read
|
Chris@0
|
126 // pointer.
|
Chris@0
|
127
|
Chris@0
|
128 size_t got = m_inbuf.peek(m_dbuf, m_wlen);
|
Chris@0
|
129 assert(got == m_wlen);
|
Chris@0
|
130
|
Chris@13
|
131 processBlock(m_dbuf, m_mashbuf, m_modulationbuf);
|
Chris@0
|
132
|
Chris@14
|
133 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
|
Chris@0
|
134 std::cerr << "writing first " << m_n2 << " from mashbuf, skipping " << m_n1 << " on inbuf " << std::endl;
|
Chris@0
|
135 #endif
|
Chris@0
|
136 m_inbuf.skip(m_n1);
|
Chris@13
|
137
|
Chris@13
|
138 for (size_t i = 0; i < m_n2; ++i) {
|
Chris@13
|
139 if (m_modulationbuf[i] > 0.f) {
|
Chris@13
|
140 m_mashbuf[i] /= m_modulationbuf[i];
|
Chris@13
|
141 }
|
Chris@13
|
142 }
|
Chris@13
|
143
|
Chris@0
|
144 m_outbuf.write(m_mashbuf, m_n2);
|
Chris@0
|
145
|
Chris@0
|
146 for (size_t i = 0; i < m_wlen - m_n2; ++i) {
|
Chris@0
|
147 m_mashbuf[i] = m_mashbuf[i + m_n2];
|
Chris@13
|
148 m_modulationbuf[i] = m_modulationbuf[i + m_n2];
|
Chris@0
|
149 }
|
Chris@13
|
150
|
Chris@0
|
151 for (size_t i = m_wlen - m_n2; i < m_wlen; ++i) {
|
Chris@0
|
152 m_mashbuf[i] = 0.0f;
|
Chris@13
|
153 m_modulationbuf[i] = 0.0f;
|
Chris@0
|
154 }
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@14
|
157 // std::cerr << "WARNING: PhaseVocoderTimeStretcher::process: writespace not enough for output increment (" << m_outbuf.getWriteSpace() << " < " << m_n2 << ")" << std::endl;
|
Chris@0
|
158 // }
|
Chris@0
|
159
|
Chris@14
|
160 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
|
Chris@0
|
161 std::cerr << "loop ended: inbuf read space " << m_inbuf.getReadSpace() << ", outbuf write space " << m_outbuf.getWriteSpace() << std::endl;
|
Chris@0
|
162 #endif
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@12
|
165 size_t toRead = lrintf(samples * m_ratio);
|
Chris@12
|
166
|
Chris@12
|
167 if (m_outbuf.getReadSpace() < toRead) {
|
Chris@14
|
168 std::cerr << "WARNING: PhaseVocoderTimeStretcher::process: not enough data (yet?) (" << m_outbuf.getReadSpace() << " < " << toRead << ")" << std::endl;
|
Chris@12
|
169 size_t fill = toRead - m_outbuf.getReadSpace();
|
Chris@0
|
170 for (size_t i = 0; i < fill; ++i) {
|
Chris@0
|
171 output[i] = 0.0;
|
Chris@0
|
172 }
|
Chris@0
|
173 m_outbuf.read(output + fill, m_outbuf.getReadSpace());
|
Chris@0
|
174 } else {
|
Chris@14
|
175 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
|
Chris@12
|
176 std::cerr << "enough data - writing " << toRead << " from outbuf" << std::endl;
|
Chris@0
|
177 #endif
|
Chris@12
|
178 m_outbuf.read(output, toRead);
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@14
|
181 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
|
Chris@14
|
182 std::cerr << "PhaseVocoderTimeStretcher::process returning" << std::endl;
|
Chris@0
|
183 #endif
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 void
|
Chris@14
|
187 PhaseVocoderTimeStretcher::processBlock(float *buf, float *out, float *modulation)
|
Chris@0
|
188 {
|
Chris@0
|
189 size_t i;
|
Chris@0
|
190
|
Chris@0
|
191 // buf contains m_wlen samples; out contains enough space for
|
Chris@0
|
192 // m_wlen * ratio samples (we mix into out, rather than replacing)
|
Chris@0
|
193
|
Chris@14
|
194 #ifdef DEBUG_PHASE_VOCODER_TIME_STRETCHER
|
Chris@14
|
195 std::cerr << "PhaseVocoderTimeStretcher::processBlock" << std::endl;
|
Chris@0
|
196 #endif
|
Chris@0
|
197
|
Chris@0
|
198 m_window->cut(buf);
|
Chris@0
|
199
|
Chris@0
|
200 for (i = 0; i < m_wlen/2; ++i) {
|
Chris@0
|
201 float temp = buf[i];
|
Chris@0
|
202 buf[i] = buf[i + m_wlen/2];
|
Chris@0
|
203 buf[i + m_wlen/2] = temp;
|
Chris@0
|
204 }
|
Chris@0
|
205
|
Chris@0
|
206 for (i = 0; i < m_wlen; ++i) {
|
Chris@0
|
207 m_time[i][0] = buf[i];
|
Chris@0
|
208 m_time[i][1] = 0.0;
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 fftwf_execute(m_plan); // m_time -> m_freq
|
Chris@0
|
212
|
Chris@0
|
213 for (i = 0; i < m_wlen; ++i) {
|
Chris@0
|
214
|
Chris@0
|
215 float mag = sqrtf(m_freq[i][0] * m_freq[i][0] +
|
Chris@0
|
216 m_freq[i][1] * m_freq[i][1]);
|
Chris@0
|
217
|
Chris@12
|
218 float phase = princargf(atan2f(m_freq[i][1], m_freq[i][0]));
|
Chris@12
|
219
|
Chris@12
|
220 float omega = (2 * M_PI * m_n1 * i) / m_wlen;
|
Chris@0
|
221
|
Chris@12
|
222 float expectedPhase = m_prevPhase[i] + omega;
|
Chris@12
|
223
|
Chris@12
|
224 float phaseError = princargf(phase - expectedPhase);
|
Chris@12
|
225
|
Chris@12
|
226 float phaseIncrement = (omega + phaseError) / m_n1;
|
Chris@12
|
227
|
Chris@12
|
228 float adjustedPhase = m_prevAdjustedPhase[i] + m_n2 * phaseIncrement;
|
Chris@0
|
229
|
Chris@12
|
230 float real = mag * cosf(adjustedPhase);
|
Chris@12
|
231 float imag = mag * sinf(adjustedPhase);
|
Chris@0
|
232 m_freq[i][0] = real;
|
Chris@0
|
233 m_freq[i][1] = imag;
|
Chris@12
|
234
|
Chris@12
|
235 m_prevPhase[i] = phase;
|
Chris@12
|
236 m_prevAdjustedPhase[i] = adjustedPhase;
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 fftwf_execute(m_iplan); // m_freq -> in, inverse fft
|
Chris@0
|
240
|
Chris@0
|
241 for (i = 0; i < m_wlen/2; ++i) {
|
Chris@0
|
242 float temp = buf[i] / m_wlen;
|
Chris@0
|
243 buf[i] = buf[i + m_wlen/2] / m_wlen;
|
Chris@0
|
244 buf[i + m_wlen/2] = temp;
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 m_window->cut(buf);
|
Chris@13
|
248 /*
|
Chris@0
|
249 int div = m_wlen / m_n2;
|
Chris@0
|
250 if (div > 1) div /= 2;
|
Chris@0
|
251 for (i = 0; i < m_wlen; ++i) {
|
Chris@0
|
252 buf[i] /= div;
|
Chris@0
|
253 }
|
Chris@13
|
254 */
|
Chris@0
|
255 for (i = 0; i < m_wlen; ++i) {
|
Chris@0
|
256 out[i] += buf[i];
|
Chris@13
|
257 modulation[i] += m_window->getValue(i);
|
Chris@0
|
258 }
|
Chris@0
|
259 }
|