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 This file by Mark Levy and Chris Cannam, Copyright 2007-2008 QMUL.
|
cannam@233
|
11
|
cannam@233
|
12 Permission is hereby granted, free of charge, to any person
|
cannam@233
|
13 obtaining a copy of this software and associated documentation
|
cannam@233
|
14 files (the "Software"), to deal in the Software without
|
cannam@233
|
15 restriction, including without limitation the rights to use, copy,
|
cannam@233
|
16 modify, merge, publish, distribute, sublicense, and/or sell copies
|
cannam@233
|
17 of the Software, and to permit persons to whom the Software is
|
cannam@233
|
18 furnished to do so, subject to the following conditions:
|
cannam@233
|
19
|
cannam@233
|
20 The above copyright notice and this permission notice shall be
|
cannam@233
|
21 included in all copies or substantial portions of the Software.
|
cannam@233
|
22
|
cannam@233
|
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@233
|
24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@233
|
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
cannam@233
|
26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
cannam@233
|
27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@233
|
28 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@233
|
29 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@233
|
30
|
cannam@233
|
31 Except as contained in this notice, the names of the Centre for
|
cannam@233
|
32 Digital Music; Queen Mary, University of London; and Chris Cannam
|
cannam@233
|
33 shall not be used in advertising or otherwise to promote the sale,
|
cannam@233
|
34 use or other dealings in this Software without prior written
|
cannam@233
|
35 authorization.
|
cannam@233
|
36 */
|
cannam@233
|
37
|
cannam@233
|
38 #include <vector>
|
cannam@233
|
39 #include <map>
|
cannam@233
|
40
|
cannam@233
|
41 #include <vamp-hostsdk/PluginBufferingAdapter.h>
|
cannam@233
|
42
|
cannam@233
|
43 using std::vector;
|
cannam@233
|
44 using std::map;
|
cannam@233
|
45
|
cannam@233
|
46 namespace Vamp {
|
cannam@233
|
47
|
cannam@233
|
48 namespace HostExt {
|
cannam@233
|
49
|
cannam@233
|
50 class PluginBufferingAdapter::Impl
|
cannam@233
|
51 {
|
cannam@233
|
52 public:
|
cannam@233
|
53 Impl(Plugin *plugin, float inputSampleRate);
|
cannam@233
|
54 ~Impl();
|
cannam@233
|
55
|
cannam@233
|
56 void setPluginStepSize(size_t stepSize);
|
cannam@233
|
57 void setPluginBlockSize(size_t blockSize);
|
cannam@233
|
58
|
cannam@233
|
59 bool initialise(size_t channels, size_t stepSize, size_t blockSize);
|
cannam@233
|
60
|
cannam@233
|
61 void getActualStepAndBlockSizes(size_t &stepSize, size_t &blockSize);
|
cannam@233
|
62
|
cannam@233
|
63 OutputList getOutputDescriptors() const;
|
cannam@233
|
64
|
cannam@233
|
65 void reset();
|
cannam@233
|
66
|
cannam@233
|
67 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
|
cannam@233
|
68
|
cannam@233
|
69 FeatureSet getRemainingFeatures();
|
cannam@233
|
70
|
cannam@233
|
71 protected:
|
cannam@233
|
72 class RingBuffer
|
cannam@233
|
73 {
|
cannam@233
|
74 public:
|
cannam@233
|
75 RingBuffer(int n) :
|
cannam@233
|
76 m_buffer(new float[n+1]), m_writer(0), m_reader(0), m_size(n+1) { }
|
cannam@233
|
77 virtual ~RingBuffer() { delete[] m_buffer; }
|
cannam@233
|
78
|
cannam@233
|
79 int getSize() const { return m_size-1; }
|
cannam@233
|
80 void reset() { m_writer = 0; m_reader = 0; }
|
cannam@233
|
81
|
cannam@233
|
82 int getReadSpace() const {
|
cannam@233
|
83 int writer = m_writer, reader = m_reader, space;
|
cannam@233
|
84 if (writer > reader) space = writer - reader;
|
cannam@233
|
85 else if (writer < reader) space = (writer + m_size) - reader;
|
cannam@233
|
86 else space = 0;
|
cannam@233
|
87 return space;
|
cannam@233
|
88 }
|
cannam@233
|
89
|
cannam@233
|
90 int getWriteSpace() const {
|
cannam@233
|
91 int writer = m_writer;
|
cannam@233
|
92 int reader = m_reader;
|
cannam@233
|
93 int space = (reader + m_size - writer - 1);
|
cannam@233
|
94 if (space >= m_size) space -= m_size;
|
cannam@233
|
95 return space;
|
cannam@233
|
96 }
|
cannam@233
|
97
|
cannam@233
|
98 int peek(float *destination, int n) const {
|
cannam@233
|
99
|
cannam@233
|
100 int available = getReadSpace();
|
cannam@233
|
101
|
cannam@233
|
102 if (n > available) {
|
cannam@233
|
103 for (int i = available; i < n; ++i) {
|
cannam@233
|
104 destination[i] = 0.f;
|
cannam@233
|
105 }
|
cannam@233
|
106 n = available;
|
cannam@233
|
107 }
|
cannam@233
|
108 if (n == 0) return n;
|
cannam@233
|
109
|
cannam@233
|
110 int reader = m_reader;
|
cannam@233
|
111 int here = m_size - reader;
|
cannam@233
|
112 const float *const bufbase = m_buffer + reader;
|
cannam@233
|
113
|
cannam@233
|
114 if (here >= n) {
|
cannam@233
|
115 for (int i = 0; i < n; ++i) {
|
cannam@233
|
116 destination[i] = bufbase[i];
|
cannam@233
|
117 }
|
cannam@233
|
118 } else {
|
cannam@233
|
119 for (int i = 0; i < here; ++i) {
|
cannam@233
|
120 destination[i] = bufbase[i];
|
cannam@233
|
121 }
|
cannam@233
|
122 float *const destbase = destination + here;
|
cannam@233
|
123 const int nh = n - here;
|
cannam@233
|
124 for (int i = 0; i < nh; ++i) {
|
cannam@233
|
125 destbase[i] = m_buffer[i];
|
cannam@233
|
126 }
|
cannam@233
|
127 }
|
cannam@233
|
128
|
cannam@233
|
129 return n;
|
cannam@233
|
130 }
|
cannam@233
|
131
|
cannam@233
|
132 int skip(int n) {
|
cannam@233
|
133
|
cannam@233
|
134 int available = getReadSpace();
|
cannam@233
|
135 if (n > available) {
|
cannam@233
|
136 n = available;
|
cannam@233
|
137 }
|
cannam@233
|
138 if (n == 0) return n;
|
cannam@233
|
139
|
cannam@233
|
140 int reader = m_reader;
|
cannam@233
|
141 reader += n;
|
cannam@233
|
142 while (reader >= m_size) reader -= m_size;
|
cannam@233
|
143 m_reader = reader;
|
cannam@233
|
144 return n;
|
cannam@233
|
145 }
|
cannam@233
|
146
|
cannam@233
|
147 int write(const float *source, int n) {
|
cannam@233
|
148
|
cannam@233
|
149 int available = getWriteSpace();
|
cannam@233
|
150 if (n > available) {
|
cannam@233
|
151 n = available;
|
cannam@233
|
152 }
|
cannam@233
|
153 if (n == 0) return n;
|
cannam@233
|
154
|
cannam@233
|
155 int writer = m_writer;
|
cannam@233
|
156 int here = m_size - writer;
|
cannam@233
|
157 float *const bufbase = m_buffer + writer;
|
cannam@233
|
158
|
cannam@233
|
159 if (here >= n) {
|
cannam@233
|
160 for (int i = 0; i < n; ++i) {
|
cannam@233
|
161 bufbase[i] = source[i];
|
cannam@233
|
162 }
|
cannam@233
|
163 } else {
|
cannam@233
|
164 for (int i = 0; i < here; ++i) {
|
cannam@233
|
165 bufbase[i] = source[i];
|
cannam@233
|
166 }
|
cannam@233
|
167 const int nh = n - here;
|
cannam@233
|
168 const float *const srcbase = source + here;
|
cannam@233
|
169 float *const buf = m_buffer;
|
cannam@233
|
170 for (int i = 0; i < nh; ++i) {
|
cannam@233
|
171 buf[i] = srcbase[i];
|
cannam@233
|
172 }
|
cannam@233
|
173 }
|
cannam@233
|
174
|
cannam@233
|
175 writer += n;
|
cannam@233
|
176 while (writer >= m_size) writer -= m_size;
|
cannam@233
|
177 m_writer = writer;
|
cannam@233
|
178
|
cannam@233
|
179 return n;
|
cannam@233
|
180 }
|
cannam@233
|
181
|
cannam@233
|
182 int zero(int n) {
|
cannam@233
|
183
|
cannam@233
|
184 int available = getWriteSpace();
|
cannam@233
|
185 if (n > available) {
|
cannam@233
|
186 n = available;
|
cannam@233
|
187 }
|
cannam@233
|
188 if (n == 0) return n;
|
cannam@233
|
189
|
cannam@233
|
190 int writer = m_writer;
|
cannam@233
|
191 int here = m_size - writer;
|
cannam@233
|
192 float *const bufbase = m_buffer + writer;
|
cannam@233
|
193
|
cannam@233
|
194 if (here >= n) {
|
cannam@233
|
195 for (int i = 0; i < n; ++i) {
|
cannam@233
|
196 bufbase[i] = 0.f;
|
cannam@233
|
197 }
|
cannam@233
|
198 } else {
|
cannam@233
|
199 for (int i = 0; i < here; ++i) {
|
cannam@233
|
200 bufbase[i] = 0.f;
|
cannam@233
|
201 }
|
cannam@233
|
202 const int nh = n - here;
|
cannam@233
|
203 for (int i = 0; i < nh; ++i) {
|
cannam@233
|
204 m_buffer[i] = 0.f;
|
cannam@233
|
205 }
|
cannam@233
|
206 }
|
cannam@233
|
207
|
cannam@233
|
208 writer += n;
|
cannam@233
|
209 while (writer >= m_size) writer -= m_size;
|
cannam@233
|
210 m_writer = writer;
|
cannam@233
|
211
|
cannam@233
|
212 return n;
|
cannam@233
|
213 }
|
cannam@233
|
214
|
cannam@233
|
215 protected:
|
cannam@233
|
216 float *m_buffer;
|
cannam@233
|
217 int m_writer;
|
cannam@233
|
218 int m_reader;
|
cannam@233
|
219 int m_size;
|
cannam@233
|
220
|
cannam@233
|
221 private:
|
cannam@233
|
222 RingBuffer(const RingBuffer &); // not provided
|
cannam@233
|
223 RingBuffer &operator=(const RingBuffer &); // not provided
|
cannam@233
|
224 };
|
cannam@233
|
225
|
cannam@233
|
226 Plugin *m_plugin;
|
cannam@233
|
227 size_t m_inputStepSize; // value passed to wrapper initialise()
|
cannam@233
|
228 size_t m_inputBlockSize; // value passed to wrapper initialise()
|
cannam@233
|
229 size_t m_setStepSize; // value passed to setPluginStepSize()
|
cannam@233
|
230 size_t m_setBlockSize; // value passed to setPluginBlockSize()
|
cannam@233
|
231 size_t m_stepSize; // value actually used to initialise plugin
|
cannam@233
|
232 size_t m_blockSize; // value actually used to initialise plugin
|
cannam@233
|
233 size_t m_channels;
|
cannam@233
|
234 vector<RingBuffer *> m_queue;
|
cannam@233
|
235 float **m_buffers;
|
cannam@233
|
236 float m_inputSampleRate;
|
cannam@233
|
237 long m_frame;
|
cannam@233
|
238 bool m_unrun;
|
cannam@233
|
239 mutable OutputList m_outputs;
|
cannam@233
|
240 mutable std::map<int, bool> m_rewriteOutputTimes;
|
cannam@233
|
241
|
cannam@233
|
242 void processBlock(FeatureSet& allFeatureSets);
|
cannam@233
|
243 };
|
cannam@233
|
244
|
cannam@233
|
245 PluginBufferingAdapter::PluginBufferingAdapter(Plugin *plugin) :
|
cannam@233
|
246 PluginWrapper(plugin)
|
cannam@233
|
247 {
|
cannam@233
|
248 m_impl = new Impl(plugin, m_inputSampleRate);
|
cannam@233
|
249 }
|
cannam@233
|
250
|
cannam@233
|
251 PluginBufferingAdapter::~PluginBufferingAdapter()
|
cannam@233
|
252 {
|
cannam@233
|
253 delete m_impl;
|
cannam@233
|
254 }
|
cannam@233
|
255
|
cannam@233
|
256 size_t
|
cannam@233
|
257 PluginBufferingAdapter::getPreferredStepSize() const
|
cannam@233
|
258 {
|
cannam@233
|
259 return getPreferredBlockSize();
|
cannam@233
|
260 }
|
cannam@233
|
261
|
cannam@233
|
262 size_t
|
cannam@233
|
263 PluginBufferingAdapter::getPreferredBlockSize() const
|
cannam@233
|
264 {
|
cannam@233
|
265 return PluginWrapper::getPreferredBlockSize();
|
cannam@233
|
266 }
|
cannam@233
|
267
|
cannam@233
|
268 size_t
|
cannam@233
|
269 PluginBufferingAdapter::getPluginPreferredStepSize() const
|
cannam@233
|
270 {
|
cannam@233
|
271 return PluginWrapper::getPreferredStepSize();
|
cannam@233
|
272 }
|
cannam@233
|
273
|
cannam@233
|
274 size_t
|
cannam@233
|
275 PluginBufferingAdapter::getPluginPreferredBlockSize() const
|
cannam@233
|
276 {
|
cannam@233
|
277 return PluginWrapper::getPreferredBlockSize();
|
cannam@233
|
278 }
|
cannam@233
|
279
|
cannam@233
|
280 void
|
cannam@233
|
281 PluginBufferingAdapter::setPluginStepSize(size_t stepSize)
|
cannam@233
|
282 {
|
cannam@233
|
283 m_impl->setPluginStepSize(stepSize);
|
cannam@233
|
284 }
|
cannam@233
|
285
|
cannam@233
|
286 void
|
cannam@233
|
287 PluginBufferingAdapter::setPluginBlockSize(size_t blockSize)
|
cannam@233
|
288 {
|
cannam@233
|
289 m_impl->setPluginBlockSize(blockSize);
|
cannam@233
|
290 }
|
cannam@233
|
291
|
cannam@233
|
292 void
|
cannam@233
|
293 PluginBufferingAdapter::getActualStepAndBlockSizes(size_t &stepSize,
|
cannam@233
|
294 size_t &blockSize)
|
cannam@233
|
295 {
|
cannam@233
|
296 m_impl->getActualStepAndBlockSizes(stepSize, blockSize);
|
cannam@233
|
297 }
|
cannam@233
|
298
|
cannam@233
|
299 bool
|
cannam@233
|
300 PluginBufferingAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
cannam@233
|
301 {
|
cannam@233
|
302 return m_impl->initialise(channels, stepSize, blockSize);
|
cannam@233
|
303 }
|
cannam@233
|
304
|
cannam@233
|
305 PluginBufferingAdapter::OutputList
|
cannam@233
|
306 PluginBufferingAdapter::getOutputDescriptors() const
|
cannam@233
|
307 {
|
cannam@233
|
308 return m_impl->getOutputDescriptors();
|
cannam@233
|
309 }
|
cannam@233
|
310
|
cannam@233
|
311 void
|
cannam@233
|
312 PluginBufferingAdapter::reset()
|
cannam@233
|
313 {
|
cannam@233
|
314 m_impl->reset();
|
cannam@233
|
315 }
|
cannam@233
|
316
|
cannam@233
|
317 PluginBufferingAdapter::FeatureSet
|
cannam@233
|
318 PluginBufferingAdapter::process(const float *const *inputBuffers,
|
cannam@233
|
319 RealTime timestamp)
|
cannam@233
|
320 {
|
cannam@233
|
321 return m_impl->process(inputBuffers, timestamp);
|
cannam@233
|
322 }
|
cannam@233
|
323
|
cannam@233
|
324 PluginBufferingAdapter::FeatureSet
|
cannam@233
|
325 PluginBufferingAdapter::getRemainingFeatures()
|
cannam@233
|
326 {
|
cannam@233
|
327 return m_impl->getRemainingFeatures();
|
cannam@233
|
328 }
|
cannam@233
|
329
|
cannam@233
|
330 PluginBufferingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
|
cannam@233
|
331 m_plugin(plugin),
|
cannam@233
|
332 m_inputStepSize(0),
|
cannam@233
|
333 m_inputBlockSize(0),
|
cannam@233
|
334 m_setStepSize(0),
|
cannam@233
|
335 m_setBlockSize(0),
|
cannam@233
|
336 m_stepSize(0),
|
cannam@233
|
337 m_blockSize(0),
|
cannam@233
|
338 m_channels(0),
|
cannam@233
|
339 m_queue(0),
|
cannam@233
|
340 m_buffers(0),
|
cannam@233
|
341 m_inputSampleRate(inputSampleRate),
|
cannam@233
|
342 m_frame(0),
|
cannam@233
|
343 m_unrun(true)
|
cannam@233
|
344 {
|
cannam@233
|
345 (void)getOutputDescriptors(); // set up m_outputs and m_rewriteOutputTimes
|
cannam@233
|
346 }
|
cannam@233
|
347
|
cannam@233
|
348 PluginBufferingAdapter::Impl::~Impl()
|
cannam@233
|
349 {
|
cannam@233
|
350 // the adapter will delete the plugin
|
cannam@233
|
351
|
cannam@233
|
352 for (size_t i = 0; i < m_channels; ++i) {
|
cannam@233
|
353 delete m_queue[i];
|
cannam@233
|
354 delete[] m_buffers[i];
|
cannam@233
|
355 }
|
cannam@233
|
356 delete[] m_buffers;
|
cannam@233
|
357 }
|
cannam@233
|
358
|
cannam@233
|
359 void
|
cannam@233
|
360 PluginBufferingAdapter::Impl::setPluginStepSize(size_t stepSize)
|
cannam@233
|
361 {
|
cannam@233
|
362 if (m_inputStepSize != 0) {
|
cannam@233
|
363 std::cerr << "PluginBufferingAdapter::setPluginStepSize: ERROR: Cannot be called after initialise()" << std::endl;
|
cannam@233
|
364 return;
|
cannam@233
|
365 }
|
cannam@233
|
366 m_setStepSize = stepSize;
|
cannam@233
|
367 }
|
cannam@233
|
368
|
cannam@233
|
369 void
|
cannam@233
|
370 PluginBufferingAdapter::Impl::setPluginBlockSize(size_t blockSize)
|
cannam@233
|
371 {
|
cannam@233
|
372 if (m_inputBlockSize != 0) {
|
cannam@233
|
373 std::cerr << "PluginBufferingAdapter::setPluginBlockSize: ERROR: Cannot be called after initialise()" << std::endl;
|
cannam@233
|
374 return;
|
cannam@233
|
375 }
|
cannam@233
|
376 m_setBlockSize = blockSize;
|
cannam@233
|
377 }
|
cannam@233
|
378
|
cannam@233
|
379 void
|
cannam@233
|
380 PluginBufferingAdapter::Impl::getActualStepAndBlockSizes(size_t &stepSize,
|
cannam@233
|
381 size_t &blockSize)
|
cannam@233
|
382 {
|
cannam@233
|
383 stepSize = m_stepSize;
|
cannam@233
|
384 blockSize = m_blockSize;
|
cannam@233
|
385 }
|
cannam@233
|
386
|
cannam@233
|
387 bool
|
cannam@233
|
388 PluginBufferingAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
cannam@233
|
389 {
|
cannam@233
|
390 if (stepSize != blockSize) {
|
cannam@233
|
391 std::cerr << "PluginBufferingAdapter::initialise: input stepSize must be equal to blockSize for this adapter (stepSize = " << stepSize << ", blockSize = " << blockSize << ")" << std::endl;
|
cannam@233
|
392 return false;
|
cannam@233
|
393 }
|
cannam@233
|
394
|
cannam@233
|
395 m_channels = channels;
|
cannam@233
|
396 m_inputStepSize = stepSize;
|
cannam@233
|
397 m_inputBlockSize = blockSize;
|
cannam@233
|
398
|
cannam@233
|
399 // if the user has requested particular step or block sizes, use
|
cannam@233
|
400 // those; otherwise use the step and block sizes which the plugin
|
cannam@233
|
401 // prefers
|
cannam@233
|
402
|
cannam@233
|
403 m_stepSize = 0;
|
cannam@233
|
404 m_blockSize = 0;
|
cannam@233
|
405
|
cannam@233
|
406 if (m_setStepSize > 0) {
|
cannam@233
|
407 m_stepSize = m_setStepSize;
|
cannam@233
|
408 }
|
cannam@233
|
409 if (m_setBlockSize > 0) {
|
cannam@233
|
410 m_blockSize = m_setBlockSize;
|
cannam@233
|
411 }
|
cannam@233
|
412
|
cannam@233
|
413 if (m_stepSize == 0 && m_blockSize == 0) {
|
cannam@233
|
414 m_stepSize = m_plugin->getPreferredStepSize();
|
cannam@233
|
415 m_blockSize = m_plugin->getPreferredBlockSize();
|
cannam@233
|
416 }
|
cannam@233
|
417
|
cannam@233
|
418 bool freq = (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain);
|
cannam@233
|
419
|
cannam@233
|
420 // or sensible defaults if it has no preference
|
cannam@233
|
421 if (m_blockSize == 0) {
|
cannam@233
|
422 if (m_stepSize == 0) {
|
cannam@233
|
423 m_blockSize = 1024;
|
cannam@233
|
424 } else if (freq) {
|
cannam@233
|
425 m_blockSize = m_stepSize * 2;
|
cannam@233
|
426 } else {
|
cannam@233
|
427 m_blockSize = m_stepSize;
|
cannam@233
|
428 }
|
cannam@233
|
429 } else if (m_stepSize == 0) { // m_blockSize != 0 (that was handled above)
|
cannam@233
|
430 if (freq) {
|
cannam@233
|
431 m_stepSize = m_blockSize/2;
|
cannam@233
|
432 } else {
|
cannam@233
|
433 m_stepSize = m_blockSize;
|
cannam@233
|
434 }
|
cannam@233
|
435 }
|
cannam@233
|
436
|
cannam@233
|
437 // current implementation breaks if step is greater than block
|
cannam@233
|
438 if (m_stepSize > m_blockSize) {
|
cannam@233
|
439 size_t newBlockSize;
|
cannam@233
|
440 if (freq) {
|
cannam@233
|
441 newBlockSize = m_stepSize * 2;
|
cannam@233
|
442 } else {
|
cannam@233
|
443 newBlockSize = m_stepSize;
|
cannam@233
|
444 }
|
cannam@233
|
445 std::cerr << "PluginBufferingAdapter::initialise: WARNING: step size " << m_stepSize << " is greater than block size " << m_blockSize << ": cannot handle this in adapter; adjusting block size to " << newBlockSize << std::endl;
|
cannam@233
|
446 m_blockSize = newBlockSize;
|
cannam@233
|
447 }
|
cannam@233
|
448
|
cannam@233
|
449 std::cerr << "PluginBufferingAdapter::initialise: NOTE: stepSize " << m_inputStepSize << " -> " << m_stepSize
|
cannam@233
|
450 << ", blockSize " << m_inputBlockSize << " -> " << m_blockSize << std::endl;
|
cannam@233
|
451
|
cannam@233
|
452 m_buffers = new float *[m_channels];
|
cannam@233
|
453
|
cannam@233
|
454 for (size_t i = 0; i < m_channels; ++i) {
|
cannam@233
|
455 m_queue.push_back(new RingBuffer(m_blockSize + m_inputBlockSize));
|
cannam@233
|
456 m_buffers[i] = new float[m_blockSize];
|
cannam@233
|
457 }
|
cannam@233
|
458
|
cannam@233
|
459 return m_plugin->initialise(m_channels, m_stepSize, m_blockSize);
|
cannam@233
|
460 }
|
cannam@233
|
461
|
cannam@233
|
462 PluginBufferingAdapter::OutputList
|
cannam@233
|
463 PluginBufferingAdapter::Impl::getOutputDescriptors() const
|
cannam@233
|
464 {
|
cannam@233
|
465 if (m_outputs.empty()) {
|
cannam@233
|
466 m_outputs = m_plugin->getOutputDescriptors();
|
cannam@233
|
467 }
|
cannam@233
|
468
|
cannam@233
|
469 PluginBufferingAdapter::OutputList outs = m_outputs;
|
cannam@233
|
470
|
cannam@233
|
471 for (size_t i = 0; i < outs.size(); ++i) {
|
cannam@233
|
472
|
cannam@233
|
473 switch (outs[i].sampleType) {
|
cannam@233
|
474
|
cannam@233
|
475 case OutputDescriptor::OneSamplePerStep:
|
cannam@233
|
476 outs[i].sampleType = OutputDescriptor::FixedSampleRate;
|
cannam@233
|
477 outs[i].sampleRate = (1.f / m_inputSampleRate) * m_stepSize;
|
cannam@233
|
478 m_rewriteOutputTimes[i] = true;
|
cannam@233
|
479 break;
|
cannam@233
|
480
|
cannam@233
|
481 case OutputDescriptor::FixedSampleRate:
|
cannam@233
|
482 if (outs[i].sampleRate == 0.f) {
|
cannam@233
|
483 outs[i].sampleRate = (1.f / m_inputSampleRate) * m_stepSize;
|
cannam@233
|
484 }
|
cannam@233
|
485 // We actually only need to rewrite output times for
|
cannam@233
|
486 // features that don't have timestamps already, but we
|
cannam@233
|
487 // can't tell from here whether our features will have
|
cannam@233
|
488 // timestamps or not
|
cannam@233
|
489 m_rewriteOutputTimes[i] = true;
|
cannam@233
|
490 break;
|
cannam@233
|
491
|
cannam@233
|
492 case OutputDescriptor::VariableSampleRate:
|
cannam@233
|
493 m_rewriteOutputTimes[i] = false;
|
cannam@233
|
494 break;
|
cannam@233
|
495 }
|
cannam@233
|
496 }
|
cannam@233
|
497
|
cannam@233
|
498 return outs;
|
cannam@233
|
499 }
|
cannam@233
|
500
|
cannam@233
|
501 void
|
cannam@233
|
502 PluginBufferingAdapter::Impl::reset()
|
cannam@233
|
503 {
|
cannam@233
|
504 m_frame = 0;
|
cannam@233
|
505 m_unrun = true;
|
cannam@233
|
506
|
cannam@233
|
507 for (size_t i = 0; i < m_queue.size(); ++i) {
|
cannam@233
|
508 m_queue[i]->reset();
|
cannam@233
|
509 }
|
cannam@233
|
510
|
cannam@233
|
511 m_plugin->reset();
|
cannam@233
|
512 }
|
cannam@233
|
513
|
cannam@233
|
514 PluginBufferingAdapter::FeatureSet
|
cannam@233
|
515 PluginBufferingAdapter::Impl::process(const float *const *inputBuffers,
|
cannam@233
|
516 RealTime timestamp)
|
cannam@233
|
517 {
|
cannam@233
|
518 if (m_inputStepSize == 0) {
|
cannam@233
|
519 std::cerr << "PluginBufferingAdapter::process: ERROR: Plugin has not been initialised" << std::endl;
|
cannam@233
|
520 return FeatureSet();
|
cannam@233
|
521 }
|
cannam@233
|
522
|
cannam@233
|
523 FeatureSet allFeatureSets;
|
cannam@233
|
524
|
cannam@233
|
525 if (m_unrun) {
|
cannam@233
|
526 m_frame = RealTime::realTime2Frame(timestamp,
|
cannam@233
|
527 int(m_inputSampleRate + 0.5));
|
cannam@233
|
528 m_unrun = false;
|
cannam@233
|
529 }
|
cannam@233
|
530
|
cannam@233
|
531 // queue the new input
|
cannam@233
|
532
|
cannam@233
|
533 for (size_t i = 0; i < m_channels; ++i) {
|
cannam@233
|
534 int written = m_queue[i]->write(inputBuffers[i], m_inputBlockSize);
|
cannam@233
|
535 if (written < int(m_inputBlockSize) && i == 0) {
|
cannam@233
|
536 std::cerr << "WARNING: PluginBufferingAdapter::Impl::process: "
|
cannam@233
|
537 << "Buffer overflow: wrote " << written
|
cannam@233
|
538 << " of " << m_inputBlockSize
|
cannam@233
|
539 << " input samples (for plugin step size "
|
cannam@233
|
540 << m_stepSize << ", block size " << m_blockSize << ")"
|
cannam@233
|
541 << std::endl;
|
cannam@233
|
542 }
|
cannam@233
|
543 }
|
cannam@233
|
544
|
cannam@233
|
545 // process as much as we can
|
cannam@233
|
546
|
cannam@233
|
547 while (m_queue[0]->getReadSpace() >= int(m_blockSize)) {
|
cannam@233
|
548 processBlock(allFeatureSets);
|
cannam@233
|
549 }
|
cannam@233
|
550
|
cannam@233
|
551 return allFeatureSets;
|
cannam@233
|
552 }
|
cannam@233
|
553
|
cannam@233
|
554 PluginBufferingAdapter::FeatureSet
|
cannam@233
|
555 PluginBufferingAdapter::Impl::getRemainingFeatures()
|
cannam@233
|
556 {
|
cannam@233
|
557 FeatureSet allFeatureSets;
|
cannam@233
|
558
|
cannam@233
|
559 // process remaining samples in queue
|
cannam@233
|
560 while (m_queue[0]->getReadSpace() >= int(m_blockSize)) {
|
cannam@233
|
561 processBlock(allFeatureSets);
|
cannam@233
|
562 }
|
cannam@233
|
563
|
cannam@233
|
564 // pad any last samples remaining and process
|
cannam@233
|
565 if (m_queue[0]->getReadSpace() > 0) {
|
cannam@233
|
566 for (size_t i = 0; i < m_channels; ++i) {
|
cannam@233
|
567 m_queue[i]->zero(m_blockSize - m_queue[i]->getReadSpace());
|
cannam@233
|
568 }
|
cannam@233
|
569 processBlock(allFeatureSets);
|
cannam@233
|
570 }
|
cannam@233
|
571
|
cannam@233
|
572 // get remaining features
|
cannam@233
|
573
|
cannam@233
|
574 FeatureSet featureSet = m_plugin->getRemainingFeatures();
|
cannam@233
|
575
|
cannam@233
|
576 for (map<int, FeatureList>::iterator iter = featureSet.begin();
|
cannam@233
|
577 iter != featureSet.end(); ++iter) {
|
cannam@233
|
578 FeatureList featureList = iter->second;
|
cannam@233
|
579 for (size_t i = 0; i < featureList.size(); ++i) {
|
cannam@233
|
580 allFeatureSets[iter->first].push_back(featureList[i]);
|
cannam@233
|
581 }
|
cannam@233
|
582 }
|
cannam@233
|
583
|
cannam@233
|
584 return allFeatureSets;
|
cannam@233
|
585 }
|
cannam@233
|
586
|
cannam@233
|
587 void
|
cannam@233
|
588 PluginBufferingAdapter::Impl::processBlock(FeatureSet& allFeatureSets)
|
cannam@233
|
589 {
|
cannam@233
|
590 for (size_t i = 0; i < m_channels; ++i) {
|
cannam@233
|
591 m_queue[i]->peek(m_buffers[i], m_blockSize);
|
cannam@233
|
592 }
|
cannam@233
|
593
|
cannam@233
|
594 long frame = m_frame;
|
cannam@233
|
595 RealTime timestamp = RealTime::frame2RealTime
|
cannam@233
|
596 (frame, int(m_inputSampleRate + 0.5));
|
cannam@233
|
597
|
cannam@233
|
598 FeatureSet featureSet = m_plugin->process(m_buffers, timestamp);
|
cannam@233
|
599
|
cannam@233
|
600 for (FeatureSet::iterator iter = featureSet.begin();
|
cannam@233
|
601 iter != featureSet.end(); ++iter) {
|
cannam@233
|
602
|
cannam@233
|
603 int outputNo = iter->first;
|
cannam@233
|
604
|
cannam@233
|
605 if (m_rewriteOutputTimes[outputNo]) {
|
cannam@233
|
606
|
cannam@233
|
607 FeatureList featureList = iter->second;
|
cannam@233
|
608
|
cannam@233
|
609 for (size_t i = 0; i < featureList.size(); ++i) {
|
cannam@233
|
610
|
cannam@233
|
611 switch (m_outputs[outputNo].sampleType) {
|
cannam@233
|
612
|
cannam@233
|
613 case OutputDescriptor::OneSamplePerStep:
|
cannam@233
|
614 // use our internal timestamp, always
|
cannam@233
|
615 featureList[i].timestamp = timestamp;
|
cannam@233
|
616 featureList[i].hasTimestamp = true;
|
cannam@233
|
617 break;
|
cannam@233
|
618
|
cannam@233
|
619 case OutputDescriptor::FixedSampleRate:
|
cannam@233
|
620 // use our internal timestamp if feature lacks one
|
cannam@233
|
621 if (!featureList[i].hasTimestamp) {
|
cannam@233
|
622 featureList[i].timestamp = timestamp;
|
cannam@233
|
623 featureList[i].hasTimestamp = true;
|
cannam@233
|
624 }
|
cannam@233
|
625 break;
|
cannam@233
|
626
|
cannam@233
|
627 case OutputDescriptor::VariableSampleRate:
|
cannam@233
|
628 break; // plugin must set timestamp
|
cannam@233
|
629
|
cannam@233
|
630 default:
|
cannam@233
|
631 break;
|
cannam@233
|
632 }
|
cannam@233
|
633
|
cannam@233
|
634 allFeatureSets[outputNo].push_back(featureList[i]);
|
cannam@233
|
635 }
|
cannam@233
|
636 } else {
|
cannam@233
|
637 for (size_t i = 0; i < iter->second.size(); ++i) {
|
cannam@233
|
638 allFeatureSets[outputNo].push_back(iter->second[i]);
|
cannam@233
|
639 }
|
cannam@233
|
640 }
|
cannam@233
|
641 }
|
cannam@233
|
642
|
cannam@233
|
643 // step forward
|
cannam@233
|
644
|
cannam@233
|
645 for (size_t i = 0; i < m_channels; ++i) {
|
cannam@233
|
646 m_queue[i]->skip(m_stepSize);
|
cannam@233
|
647 }
|
cannam@233
|
648
|
cannam@233
|
649 // increment internal frame counter each time we step forward
|
cannam@233
|
650 m_frame += m_stepSize;
|
cannam@233
|
651 }
|
cannam@233
|
652
|
cannam@233
|
653 }
|
cannam@233
|
654
|
cannam@233
|
655 }
|
cannam@233
|
656
|
cannam@233
|
657
|