c@362
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@375
|
2 /*
|
c@375
|
3 QM DSP Library
|
c@375
|
4
|
c@375
|
5 Centre for Digital Music, Queen Mary, University of London.
|
c@375
|
6 This file by Chris Cannam.
|
c@375
|
7
|
c@375
|
8 This program is free software; you can redistribute it and/or
|
c@375
|
9 modify it under the terms of the GNU General Public License as
|
c@375
|
10 published by the Free Software Foundation; either version 2 of the
|
c@375
|
11 License, or (at your option) any later version. See the file
|
c@375
|
12 COPYING included with this distribution for more information.
|
c@375
|
13 */
|
c@362
|
14
|
c@362
|
15 #include "Resampler.h"
|
c@362
|
16
|
c@375
|
17 #include "maths/MathUtilities.h"
|
c@375
|
18 #include "base/KaiserWindow.h"
|
c@375
|
19 #include "base/SincWindow.h"
|
c@362
|
20
|
c@362
|
21 #include <iostream>
|
c@363
|
22 #include <vector>
|
c@370
|
23 #include <map>
|
c@372
|
24 #include <cassert>
|
c@419
|
25 #include <algorithm>
|
c@363
|
26
|
c@363
|
27 using std::vector;
|
c@370
|
28 using std::map;
|
c@398
|
29 using std::cerr;
|
c@398
|
30 using std::endl;
|
c@362
|
31
|
c@366
|
32 //#define DEBUG_RESAMPLER 1
|
c@398
|
33 //#define DEBUG_RESAMPLER_VERBOSE 1
|
c@366
|
34
|
c@362
|
35 Resampler::Resampler(int sourceRate, int targetRate) :
|
c@362
|
36 m_sourceRate(sourceRate),
|
c@362
|
37 m_targetRate(targetRate)
|
c@362
|
38 {
|
c@419
|
39 #ifdef DEBUG_RESAMPLER
|
c@419
|
40 cerr << "Resampler::Resampler(" << sourceRate << "," << targetRate << ")" << endl;
|
c@419
|
41 #endif
|
c@374
|
42 initialise(100, 0.02);
|
c@374
|
43 }
|
c@374
|
44
|
c@374
|
45 Resampler::Resampler(int sourceRate, int targetRate,
|
c@374
|
46 double snr, double bandwidth) :
|
c@374
|
47 m_sourceRate(sourceRate),
|
c@374
|
48 m_targetRate(targetRate)
|
c@374
|
49 {
|
c@374
|
50 initialise(snr, bandwidth);
|
c@362
|
51 }
|
c@362
|
52
|
c@362
|
53 Resampler::~Resampler()
|
c@362
|
54 {
|
c@362
|
55 delete[] m_phaseData;
|
c@362
|
56 }
|
c@362
|
57
|
c@362
|
58 void
|
c@374
|
59 Resampler::initialise(double snr, double bandwidth)
|
c@362
|
60 {
|
c@362
|
61 int higher = std::max(m_sourceRate, m_targetRate);
|
c@362
|
62 int lower = std::min(m_sourceRate, m_targetRate);
|
c@362
|
63
|
c@362
|
64 m_gcd = MathUtilities::gcd(lower, higher);
|
c@381
|
65 m_peakToPole = higher / m_gcd;
|
c@362
|
66
|
c@381
|
67 if (m_targetRate < m_sourceRate) {
|
c@381
|
68 // antialiasing filter, should be slightly below nyquist
|
c@381
|
69 m_peakToPole = m_peakToPole / (1.0 - bandwidth/2.0);
|
c@381
|
70 }
|
c@362
|
71
|
c@362
|
72 KaiserWindow::Parameters params =
|
cannam@483
|
73 KaiserWindow::parametersForBandwidth(snr, bandwidth, higher / m_gcd);
|
c@362
|
74
|
c@362
|
75 params.length =
|
cannam@483
|
76 (params.length % 2 == 0 ? params.length + 1 : params.length);
|
c@362
|
77
|
c@372
|
78 params.length =
|
c@372
|
79 (params.length > 200001 ? 200001 : params.length);
|
c@372
|
80
|
c@362
|
81 m_filterLength = params.length;
|
c@370
|
82
|
c@371
|
83 vector<double> filter;
|
c@362
|
84
|
c@419
|
85 KaiserWindow kw(params);
|
c@419
|
86 SincWindow sw(m_filterLength, m_peakToPole * 2);
|
c@371
|
87
|
c@419
|
88 filter = vector<double>(m_filterLength, 0.0);
|
c@419
|
89 for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0;
|
c@419
|
90 sw.cut(filter.data());
|
c@419
|
91 kw.cut(filter.data());
|
c@419
|
92
|
c@362
|
93 int inputSpacing = m_targetRate / m_gcd;
|
c@362
|
94 int outputSpacing = m_sourceRate / m_gcd;
|
c@362
|
95
|
c@366
|
96 #ifdef DEBUG_RESAMPLER
|
c@398
|
97 cerr << "resample " << m_sourceRate << " -> " << m_targetRate
|
c@400
|
98 << ": inputSpacing " << inputSpacing << ", outputSpacing "
|
c@400
|
99 << outputSpacing << ": filter length " << m_filterLength
|
c@400
|
100 << endl;
|
c@366
|
101 #endif
|
c@362
|
102
|
c@372
|
103 // Now we have a filter of (odd) length flen in which the lower
|
c@372
|
104 // sample rate corresponds to every n'th point and the higher rate
|
c@372
|
105 // to every m'th where n and m are higher and lower rates divided
|
c@372
|
106 // by their gcd respectively. So if x coordinates are on the same
|
c@372
|
107 // scale as our filter resolution, then source sample i is at i *
|
c@372
|
108 // (targetRate / gcd) and target sample j is at j * (sourceRate /
|
c@372
|
109 // gcd).
|
c@372
|
110
|
c@372
|
111 // To reconstruct a single target sample, we want a buffer (real
|
c@372
|
112 // or virtual) of flen values formed of source samples spaced at
|
c@372
|
113 // intervals of (targetRate / gcd), in our example case 3. This
|
c@372
|
114 // is initially formed with the first sample at the filter peak.
|
c@372
|
115 //
|
c@372
|
116 // 0 0 0 0 a 0 0 b 0
|
c@372
|
117 //
|
c@372
|
118 // and of course we have our filter
|
c@372
|
119 //
|
c@372
|
120 // f1 f2 f3 f4 f5 f6 f7 f8 f9
|
c@372
|
121 //
|
c@372
|
122 // We take the sum of products of non-zero values from this buffer
|
c@372
|
123 // with corresponding values in the filter
|
c@372
|
124 //
|
c@372
|
125 // a * f5 + b * f8
|
c@372
|
126 //
|
c@372
|
127 // Then we drop (sourceRate / gcd) values, in our example case 4,
|
c@372
|
128 // from the start of the buffer and fill until it has flen values
|
c@372
|
129 // again
|
c@372
|
130 //
|
c@372
|
131 // a 0 0 b 0 0 c 0 0
|
c@372
|
132 //
|
c@372
|
133 // repeat to reconstruct the next target sample
|
c@372
|
134 //
|
c@372
|
135 // a * f1 + b * f4 + c * f7
|
c@372
|
136 //
|
c@372
|
137 // and so on.
|
c@372
|
138 //
|
c@372
|
139 // Above I said the buffer could be "real or virtual" -- ours is
|
c@372
|
140 // virtual. We don't actually store all the zero spacing values,
|
c@372
|
141 // except for padding at the start; normally we store only the
|
c@372
|
142 // values that actually came from the source stream, along with a
|
c@372
|
143 // phase value that tells us how many virtual zeroes there are at
|
c@372
|
144 // the start of the virtual buffer. So the two examples above are
|
c@372
|
145 //
|
c@372
|
146 // 0 a b [ with phase 1 ]
|
c@372
|
147 // a b c [ with phase 0 ]
|
c@372
|
148 //
|
c@372
|
149 // Having thus broken down the buffer so that only the elements we
|
c@372
|
150 // need to multiply are present, we can also unzip the filter into
|
c@372
|
151 // every-nth-element subsets at each phase, allowing us to do the
|
c@372
|
152 // filter multiplication as a simply vector multiply. That is, rather
|
c@372
|
153 // than store
|
c@372
|
154 //
|
c@372
|
155 // f1 f2 f3 f4 f5 f6 f7 f8 f9
|
c@372
|
156 //
|
c@372
|
157 // we store separately
|
c@372
|
158 //
|
c@372
|
159 // f1 f4 f7
|
c@372
|
160 // f2 f5 f8
|
c@372
|
161 // f3 f6 f9
|
c@372
|
162 //
|
c@372
|
163 // Each time we complete a multiply-and-sum, we need to work out
|
c@372
|
164 // how many (real) samples to drop from the start of our buffer,
|
c@372
|
165 // and how many to add at the end of it for the next multiply. We
|
c@372
|
166 // know we want to drop enough real samples to move along by one
|
c@372
|
167 // computed output sample, which is our outputSpacing number of
|
c@372
|
168 // virtual buffer samples. Depending on the relationship between
|
c@372
|
169 // input and output spacings, this may mean dropping several real
|
c@372
|
170 // samples, one real sample, or none at all (and simply moving to
|
c@372
|
171 // a different "phase").
|
c@372
|
172
|
c@362
|
173 m_phaseData = new Phase[inputSpacing];
|
c@362
|
174
|
c@362
|
175 for (int phase = 0; phase < inputSpacing; ++phase) {
|
c@362
|
176
|
cannam@483
|
177 Phase p;
|
c@362
|
178
|
cannam@483
|
179 p.nextPhase = phase - outputSpacing;
|
cannam@483
|
180 while (p.nextPhase < 0) p.nextPhase += inputSpacing;
|
cannam@483
|
181 p.nextPhase %= inputSpacing;
|
cannam@483
|
182
|
cannam@483
|
183 p.drop = int(ceil(std::max(0.0, double(outputSpacing - phase))
|
cannam@483
|
184 / inputSpacing));
|
c@362
|
185
|
cannam@483
|
186 int filtZipLength = int(ceil(double(m_filterLength - phase)
|
cannam@483
|
187 / inputSpacing));
|
c@372
|
188
|
cannam@483
|
189 for (int i = 0; i < filtZipLength; ++i) {
|
cannam@483
|
190 p.filter.push_back(filter[i * inputSpacing + phase]);
|
cannam@483
|
191 }
|
c@362
|
192
|
cannam@483
|
193 m_phaseData[phase] = p;
|
c@362
|
194 }
|
c@362
|
195
|
c@398
|
196 #ifdef DEBUG_RESAMPLER
|
c@398
|
197 int cp = 0;
|
c@398
|
198 int totDrop = 0;
|
c@398
|
199 for (int i = 0; i < inputSpacing; ++i) {
|
c@398
|
200 cerr << "phase = " << cp << ", drop = " << m_phaseData[cp].drop
|
c@398
|
201 << ", filter length = " << m_phaseData[cp].filter.size()
|
c@398
|
202 << ", next phase = " << m_phaseData[cp].nextPhase << endl;
|
c@398
|
203 totDrop += m_phaseData[cp].drop;
|
c@398
|
204 cp = m_phaseData[cp].nextPhase;
|
c@398
|
205 }
|
c@398
|
206 cerr << "total drop = " << totDrop << endl;
|
c@398
|
207 #endif
|
c@398
|
208
|
c@362
|
209 // The May implementation of this uses a pull model -- we ask the
|
c@362
|
210 // resampler for a certain number of output samples, and it asks
|
c@362
|
211 // its source stream for as many as it needs to calculate
|
c@362
|
212 // those. This means (among other things) that the source stream
|
c@362
|
213 // can be asked for enough samples up-front to fill the buffer
|
c@362
|
214 // before the first output sample is generated.
|
c@362
|
215 //
|
c@362
|
216 // In this implementation we're using a push model in which a
|
c@362
|
217 // certain number of source samples is provided and we're asked
|
c@362
|
218 // for as many output samples as that makes available. But we
|
c@362
|
219 // can't return any samples from the beginning until half the
|
c@362
|
220 // filter length has been provided as input. This means we must
|
c@362
|
221 // either return a very variable number of samples (none at all
|
c@362
|
222 // until the filter fills, then half the filter length at once) or
|
c@362
|
223 // else have a lengthy declared latency on the output. We do the
|
c@362
|
224 // latter. (What do other implementations do?)
|
c@373
|
225 //
|
c@372
|
226 // We want to make sure the first "real" sample will eventually be
|
c@372
|
227 // aligned with the centre sample in the filter (it's tidier, and
|
c@372
|
228 // easier to do diagnostic calculations that way). So we need to
|
c@372
|
229 // pick the initial phase and buffer fill accordingly.
|
c@372
|
230 //
|
c@372
|
231 // Example: if the inputSpacing is 2, outputSpacing is 3, and
|
c@372
|
232 // filter length is 7,
|
c@372
|
233 //
|
c@372
|
234 // x x x x a b c ... input samples
|
c@372
|
235 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
|
c@372
|
236 // i j k l ... output samples
|
c@372
|
237 // [--------|--------] <- filter with centre mark
|
c@372
|
238 //
|
c@372
|
239 // Let h be the index of the centre mark, here 3 (generally
|
c@372
|
240 // int(filterLength/2) for odd-length filters).
|
c@372
|
241 //
|
c@372
|
242 // The smallest n such that h + n * outputSpacing > filterLength
|
c@372
|
243 // is 2 (that is, ceil((filterLength - h) / outputSpacing)), and
|
c@372
|
244 // (h + 2 * outputSpacing) % inputSpacing == 1, so the initial
|
c@372
|
245 // phase is 1.
|
c@372
|
246 //
|
c@372
|
247 // To achieve our n, we need to pre-fill the "virtual" buffer with
|
c@372
|
248 // 4 zero samples: the x's above. This is int((h + n *
|
c@372
|
249 // outputSpacing) / inputSpacing). It's the phase that makes this
|
c@372
|
250 // buffer get dealt with in such a way as to give us an effective
|
c@372
|
251 // index for sample a of 9 rather than 8 or 10 or whatever.
|
c@372
|
252 //
|
c@372
|
253 // This gives us output latency of 2 (== n), i.e. output samples i
|
c@372
|
254 // and j will appear before the one in which input sample a is at
|
c@372
|
255 // the centre of the filter.
|
c@372
|
256
|
c@372
|
257 int h = int(m_filterLength / 2);
|
c@372
|
258 int n = ceil(double(m_filterLength - h) / outputSpacing);
|
c@366
|
259
|
c@372
|
260 m_phase = (h + n * outputSpacing) % inputSpacing;
|
c@372
|
261
|
c@372
|
262 int fill = (h + n * outputSpacing) / inputSpacing;
|
c@372
|
263
|
c@372
|
264 m_latency = n;
|
c@372
|
265
|
c@372
|
266 m_buffer = vector<double>(fill, 0);
|
c@370
|
267 m_bufferOrigin = 0;
|
c@366
|
268
|
c@366
|
269 #ifdef DEBUG_RESAMPLER
|
c@398
|
270 cerr << "initial phase " << m_phase << " (as " << (m_filterLength/2) << " % " << inputSpacing << ")"
|
cannam@483
|
271 << ", latency " << m_latency << endl;
|
c@366
|
272 #endif
|
c@362
|
273 }
|
c@362
|
274
|
c@362
|
275 double
|
c@366
|
276 Resampler::reconstructOne()
|
c@362
|
277 {
|
c@362
|
278 Phase &pd = m_phaseData[m_phase];
|
c@366
|
279 double v = 0.0;
|
c@362
|
280 int n = pd.filter.size();
|
c@372
|
281
|
c@419
|
282 if (n + m_bufferOrigin > (int)m_buffer.size()) {
|
c@419
|
283 cerr << "ERROR: n + m_bufferOrigin > m_buffer.size() [" << n << " + "
|
c@419
|
284 << m_bufferOrigin << " > " << m_buffer.size() << "]" << endl;
|
c@419
|
285 throw std::logic_error("n + m_bufferOrigin > m_buffer.size()");
|
c@419
|
286 }
|
c@372
|
287
|
c@419
|
288 #if defined(__MSVC__)
|
c@419
|
289 #define R__ __restrict
|
c@419
|
290 #elif defined(__GNUC__)
|
c@419
|
291 #define R__ __restrict__
|
c@419
|
292 #else
|
c@419
|
293 #define R__
|
c@419
|
294 #endif
|
c@419
|
295
|
c@419
|
296 const double *const R__ buf(m_buffer.data() + m_bufferOrigin);
|
c@419
|
297 const double *const R__ filt(pd.filter.data());
|
c@372
|
298
|
c@362
|
299 for (int i = 0; i < n; ++i) {
|
cannam@483
|
300 // NB gcc can only vectorize this with -ffast-math
|
cannam@483
|
301 v += buf[i] * filt[i];
|
c@362
|
302 }
|
c@374
|
303
|
c@370
|
304 m_bufferOrigin += pd.drop;
|
c@366
|
305 m_phase = pd.nextPhase;
|
c@362
|
306 return v;
|
c@362
|
307 }
|
c@362
|
308
|
c@362
|
309 int
|
c@366
|
310 Resampler::process(const double *src, double *dst, int n)
|
c@362
|
311 {
|
c@419
|
312 m_buffer.insert(m_buffer.end(), src, src + n);
|
c@362
|
313
|
c@366
|
314 int maxout = int(ceil(double(n) * m_targetRate / m_sourceRate));
|
c@366
|
315 int outidx = 0;
|
c@364
|
316
|
c@366
|
317 #ifdef DEBUG_RESAMPLER
|
c@398
|
318 cerr << "process: buf siz " << m_buffer.size() << " filt siz for phase " << m_phase << " " << m_phaseData[m_phase].filter.size() << endl;
|
c@366
|
319 #endif
|
c@366
|
320
|
c@381
|
321 double scaleFactor = (double(m_targetRate) / m_gcd) / m_peakToPole;
|
c@367
|
322
|
c@366
|
323 while (outidx < maxout &&
|
cannam@483
|
324 m_buffer.size() >= m_phaseData[m_phase].filter.size() + m_bufferOrigin) {
|
cannam@483
|
325 dst[outidx] = scaleFactor * reconstructOne();
|
cannam@483
|
326 outidx++;
|
c@364
|
327 }
|
c@370
|
328
|
c@419
|
329 if (m_bufferOrigin > (int)m_buffer.size()) {
|
c@419
|
330 cerr << "ERROR: m_bufferOrigin > m_buffer.size() ["
|
c@419
|
331 << m_bufferOrigin << " > " << m_buffer.size() << "]" << endl;
|
c@419
|
332 throw std::logic_error("m_bufferOrigin > m_buffer.size()");
|
c@419
|
333 }
|
c@419
|
334
|
c@370
|
335 m_buffer = vector<double>(m_buffer.begin() + m_bufferOrigin, m_buffer.end());
|
c@370
|
336 m_bufferOrigin = 0;
|
c@366
|
337
|
c@366
|
338 return outidx;
|
c@362
|
339 }
|
c@366
|
340
|
c@398
|
341 vector<double>
|
c@385
|
342 Resampler::process(const double *src, int n)
|
c@385
|
343 {
|
c@385
|
344 int maxout = int(ceil(double(n) * m_targetRate / m_sourceRate));
|
c@398
|
345 vector<double> out(maxout, 0.0);
|
c@385
|
346 int got = process(src, out.data(), n);
|
c@385
|
347 assert(got <= maxout);
|
c@385
|
348 if (got < maxout) out.resize(got);
|
c@385
|
349 return out;
|
c@385
|
350 }
|
c@385
|
351
|
c@398
|
352 vector<double>
|
c@363
|
353 Resampler::resample(int sourceRate, int targetRate, const double *data, int n)
|
c@363
|
354 {
|
c@363
|
355 Resampler r(sourceRate, targetRate);
|
c@363
|
356
|
c@363
|
357 int latency = r.getLatency();
|
c@363
|
358
|
c@368
|
359 // latency is the output latency. We need to provide enough
|
c@368
|
360 // padding input samples at the end of input to guarantee at
|
c@368
|
361 // *least* the latency's worth of output samples. that is,
|
c@368
|
362
|
c@373
|
363 int inputPad = int(ceil((double(latency) * sourceRate) / targetRate));
|
c@368
|
364
|
c@368
|
365 // that means we are providing this much input in total:
|
c@368
|
366
|
c@368
|
367 int n1 = n + inputPad;
|
c@368
|
368
|
c@368
|
369 // and obtaining this much output in total:
|
c@368
|
370
|
c@373
|
371 int m1 = int(ceil((double(n1) * targetRate) / sourceRate));
|
c@368
|
372
|
c@368
|
373 // in order to return this much output to the user:
|
c@368
|
374
|
c@373
|
375 int m = int(ceil((double(n) * targetRate) / sourceRate));
|
c@368
|
376
|
c@398
|
377 #ifdef DEBUG_RESAMPLER
|
c@398
|
378 cerr << "n = " << n << ", sourceRate = " << sourceRate << ", targetRate = " << targetRate << ", m = " << m << ", latency = " << latency << ", inputPad = " << inputPad << ", m1 = " << m1 << ", n1 = " << n1 << ", n1 - n = " << n1 - n << endl;
|
c@398
|
379 #endif
|
c@363
|
380
|
c@363
|
381 vector<double> pad(n1 - n, 0.0);
|
c@368
|
382 vector<double> out(m1 + 1, 0.0);
|
c@363
|
383
|
c@398
|
384 int gotData = r.process(data, out.data(), n);
|
c@398
|
385 int gotPad = r.process(pad.data(), out.data() + gotData, pad.size());
|
c@398
|
386 int got = gotData + gotPad;
|
c@398
|
387
|
c@366
|
388 #ifdef DEBUG_RESAMPLER
|
c@398
|
389 cerr << "resample: " << n << " in, " << pad.size() << " padding, " << got << " out (" << gotData << " data, " << gotPad << " padding, latency = " << latency << ")" << endl;
|
c@396
|
390 #endif
|
c@396
|
391 #ifdef DEBUG_RESAMPLER_VERBOSE
|
c@398
|
392 int printN = 50;
|
c@398
|
393 cerr << "first " << printN << " in:" << endl;
|
c@398
|
394 for (int i = 0; i < printN && i < n; ++i) {
|
cannam@483
|
395 if (i % 5 == 0) cerr << endl << i << "... ";
|
c@398
|
396 cerr << data[i] << " ";
|
c@366
|
397 }
|
c@398
|
398 cerr << endl;
|
c@366
|
399 #endif
|
c@366
|
400
|
c@368
|
401 int toReturn = got - latency;
|
c@368
|
402 if (toReturn > m) toReturn = m;
|
c@368
|
403
|
c@372
|
404 vector<double> sliced(out.begin() + latency,
|
cannam@483
|
405 out.begin() + latency + toReturn);
|
c@372
|
406
|
c@396
|
407 #ifdef DEBUG_RESAMPLER_VERBOSE
|
c@398
|
408 cerr << "first " << printN << " out (after latency compensation), length " << sliced.size() << ":";
|
c@398
|
409 for (int i = 0; i < printN && i < sliced.size(); ++i) {
|
cannam@483
|
410 if (i % 5 == 0) cerr << endl << i << "... ";
|
cannam@483
|
411 cerr << sliced[i] << " ";
|
c@372
|
412 }
|
c@398
|
413 cerr << endl;
|
c@372
|
414 #endif
|
c@372
|
415
|
c@372
|
416 return sliced;
|
c@363
|
417 }
|
c@363
|
418
|