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