c@116
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@116
|
2 /*
|
c@116
|
3 Constant-Q library
|
c@116
|
4 Copyright (c) 2013-2014 Queen Mary, University of London
|
c@116
|
5
|
c@116
|
6 Permission is hereby granted, free of charge, to any person
|
c@116
|
7 obtaining a copy of this software and associated documentation
|
c@116
|
8 files (the "Software"), to deal in the Software without
|
c@116
|
9 restriction, including without limitation the rights to use, copy,
|
c@116
|
10 modify, merge, publish, distribute, sublicense, and/or sell copies
|
c@116
|
11 of the Software, and to permit persons to whom the Software is
|
c@116
|
12 furnished to do so, subject to the following conditions:
|
c@116
|
13
|
c@116
|
14 The above copyright notice and this permission notice shall be
|
c@116
|
15 included in all copies or substantial portions of the Software.
|
c@116
|
16
|
c@116
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
c@116
|
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
c@116
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
c@116
|
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
c@116
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
c@116
|
22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
c@116
|
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
c@116
|
24
|
c@116
|
25 Except as contained in this notice, the names of the Centre for
|
c@116
|
26 Digital Music; Queen Mary, University of London; and Chris Cannam
|
c@116
|
27 shall not be used in advertising or otherwise to promote the sale,
|
c@116
|
28 use or other dealings in this Software without prior written
|
c@116
|
29 authorization.
|
c@116
|
30 */
|
c@116
|
31
|
c@116
|
32 #include "CQInverse.h"
|
c@116
|
33
|
c@116
|
34 #include "dsp/rateconversion/Resampler.h"
|
c@116
|
35 #include "maths/MathUtilities.h"
|
c@116
|
36 #include "dsp/transforms/FFT.h"
|
c@116
|
37
|
c@116
|
38 #include <algorithm>
|
c@116
|
39 #include <iostream>
|
c@116
|
40 #include <stdexcept>
|
c@116
|
41
|
c@116
|
42 using std::vector;
|
c@116
|
43 using std::cerr;
|
c@116
|
44 using std::endl;
|
c@116
|
45
|
c@116
|
46 //#define DEBUG_CQ 1
|
c@116
|
47
|
c@116
|
48 CQInverse::CQInverse(double sampleRate,
|
c@116
|
49 double minFreq,
|
c@116
|
50 double maxFreq,
|
c@116
|
51 int binsPerOctave) :
|
c@116
|
52 m_sampleRate(sampleRate),
|
c@116
|
53 m_maxFrequency(maxFreq),
|
c@116
|
54 m_minFrequency(minFreq),
|
c@116
|
55 m_binsPerOctave(binsPerOctave),
|
c@116
|
56 m_fft(0)
|
c@116
|
57 {
|
c@116
|
58 if (minFreq <= 0.0 || maxFreq <= 0.0) {
|
c@116
|
59 throw std::invalid_argument("Frequency extents must be positive");
|
c@116
|
60 }
|
c@116
|
61
|
c@116
|
62 initialise();
|
c@116
|
63 }
|
c@116
|
64
|
c@116
|
65 CQInverse::~CQInverse()
|
c@116
|
66 {
|
c@116
|
67 delete m_fft;
|
c@116
|
68 for (int i = 0; i < (int)m_upsamplers.size(); ++i) {
|
c@116
|
69 delete m_upsamplers[i];
|
c@116
|
70 }
|
c@116
|
71 delete m_kernel;
|
c@116
|
72 }
|
c@116
|
73
|
c@116
|
74 double
|
c@116
|
75 CQInverse::getMinFrequency() const
|
c@116
|
76 {
|
c@116
|
77 return m_p.minFrequency / pow(2.0, m_octaves - 1);
|
c@116
|
78 }
|
c@116
|
79
|
c@116
|
80 double
|
c@116
|
81 CQInverse::getBinFrequency(int bin) const
|
c@116
|
82 {
|
c@116
|
83 return getMinFrequency() * pow(2, (double(bin) / getBinsPerOctave()));
|
c@116
|
84 }
|
c@116
|
85
|
c@116
|
86 void
|
c@116
|
87 CQInverse::initialise()
|
c@116
|
88 {
|
c@116
|
89 m_octaves = int(ceil(log2(m_maxFrequency / m_minFrequency)));
|
c@116
|
90 m_kernel = new CQKernel(m_sampleRate, m_maxFrequency, m_binsPerOctave);
|
c@116
|
91 m_p = m_kernel->getProperties();
|
c@116
|
92
|
c@116
|
93 // Use exact powers of two for resampling rates. They don't have
|
c@116
|
94 // to be related to our actual samplerate: the resampler only
|
c@116
|
95 // cares about the ratio, but it only accepts integer source and
|
c@116
|
96 // target rates, and if we start from the actual samplerate we
|
c@116
|
97 // risk getting non-integer rates for lower octaves
|
c@116
|
98
|
c@116
|
99 int sourceRate = pow(2, m_octaves);
|
c@116
|
100 vector<int> latencies;
|
c@116
|
101
|
c@116
|
102 // top octave, no resampling
|
c@116
|
103 latencies.push_back(0);
|
c@116
|
104 m_upsamplers.push_back(0);
|
c@116
|
105
|
c@116
|
106 for (int i = 1; i < m_octaves; ++i) {
|
c@116
|
107
|
c@116
|
108 int factor = pow(2, i);
|
c@116
|
109
|
c@116
|
110 Resampler *r = new Resampler
|
c@116
|
111 (sourceRate / factor, sourceRate, 50, 0.05);
|
c@116
|
112
|
c@116
|
113 #ifdef DEBUG_CQ
|
c@116
|
114 cerr << "inverse: octave " << i << ": resample from " << sourceRate/factor << " to " << sourceRate << endl;
|
c@116
|
115 #endif
|
c@116
|
116
|
c@116
|
117 // See ConstantQ.cpp for discussion on latency -- output
|
c@116
|
118 // latency here is at target rate which, this way around, is
|
c@116
|
119 // what we want
|
c@116
|
120
|
c@116
|
121 latencies.push_back(r->getLatency());
|
c@116
|
122 m_upsamplers.push_back(r);
|
c@116
|
123 }
|
c@116
|
124
|
c@116
|
125 // additionally we will have fftHop latency at individual octave
|
c@116
|
126 // rate (before upsampling) for the overlap-add in each octave
|
c@116
|
127 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
128 latencies[i] += m_p.fftHop * pow(2, i);
|
c@116
|
129 }
|
c@116
|
130
|
c@116
|
131 // Now reverse the drop adjustment made in ConstantQ to align the
|
c@116
|
132 // atom centres across different octaves (but this time at output
|
c@116
|
133 // sample rate)
|
c@116
|
134
|
c@116
|
135 int emptyHops = m_p.firstCentre / m_p.atomSpacing;
|
c@116
|
136
|
c@116
|
137 vector<int> pushes;
|
c@116
|
138 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
139 int factor = pow(2, i);
|
c@116
|
140 int pushHops = emptyHops * pow(2, m_octaves - i - 1) - emptyHops;
|
c@116
|
141 int push = ((pushHops * m_p.fftHop) * factor) / m_p.atomsPerFrame;
|
c@116
|
142 pushes.push_back(push);
|
c@116
|
143 }
|
c@116
|
144
|
c@116
|
145 int maxLatLessPush = 0;
|
c@116
|
146 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
147 int latLessPush = latencies[i] - pushes[i];
|
c@116
|
148 if (latLessPush > maxLatLessPush) maxLatLessPush = latLessPush;
|
c@116
|
149 }
|
c@116
|
150
|
c@116
|
151 int totalLatency = maxLatLessPush + 10;
|
c@116
|
152 if (totalLatency < 0) totalLatency = 0;
|
c@116
|
153
|
c@116
|
154 m_outputLatency = totalLatency + m_p.firstCentre * pow(2, m_octaves-1);
|
c@116
|
155
|
c@116
|
156 #ifdef DEBUG_CQ
|
c@116
|
157 cerr << "totalLatency = " << totalLatency << ", m_outputLatency = " << m_outputLatency << endl;
|
c@116
|
158 #endif
|
c@116
|
159
|
c@116
|
160 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
161
|
c@116
|
162 // Calculate the difference between the total latency applied
|
c@116
|
163 // across all octaves, and the existing latency due to the
|
c@116
|
164 // upsampler for this octave.
|
c@116
|
165
|
c@116
|
166 int latencyPadding = totalLatency - latencies[i] + pushes[i];
|
c@116
|
167
|
c@116
|
168 #ifdef DEBUG_CQ
|
c@116
|
169 cerr << "octave " << i << ": push " << pushes[i] << ", resampler latency inc overlap space " << latencies[i] << ", latencyPadding = " << latencyPadding << " (/factor = " << latencyPadding / pow(2, i) << ")" << endl;
|
c@116
|
170 #endif
|
c@116
|
171
|
c@116
|
172 m_buffers.push_back(RealSequence(latencyPadding, 0.0));
|
c@116
|
173 }
|
c@116
|
174
|
c@116
|
175 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
176 // Fixed-size buffer for IFFT overlap-add
|
c@116
|
177 m_olaBufs.push_back(RealSequence(m_p.fftSize, 0.0));
|
c@116
|
178 }
|
c@116
|
179
|
c@116
|
180 m_fft = new FFTReal(m_p.fftSize);
|
c@116
|
181 }
|
c@116
|
182
|
c@116
|
183 CQInverse::RealSequence
|
c@116
|
184 CQInverse::process(const ComplexBlock &block)
|
c@116
|
185 {
|
c@116
|
186 // The input data is of the form produced by ConstantQ::process --
|
c@116
|
187 // an unknown number N of columns of varying height. We assert
|
c@116
|
188 // that N is a multiple of atomsPerFrame * 2^(octaves-1), as must
|
c@116
|
189 // be the case for data that came directly from our ConstantQ
|
c@116
|
190 // implementation.
|
c@116
|
191
|
c@116
|
192 int widthProvided = block.size();
|
c@116
|
193
|
c@116
|
194 if (widthProvided == 0) {
|
c@116
|
195 return drawFromBuffers();
|
c@116
|
196 }
|
c@116
|
197
|
c@116
|
198 int blockWidth = m_p.atomsPerFrame * int(pow(2, m_octaves - 1));
|
c@116
|
199
|
c@116
|
200 if (widthProvided % blockWidth != 0) {
|
c@116
|
201 cerr << "ERROR: CQInverse::process: Input block size ("
|
c@116
|
202 << widthProvided
|
c@116
|
203 << ") must be a multiple of processing block width "
|
c@116
|
204 << "(atoms-per-frame * 2^(octaves-1) = "
|
c@116
|
205 << m_p.atomsPerFrame << " * 2^(" << m_octaves << "-1) = "
|
c@116
|
206 << blockWidth << ")" << endl;
|
c@116
|
207 throw std::invalid_argument
|
c@116
|
208 ("Input block size must be a multiple of processing block width");
|
c@116
|
209 }
|
c@116
|
210
|
c@116
|
211 // Procedure:
|
c@116
|
212 //
|
c@116
|
213 // 1. Slice the list of columns into a set of lists of columns,
|
c@116
|
214 // one per octave, each of width N / (2^octave-1) and height
|
c@116
|
215 // binsPerOctave, containing the values present in that octave
|
c@116
|
216 //
|
c@116
|
217 // 2. Group each octave list by atomsPerFrame columns at a time,
|
c@116
|
218 // and stack these so as to achieve a list, for each octave, of
|
c@116
|
219 // taller columns of height binsPerOctave * atomsPerFrame
|
c@116
|
220 //
|
c@116
|
221 // 3. For each taller column, take the product with the inverse CQ
|
c@116
|
222 // kernel (which is the conjugate of the forward kernel) and
|
c@116
|
223 // perform an inverse FFT
|
c@116
|
224 //
|
c@116
|
225 // 4. Overlap-add each octave's resynthesised blocks (unwindowed)
|
c@116
|
226 //
|
c@116
|
227 // 5. Resample each octave's overlap-add stream to the original
|
c@116
|
228 // rate
|
c@116
|
229 //
|
c@116
|
230 // 6. Sum the resampled streams and return
|
c@116
|
231
|
c@116
|
232 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
233
|
c@116
|
234 // Step 1
|
c@116
|
235
|
c@116
|
236 ComplexBlock oct;
|
c@116
|
237
|
c@116
|
238 for (int j = 0; j < widthProvided; ++j) {
|
c@116
|
239 int h = block[j].size();
|
c@116
|
240 if (h < m_binsPerOctave * (i+1)) {
|
c@116
|
241 continue;
|
c@116
|
242 }
|
c@116
|
243 ComplexColumn col(block[j].begin() + m_binsPerOctave * i,
|
c@116
|
244 block[j].begin() + m_binsPerOctave * (i+1));
|
c@116
|
245 oct.push_back(col);
|
c@116
|
246 }
|
c@116
|
247
|
c@116
|
248 // Steps 2, 3, 4, 5
|
c@116
|
249 processOctave(i, oct);
|
c@116
|
250 }
|
c@116
|
251
|
c@116
|
252 // Step 6
|
c@116
|
253 return drawFromBuffers();
|
c@116
|
254 }
|
c@116
|
255
|
c@116
|
256 CQInverse::RealSequence
|
c@116
|
257 CQInverse::drawFromBuffers()
|
c@116
|
258 {
|
c@116
|
259 // 6. Sum the resampled streams and return
|
c@116
|
260
|
c@116
|
261 int available = 0;
|
c@116
|
262
|
c@116
|
263 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
264 if (i == 0 || int(m_buffers[i].size()) < available) {
|
c@116
|
265 available = m_buffers[i].size();
|
c@116
|
266 }
|
c@116
|
267 }
|
c@116
|
268
|
c@116
|
269 RealSequence result(available, 0);
|
c@116
|
270
|
c@116
|
271 if (available == 0) {
|
c@116
|
272 return result;
|
c@116
|
273 }
|
c@116
|
274
|
c@116
|
275 for (int i = 0; i < m_octaves; ++i) {
|
c@116
|
276 for (int j = 0; j < available; ++j) {
|
c@116
|
277 result[j] += m_buffers[i][j];
|
c@116
|
278 }
|
c@116
|
279 m_buffers[i] = RealSequence(m_buffers[i].begin() + available,
|
c@116
|
280 m_buffers[i].end());
|
c@116
|
281 }
|
c@116
|
282
|
c@116
|
283 return result;
|
c@116
|
284 }
|
c@116
|
285
|
c@116
|
286 CQInverse::RealSequence
|
c@116
|
287 CQInverse::getRemainingOutput()
|
c@116
|
288 {
|
c@116
|
289 for (int j = 0; j < m_octaves; ++j) {
|
c@116
|
290 int factor = pow(2, j);
|
c@116
|
291 int latency = (j > 0 ? m_upsamplers[j]->getLatency() : 0) / factor;
|
c@116
|
292 for (int i = 0; i < (latency + m_p.fftSize) / m_p.fftHop; ++i) {
|
c@116
|
293 overlapAddAndResample(j, RealSequence(m_olaBufs[j].size(), 0));
|
c@116
|
294 }
|
c@116
|
295 }
|
c@116
|
296
|
c@116
|
297 return drawFromBuffers();
|
c@116
|
298 }
|
c@116
|
299
|
c@116
|
300 void
|
c@116
|
301 CQInverse::processOctave(int octave, const ComplexBlock &columns)
|
c@116
|
302 {
|
c@116
|
303 // 2. Group each octave list by atomsPerFrame columns at a time,
|
c@116
|
304 // and stack these so as to achieve a list, for each octave, of
|
c@116
|
305 // taller columns of height binsPerOctave * atomsPerFrame
|
c@116
|
306
|
c@116
|
307 int ncols = columns.size();
|
c@116
|
308
|
c@116
|
309 if (ncols % m_p.atomsPerFrame != 0) {
|
c@116
|
310 cerr << "ERROR: CQInverse::process: Number of columns ("
|
c@116
|
311 << ncols
|
c@116
|
312 << ") in octave " << octave
|
c@116
|
313 << " must be a multiple of atoms-per-frame ("
|
c@116
|
314 << m_p.atomsPerFrame << ")" << endl;
|
c@116
|
315 throw std::invalid_argument
|
c@116
|
316 ("Columns in octave must be a multiple of atoms per frame");
|
c@116
|
317 }
|
c@116
|
318
|
c@116
|
319 for (int i = 0; i < ncols; i += m_p.atomsPerFrame) {
|
c@116
|
320
|
c@116
|
321 ComplexColumn tallcol;
|
c@116
|
322 for (int b = 0; b < m_binsPerOctave; ++b) {
|
c@116
|
323 for (int a = 0; a < m_p.atomsPerFrame; ++a) {
|
c@116
|
324 tallcol.push_back(columns[i + a][m_binsPerOctave - b - 1]);
|
c@116
|
325 }
|
c@116
|
326 }
|
c@116
|
327
|
c@116
|
328 processOctaveColumn(octave, tallcol);
|
c@116
|
329 }
|
c@116
|
330 }
|
c@116
|
331
|
c@116
|
332 void
|
c@116
|
333 CQInverse::processOctaveColumn(int octave, const ComplexColumn &column)
|
c@116
|
334 {
|
c@116
|
335 // 3. For each taller column, take the product with the inverse CQ
|
c@116
|
336 // kernel (which is the conjugate of the forward kernel) and
|
c@116
|
337 // perform an inverse FFT
|
c@116
|
338
|
c@116
|
339 if ((int)column.size() != m_p.atomsPerFrame * m_binsPerOctave) {
|
c@116
|
340 cerr << "ERROR: CQInverse::processOctaveColumn: Height of column ("
|
c@116
|
341 << column.size() << ") in octave " << octave
|
c@116
|
342 << " must be atoms-per-frame * bins-per-octave ("
|
c@116
|
343 << m_p.atomsPerFrame << " * " << m_binsPerOctave << " = "
|
c@116
|
344 << m_p.atomsPerFrame * m_binsPerOctave << ")" << endl;
|
c@116
|
345 throw std::invalid_argument
|
c@116
|
346 ("Column height must match atoms-per-frame * bins-per-octave");
|
c@116
|
347 }
|
c@116
|
348
|
c@116
|
349 ComplexSequence transformed = m_kernel->processInverse(column);
|
c@116
|
350
|
c@116
|
351 int halfLen = m_p.fftSize/2 + 1;
|
c@116
|
352
|
c@116
|
353 RealSequence ri(halfLen, 0);
|
c@116
|
354 RealSequence ii(halfLen, 0);
|
c@116
|
355
|
c@116
|
356 for (int i = 0; i < halfLen; ++i) {
|
c@116
|
357 ri[i] = transformed[i].real();
|
c@116
|
358 ii[i] = transformed[i].imag();
|
c@116
|
359 }
|
c@116
|
360
|
c@116
|
361 RealSequence timeDomain(m_p.fftSize, 0);
|
c@116
|
362
|
c@116
|
363 m_fft->inverse(ri.data(), ii.data(), timeDomain.data());
|
c@116
|
364
|
c@116
|
365 overlapAddAndResample(octave, timeDomain);
|
c@116
|
366 }
|
c@116
|
367
|
c@116
|
368 void
|
c@116
|
369 CQInverse::overlapAddAndResample(int octave, const RealSequence &seq)
|
c@116
|
370 {
|
c@116
|
371 // 4. Overlap-add each octave's resynthesised blocks (unwindowed)
|
c@116
|
372 //
|
c@116
|
373 // and
|
c@116
|
374 //
|
c@116
|
375 // 5. Resample each octave's overlap-add stream to the original
|
c@116
|
376 // rate
|
c@116
|
377
|
c@116
|
378 if (seq.size() != m_olaBufs[octave].size()) {
|
c@116
|
379 cerr << "ERROR: CQInverse::overlapAdd: input sequence length ("
|
c@116
|
380 << seq.size() << ") is expected to match OLA buffer size ("
|
c@116
|
381 << m_olaBufs[octave].size() << ")" << endl;
|
c@116
|
382 throw std::invalid_argument
|
c@116
|
383 ("Input sequence length should match OLA buffer size");
|
c@116
|
384 }
|
c@116
|
385
|
c@116
|
386 RealSequence toResample(m_olaBufs[octave].begin(),
|
c@116
|
387 m_olaBufs[octave].begin() + m_p.fftHop);
|
c@116
|
388
|
c@116
|
389 RealSequence resampled =
|
c@116
|
390 octave > 0 ?
|
c@116
|
391 m_upsamplers[octave]->process(toResample.data(), toResample.size()) :
|
c@116
|
392 toResample;
|
c@116
|
393
|
c@116
|
394 m_buffers[octave].insert(m_buffers[octave].end(),
|
c@116
|
395 resampled.begin(),
|
c@116
|
396 resampled.end());
|
c@116
|
397
|
c@116
|
398 m_olaBufs[octave] = RealSequence(m_olaBufs[octave].begin() + m_p.fftHop,
|
c@116
|
399 m_olaBufs[octave].end());
|
c@116
|
400
|
c@116
|
401 RealSequence pad(m_p.fftHop, 0);
|
c@116
|
402
|
c@116
|
403 m_olaBufs[octave].insert(m_olaBufs[octave].end(),
|
c@116
|
404 pad.begin(),
|
c@116
|
405 pad.end());
|
c@116
|
406
|
c@116
|
407 for (int i = 0; i < m_p.fftSize; ++i) {
|
c@116
|
408 m_olaBufs[octave][i] += seq[i];
|
c@116
|
409 }
|
c@116
|
410 }
|
c@116
|
411
|