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@263
|
73 _VAMP_SDK_HOSTSPACE_BEGIN(PluginInputDomainAdapter.cpp)
|
cannam@263
|
74
|
cannam@233
|
75 namespace Vamp {
|
cannam@233
|
76
|
cannam@233
|
77 namespace HostExt {
|
cannam@233
|
78
|
cannam@233
|
79 class PluginInputDomainAdapter::Impl
|
cannam@233
|
80 {
|
cannam@233
|
81 public:
|
cannam@233
|
82 Impl(Plugin *plugin, float inputSampleRate);
|
cannam@233
|
83 ~Impl();
|
cannam@233
|
84
|
cannam@233
|
85 bool initialise(size_t channels, size_t stepSize, size_t blockSize);
|
cannam@288
|
86 void reset();
|
cannam@233
|
87
|
cannam@233
|
88 size_t getPreferredStepSize() const;
|
cannam@233
|
89 size_t getPreferredBlockSize() const;
|
cannam@233
|
90
|
cannam@233
|
91 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
|
cannam@288
|
92
|
cannam@288
|
93 void setProcessTimestampMethod(ProcessTimestampMethod m);
|
cannam@288
|
94 ProcessTimestampMethod getProcessTimestampMethod() const;
|
cannam@233
|
95
|
cannam@233
|
96 RealTime getTimestampAdjustment() const;
|
cannam@233
|
97
|
cannam@233
|
98 protected:
|
cannam@233
|
99 Plugin *m_plugin;
|
cannam@233
|
100 float m_inputSampleRate;
|
cannam@233
|
101 int m_channels;
|
cannam@288
|
102 int m_stepSize;
|
cannam@233
|
103 int m_blockSize;
|
cannam@233
|
104 float **m_freqbuf;
|
cannam@233
|
105
|
cannam@233
|
106 double *m_ri;
|
cannam@233
|
107 double *m_window;
|
cannam@233
|
108
|
cannam@288
|
109 ProcessTimestampMethod m_method;
|
cannam@288
|
110 int m_processCount;
|
cannam@288
|
111 FeatureSet prepadProcess(const float *const *inputBuffers,
|
cannam@288
|
112 RealTime timestamp);
|
cannam@288
|
113
|
cannam@233
|
114 #ifdef HAVE_FFTW3
|
cannam@233
|
115 fftw_plan m_plan;
|
cannam@233
|
116 fftw_complex *m_cbuf;
|
cannam@233
|
117 #else
|
cannam@233
|
118 double *m_ro;
|
cannam@233
|
119 double *m_io;
|
cannam@233
|
120 void fft(unsigned int n, bool inverse,
|
cannam@233
|
121 double *ri, double *ii, double *ro, double *io);
|
cannam@233
|
122 #endif
|
cannam@233
|
123
|
cannam@233
|
124 size_t makeBlockSizeAcceptable(size_t) const;
|
cannam@233
|
125 };
|
cannam@233
|
126
|
cannam@233
|
127 PluginInputDomainAdapter::PluginInputDomainAdapter(Plugin *plugin) :
|
cannam@233
|
128 PluginWrapper(plugin)
|
cannam@233
|
129 {
|
cannam@233
|
130 m_impl = new Impl(plugin, m_inputSampleRate);
|
cannam@233
|
131 }
|
cannam@233
|
132
|
cannam@233
|
133 PluginInputDomainAdapter::~PluginInputDomainAdapter()
|
cannam@233
|
134 {
|
cannam@233
|
135 delete m_impl;
|
cannam@233
|
136 }
|
cannam@233
|
137
|
cannam@233
|
138 bool
|
cannam@233
|
139 PluginInputDomainAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
cannam@233
|
140 {
|
cannam@233
|
141 return m_impl->initialise(channels, stepSize, blockSize);
|
cannam@233
|
142 }
|
cannam@233
|
143
|
cannam@288
|
144 void
|
cannam@288
|
145 PluginInputDomainAdapter::reset()
|
cannam@288
|
146 {
|
cannam@288
|
147 m_impl->reset();
|
cannam@288
|
148 }
|
cannam@288
|
149
|
cannam@233
|
150 Plugin::InputDomain
|
cannam@233
|
151 PluginInputDomainAdapter::getInputDomain() const
|
cannam@233
|
152 {
|
cannam@233
|
153 return TimeDomain;
|
cannam@233
|
154 }
|
cannam@233
|
155
|
cannam@233
|
156 size_t
|
cannam@233
|
157 PluginInputDomainAdapter::getPreferredStepSize() const
|
cannam@233
|
158 {
|
cannam@233
|
159 return m_impl->getPreferredStepSize();
|
cannam@233
|
160 }
|
cannam@233
|
161
|
cannam@233
|
162 size_t
|
cannam@233
|
163 PluginInputDomainAdapter::getPreferredBlockSize() const
|
cannam@233
|
164 {
|
cannam@233
|
165 return m_impl->getPreferredBlockSize();
|
cannam@233
|
166 }
|
cannam@233
|
167
|
cannam@233
|
168 Plugin::FeatureSet
|
cannam@233
|
169 PluginInputDomainAdapter::process(const float *const *inputBuffers, RealTime timestamp)
|
cannam@233
|
170 {
|
cannam@233
|
171 return m_impl->process(inputBuffers, timestamp);
|
cannam@233
|
172 }
|
cannam@233
|
173
|
cannam@288
|
174 void
|
cannam@288
|
175 PluginInputDomainAdapter::setProcessTimestampMethod(ProcessTimestampMethod m)
|
cannam@288
|
176 {
|
cannam@288
|
177 m_impl->setProcessTimestampMethod(m);
|
cannam@288
|
178 }
|
cannam@288
|
179
|
cannam@288
|
180 PluginInputDomainAdapter::ProcessTimestampMethod
|
cannam@288
|
181 PluginInputDomainAdapter::getProcessTimestampMethod() const
|
cannam@288
|
182 {
|
cannam@288
|
183 return m_impl->getProcessTimestampMethod();
|
cannam@288
|
184 }
|
cannam@288
|
185
|
cannam@233
|
186 RealTime
|
cannam@233
|
187 PluginInputDomainAdapter::getTimestampAdjustment() const
|
cannam@233
|
188 {
|
cannam@233
|
189 return m_impl->getTimestampAdjustment();
|
cannam@233
|
190 }
|
cannam@233
|
191
|
cannam@233
|
192
|
cannam@233
|
193 PluginInputDomainAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
|
cannam@233
|
194 m_plugin(plugin),
|
cannam@233
|
195 m_inputSampleRate(inputSampleRate),
|
cannam@233
|
196 m_channels(0),
|
cannam@288
|
197 m_stepSize(0),
|
cannam@233
|
198 m_blockSize(0),
|
cannam@233
|
199 m_freqbuf(0),
|
cannam@233
|
200 m_ri(0),
|
cannam@233
|
201 m_window(0),
|
cannam@288
|
202 m_method(ShiftTimestamp),
|
cannam@288
|
203 m_processCount(0),
|
cannam@233
|
204 #ifdef HAVE_FFTW3
|
cannam@233
|
205 m_plan(0),
|
cannam@233
|
206 m_cbuf(0)
|
cannam@233
|
207 #else
|
cannam@233
|
208 m_ro(0),
|
cannam@233
|
209 m_io(0)
|
cannam@233
|
210 #endif
|
cannam@233
|
211 {
|
cannam@233
|
212 }
|
cannam@233
|
213
|
cannam@233
|
214 PluginInputDomainAdapter::Impl::~Impl()
|
cannam@233
|
215 {
|
cannam@233
|
216 // the adapter will delete the plugin
|
cannam@233
|
217
|
cannam@233
|
218 if (m_channels > 0) {
|
cannam@233
|
219 for (int c = 0; c < m_channels; ++c) {
|
cannam@233
|
220 delete[] m_freqbuf[c];
|
cannam@233
|
221 }
|
cannam@233
|
222 delete[] m_freqbuf;
|
cannam@233
|
223 #ifdef HAVE_FFTW3
|
cannam@233
|
224 if (m_plan) {
|
cannam@233
|
225 fftw_destroy_plan(m_plan);
|
cannam@233
|
226 fftw_free(m_ri);
|
cannam@233
|
227 fftw_free(m_cbuf);
|
cannam@233
|
228 m_plan = 0;
|
cannam@233
|
229 }
|
cannam@233
|
230 #else
|
cannam@233
|
231 delete[] m_ri;
|
cannam@233
|
232 delete[] m_ro;
|
cannam@233
|
233 delete[] m_io;
|
cannam@233
|
234 #endif
|
cannam@233
|
235 delete[] m_window;
|
cannam@233
|
236 }
|
cannam@233
|
237 }
|
cannam@233
|
238
|
cannam@233
|
239 // for some visual studii apparently
|
cannam@233
|
240 #ifndef M_PI
|
cannam@233
|
241 #define M_PI 3.14159265358979232846
|
cannam@233
|
242 #endif
|
cannam@233
|
243
|
cannam@233
|
244 bool
|
cannam@233
|
245 PluginInputDomainAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
cannam@233
|
246 {
|
cannam@233
|
247 if (m_plugin->getInputDomain() == TimeDomain) {
|
cannam@233
|
248
|
cannam@288
|
249 m_stepSize = int(stepSize);
|
cannam@233
|
250 m_blockSize = int(blockSize);
|
cannam@233
|
251 m_channels = int(channels);
|
cannam@233
|
252
|
cannam@233
|
253 return m_plugin->initialise(channels, stepSize, blockSize);
|
cannam@233
|
254 }
|
cannam@233
|
255
|
cannam@233
|
256 if (blockSize < 2) {
|
cannam@283
|
257 std::cerr << "ERROR: PluginInputDomainAdapter::initialise: blocksize < 2 not supported" << std::endl;
|
cannam@233
|
258 return false;
|
cannam@233
|
259 }
|
cannam@233
|
260
|
cannam@233
|
261 if (blockSize & (blockSize-1)) {
|
cannam@283
|
262 std::cerr << "ERROR: PluginInputDomainAdapter::initialise: non-power-of-two\nblocksize " << blockSize << " not supported" << std::endl;
|
cannam@233
|
263 return false;
|
cannam@233
|
264 }
|
cannam@233
|
265
|
cannam@233
|
266 if (m_channels > 0) {
|
cannam@233
|
267 for (int c = 0; c < m_channels; ++c) {
|
cannam@233
|
268 delete[] m_freqbuf[c];
|
cannam@233
|
269 }
|
cannam@233
|
270 delete[] m_freqbuf;
|
cannam@233
|
271 #ifdef HAVE_FFTW3
|
cannam@233
|
272 if (m_plan) {
|
cannam@233
|
273 fftw_destroy_plan(m_plan);
|
cannam@233
|
274 fftw_free(m_ri);
|
cannam@233
|
275 fftw_free(m_cbuf);
|
cannam@233
|
276 m_plan = 0;
|
cannam@233
|
277 }
|
cannam@233
|
278 #else
|
cannam@233
|
279 delete[] m_ri;
|
cannam@233
|
280 delete[] m_ro;
|
cannam@233
|
281 delete[] m_io;
|
cannam@233
|
282 #endif
|
cannam@233
|
283 delete[] m_window;
|
cannam@233
|
284 }
|
cannam@233
|
285
|
cannam@288
|
286 m_stepSize = int(stepSize);
|
cannam@233
|
287 m_blockSize = int(blockSize);
|
cannam@233
|
288 m_channels = int(channels);
|
cannam@233
|
289
|
cannam@233
|
290 m_freqbuf = new float *[m_channels];
|
cannam@233
|
291 for (int c = 0; c < m_channels; ++c) {
|
cannam@233
|
292 m_freqbuf[c] = new float[m_blockSize + 2];
|
cannam@233
|
293 }
|
cannam@233
|
294 m_window = new double[m_blockSize];
|
cannam@233
|
295
|
cannam@233
|
296 for (int i = 0; i < m_blockSize; ++i) {
|
cannam@233
|
297 // Hanning window
|
cannam@233
|
298 m_window[i] = (0.50 - 0.50 * cos((2.0 * M_PI * i) / m_blockSize));
|
cannam@233
|
299 }
|
cannam@233
|
300
|
cannam@233
|
301 #ifdef HAVE_FFTW3
|
cannam@233
|
302 m_ri = (double *)fftw_malloc(blockSize * sizeof(double));
|
cannam@233
|
303 m_cbuf = (fftw_complex *)fftw_malloc((blockSize/2 + 1) * sizeof(fftw_complex));
|
cannam@233
|
304 m_plan = fftw_plan_dft_r2c_1d(blockSize, m_ri, m_cbuf, FFTW_MEASURE);
|
cannam@233
|
305 #else
|
cannam@233
|
306 m_ri = new double[m_blockSize];
|
cannam@233
|
307 m_ro = new double[m_blockSize];
|
cannam@233
|
308 m_io = new double[m_blockSize];
|
cannam@233
|
309 #endif
|
cannam@233
|
310
|
cannam@288
|
311 m_processCount = 0;
|
cannam@288
|
312
|
cannam@233
|
313 return m_plugin->initialise(channels, stepSize, blockSize);
|
cannam@233
|
314 }
|
cannam@233
|
315
|
cannam@288
|
316 void
|
cannam@288
|
317 PluginInputDomainAdapter::Impl::reset()
|
cannam@288
|
318 {
|
cannam@288
|
319 m_processCount = 0;
|
cannam@288
|
320 m_plugin->reset();
|
cannam@288
|
321 }
|
cannam@288
|
322
|
cannam@233
|
323 size_t
|
cannam@233
|
324 PluginInputDomainAdapter::Impl::getPreferredStepSize() const
|
cannam@233
|
325 {
|
cannam@233
|
326 size_t step = m_plugin->getPreferredStepSize();
|
cannam@233
|
327
|
cannam@233
|
328 if (step == 0 && (m_plugin->getInputDomain() == FrequencyDomain)) {
|
cannam@233
|
329 step = getPreferredBlockSize() / 2;
|
cannam@233
|
330 }
|
cannam@233
|
331
|
cannam@233
|
332 return step;
|
cannam@233
|
333 }
|
cannam@233
|
334
|
cannam@233
|
335 size_t
|
cannam@233
|
336 PluginInputDomainAdapter::Impl::getPreferredBlockSize() const
|
cannam@233
|
337 {
|
cannam@233
|
338 size_t block = m_plugin->getPreferredBlockSize();
|
cannam@233
|
339
|
cannam@233
|
340 if (m_plugin->getInputDomain() == FrequencyDomain) {
|
cannam@233
|
341 if (block == 0) {
|
cannam@233
|
342 block = 1024;
|
cannam@233
|
343 } else {
|
cannam@233
|
344 block = makeBlockSizeAcceptable(block);
|
cannam@233
|
345 }
|
cannam@233
|
346 }
|
cannam@233
|
347
|
cannam@233
|
348 return block;
|
cannam@233
|
349 }
|
cannam@233
|
350
|
cannam@233
|
351 size_t
|
cannam@233
|
352 PluginInputDomainAdapter::Impl::makeBlockSizeAcceptable(size_t blockSize) const
|
cannam@233
|
353 {
|
cannam@233
|
354 if (blockSize < 2) {
|
cannam@233
|
355
|
cannam@283
|
356 std::cerr << "WARNING: PluginInputDomainAdapter::initialise: blocksize < 2 not" << std::endl
|
cannam@233
|
357 << "supported, increasing from " << blockSize << " to 2" << std::endl;
|
cannam@233
|
358 blockSize = 2;
|
cannam@233
|
359
|
cannam@233
|
360 } else if (blockSize & (blockSize-1)) {
|
cannam@233
|
361
|
cannam@233
|
362 #ifdef HAVE_FFTW3
|
cannam@233
|
363 // not an issue with FFTW
|
cannam@233
|
364 #else
|
cannam@233
|
365
|
cannam@233
|
366 // not a power of two, can't handle that with our built-in FFT
|
cannam@233
|
367 // implementation
|
cannam@233
|
368
|
cannam@233
|
369 size_t nearest = blockSize;
|
cannam@233
|
370 size_t power = 0;
|
cannam@233
|
371 while (nearest > 1) {
|
cannam@233
|
372 nearest >>= 1;
|
cannam@233
|
373 ++power;
|
cannam@233
|
374 }
|
cannam@233
|
375 nearest = 1;
|
cannam@233
|
376 while (power) {
|
cannam@233
|
377 nearest <<= 1;
|
cannam@233
|
378 --power;
|
cannam@233
|
379 }
|
cannam@233
|
380
|
cannam@233
|
381 if (blockSize - nearest > (nearest*2) - blockSize) {
|
cannam@233
|
382 nearest = nearest*2;
|
cannam@233
|
383 }
|
cannam@233
|
384
|
cannam@283
|
385 std::cerr << "WARNING: PluginInputDomainAdapter::initialise: non-power-of-two\nblocksize " << blockSize << " not supported, using blocksize " << nearest << " instead" << std::endl;
|
cannam@233
|
386 blockSize = nearest;
|
cannam@233
|
387
|
cannam@233
|
388 #endif
|
cannam@233
|
389 }
|
cannam@233
|
390
|
cannam@233
|
391 return blockSize;
|
cannam@233
|
392 }
|
cannam@233
|
393
|
cannam@233
|
394 RealTime
|
cannam@233
|
395 PluginInputDomainAdapter::Impl::getTimestampAdjustment() const
|
cannam@233
|
396 {
|
cannam@233
|
397 if (m_plugin->getInputDomain() == TimeDomain) {
|
cannam@233
|
398 return RealTime::zeroTime;
|
cannam@233
|
399 } else {
|
cannam@233
|
400 return RealTime::frame2RealTime
|
cannam@233
|
401 (m_blockSize/2, int(m_inputSampleRate + 0.5));
|
cannam@233
|
402 }
|
cannam@233
|
403 }
|
cannam@233
|
404
|
cannam@288
|
405 void
|
cannam@288
|
406 PluginInputDomainAdapter::Impl::setProcessTimestampMethod(ProcessTimestampMethod m)
|
cannam@288
|
407 {
|
cannam@288
|
408 m_method = m;
|
cannam@288
|
409 }
|
cannam@288
|
410
|
cannam@288
|
411 PluginInputDomainAdapter::ProcessTimestampMethod
|
cannam@288
|
412 PluginInputDomainAdapter::Impl::getProcessTimestampMethod() const
|
cannam@288
|
413 {
|
cannam@288
|
414 return m_method;
|
cannam@288
|
415 }
|
cannam@288
|
416
|
cannam@233
|
417 Plugin::FeatureSet
|
cannam@233
|
418 PluginInputDomainAdapter::Impl::process(const float *const *inputBuffers,
|
cannam@233
|
419 RealTime timestamp)
|
cannam@233
|
420 {
|
cannam@233
|
421 if (m_plugin->getInputDomain() == TimeDomain) {
|
cannam@233
|
422 return m_plugin->process(inputBuffers, timestamp);
|
cannam@233
|
423 }
|
cannam@233
|
424
|
cannam@233
|
425 // The timestamp supplied should be (according to the Vamp::Plugin
|
cannam@233
|
426 // spec) the time of the start of the time-domain input block.
|
cannam@233
|
427 // However, we want to pass to the plugin an FFT output calculated
|
cannam@233
|
428 // from the block of samples _centred_ on that timestamp.
|
cannam@233
|
429 //
|
cannam@233
|
430 // We have two options:
|
cannam@233
|
431 //
|
cannam@233
|
432 // 1. Buffer the input, calculating the fft of the values at the
|
cannam@233
|
433 // passed-in block minus blockSize/2 rather than starting at the
|
cannam@233
|
434 // passed-in block. So each time we call process on the plugin,
|
cannam@233
|
435 // we are passing in the same timestamp as was passed to our own
|
cannam@233
|
436 // process plugin, but not (the frequency domain representation
|
cannam@233
|
437 // of) the same set of samples. Advantages: avoids confusion in
|
cannam@233
|
438 // the host by ensuring the returned values have timestamps
|
cannam@233
|
439 // comparable with that passed in to this function (in fact this
|
cannam@233
|
440 // is pretty much essential for one-value-per-block outputs);
|
cannam@233
|
441 // consistent with hosts such as SV that deal with the
|
cannam@233
|
442 // frequency-domain transform themselves. Disadvantages: means
|
cannam@233
|
443 // making the not necessarily correct assumption that the samples
|
cannam@233
|
444 // preceding the first official block are all zero (or some other
|
cannam@233
|
445 // known value).
|
cannam@233
|
446 //
|
cannam@233
|
447 // 2. Increase the passed-in timestamps by half the blocksize. So
|
cannam@233
|
448 // when we call process, we are passing in the frequency domain
|
cannam@233
|
449 // representation of the same set of samples as passed to us, but
|
cannam@233
|
450 // with a different timestamp. Advantages: simplicity; avoids
|
cannam@233
|
451 // iffy assumption mentioned above. Disadvantages: inconsistency
|
cannam@233
|
452 // with SV in cases where stepSize != blockSize/2; potential
|
cannam@233
|
453 // confusion arising from returned timestamps being calculated
|
cannam@233
|
454 // from the adjusted input timestamps rather than the original
|
cannam@233
|
455 // ones (and inaccuracy where the returned timestamp is implied,
|
cannam@233
|
456 // as in one-value-per-block).
|
cannam@233
|
457 //
|
cannam@233
|
458 // Neither way is ideal, but I don't think either is strictly
|
cannam@233
|
459 // incorrect either. I think this is just a case where the same
|
cannam@233
|
460 // plugin can legitimately produce differing results from the same
|
cannam@233
|
461 // input data, depending on how that data is packaged.
|
cannam@233
|
462 //
|
cannam@233
|
463 // We'll go for option 2, adjusting the timestamps. Note in
|
cannam@233
|
464 // particular that this means some results can differ from those
|
cannam@233
|
465 // produced by SV.
|
cannam@233
|
466
|
cannam@233
|
467 // std::cerr << "PluginInputDomainAdapter: sampleRate " << m_inputSampleRate << ", blocksize " << m_blockSize << ", adjusting time from " << timestamp;
|
cannam@233
|
468
|
cannam@288
|
469 //!!! update the above comment for ProcessTimestampMethod
|
cannam@288
|
470
|
cannam@288
|
471 FeatureSet fs;
|
cannam@288
|
472 if (m_method == ShiftTimestamp) {
|
cannam@288
|
473 timestamp = timestamp + getTimestampAdjustment();
|
cannam@288
|
474 } else if (m_processCount == 0) {
|
cannam@288
|
475 fs = prepadProcess(inputBuffers, timestamp);
|
cannam@288
|
476 }
|
cannam@288
|
477 ++m_processCount;
|
cannam@233
|
478
|
cannam@233
|
479 // std::cerr << " to " << timestamp << std::endl;
|
cannam@233
|
480
|
cannam@233
|
481 for (int c = 0; c < m_channels; ++c) {
|
cannam@233
|
482
|
cannam@233
|
483 for (int i = 0; i < m_blockSize; ++i) {
|
cannam@233
|
484 m_ri[i] = double(inputBuffers[c][i]) * m_window[i];
|
cannam@233
|
485 }
|
cannam@233
|
486
|
cannam@233
|
487 for (int i = 0; i < m_blockSize/2; ++i) {
|
cannam@233
|
488 // FFT shift
|
cannam@233
|
489 double value = m_ri[i];
|
cannam@233
|
490 m_ri[i] = m_ri[i + m_blockSize/2];
|
cannam@233
|
491 m_ri[i + m_blockSize/2] = value;
|
cannam@233
|
492 }
|
cannam@233
|
493
|
cannam@233
|
494 #ifdef HAVE_FFTW3
|
cannam@233
|
495
|
cannam@233
|
496 fftw_execute(m_plan);
|
cannam@233
|
497
|
cannam@233
|
498 for (int i = 0; i <= m_blockSize/2; ++i) {
|
cannam@233
|
499 m_freqbuf[c][i * 2] = float(m_cbuf[i][0]);
|
cannam@233
|
500 m_freqbuf[c][i * 2 + 1] = float(m_cbuf[i][1]);
|
cannam@233
|
501 }
|
cannam@233
|
502
|
cannam@233
|
503 #else
|
cannam@233
|
504
|
cannam@233
|
505 fft(m_blockSize, false, m_ri, 0, m_ro, m_io);
|
cannam@233
|
506
|
cannam@233
|
507 for (int i = 0; i <= m_blockSize/2; ++i) {
|
cannam@233
|
508 m_freqbuf[c][i * 2] = float(m_ro[i]);
|
cannam@233
|
509 m_freqbuf[c][i * 2 + 1] = float(m_io[i]);
|
cannam@233
|
510 }
|
cannam@233
|
511
|
cannam@233
|
512 #endif
|
cannam@233
|
513 }
|
cannam@233
|
514
|
cannam@288
|
515 FeatureSet pfs(m_plugin->process(m_freqbuf, timestamp));
|
cannam@288
|
516
|
cannam@288
|
517 if (!fs.empty()) { // add any prepad results back in
|
cannam@288
|
518 for (FeatureSet::const_iterator i = pfs.begin(); i != pfs.end(); ++i) {
|
cannam@288
|
519 for (FeatureList::const_iterator j = i->second.begin();
|
cannam@288
|
520 j != i->second.end(); ++j) {
|
cannam@288
|
521 fs[i->first].push_back(*j);
|
cannam@288
|
522 }
|
cannam@288
|
523 }
|
cannam@288
|
524 pfs = fs;
|
cannam@288
|
525 }
|
cannam@288
|
526
|
cannam@288
|
527 return pfs;
|
cannam@288
|
528 }
|
cannam@288
|
529
|
cannam@288
|
530 Plugin::FeatureSet
|
cannam@288
|
531 PluginInputDomainAdapter::Impl::prepadProcess(const float *const *inputBuffers,
|
cannam@288
|
532 RealTime timestamp)
|
cannam@288
|
533 {
|
cannam@288
|
534 FeatureSet fs;
|
cannam@288
|
535 //!!!
|
cannam@288
|
536 return fs;
|
cannam@233
|
537 }
|
cannam@233
|
538
|
cannam@233
|
539 #ifndef HAVE_FFTW3
|
cannam@233
|
540
|
cannam@233
|
541 void
|
cannam@233
|
542 PluginInputDomainAdapter::Impl::fft(unsigned int n, bool inverse,
|
cannam@233
|
543 double *ri, double *ii, double *ro, double *io)
|
cannam@233
|
544 {
|
cannam@233
|
545 if (!ri || !ro || !io) return;
|
cannam@233
|
546
|
cannam@233
|
547 unsigned int bits;
|
cannam@233
|
548 unsigned int i, j, k, m;
|
cannam@233
|
549 unsigned int blockSize, blockEnd;
|
cannam@233
|
550
|
cannam@233
|
551 double tr, ti;
|
cannam@233
|
552
|
cannam@233
|
553 if (n < 2) return;
|
cannam@233
|
554 if (n & (n-1)) return;
|
cannam@233
|
555
|
cannam@233
|
556 double angle = 2.0 * M_PI;
|
cannam@233
|
557 if (inverse) angle = -angle;
|
cannam@233
|
558
|
cannam@233
|
559 for (i = 0; ; ++i) {
|
cannam@233
|
560 if (n & (1 << i)) {
|
cannam@233
|
561 bits = i;
|
cannam@233
|
562 break;
|
cannam@233
|
563 }
|
cannam@233
|
564 }
|
cannam@233
|
565
|
cannam@233
|
566 static unsigned int tableSize = 0;
|
cannam@233
|
567 static int *table = 0;
|
cannam@233
|
568
|
cannam@233
|
569 if (tableSize != n) {
|
cannam@233
|
570
|
cannam@233
|
571 delete[] table;
|
cannam@233
|
572
|
cannam@233
|
573 table = new int[n];
|
cannam@233
|
574
|
cannam@233
|
575 for (i = 0; i < n; ++i) {
|
cannam@233
|
576
|
cannam@233
|
577 m = i;
|
cannam@233
|
578
|
cannam@233
|
579 for (j = k = 0; j < bits; ++j) {
|
cannam@233
|
580 k = (k << 1) | (m & 1);
|
cannam@233
|
581 m >>= 1;
|
cannam@233
|
582 }
|
cannam@233
|
583
|
cannam@233
|
584 table[i] = k;
|
cannam@233
|
585 }
|
cannam@233
|
586
|
cannam@233
|
587 tableSize = n;
|
cannam@233
|
588 }
|
cannam@233
|
589
|
cannam@233
|
590 if (ii) {
|
cannam@233
|
591 for (i = 0; i < n; ++i) {
|
cannam@233
|
592 ro[table[i]] = ri[i];
|
cannam@233
|
593 io[table[i]] = ii[i];
|
cannam@233
|
594 }
|
cannam@233
|
595 } else {
|
cannam@233
|
596 for (i = 0; i < n; ++i) {
|
cannam@233
|
597 ro[table[i]] = ri[i];
|
cannam@233
|
598 io[table[i]] = 0.0;
|
cannam@233
|
599 }
|
cannam@233
|
600 }
|
cannam@233
|
601
|
cannam@233
|
602 blockEnd = 1;
|
cannam@233
|
603
|
cannam@233
|
604 for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
|
cannam@233
|
605
|
cannam@233
|
606 double delta = angle / (double)blockSize;
|
cannam@233
|
607 double sm2 = -sin(-2 * delta);
|
cannam@233
|
608 double sm1 = -sin(-delta);
|
cannam@233
|
609 double cm2 = cos(-2 * delta);
|
cannam@233
|
610 double cm1 = cos(-delta);
|
cannam@233
|
611 double w = 2 * cm1;
|
cannam@233
|
612 double ar[3], ai[3];
|
cannam@233
|
613
|
cannam@233
|
614 for (i = 0; i < n; i += blockSize) {
|
cannam@233
|
615
|
cannam@233
|
616 ar[2] = cm2;
|
cannam@233
|
617 ar[1] = cm1;
|
cannam@233
|
618
|
cannam@233
|
619 ai[2] = sm2;
|
cannam@233
|
620 ai[1] = sm1;
|
cannam@233
|
621
|
cannam@233
|
622 for (j = i, m = 0; m < blockEnd; j++, m++) {
|
cannam@233
|
623
|
cannam@233
|
624 ar[0] = w * ar[1] - ar[2];
|
cannam@233
|
625 ar[2] = ar[1];
|
cannam@233
|
626 ar[1] = ar[0];
|
cannam@233
|
627
|
cannam@233
|
628 ai[0] = w * ai[1] - ai[2];
|
cannam@233
|
629 ai[2] = ai[1];
|
cannam@233
|
630 ai[1] = ai[0];
|
cannam@233
|
631
|
cannam@233
|
632 k = j + blockEnd;
|
cannam@233
|
633 tr = ar[0] * ro[k] - ai[0] * io[k];
|
cannam@233
|
634 ti = ar[0] * io[k] + ai[0] * ro[k];
|
cannam@233
|
635
|
cannam@233
|
636 ro[k] = ro[j] - tr;
|
cannam@233
|
637 io[k] = io[j] - ti;
|
cannam@233
|
638
|
cannam@233
|
639 ro[j] += tr;
|
cannam@233
|
640 io[j] += ti;
|
cannam@233
|
641 }
|
cannam@233
|
642 }
|
cannam@233
|
643
|
cannam@233
|
644 blockEnd = blockSize;
|
cannam@233
|
645 }
|
cannam@233
|
646
|
cannam@233
|
647 if (inverse) {
|
cannam@233
|
648
|
cannam@233
|
649 double denom = (double)n;
|
cannam@233
|
650
|
cannam@233
|
651 for (i = 0; i < n; i++) {
|
cannam@233
|
652 ro[i] /= denom;
|
cannam@233
|
653 io[i] /= denom;
|
cannam@233
|
654 }
|
cannam@233
|
655 }
|
cannam@233
|
656 }
|
cannam@233
|
657
|
cannam@233
|
658 #endif
|
cannam@233
|
659
|
cannam@233
|
660 }
|
cannam@233
|
661
|
cannam@233
|
662 }
|
cannam@233
|
663
|
cannam@263
|
664 _VAMP_SDK_HOSTSPACE_END(PluginInputDomainAdapter.cpp)
|
cannam@263
|
665
|