cannam@233
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
cannam@233
|
2
|
cannam@233
|
3 /*
|
cannam@233
|
4 Vamp
|
cannam@233
|
5
|
cannam@233
|
6 An API for audio analysis and feature extraction plugins.
|
cannam@233
|
7
|
cannam@233
|
8 Centre for Digital Music, Queen Mary, University of London.
|
cannam@233
|
9 Copyright 2006-2007 Chris Cannam and QMUL.
|
cannam@233
|
10
|
cannam@233
|
11 This file is based in part on Don Cross's public domain FFT
|
cannam@233
|
12 implementation.
|
cannam@233
|
13
|
cannam@233
|
14 Permission is hereby granted, free of charge, to any person
|
cannam@233
|
15 obtaining a copy of this software and associated documentation
|
cannam@233
|
16 files (the "Software"), to deal in the Software without
|
cannam@233
|
17 restriction, including without limitation the rights to use, copy,
|
cannam@233
|
18 modify, merge, publish, distribute, sublicense, and/or sell copies
|
cannam@233
|
19 of the Software, and to permit persons to whom the Software is
|
cannam@233
|
20 furnished to do so, subject to the following conditions:
|
cannam@233
|
21
|
cannam@233
|
22 The above copyright notice and this permission notice shall be
|
cannam@233
|
23 included in all copies or substantial portions of the Software.
|
cannam@233
|
24
|
cannam@233
|
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@233
|
26 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@233
|
27 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
cannam@233
|
28 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
cannam@233
|
29 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@233
|
30 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@233
|
31 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@233
|
32
|
cannam@233
|
33 Except as contained in this notice, the names of the Centre for
|
cannam@233
|
34 Digital Music; Queen Mary, University of London; and Chris Cannam
|
cannam@233
|
35 shall not be used in advertising or otherwise to promote the sale,
|
cannam@233
|
36 use or other dealings in this Software without prior written
|
cannam@233
|
37 authorization.
|
cannam@233
|
38 */
|
cannam@233
|
39
|
cannam@233
|
40 #include <vamp-hostsdk/PluginInputDomainAdapter.h>
|
cannam@233
|
41
|
cannam@233
|
42 #include <cmath>
|
cannam@233
|
43
|
cannam@233
|
44
|
cannam@233
|
45 /**
|
cannam@233
|
46 * If you want to compile using FFTW instead of the built-in FFT
|
cannam@233
|
47 * implementation for the PluginInputDomainAdapter, define HAVE_FFTW3
|
cannam@233
|
48 * in the Makefile.
|
cannam@233
|
49 *
|
cannam@233
|
50 * Be aware that FFTW is licensed under the GPL -- unlike this SDK,
|
cannam@233
|
51 * which is provided under a more liberal BSD license in order to
|
cannam@233
|
52 * permit use in closed source applications. The use of FFTW would
|
cannam@233
|
53 * mean that your code would need to be licensed under the GPL as
|
cannam@233
|
54 * well. Do not define this symbol unless you understand and accept
|
cannam@233
|
55 * the implications of this.
|
cannam@233
|
56 *
|
cannam@233
|
57 * Parties such as Linux distribution packagers who redistribute this
|
cannam@233
|
58 * SDK for use in other programs should _not_ define this symbol, as
|
cannam@233
|
59 * it would change the effective licensing terms under which the SDK
|
cannam@233
|
60 * was available to third party developers.
|
cannam@233
|
61 *
|
cannam@233
|
62 * The default is not to use FFTW, and to use the built-in FFT instead.
|
cannam@233
|
63 *
|
cannam@233
|
64 * Note: The FFTW code uses FFTW_MEASURE, and so will perform badly on
|
cannam@233
|
65 * its first invocation unless the host has saved and restored FFTW
|
cannam@233
|
66 * wisdom (see the FFTW documentation).
|
cannam@233
|
67 */
|
cannam@233
|
68 #ifdef HAVE_FFTW3
|
cannam@233
|
69 #include <fftw3.h>
|
cannam@233
|
70 #endif
|
cannam@233
|
71
|
cannam@233
|
72
|
cannam@233
|
73 namespace Vamp {
|
cannam@233
|
74
|
cannam@233
|
75 namespace HostExt {
|
cannam@233
|
76
|
cannam@233
|
77 class PluginInputDomainAdapter::Impl
|
cannam@233
|
78 {
|
cannam@233
|
79 public:
|
cannam@233
|
80 Impl(Plugin *plugin, float inputSampleRate);
|
cannam@233
|
81 ~Impl();
|
cannam@233
|
82
|
cannam@233
|
83 bool initialise(size_t channels, size_t stepSize, size_t blockSize);
|
cannam@233
|
84
|
cannam@233
|
85 size_t getPreferredStepSize() const;
|
cannam@233
|
86 size_t getPreferredBlockSize() const;
|
cannam@233
|
87
|
cannam@233
|
88 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
|
cannam@233
|
89
|
cannam@233
|
90 RealTime getTimestampAdjustment() const;
|
cannam@233
|
91
|
cannam@233
|
92 protected:
|
cannam@233
|
93 Plugin *m_plugin;
|
cannam@233
|
94 float m_inputSampleRate;
|
cannam@233
|
95 int m_channels;
|
cannam@233
|
96 int m_blockSize;
|
cannam@233
|
97 float **m_freqbuf;
|
cannam@233
|
98
|
cannam@233
|
99 double *m_ri;
|
cannam@233
|
100 double *m_window;
|
cannam@233
|
101
|
cannam@233
|
102 #ifdef HAVE_FFTW3
|
cannam@233
|
103 fftw_plan m_plan;
|
cannam@233
|
104 fftw_complex *m_cbuf;
|
cannam@233
|
105 #else
|
cannam@233
|
106 double *m_ro;
|
cannam@233
|
107 double *m_io;
|
cannam@233
|
108 void fft(unsigned int n, bool inverse,
|
cannam@233
|
109 double *ri, double *ii, double *ro, double *io);
|
cannam@233
|
110 #endif
|
cannam@233
|
111
|
cannam@233
|
112 size_t makeBlockSizeAcceptable(size_t) const;
|
cannam@233
|
113 };
|
cannam@233
|
114
|
cannam@233
|
115 PluginInputDomainAdapter::PluginInputDomainAdapter(Plugin *plugin) :
|
cannam@233
|
116 PluginWrapper(plugin)
|
cannam@233
|
117 {
|
cannam@233
|
118 m_impl = new Impl(plugin, m_inputSampleRate);
|
cannam@233
|
119 }
|
cannam@233
|
120
|
cannam@233
|
121 PluginInputDomainAdapter::~PluginInputDomainAdapter()
|
cannam@233
|
122 {
|
cannam@233
|
123 delete m_impl;
|
cannam@233
|
124 }
|
cannam@233
|
125
|
cannam@233
|
126 bool
|
cannam@233
|
127 PluginInputDomainAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
cannam@233
|
128 {
|
cannam@233
|
129 return m_impl->initialise(channels, stepSize, blockSize);
|
cannam@233
|
130 }
|
cannam@233
|
131
|
cannam@233
|
132 Plugin::InputDomain
|
cannam@233
|
133 PluginInputDomainAdapter::getInputDomain() const
|
cannam@233
|
134 {
|
cannam@233
|
135 return TimeDomain;
|
cannam@233
|
136 }
|
cannam@233
|
137
|
cannam@233
|
138 size_t
|
cannam@233
|
139 PluginInputDomainAdapter::getPreferredStepSize() const
|
cannam@233
|
140 {
|
cannam@233
|
141 return m_impl->getPreferredStepSize();
|
cannam@233
|
142 }
|
cannam@233
|
143
|
cannam@233
|
144 size_t
|
cannam@233
|
145 PluginInputDomainAdapter::getPreferredBlockSize() const
|
cannam@233
|
146 {
|
cannam@233
|
147 return m_impl->getPreferredBlockSize();
|
cannam@233
|
148 }
|
cannam@233
|
149
|
cannam@233
|
150 Plugin::FeatureSet
|
cannam@233
|
151 PluginInputDomainAdapter::process(const float *const *inputBuffers, RealTime timestamp)
|
cannam@233
|
152 {
|
cannam@233
|
153 return m_impl->process(inputBuffers, timestamp);
|
cannam@233
|
154 }
|
cannam@233
|
155
|
cannam@233
|
156 RealTime
|
cannam@233
|
157 PluginInputDomainAdapter::getTimestampAdjustment() const
|
cannam@233
|
158 {
|
cannam@233
|
159 return m_impl->getTimestampAdjustment();
|
cannam@233
|
160 }
|
cannam@233
|
161
|
cannam@233
|
162
|
cannam@233
|
163 PluginInputDomainAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
|
cannam@233
|
164 m_plugin(plugin),
|
cannam@233
|
165 m_inputSampleRate(inputSampleRate),
|
cannam@233
|
166 m_channels(0),
|
cannam@233
|
167 m_blockSize(0),
|
cannam@233
|
168 m_freqbuf(0),
|
cannam@233
|
169 m_ri(0),
|
cannam@233
|
170 m_window(0),
|
cannam@233
|
171 #ifdef HAVE_FFTW3
|
cannam@233
|
172 m_plan(0),
|
cannam@233
|
173 m_cbuf(0)
|
cannam@233
|
174 #else
|
cannam@233
|
175 m_ro(0),
|
cannam@233
|
176 m_io(0)
|
cannam@233
|
177 #endif
|
cannam@233
|
178 {
|
cannam@233
|
179 }
|
cannam@233
|
180
|
cannam@233
|
181 PluginInputDomainAdapter::Impl::~Impl()
|
cannam@233
|
182 {
|
cannam@233
|
183 // the adapter will delete the plugin
|
cannam@233
|
184
|
cannam@233
|
185 if (m_channels > 0) {
|
cannam@233
|
186 for (int c = 0; c < m_channels; ++c) {
|
cannam@233
|
187 delete[] m_freqbuf[c];
|
cannam@233
|
188 }
|
cannam@233
|
189 delete[] m_freqbuf;
|
cannam@233
|
190 #ifdef HAVE_FFTW3
|
cannam@233
|
191 if (m_plan) {
|
cannam@233
|
192 fftw_destroy_plan(m_plan);
|
cannam@233
|
193 fftw_free(m_ri);
|
cannam@233
|
194 fftw_free(m_cbuf);
|
cannam@233
|
195 m_plan = 0;
|
cannam@233
|
196 }
|
cannam@233
|
197 #else
|
cannam@233
|
198 delete[] m_ri;
|
cannam@233
|
199 delete[] m_ro;
|
cannam@233
|
200 delete[] m_io;
|
cannam@233
|
201 #endif
|
cannam@233
|
202 delete[] m_window;
|
cannam@233
|
203 }
|
cannam@233
|
204 }
|
cannam@233
|
205
|
cannam@233
|
206 // for some visual studii apparently
|
cannam@233
|
207 #ifndef M_PI
|
cannam@233
|
208 #define M_PI 3.14159265358979232846
|
cannam@233
|
209 #endif
|
cannam@233
|
210
|
cannam@233
|
211 bool
|
cannam@233
|
212 PluginInputDomainAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
cannam@233
|
213 {
|
cannam@233
|
214 if (m_plugin->getInputDomain() == TimeDomain) {
|
cannam@233
|
215
|
cannam@233
|
216 m_blockSize = int(blockSize);
|
cannam@233
|
217 m_channels = int(channels);
|
cannam@233
|
218
|
cannam@233
|
219 return m_plugin->initialise(channels, stepSize, blockSize);
|
cannam@233
|
220 }
|
cannam@233
|
221
|
cannam@233
|
222 if (blockSize < 2) {
|
cannam@233
|
223 std::cerr << "ERROR: Vamp::HostExt::PluginInputDomainAdapter::Impl::initialise: blocksize < 2 not supported" << std::endl;
|
cannam@233
|
224 return false;
|
cannam@233
|
225 }
|
cannam@233
|
226
|
cannam@233
|
227 if (blockSize & (blockSize-1)) {
|
cannam@233
|
228 std::cerr << "ERROR: Vamp::HostExt::PluginInputDomainAdapter::Impl::initialise: non-power-of-two\nblocksize " << blockSize << " not supported" << std::endl;
|
cannam@233
|
229 return false;
|
cannam@233
|
230 }
|
cannam@233
|
231
|
cannam@233
|
232 if (m_channels > 0) {
|
cannam@233
|
233 for (int c = 0; c < m_channels; ++c) {
|
cannam@233
|
234 delete[] m_freqbuf[c];
|
cannam@233
|
235 }
|
cannam@233
|
236 delete[] m_freqbuf;
|
cannam@233
|
237 #ifdef HAVE_FFTW3
|
cannam@233
|
238 if (m_plan) {
|
cannam@233
|
239 fftw_destroy_plan(m_plan);
|
cannam@233
|
240 fftw_free(m_ri);
|
cannam@233
|
241 fftw_free(m_cbuf);
|
cannam@233
|
242 m_plan = 0;
|
cannam@233
|
243 }
|
cannam@233
|
244 #else
|
cannam@233
|
245 delete[] m_ri;
|
cannam@233
|
246 delete[] m_ro;
|
cannam@233
|
247 delete[] m_io;
|
cannam@233
|
248 #endif
|
cannam@233
|
249 delete[] m_window;
|
cannam@233
|
250 }
|
cannam@233
|
251
|
cannam@233
|
252 m_blockSize = int(blockSize);
|
cannam@233
|
253 m_channels = int(channels);
|
cannam@233
|
254
|
cannam@233
|
255 m_freqbuf = new float *[m_channels];
|
cannam@233
|
256 for (int c = 0; c < m_channels; ++c) {
|
cannam@233
|
257 m_freqbuf[c] = new float[m_blockSize + 2];
|
cannam@233
|
258 }
|
cannam@233
|
259 m_window = new double[m_blockSize];
|
cannam@233
|
260
|
cannam@233
|
261 for (int i = 0; i < m_blockSize; ++i) {
|
cannam@233
|
262 // Hanning window
|
cannam@233
|
263 m_window[i] = (0.50 - 0.50 * cos((2.0 * M_PI * i) / m_blockSize));
|
cannam@233
|
264 }
|
cannam@233
|
265
|
cannam@233
|
266 #ifdef HAVE_FFTW3
|
cannam@233
|
267 m_ri = (double *)fftw_malloc(blockSize * sizeof(double));
|
cannam@233
|
268 m_cbuf = (fftw_complex *)fftw_malloc((blockSize/2 + 1) * sizeof(fftw_complex));
|
cannam@233
|
269 m_plan = fftw_plan_dft_r2c_1d(blockSize, m_ri, m_cbuf, FFTW_MEASURE);
|
cannam@233
|
270 #else
|
cannam@233
|
271 m_ri = new double[m_blockSize];
|
cannam@233
|
272 m_ro = new double[m_blockSize];
|
cannam@233
|
273 m_io = new double[m_blockSize];
|
cannam@233
|
274 #endif
|
cannam@233
|
275
|
cannam@233
|
276 return m_plugin->initialise(channels, stepSize, blockSize);
|
cannam@233
|
277 }
|
cannam@233
|
278
|
cannam@233
|
279 size_t
|
cannam@233
|
280 PluginInputDomainAdapter::Impl::getPreferredStepSize() const
|
cannam@233
|
281 {
|
cannam@233
|
282 size_t step = m_plugin->getPreferredStepSize();
|
cannam@233
|
283
|
cannam@233
|
284 if (step == 0 && (m_plugin->getInputDomain() == FrequencyDomain)) {
|
cannam@233
|
285 step = getPreferredBlockSize() / 2;
|
cannam@233
|
286 }
|
cannam@233
|
287
|
cannam@233
|
288 return step;
|
cannam@233
|
289 }
|
cannam@233
|
290
|
cannam@233
|
291 size_t
|
cannam@233
|
292 PluginInputDomainAdapter::Impl::getPreferredBlockSize() const
|
cannam@233
|
293 {
|
cannam@233
|
294 size_t block = m_plugin->getPreferredBlockSize();
|
cannam@233
|
295
|
cannam@233
|
296 if (m_plugin->getInputDomain() == FrequencyDomain) {
|
cannam@233
|
297 if (block == 0) {
|
cannam@233
|
298 block = 1024;
|
cannam@233
|
299 } else {
|
cannam@233
|
300 block = makeBlockSizeAcceptable(block);
|
cannam@233
|
301 }
|
cannam@233
|
302 }
|
cannam@233
|
303
|
cannam@233
|
304 return block;
|
cannam@233
|
305 }
|
cannam@233
|
306
|
cannam@233
|
307 size_t
|
cannam@233
|
308 PluginInputDomainAdapter::Impl::makeBlockSizeAcceptable(size_t blockSize) const
|
cannam@233
|
309 {
|
cannam@233
|
310 if (blockSize < 2) {
|
cannam@233
|
311
|
cannam@233
|
312 std::cerr << "WARNING: Vamp::HostExt::PluginInputDomainAdapter::Impl::initialise: blocksize < 2 not" << std::endl
|
cannam@233
|
313 << "supported, increasing from " << blockSize << " to 2" << std::endl;
|
cannam@233
|
314 blockSize = 2;
|
cannam@233
|
315
|
cannam@233
|
316 } else if (blockSize & (blockSize-1)) {
|
cannam@233
|
317
|
cannam@233
|
318 #ifdef HAVE_FFTW3
|
cannam@233
|
319 // not an issue with FFTW
|
cannam@233
|
320 #else
|
cannam@233
|
321
|
cannam@233
|
322 // not a power of two, can't handle that with our built-in FFT
|
cannam@233
|
323 // implementation
|
cannam@233
|
324
|
cannam@233
|
325 size_t nearest = blockSize;
|
cannam@233
|
326 size_t power = 0;
|
cannam@233
|
327 while (nearest > 1) {
|
cannam@233
|
328 nearest >>= 1;
|
cannam@233
|
329 ++power;
|
cannam@233
|
330 }
|
cannam@233
|
331 nearest = 1;
|
cannam@233
|
332 while (power) {
|
cannam@233
|
333 nearest <<= 1;
|
cannam@233
|
334 --power;
|
cannam@233
|
335 }
|
cannam@233
|
336
|
cannam@233
|
337 if (blockSize - nearest > (nearest*2) - blockSize) {
|
cannam@233
|
338 nearest = nearest*2;
|
cannam@233
|
339 }
|
cannam@233
|
340
|
cannam@233
|
341 std::cerr << "WARNING: Vamp::HostExt::PluginInputDomainAdapter::Impl::initialise: non-power-of-two\nblocksize " << blockSize << " not supported, using blocksize " << nearest << " instead" << std::endl;
|
cannam@233
|
342 blockSize = nearest;
|
cannam@233
|
343
|
cannam@233
|
344 #endif
|
cannam@233
|
345 }
|
cannam@233
|
346
|
cannam@233
|
347 return blockSize;
|
cannam@233
|
348 }
|
cannam@233
|
349
|
cannam@233
|
350 RealTime
|
cannam@233
|
351 PluginInputDomainAdapter::Impl::getTimestampAdjustment() const
|
cannam@233
|
352 {
|
cannam@233
|
353 if (m_plugin->getInputDomain() == TimeDomain) {
|
cannam@233
|
354 return RealTime::zeroTime;
|
cannam@233
|
355 } else {
|
cannam@233
|
356 return RealTime::frame2RealTime
|
cannam@233
|
357 (m_blockSize/2, int(m_inputSampleRate + 0.5));
|
cannam@233
|
358 }
|
cannam@233
|
359 }
|
cannam@233
|
360
|
cannam@233
|
361 Plugin::FeatureSet
|
cannam@233
|
362 PluginInputDomainAdapter::Impl::process(const float *const *inputBuffers,
|
cannam@233
|
363 RealTime timestamp)
|
cannam@233
|
364 {
|
cannam@233
|
365 if (m_plugin->getInputDomain() == TimeDomain) {
|
cannam@233
|
366 return m_plugin->process(inputBuffers, timestamp);
|
cannam@233
|
367 }
|
cannam@233
|
368
|
cannam@233
|
369 // The timestamp supplied should be (according to the Vamp::Plugin
|
cannam@233
|
370 // spec) the time of the start of the time-domain input block.
|
cannam@233
|
371 // However, we want to pass to the plugin an FFT output calculated
|
cannam@233
|
372 // from the block of samples _centred_ on that timestamp.
|
cannam@233
|
373 //
|
cannam@233
|
374 // We have two options:
|
cannam@233
|
375 //
|
cannam@233
|
376 // 1. Buffer the input, calculating the fft of the values at the
|
cannam@233
|
377 // passed-in block minus blockSize/2 rather than starting at the
|
cannam@233
|
378 // passed-in block. So each time we call process on the plugin,
|
cannam@233
|
379 // we are passing in the same timestamp as was passed to our own
|
cannam@233
|
380 // process plugin, but not (the frequency domain representation
|
cannam@233
|
381 // of) the same set of samples. Advantages: avoids confusion in
|
cannam@233
|
382 // the host by ensuring the returned values have timestamps
|
cannam@233
|
383 // comparable with that passed in to this function (in fact this
|
cannam@233
|
384 // is pretty much essential for one-value-per-block outputs);
|
cannam@233
|
385 // consistent with hosts such as SV that deal with the
|
cannam@233
|
386 // frequency-domain transform themselves. Disadvantages: means
|
cannam@233
|
387 // making the not necessarily correct assumption that the samples
|
cannam@233
|
388 // preceding the first official block are all zero (or some other
|
cannam@233
|
389 // known value).
|
cannam@233
|
390 //
|
cannam@233
|
391 // 2. Increase the passed-in timestamps by half the blocksize. So
|
cannam@233
|
392 // when we call process, we are passing in the frequency domain
|
cannam@233
|
393 // representation of the same set of samples as passed to us, but
|
cannam@233
|
394 // with a different timestamp. Advantages: simplicity; avoids
|
cannam@233
|
395 // iffy assumption mentioned above. Disadvantages: inconsistency
|
cannam@233
|
396 // with SV in cases where stepSize != blockSize/2; potential
|
cannam@233
|
397 // confusion arising from returned timestamps being calculated
|
cannam@233
|
398 // from the adjusted input timestamps rather than the original
|
cannam@233
|
399 // ones (and inaccuracy where the returned timestamp is implied,
|
cannam@233
|
400 // as in one-value-per-block).
|
cannam@233
|
401 //
|
cannam@233
|
402 // Neither way is ideal, but I don't think either is strictly
|
cannam@233
|
403 // incorrect either. I think this is just a case where the same
|
cannam@233
|
404 // plugin can legitimately produce differing results from the same
|
cannam@233
|
405 // input data, depending on how that data is packaged.
|
cannam@233
|
406 //
|
cannam@233
|
407 // We'll go for option 2, adjusting the timestamps. Note in
|
cannam@233
|
408 // particular that this means some results can differ from those
|
cannam@233
|
409 // produced by SV.
|
cannam@233
|
410
|
cannam@233
|
411 // std::cerr << "PluginInputDomainAdapter: sampleRate " << m_inputSampleRate << ", blocksize " << m_blockSize << ", adjusting time from " << timestamp;
|
cannam@233
|
412
|
cannam@233
|
413 timestamp = timestamp + getTimestampAdjustment();
|
cannam@233
|
414
|
cannam@233
|
415 // std::cerr << " to " << timestamp << std::endl;
|
cannam@233
|
416
|
cannam@233
|
417 for (int c = 0; c < m_channels; ++c) {
|
cannam@233
|
418
|
cannam@233
|
419 for (int i = 0; i < m_blockSize; ++i) {
|
cannam@233
|
420 m_ri[i] = double(inputBuffers[c][i]) * m_window[i];
|
cannam@233
|
421 }
|
cannam@233
|
422
|
cannam@233
|
423 for (int i = 0; i < m_blockSize/2; ++i) {
|
cannam@233
|
424 // FFT shift
|
cannam@233
|
425 double value = m_ri[i];
|
cannam@233
|
426 m_ri[i] = m_ri[i + m_blockSize/2];
|
cannam@233
|
427 m_ri[i + m_blockSize/2] = value;
|
cannam@233
|
428 }
|
cannam@233
|
429
|
cannam@233
|
430 #ifdef HAVE_FFTW3
|
cannam@233
|
431
|
cannam@233
|
432 fftw_execute(m_plan);
|
cannam@233
|
433
|
cannam@233
|
434 for (int i = 0; i <= m_blockSize/2; ++i) {
|
cannam@233
|
435 m_freqbuf[c][i * 2] = float(m_cbuf[i][0]);
|
cannam@233
|
436 m_freqbuf[c][i * 2 + 1] = float(m_cbuf[i][1]);
|
cannam@233
|
437 }
|
cannam@233
|
438
|
cannam@233
|
439 #else
|
cannam@233
|
440
|
cannam@233
|
441 fft(m_blockSize, false, m_ri, 0, m_ro, m_io);
|
cannam@233
|
442
|
cannam@233
|
443 for (int i = 0; i <= m_blockSize/2; ++i) {
|
cannam@233
|
444 m_freqbuf[c][i * 2] = float(m_ro[i]);
|
cannam@233
|
445 m_freqbuf[c][i * 2 + 1] = float(m_io[i]);
|
cannam@233
|
446 }
|
cannam@233
|
447
|
cannam@233
|
448 #endif
|
cannam@233
|
449 }
|
cannam@233
|
450
|
cannam@233
|
451 return m_plugin->process(m_freqbuf, timestamp);
|
cannam@233
|
452 }
|
cannam@233
|
453
|
cannam@233
|
454 #ifndef HAVE_FFTW3
|
cannam@233
|
455
|
cannam@233
|
456 void
|
cannam@233
|
457 PluginInputDomainAdapter::Impl::fft(unsigned int n, bool inverse,
|
cannam@233
|
458 double *ri, double *ii, double *ro, double *io)
|
cannam@233
|
459 {
|
cannam@233
|
460 if (!ri || !ro || !io) return;
|
cannam@233
|
461
|
cannam@233
|
462 unsigned int bits;
|
cannam@233
|
463 unsigned int i, j, k, m;
|
cannam@233
|
464 unsigned int blockSize, blockEnd;
|
cannam@233
|
465
|
cannam@233
|
466 double tr, ti;
|
cannam@233
|
467
|
cannam@233
|
468 if (n < 2) return;
|
cannam@233
|
469 if (n & (n-1)) return;
|
cannam@233
|
470
|
cannam@233
|
471 double angle = 2.0 * M_PI;
|
cannam@233
|
472 if (inverse) angle = -angle;
|
cannam@233
|
473
|
cannam@233
|
474 for (i = 0; ; ++i) {
|
cannam@233
|
475 if (n & (1 << i)) {
|
cannam@233
|
476 bits = i;
|
cannam@233
|
477 break;
|
cannam@233
|
478 }
|
cannam@233
|
479 }
|
cannam@233
|
480
|
cannam@233
|
481 static unsigned int tableSize = 0;
|
cannam@233
|
482 static int *table = 0;
|
cannam@233
|
483
|
cannam@233
|
484 if (tableSize != n) {
|
cannam@233
|
485
|
cannam@233
|
486 delete[] table;
|
cannam@233
|
487
|
cannam@233
|
488 table = new int[n];
|
cannam@233
|
489
|
cannam@233
|
490 for (i = 0; i < n; ++i) {
|
cannam@233
|
491
|
cannam@233
|
492 m = i;
|
cannam@233
|
493
|
cannam@233
|
494 for (j = k = 0; j < bits; ++j) {
|
cannam@233
|
495 k = (k << 1) | (m & 1);
|
cannam@233
|
496 m >>= 1;
|
cannam@233
|
497 }
|
cannam@233
|
498
|
cannam@233
|
499 table[i] = k;
|
cannam@233
|
500 }
|
cannam@233
|
501
|
cannam@233
|
502 tableSize = n;
|
cannam@233
|
503 }
|
cannam@233
|
504
|
cannam@233
|
505 if (ii) {
|
cannam@233
|
506 for (i = 0; i < n; ++i) {
|
cannam@233
|
507 ro[table[i]] = ri[i];
|
cannam@233
|
508 io[table[i]] = ii[i];
|
cannam@233
|
509 }
|
cannam@233
|
510 } else {
|
cannam@233
|
511 for (i = 0; i < n; ++i) {
|
cannam@233
|
512 ro[table[i]] = ri[i];
|
cannam@233
|
513 io[table[i]] = 0.0;
|
cannam@233
|
514 }
|
cannam@233
|
515 }
|
cannam@233
|
516
|
cannam@233
|
517 blockEnd = 1;
|
cannam@233
|
518
|
cannam@233
|
519 for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
|
cannam@233
|
520
|
cannam@233
|
521 double delta = angle / (double)blockSize;
|
cannam@233
|
522 double sm2 = -sin(-2 * delta);
|
cannam@233
|
523 double sm1 = -sin(-delta);
|
cannam@233
|
524 double cm2 = cos(-2 * delta);
|
cannam@233
|
525 double cm1 = cos(-delta);
|
cannam@233
|
526 double w = 2 * cm1;
|
cannam@233
|
527 double ar[3], ai[3];
|
cannam@233
|
528
|
cannam@233
|
529 for (i = 0; i < n; i += blockSize) {
|
cannam@233
|
530
|
cannam@233
|
531 ar[2] = cm2;
|
cannam@233
|
532 ar[1] = cm1;
|
cannam@233
|
533
|
cannam@233
|
534 ai[2] = sm2;
|
cannam@233
|
535 ai[1] = sm1;
|
cannam@233
|
536
|
cannam@233
|
537 for (j = i, m = 0; m < blockEnd; j++, m++) {
|
cannam@233
|
538
|
cannam@233
|
539 ar[0] = w * ar[1] - ar[2];
|
cannam@233
|
540 ar[2] = ar[1];
|
cannam@233
|
541 ar[1] = ar[0];
|
cannam@233
|
542
|
cannam@233
|
543 ai[0] = w * ai[1] - ai[2];
|
cannam@233
|
544 ai[2] = ai[1];
|
cannam@233
|
545 ai[1] = ai[0];
|
cannam@233
|
546
|
cannam@233
|
547 k = j + blockEnd;
|
cannam@233
|
548 tr = ar[0] * ro[k] - ai[0] * io[k];
|
cannam@233
|
549 ti = ar[0] * io[k] + ai[0] * ro[k];
|
cannam@233
|
550
|
cannam@233
|
551 ro[k] = ro[j] - tr;
|
cannam@233
|
552 io[k] = io[j] - ti;
|
cannam@233
|
553
|
cannam@233
|
554 ro[j] += tr;
|
cannam@233
|
555 io[j] += ti;
|
cannam@233
|
556 }
|
cannam@233
|
557 }
|
cannam@233
|
558
|
cannam@233
|
559 blockEnd = blockSize;
|
cannam@233
|
560 }
|
cannam@233
|
561
|
cannam@233
|
562 if (inverse) {
|
cannam@233
|
563
|
cannam@233
|
564 double denom = (double)n;
|
cannam@233
|
565
|
cannam@233
|
566 for (i = 0; i < n; i++) {
|
cannam@233
|
567 ro[i] /= denom;
|
cannam@233
|
568 io[i] /= denom;
|
cannam@233
|
569 }
|
cannam@233
|
570 }
|
cannam@233
|
571 }
|
cannam@233
|
572
|
cannam@233
|
573 #endif
|
cannam@233
|
574
|
cannam@233
|
575 }
|
cannam@233
|
576
|
cannam@233
|
577 }
|
cannam@233
|
578
|