annotate vamp-sdk/hostext/PluginBufferingAdapter.cpp @ 127:1da43924fa14 vamp-plugin-sdk-v1.2

...
author cannam
date Thu, 28 Feb 2008 13:41:57 +0000
parents 08d8c8ee6097
children 92ca8e401044
rev   line source
cannam@92 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@92 2
cannam@92 3 /*
cannam@92 4 Vamp
cannam@92 5
cannam@92 6 An API for audio analysis and feature extraction plugins.
cannam@92 7
cannam@92 8 Centre for Digital Music, Queen Mary, University of London.
cannam@92 9 Copyright 2006-2007 Chris Cannam and QMUL.
cannam@102 10 This file by Mark Levy and Chris Cannam.
cannam@92 11
cannam@92 12 Permission is hereby granted, free of charge, to any person
cannam@92 13 obtaining a copy of this software and associated documentation
cannam@92 14 files (the "Software"), to deal in the Software without
cannam@92 15 restriction, including without limitation the rights to use, copy,
cannam@92 16 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@92 17 of the Software, and to permit persons to whom the Software is
cannam@92 18 furnished to do so, subject to the following conditions:
cannam@92 19
cannam@92 20 The above copyright notice and this permission notice shall be
cannam@92 21 included in all copies or substantial portions of the Software.
cannam@92 22
cannam@92 23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@92 24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@92 25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@92 26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@92 27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@92 28 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@92 29 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@92 30
cannam@92 31 Except as contained in this notice, the names of the Centre for
cannam@92 32 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@92 33 shall not be used in advertising or otherwise to promote the sale,
cannam@92 34 use or other dealings in this Software without prior written
cannam@92 35 authorization.
cannam@92 36 */
cannam@92 37
cannam@92 38 #include <vector>
cannam@92 39 #include <map>
cannam@92 40
cannam@92 41 #include "PluginBufferingAdapter.h"
cannam@92 42
cannam@92 43 using std::vector;
cannam@92 44 using std::map;
cannam@92 45
cannam@92 46 namespace Vamp {
cannam@92 47
cannam@92 48 namespace HostExt {
cannam@92 49
cannam@92 50 class PluginBufferingAdapter::Impl
cannam@92 51 {
cannam@92 52 public:
cannam@92 53 Impl(Plugin *plugin, float inputSampleRate);
cannam@92 54 ~Impl();
cannam@92 55
cannam@92 56 bool initialise(size_t channels, size_t stepSize, size_t blockSize);
cannam@92 57
cannam@92 58 OutputList getOutputDescriptors() const;
cannam@92 59
cannam@104 60 void reset();
cannam@104 61
cannam@92 62 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
cannam@92 63
cannam@92 64 FeatureSet getRemainingFeatures();
cannam@92 65
cannam@92 66 protected:
cannam@102 67 class RingBuffer
cannam@102 68 {
cannam@102 69 public:
cannam@102 70 RingBuffer(int n) :
cannam@102 71 m_buffer(new float[n+1]), m_writer(0), m_reader(0), m_size(n+1) { }
cannam@102 72 virtual ~RingBuffer() { delete[] m_buffer; }
cannam@102 73
cannam@102 74 int getSize() const { return m_size-1; }
cannam@102 75 void reset() { m_writer = 0; m_reader = 0; }
cannam@102 76
cannam@102 77 int getReadSpace() const {
cannam@102 78 int writer = m_writer, reader = m_reader, space;
cannam@102 79 if (writer > reader) space = writer - reader;
cannam@102 80 else if (writer < reader) space = (writer + m_size) - reader;
cannam@102 81 else space = 0;
cannam@102 82 return space;
cannam@102 83 }
cannam@102 84
cannam@102 85 int getWriteSpace() const {
cannam@102 86 int writer = m_writer;
cannam@102 87 int reader = m_reader;
cannam@102 88 int space = (reader + m_size - writer - 1);
cannam@102 89 if (space >= m_size) space -= m_size;
cannam@102 90 return space;
cannam@102 91 }
cannam@102 92
cannam@102 93 int peek(float *destination, int n) const {
cannam@102 94
cannam@102 95 int available = getReadSpace();
cannam@102 96
cannam@102 97 if (n > available) {
cannam@102 98 for (int i = available; i < n; ++i) {
cannam@102 99 destination[i] = 0.f;
cannam@102 100 }
cannam@102 101 n = available;
cannam@102 102 }
cannam@102 103 if (n == 0) return n;
cannam@102 104
cannam@102 105 int reader = m_reader;
cannam@102 106 int here = m_size - reader;
cannam@102 107 const float *const bufbase = m_buffer + reader;
cannam@102 108
cannam@102 109 if (here >= n) {
cannam@102 110 for (int i = 0; i < n; ++i) {
cannam@102 111 destination[i] = bufbase[i];
cannam@102 112 }
cannam@102 113 } else {
cannam@102 114 for (int i = 0; i < here; ++i) {
cannam@102 115 destination[i] = bufbase[i];
cannam@102 116 }
cannam@102 117 float *const destbase = destination + here;
cannam@102 118 const int nh = n - here;
cannam@102 119 for (int i = 0; i < nh; ++i) {
cannam@102 120 destbase[i] = m_buffer[i];
cannam@102 121 }
cannam@102 122 }
cannam@102 123
cannam@102 124 return n;
cannam@102 125 }
cannam@102 126
cannam@102 127 int skip(int n) {
cannam@102 128
cannam@102 129 int available = getReadSpace();
cannam@102 130 if (n > available) {
cannam@102 131 n = available;
cannam@102 132 }
cannam@102 133 if (n == 0) return n;
cannam@102 134
cannam@102 135 int reader = m_reader;
cannam@102 136 reader += n;
cannam@102 137 while (reader >= m_size) reader -= m_size;
cannam@102 138 m_reader = reader;
cannam@102 139 return n;
cannam@102 140 }
cannam@102 141
cannam@102 142 int write(const float *source, int n) {
cannam@102 143
cannam@102 144 int available = getWriteSpace();
cannam@102 145 if (n > available) {
cannam@102 146 n = available;
cannam@102 147 }
cannam@102 148 if (n == 0) return n;
cannam@102 149
cannam@102 150 int writer = m_writer;
cannam@102 151 int here = m_size - writer;
cannam@102 152 float *const bufbase = m_buffer + writer;
cannam@102 153
cannam@102 154 if (here >= n) {
cannam@102 155 for (int i = 0; i < n; ++i) {
cannam@102 156 bufbase[i] = source[i];
cannam@102 157 }
cannam@102 158 } else {
cannam@102 159 for (int i = 0; i < here; ++i) {
cannam@102 160 bufbase[i] = source[i];
cannam@102 161 }
cannam@102 162 const int nh = n - here;
cannam@102 163 const float *const srcbase = source + here;
cannam@102 164 float *const buf = m_buffer;
cannam@102 165 for (int i = 0; i < nh; ++i) {
cannam@102 166 buf[i] = srcbase[i];
cannam@102 167 }
cannam@102 168 }
cannam@102 169
cannam@102 170 writer += n;
cannam@102 171 while (writer >= m_size) writer -= m_size;
cannam@102 172 m_writer = writer;
cannam@102 173
cannam@102 174 return n;
cannam@102 175 }
cannam@102 176
cannam@102 177 int zero(int n) {
cannam@102 178
cannam@102 179 int available = getWriteSpace();
cannam@102 180 if (n > available) {
cannam@102 181 n = available;
cannam@102 182 }
cannam@102 183 if (n == 0) return n;
cannam@102 184
cannam@102 185 int writer = m_writer;
cannam@102 186 int here = m_size - writer;
cannam@102 187 float *const bufbase = m_buffer + writer;
cannam@102 188
cannam@102 189 if (here >= n) {
cannam@102 190 for (int i = 0; i < n; ++i) {
cannam@102 191 bufbase[i] = 0.f;
cannam@102 192 }
cannam@102 193 } else {
cannam@102 194 for (int i = 0; i < here; ++i) {
cannam@102 195 bufbase[i] = 0.f;
cannam@102 196 }
cannam@102 197 const int nh = n - here;
cannam@102 198 for (int i = 0; i < nh; ++i) {
cannam@102 199 m_buffer[i] = 0.f;
cannam@102 200 }
cannam@102 201 }
cannam@102 202
cannam@102 203 writer += n;
cannam@102 204 while (writer >= m_size) writer -= m_size;
cannam@102 205 m_writer = writer;
cannam@102 206
cannam@102 207 return n;
cannam@102 208 }
cannam@102 209
cannam@102 210 protected:
cannam@102 211 float *m_buffer;
cannam@102 212 int m_writer;
cannam@102 213 int m_reader;
cannam@102 214 int m_size;
cannam@102 215
cannam@102 216 private:
cannam@102 217 RingBuffer(const RingBuffer &); // not provided
cannam@102 218 RingBuffer &operator=(const RingBuffer &); // not provided
cannam@102 219 };
cannam@102 220
cannam@92 221 Plugin *m_plugin;
cannam@92 222 size_t m_inputStepSize;
cannam@92 223 size_t m_inputBlockSize;
cannam@92 224 size_t m_stepSize;
cannam@92 225 size_t m_blockSize;
cannam@92 226 size_t m_channels;
cannam@102 227 vector<RingBuffer *> m_queue;
cannam@102 228 float **m_buffers;
cannam@92 229 float m_inputSampleRate;
cannam@104 230 RealTime m_timestamp;
cannam@104 231 bool m_unrun;
cannam@92 232 OutputList m_outputs;
cannam@92 233
cannam@92 234 void processBlock(FeatureSet& allFeatureSets, RealTime timestamp);
cannam@92 235 };
cannam@92 236
cannam@92 237 PluginBufferingAdapter::PluginBufferingAdapter(Plugin *plugin) :
cannam@92 238 PluginWrapper(plugin)
cannam@92 239 {
cannam@92 240 m_impl = new Impl(plugin, m_inputSampleRate);
cannam@92 241 }
cannam@92 242
cannam@92 243 PluginBufferingAdapter::~PluginBufferingAdapter()
cannam@92 244 {
cannam@92 245 delete m_impl;
cannam@92 246 }
cannam@92 247
cannam@92 248 bool
cannam@92 249 PluginBufferingAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@92 250 {
cannam@92 251 return m_impl->initialise(channels, stepSize, blockSize);
cannam@92 252 }
cannam@92 253
cannam@92 254 PluginBufferingAdapter::OutputList
cannam@92 255 PluginBufferingAdapter::getOutputDescriptors() const
cannam@92 256 {
cannam@92 257 return m_impl->getOutputDescriptors();
cannam@92 258 }
cannam@104 259
cannam@104 260 void
cannam@104 261 PluginBufferingAdapter::reset()
cannam@104 262 {
cannam@104 263 m_impl->reset();
cannam@104 264 }
cannam@92 265
cannam@92 266 PluginBufferingAdapter::FeatureSet
cannam@92 267 PluginBufferingAdapter::process(const float *const *inputBuffers,
cannam@92 268 RealTime timestamp)
cannam@92 269 {
cannam@92 270 return m_impl->process(inputBuffers, timestamp);
cannam@92 271 }
cannam@92 272
cannam@92 273 PluginBufferingAdapter::FeatureSet
cannam@92 274 PluginBufferingAdapter::getRemainingFeatures()
cannam@92 275 {
cannam@92 276 return m_impl->getRemainingFeatures();
cannam@92 277 }
cannam@92 278
cannam@92 279 PluginBufferingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
cannam@92 280 m_plugin(plugin),
cannam@92 281 m_inputStepSize(0),
cannam@92 282 m_inputBlockSize(0),
cannam@92 283 m_stepSize(0),
cannam@92 284 m_blockSize(0),
cannam@92 285 m_channels(0),
cannam@102 286 m_queue(0),
cannam@92 287 m_buffers(0),
cannam@92 288 m_inputSampleRate(inputSampleRate),
cannam@104 289 m_timestamp(RealTime::zeroTime),
cannam@104 290 m_unrun(true)
cannam@92 291 {
cannam@92 292 m_outputs = plugin->getOutputDescriptors();
cannam@92 293 }
cannam@92 294
cannam@92 295 PluginBufferingAdapter::Impl::~Impl()
cannam@92 296 {
cannam@92 297 // the adapter will delete the plugin
cannam@102 298
cannam@102 299 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 300 delete m_queue[i];
cannam@102 301 delete[] m_buffers[i];
cannam@102 302 }
cannam@102 303 delete[] m_buffers;
cannam@92 304 }
cannam@92 305
cannam@92 306 size_t
cannam@92 307 PluginBufferingAdapter::getPreferredStepSize() const
cannam@92 308 {
cannam@92 309 return getPreferredBlockSize();
cannam@92 310 }
cannam@92 311
cannam@92 312 bool
cannam@92 313 PluginBufferingAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@92 314 {
cannam@92 315 if (stepSize != blockSize) {
cannam@92 316 std::cerr << "PluginBufferingAdapter::initialise: input stepSize must be equal to blockSize for this adapter (stepSize = " << stepSize << ", blockSize = " << blockSize << ")" << std::endl;
cannam@92 317 return false;
cannam@92 318 }
cannam@92 319
cannam@92 320 m_channels = channels;
cannam@92 321 m_inputStepSize = stepSize;
cannam@92 322 m_inputBlockSize = blockSize;
cannam@92 323
cannam@92 324 // use the step and block sizes which the plugin prefers
cannam@92 325 m_stepSize = m_plugin->getPreferredStepSize();
cannam@92 326 m_blockSize = m_plugin->getPreferredBlockSize();
cannam@92 327
cannam@92 328 // or sensible defaults if it has no preference
cannam@92 329 if (m_blockSize == 0) {
cannam@92 330 m_blockSize = 1024;
cannam@92 331 }
cannam@92 332 if (m_stepSize == 0) {
cannam@92 333 if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@92 334 m_stepSize = m_blockSize/2;
cannam@92 335 } else {
cannam@92 336 m_stepSize = m_blockSize;
cannam@92 337 }
cannam@92 338 } else if (m_stepSize > m_blockSize) {
cannam@92 339 if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@92 340 m_blockSize = m_stepSize * 2;
cannam@92 341 } else {
cannam@92 342 m_blockSize = m_stepSize;
cannam@92 343 }
cannam@92 344 }
cannam@92 345
cannam@92 346 std::cerr << "PluginBufferingAdapter::initialise: stepSize " << m_inputStepSize << " -> " << m_stepSize
cannam@92 347 << ", blockSize " << m_inputBlockSize << " -> " << m_blockSize << std::endl;
cannam@92 348
cannam@92 349 // current implementation breaks if step is greater than block
cannam@92 350 if (m_stepSize > m_blockSize) {
cannam@92 351 std::cerr << "PluginBufferingAdapter::initialise: plugin's preferred stepSize greater than blockSize, giving up!" << std::endl;
cannam@92 352 return false;
cannam@92 353 }
cannam@102 354
cannam@102 355 m_buffers = new float *[m_channels];
cannam@102 356
cannam@102 357 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 358 m_queue.push_back(new RingBuffer(m_blockSize + m_inputBlockSize));
cannam@102 359 m_buffers[i] = new float[m_blockSize];
cannam@102 360 }
cannam@92 361
cannam@92 362 return m_plugin->initialise(m_channels, m_stepSize, m_blockSize);
cannam@92 363 }
cannam@92 364
cannam@92 365 PluginBufferingAdapter::OutputList
cannam@92 366 PluginBufferingAdapter::Impl::getOutputDescriptors() const
cannam@92 367 {
cannam@92 368 OutputList outs = m_plugin->getOutputDescriptors();
cannam@92 369 for (size_t i = 0; i < outs.size(); ++i) {
cannam@92 370 if (outs[i].sampleType == OutputDescriptor::OneSamplePerStep) {
cannam@92 371 outs[i].sampleRate = 1.f / m_stepSize;
cannam@92 372 }
cannam@92 373 outs[i].sampleType = OutputDescriptor::VariableSampleRate;
cannam@92 374 }
cannam@92 375 return outs;
cannam@92 376 }
cannam@92 377
cannam@104 378 void
cannam@104 379 PluginBufferingAdapter::Impl::reset()
cannam@104 380 {
cannam@104 381 m_timestamp = RealTime::zeroTime;
cannam@104 382 m_unrun = true;
cannam@104 383
cannam@104 384 for (size_t i = 0; i < m_queue.size(); ++i) {
cannam@104 385 m_queue[i]->reset();
cannam@104 386 }
cannam@104 387 }
cannam@104 388
cannam@92 389 PluginBufferingAdapter::FeatureSet
cannam@92 390 PluginBufferingAdapter::Impl::process(const float *const *inputBuffers,
cannam@92 391 RealTime timestamp)
cannam@92 392 {
cannam@92 393 FeatureSet allFeatureSets;
cannam@104 394
cannam@104 395 if (m_unrun) {
cannam@104 396 m_timestamp = timestamp;
cannam@104 397 m_unrun = false;
cannam@104 398 }
cannam@92 399
cannam@92 400 // queue the new input
cannam@92 401
cannam@102 402 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 403 int written = m_queue[i]->write(inputBuffers[i], m_inputBlockSize);
cannam@102 404 if (written < int(m_inputBlockSize) && i == 0) {
cannam@102 405 std::cerr << "WARNING: PluginBufferingAdapter::Impl::process: "
cannam@102 406 << "Buffer overflow: wrote " << written
cannam@102 407 << " of " << m_inputBlockSize
cannam@102 408 << " input samples (for plugin step size "
cannam@102 409 << m_stepSize << ", block size " << m_blockSize << ")"
cannam@102 410 << std::endl;
cannam@102 411 }
cannam@102 412 }
cannam@92 413
cannam@92 414 // process as much as we can
cannam@102 415
cannam@102 416 while (m_queue[0]->getReadSpace() >= int(m_blockSize)) {
cannam@92 417 processBlock(allFeatureSets, timestamp);
cannam@92 418 }
cannam@92 419
cannam@92 420 return allFeatureSets;
cannam@92 421 }
cannam@92 422
cannam@92 423 PluginBufferingAdapter::FeatureSet
cannam@92 424 PluginBufferingAdapter::Impl::getRemainingFeatures()
cannam@92 425 {
cannam@92 426 FeatureSet allFeatureSets;
cannam@92 427
cannam@92 428 // process remaining samples in queue
cannam@102 429 while (m_queue[0]->getReadSpace() >= int(m_blockSize)) {
cannam@92 430 processBlock(allFeatureSets, m_timestamp);
cannam@92 431 }
cannam@92 432
cannam@92 433 // pad any last samples remaining and process
cannam@102 434 if (m_queue[0]->getReadSpace() > 0) {
cannam@102 435 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 436 m_queue[i]->zero(m_blockSize - m_queue[i]->getReadSpace());
cannam@102 437 }
cannam@92 438 processBlock(allFeatureSets, m_timestamp);
cannam@92 439 }
cannam@92 440
cannam@92 441 // get remaining features
cannam@102 442
cannam@92 443 FeatureSet featureSet = m_plugin->getRemainingFeatures();
cannam@102 444
cannam@92 445 for (map<int, FeatureList>::iterator iter = featureSet.begin();
cannam@102 446 iter != featureSet.end(); ++iter) {
cannam@92 447 FeatureList featureList = iter->second;
cannam@102 448 for (size_t i = 0; i < featureList.size(); ++i) {
cannam@102 449 allFeatureSets[iter->first].push_back(featureList[i]);
cannam@102 450 }
cannam@92 451 }
cannam@92 452
cannam@92 453 return allFeatureSets;
cannam@92 454 }
cannam@92 455
cannam@92 456 void
cannam@102 457 PluginBufferingAdapter::Impl::processBlock(FeatureSet& allFeatureSets,
cannam@102 458 RealTime timestamp)
cannam@92 459 {
cannam@102 460 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 461 m_queue[i]->peek(m_buffers[i], m_blockSize);
cannam@102 462 }
cannam@102 463
cannam@92 464 FeatureSet featureSet = m_plugin->process(m_buffers, m_timestamp);
cannam@92 465
cannam@92 466 for (map<int, FeatureList>::iterator iter = featureSet.begin();
cannam@102 467 iter != featureSet.end(); ++iter) {
cannam@92 468
cannam@92 469 FeatureList featureList = iter->second;
cannam@92 470 int outputNo = iter->first;
cannam@92 471
cannam@102 472 for (size_t i = 0; i < featureList.size(); ++i) {
cannam@92 473
cannam@92 474 // make sure the timestamp is set
cannam@102 475 switch (m_outputs[outputNo].sampleType) {
cannam@102 476
cannam@92 477 case OutputDescriptor::OneSamplePerStep:
cannam@92 478 // use our internal timestamp - OK????
cannam@92 479 featureList[i].timestamp = m_timestamp;
cannam@92 480 break;
cannam@102 481
cannam@92 482 case OutputDescriptor::FixedSampleRate:
cannam@92 483 // use our internal timestamp
cannam@92 484 featureList[i].timestamp = m_timestamp;
cannam@92 485 break;
cannam@102 486
cannam@92 487 case OutputDescriptor::VariableSampleRate:
cannam@92 488 break; // plugin must set timestamp
cannam@102 489
cannam@92 490 default:
cannam@92 491 break;
cannam@92 492 }
cannam@92 493
cannam@92 494 allFeatureSets[outputNo].push_back(featureList[i]);
cannam@92 495 }
cannam@92 496 }
cannam@92 497
cannam@92 498 // step forward
cannam@102 499
cannam@102 500 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 501 m_queue[i]->skip(m_stepSize);
cannam@102 502 }
cannam@92 503
cannam@92 504 // fake up the timestamp each time we step forward
cannam@102 505
cannam@102 506 long frame = RealTime::realTime2Frame(m_timestamp,
cannam@102 507 int(m_inputSampleRate + 0.5));
cannam@102 508 m_timestamp = RealTime::frame2RealTime(frame + m_stepSize,
cannam@102 509 int(m_inputSampleRate + 0.5));
cannam@92 510 }
cannam@92 511
cannam@92 512 }
cannam@92 513
cannam@92 514 }
cannam@92 515
cannam@92 516