comparison vamp-sdk/hostext/PluginBufferingAdapter.cpp @ 169:af8e59f43d1d

* Add capability for setting underlying plugin's step and block sizes to PluginBufferingAdapter
author cannam
date Fri, 25 Jul 2008 11:49:06 +0000
parents c1dce0b033cb
children ff72d97823f7
comparison
equal deleted inserted replaced
168:006a775133b1 169:af8e59f43d1d
50 class PluginBufferingAdapter::Impl 50 class PluginBufferingAdapter::Impl
51 { 51 {
52 public: 52 public:
53 Impl(Plugin *plugin, float inputSampleRate); 53 Impl(Plugin *plugin, float inputSampleRate);
54 ~Impl(); 54 ~Impl();
55 55
56 void setPluginStepSize(size_t stepSize);
57 void setPluginBlockSize(size_t blockSize);
58
56 bool initialise(size_t channels, size_t stepSize, size_t blockSize); 59 bool initialise(size_t channels, size_t stepSize, size_t blockSize);
57 60
58 OutputList getOutputDescriptors() const; 61 OutputList getOutputDescriptors() const;
59 62
60 void reset(); 63 void reset();
217 RingBuffer(const RingBuffer &); // not provided 220 RingBuffer(const RingBuffer &); // not provided
218 RingBuffer &operator=(const RingBuffer &); // not provided 221 RingBuffer &operator=(const RingBuffer &); // not provided
219 }; 222 };
220 223
221 Plugin *m_plugin; 224 Plugin *m_plugin;
222 size_t m_inputStepSize; 225 size_t m_inputStepSize; // value passed to wrapper initialise()
223 size_t m_inputBlockSize; 226 size_t m_inputBlockSize; // value passed to wrapper initialise()
224 size_t m_stepSize; 227 size_t m_setStepSize; // value passed to setPluginStepSize()
225 size_t m_blockSize; 228 size_t m_setBlockSize; // value passed to setPluginBlockSize()
229 size_t m_stepSize; // value actually used to initialise plugin
230 size_t m_blockSize; // value actually used to initialise plugin
226 size_t m_channels; 231 size_t m_channels;
227 vector<RingBuffer *> m_queue; 232 vector<RingBuffer *> m_queue;
228 float **m_buffers; 233 float **m_buffers;
229 float m_inputSampleRate; 234 float m_inputSampleRate;
230 long m_frame; 235 long m_frame;
243 248
244 PluginBufferingAdapter::~PluginBufferingAdapter() 249 PluginBufferingAdapter::~PluginBufferingAdapter()
245 { 250 {
246 delete m_impl; 251 delete m_impl;
247 } 252 }
253
254 size_t
255 PluginBufferingAdapter::getPreferredStepSize() const
256 {
257 return getPreferredBlockSize();
258 }
259
260 size_t
261 PluginBufferingAdapter::getPreferredBlockSize() const
262 {
263 return PluginWrapper::getPreferredBlockSize();
264 }
265
266 size_t
267 PluginBufferingAdapter::getPluginPreferredStepSize() const
268 {
269 return PluginWrapper::getPreferredStepSize();
270 }
271
272 size_t
273 PluginBufferingAdapter::getPluginPreferredBlockSize() const
274 {
275 return PluginWrapper::getPreferredBlockSize();
276 }
277
278 void
279 PluginBufferingAdapter::setPluginStepSize(size_t stepSize)
280 {
281 m_impl->setPluginStepSize(stepSize);
282 }
283
284 void
285 PluginBufferingAdapter::setPluginBlockSize(size_t blockSize)
286 {
287 m_impl->setPluginBlockSize(blockSize);
288 }
248 289
249 bool 290 bool
250 PluginBufferingAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize) 291 PluginBufferingAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
251 { 292 {
252 return m_impl->initialise(channels, stepSize, blockSize); 293 return m_impl->initialise(channels, stepSize, blockSize);
279 320
280 PluginBufferingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) : 321 PluginBufferingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
281 m_plugin(plugin), 322 m_plugin(plugin),
282 m_inputStepSize(0), 323 m_inputStepSize(0),
283 m_inputBlockSize(0), 324 m_inputBlockSize(0),
325 m_setStepSize(0),
326 m_setBlockSize(0),
284 m_stepSize(0), 327 m_stepSize(0),
285 m_blockSize(0), 328 m_blockSize(0),
286 m_channels(0), 329 m_channels(0),
287 m_queue(0), 330 m_queue(0),
288 m_buffers(0), 331 m_buffers(0),
301 delete m_queue[i]; 344 delete m_queue[i];
302 delete[] m_buffers[i]; 345 delete[] m_buffers[i];
303 } 346 }
304 delete[] m_buffers; 347 delete[] m_buffers;
305 } 348 }
306 349
307 size_t 350 void
308 PluginBufferingAdapter::getPreferredStepSize() const 351 PluginBufferingAdapter::Impl::setPluginStepSize(size_t stepSize)
309 { 352 {
310 return getPreferredBlockSize(); 353 if (m_inputStepSize != 0) {
311 } 354 std::cerr << "PluginBufferingAdapter::setPluginStepSize: ERROR: Cannot be called after initialise()" << std::endl;
312 355 return;
356 }
357 m_setStepSize = stepSize;
358 }
359
360 void
361 PluginBufferingAdapter::Impl::setPluginBlockSize(size_t blockSize)
362 {
363 if (m_inputBlockSize != 0) {
364 std::cerr << "PluginBufferingAdapter::setPluginBlockSize: ERROR: Cannot be called after initialise()" << std::endl;
365 return;
366 }
367 m_setBlockSize = blockSize;
368 }
369
313 bool 370 bool
314 PluginBufferingAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize) 371 PluginBufferingAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
315 { 372 {
316 if (stepSize != blockSize) { 373 if (stepSize != blockSize) {
317 std::cerr << "PluginBufferingAdapter::initialise: input stepSize must be equal to blockSize for this adapter (stepSize = " << stepSize << ", blockSize = " << blockSize << ")" << std::endl; 374 std::cerr << "PluginBufferingAdapter::initialise: input stepSize must be equal to blockSize for this adapter (stepSize = " << stepSize << ", blockSize = " << blockSize << ")" << std::endl;
319 } 376 }
320 377
321 m_channels = channels; 378 m_channels = channels;
322 m_inputStepSize = stepSize; 379 m_inputStepSize = stepSize;
323 m_inputBlockSize = blockSize; 380 m_inputBlockSize = blockSize;
324 381
325 // use the step and block sizes which the plugin prefers 382 // if the user has requested particular step or block sizes, use
326 m_stepSize = m_plugin->getPreferredStepSize(); 383 // those; otherwise use the step and block sizes which the plugin
327 m_blockSize = m_plugin->getPreferredBlockSize(); 384 // prefers
385
386 m_stepSize = 0;
387 m_blockSize = 0;
388
389 if (m_setStepSize > 0) {
390 m_stepSize = m_setStepSize;
391 }
392 if (m_setBlockSize > 0) {
393 m_blockSize = m_setBlockSize;
394 }
395
396 if (m_stepSize == 0 && m_blockSize == 0) {
397 m_stepSize = m_plugin->getPreferredStepSize();
398 m_blockSize = m_plugin->getPreferredBlockSize();
399 }
400
401 bool freq = (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain);
328 402
329 // or sensible defaults if it has no preference 403 // or sensible defaults if it has no preference
330 if (m_blockSize == 0) { 404 if (m_blockSize == 0) {
331 m_blockSize = 1024; 405 if (m_stepSize == 0) {
332 } 406 m_blockSize = 1024;
333 if (m_stepSize == 0) { 407 } else if (freq) {
334 if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) { 408 m_blockSize = m_stepSize * 2;
409 } else {
410 m_blockSize = m_stepSize;
411 }
412 } else if (m_stepSize == 0) { // m_blockSize != 0 (that was handled above)
413 if (freq) {
335 m_stepSize = m_blockSize/2; 414 m_stepSize = m_blockSize/2;
336 } else { 415 } else {
337 m_stepSize = m_blockSize; 416 m_stepSize = m_blockSize;
338 } 417 }
339 } else if (m_stepSize > m_blockSize) { 418 }
340 if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
341 m_blockSize = m_stepSize * 2;
342 } else {
343 m_blockSize = m_stepSize;
344 }
345 }
346
347 std::cerr << "PluginBufferingAdapter::initialise: stepSize " << m_inputStepSize << " -> " << m_stepSize
348 << ", blockSize " << m_inputBlockSize << " -> " << m_blockSize << std::endl;
349 419
350 // current implementation breaks if step is greater than block 420 // current implementation breaks if step is greater than block
351 if (m_stepSize > m_blockSize) { 421 if (m_stepSize > m_blockSize) {
352 std::cerr << "PluginBufferingAdapter::initialise: plugin's preferred stepSize greater than blockSize, giving up!" << std::endl; 422 size_t newBlockSize;
353 return false; 423 if (freq) {
354 } 424 newBlockSize = m_stepSize * 2;
425 } else {
426 newBlockSize = m_stepSize;
427 }
428 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;
429 m_blockSize = newBlockSize;
430 }
431
432 std::cerr << "PluginBufferingAdapter::initialise: NOTE: stepSize " << m_inputStepSize << " -> " << m_stepSize
433 << ", blockSize " << m_inputBlockSize << " -> " << m_blockSize << std::endl;
355 434
356 m_buffers = new float *[m_channels]; 435 m_buffers = new float *[m_channels];
357 436
358 for (size_t i = 0; i < m_channels; ++i) { 437 for (size_t i = 0; i < m_channels; ++i) {
359 m_queue.push_back(new RingBuffer(m_blockSize + m_inputBlockSize)); 438 m_queue.push_back(new RingBuffer(m_blockSize + m_inputBlockSize));
415 494
416 PluginBufferingAdapter::FeatureSet 495 PluginBufferingAdapter::FeatureSet
417 PluginBufferingAdapter::Impl::process(const float *const *inputBuffers, 496 PluginBufferingAdapter::Impl::process(const float *const *inputBuffers,
418 RealTime timestamp) 497 RealTime timestamp)
419 { 498 {
499 if (m_inputStepSize == 0) {
500 std::cerr << "PluginBufferingAdapter::process: ERROR: Plugin has not been initialised" << std::endl;
501 return FeatureSet();
502 }
503
420 FeatureSet allFeatureSets; 504 FeatureSet allFeatureSets;
421 505
422 if (m_unrun) { 506 if (m_unrun) {
423 m_frame = RealTime::realTime2Frame(timestamp, 507 m_frame = RealTime::realTime2Frame(timestamp,
424 int(m_inputSampleRate + 0.5)); 508 int(m_inputSampleRate + 0.5));