c@362
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@362
|
2
|
c@362
|
3 #include "Resampler.h"
|
c@362
|
4
|
c@362
|
5 #include "qm-dsp/maths/MathUtilities.h"
|
c@362
|
6 #include "qm-dsp/base/KaiserWindow.h"
|
c@362
|
7 #include "qm-dsp/base/SincWindow.h"
|
c@362
|
8
|
c@362
|
9 #include <iostream>
|
c@363
|
10 #include <vector>
|
c@370
|
11 #include <map>
|
c@363
|
12
|
c@363
|
13 using std::vector;
|
c@370
|
14 using std::map;
|
c@362
|
15
|
c@366
|
16 //#define DEBUG_RESAMPLER 1
|
c@366
|
17
|
c@362
|
18 Resampler::Resampler(int sourceRate, int targetRate) :
|
c@362
|
19 m_sourceRate(sourceRate),
|
c@362
|
20 m_targetRate(targetRate)
|
c@362
|
21 {
|
c@362
|
22 initialise();
|
c@362
|
23 }
|
c@362
|
24
|
c@362
|
25 Resampler::~Resampler()
|
c@362
|
26 {
|
c@362
|
27 delete[] m_phaseData;
|
c@362
|
28 }
|
c@362
|
29
|
c@362
|
30 void
|
c@362
|
31 Resampler::initialise()
|
c@362
|
32 {
|
c@362
|
33 int higher = std::max(m_sourceRate, m_targetRate);
|
c@362
|
34 int lower = std::min(m_sourceRate, m_targetRate);
|
c@362
|
35
|
c@362
|
36 m_gcd = MathUtilities::gcd(lower, higher);
|
c@362
|
37
|
c@362
|
38 int peakToPole = higher / m_gcd;
|
c@362
|
39
|
c@362
|
40 KaiserWindow::Parameters params =
|
c@362
|
41 KaiserWindow::parametersForBandwidth(100, 0.02, peakToPole);
|
c@362
|
42
|
c@362
|
43 params.length =
|
c@362
|
44 (params.length % 2 == 0 ? params.length + 1 : params.length);
|
c@362
|
45
|
c@362
|
46 m_filterLength = params.length;
|
c@370
|
47
|
c@362
|
48 KaiserWindow kw(params);
|
c@362
|
49 SincWindow sw(m_filterLength, peakToPole * 2);
|
c@362
|
50
|
c@370
|
51 vector<double> filter(m_filterLength, 0.0);
|
c@362
|
52 for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0;
|
c@370
|
53 sw.cut(filter.data());
|
c@370
|
54 kw.cut(filter.data());
|
c@362
|
55
|
c@362
|
56 int inputSpacing = m_targetRate / m_gcd;
|
c@362
|
57 int outputSpacing = m_sourceRate / m_gcd;
|
c@362
|
58
|
c@366
|
59 #ifdef DEBUG_RESAMPLER
|
c@366
|
60 std::cerr << "resample " << m_sourceRate << " -> " << m_targetRate
|
c@366
|
61 << ": inputSpacing " << inputSpacing << ", outputSpacing "
|
c@366
|
62 << outputSpacing << ": filter length " << m_filterLength
|
c@366
|
63 << std::endl;
|
c@366
|
64 #endif
|
c@362
|
65
|
c@362
|
66 m_phaseData = new Phase[inputSpacing];
|
c@362
|
67
|
c@362
|
68 for (int phase = 0; phase < inputSpacing; ++phase) {
|
c@362
|
69
|
c@362
|
70 Phase p;
|
c@362
|
71
|
c@362
|
72 p.nextPhase = phase - outputSpacing;
|
c@362
|
73 while (p.nextPhase < 0) p.nextPhase += inputSpacing;
|
c@362
|
74 p.nextPhase %= inputSpacing;
|
c@362
|
75
|
c@366
|
76 p.drop = int(ceil(std::max(0.0, double(outputSpacing - phase))
|
c@366
|
77 / inputSpacing));
|
c@362
|
78
|
c@366
|
79 int filtZipLength = int(ceil(double(m_filterLength - phase)
|
c@366
|
80 / inputSpacing));
|
c@362
|
81 for (int i = 0; i < filtZipLength; ++i) {
|
c@362
|
82 p.filter.push_back(filter[i * inputSpacing + phase]);
|
c@362
|
83 }
|
c@362
|
84
|
c@362
|
85 m_phaseData[phase] = p;
|
c@362
|
86 }
|
c@362
|
87
|
c@362
|
88 // The May implementation of this uses a pull model -- we ask the
|
c@362
|
89 // resampler for a certain number of output samples, and it asks
|
c@362
|
90 // its source stream for as many as it needs to calculate
|
c@362
|
91 // those. This means (among other things) that the source stream
|
c@362
|
92 // can be asked for enough samples up-front to fill the buffer
|
c@362
|
93 // before the first output sample is generated.
|
c@362
|
94 //
|
c@362
|
95 // In this implementation we're using a push model in which a
|
c@362
|
96 // certain number of source samples is provided and we're asked
|
c@362
|
97 // for as many output samples as that makes available. But we
|
c@362
|
98 // can't return any samples from the beginning until half the
|
c@362
|
99 // filter length has been provided as input. This means we must
|
c@362
|
100 // either return a very variable number of samples (none at all
|
c@362
|
101 // until the filter fills, then half the filter length at once) or
|
c@362
|
102 // else have a lengthy declared latency on the output. We do the
|
c@362
|
103 // latter. (What do other implementations do?)
|
c@362
|
104
|
c@366
|
105 m_phase = (m_filterLength/2) % inputSpacing;
|
c@366
|
106
|
c@366
|
107 m_buffer = vector<double>(m_phaseData[0].filter.size(), 0);
|
c@370
|
108 m_bufferOrigin = 0;
|
c@366
|
109
|
c@366
|
110 m_latency =
|
c@366
|
111 ((m_buffer.size() * inputSpacing) - (m_filterLength/2)) / outputSpacing
|
c@366
|
112 + m_phase;
|
c@366
|
113
|
c@366
|
114 #ifdef DEBUG_RESAMPLER
|
c@366
|
115 std::cerr << "initial phase " << m_phase << " (as " << (m_filterLength/2) << " % " << inputSpacing << ")"
|
c@366
|
116 << ", latency " << m_latency << std::endl;
|
c@366
|
117 #endif
|
c@362
|
118 }
|
c@362
|
119
|
c@362
|
120 double
|
c@366
|
121 Resampler::reconstructOne()
|
c@362
|
122 {
|
c@362
|
123 Phase &pd = m_phaseData[m_phase];
|
c@366
|
124 double v = 0.0;
|
c@362
|
125 int n = pd.filter.size();
|
c@370
|
126 const double *const __restrict__ buf = m_buffer.data() + m_bufferOrigin;
|
c@370
|
127 const double *const __restrict__ filt = pd.filter.data();
|
c@362
|
128 for (int i = 0; i < n; ++i) {
|
c@370
|
129 // NB gcc can only vectorize this with -ffast-math
|
c@370
|
130 v += buf[i] * filt[i];
|
c@362
|
131 }
|
c@370
|
132 m_bufferOrigin += pd.drop;
|
c@366
|
133 m_phase = pd.nextPhase;
|
c@362
|
134 return v;
|
c@362
|
135 }
|
c@362
|
136
|
c@362
|
137 int
|
c@366
|
138 Resampler::process(const double *src, double *dst, int n)
|
c@362
|
139 {
|
c@366
|
140 for (int i = 0; i < n; ++i) {
|
c@366
|
141 m_buffer.push_back(src[i]);
|
c@362
|
142 }
|
c@362
|
143
|
c@366
|
144 int maxout = int(ceil(double(n) * m_targetRate / m_sourceRate));
|
c@366
|
145 int outidx = 0;
|
c@364
|
146
|
c@366
|
147 #ifdef DEBUG_RESAMPLER
|
c@366
|
148 std::cerr << "process: buf siz " << m_buffer.size() << " filt siz for phase " << m_phase << " " << m_phaseData[m_phase].filter.size() << std::endl;
|
c@366
|
149 #endif
|
c@366
|
150
|
c@367
|
151 double scaleFactor = 1.0;
|
c@367
|
152 if (m_targetRate < m_sourceRate) {
|
c@367
|
153 scaleFactor = double(m_targetRate) / double(m_sourceRate);
|
c@367
|
154 }
|
c@367
|
155
|
c@366
|
156 while (outidx < maxout &&
|
c@370
|
157 m_buffer.size() >= m_phaseData[m_phase].filter.size() + m_bufferOrigin) {
|
c@367
|
158 dst[outidx] = scaleFactor * reconstructOne();
|
c@366
|
159 outidx++;
|
c@364
|
160 }
|
c@370
|
161
|
c@370
|
162 m_buffer = vector<double>(m_buffer.begin() + m_bufferOrigin, m_buffer.end());
|
c@370
|
163 m_bufferOrigin = 0;
|
c@366
|
164
|
c@366
|
165 return outidx;
|
c@362
|
166 }
|
c@366
|
167
|
c@363
|
168 std::vector<double>
|
c@363
|
169 Resampler::resample(int sourceRate, int targetRate, const double *data, int n)
|
c@363
|
170 {
|
c@363
|
171 Resampler r(sourceRate, targetRate);
|
c@363
|
172
|
c@363
|
173 int latency = r.getLatency();
|
c@363
|
174
|
c@368
|
175 // latency is the output latency. We need to provide enough
|
c@368
|
176 // padding input samples at the end of input to guarantee at
|
c@368
|
177 // *least* the latency's worth of output samples. that is,
|
c@368
|
178
|
c@368
|
179 int inputPad = int(ceil(double(latency * sourceRate) / targetRate));
|
c@368
|
180
|
c@368
|
181 // that means we are providing this much input in total:
|
c@368
|
182
|
c@368
|
183 int n1 = n + inputPad;
|
c@368
|
184
|
c@368
|
185 // and obtaining this much output in total:
|
c@368
|
186
|
c@368
|
187 int m1 = int(ceil(double(n1 * targetRate) / sourceRate));
|
c@368
|
188
|
c@368
|
189 // in order to return this much output to the user:
|
c@368
|
190
|
c@366
|
191 int m = int(ceil(double(n * targetRate) / sourceRate));
|
c@368
|
192
|
c@370
|
193 // std::cerr << "n = " << n << ", sourceRate = " << sourceRate << ", targetRate = " << targetRate << ", m = " << m << ", latency = " << latency << ", m1 = " << m1 << ", n1 = " << n1 << ", n1 - n = " << n1 - n << std::endl;
|
c@363
|
194
|
c@363
|
195 vector<double> pad(n1 - n, 0.0);
|
c@368
|
196 vector<double> out(m1 + 1, 0.0);
|
c@363
|
197
|
c@363
|
198 int got = r.process(data, out.data(), n);
|
c@363
|
199 got += r.process(pad.data(), out.data() + got, pad.size());
|
c@363
|
200
|
c@366
|
201 #ifdef DEBUG_RESAMPLER
|
c@366
|
202 std::cerr << "resample: " << n << " in, " << got << " out" << std::endl;
|
c@366
|
203 for (int i = 0; i < got; ++i) {
|
c@366
|
204 if (i % 5 == 0) std::cout << std::endl << i << "... ";
|
c@366
|
205 std::cout << (float) out[i] << " ";
|
c@366
|
206 }
|
c@366
|
207 std::cout << std::endl;
|
c@366
|
208 #endif
|
c@366
|
209
|
c@368
|
210 int toReturn = got - latency;
|
c@368
|
211 if (toReturn > m) toReturn = m;
|
c@368
|
212
|
c@368
|
213 return vector<double>(out.begin() + latency,
|
c@368
|
214 out.begin() + latency + toReturn);
|
c@363
|
215 }
|
c@363
|
216
|