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