annotate vamp-sdk/hostext/PluginBufferingAdapter.cpp @ 103:2cb46126ef59

* Attempt to clarify licensing implications of HAVE_FFTW3
author cannam
date Tue, 29 Jan 2008 16:13:22 +0000
parents ca40f3bc99f0
children 08d8c8ee6097
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@92 60 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
cannam@92 61
cannam@92 62 FeatureSet getRemainingFeatures();
cannam@92 63
cannam@92 64 protected:
cannam@102 65 class RingBuffer
cannam@102 66 {
cannam@102 67 public:
cannam@102 68 RingBuffer(int n) :
cannam@102 69 m_buffer(new float[n+1]), m_writer(0), m_reader(0), m_size(n+1) { }
cannam@102 70 virtual ~RingBuffer() { delete[] m_buffer; }
cannam@102 71
cannam@102 72 int getSize() const { return m_size-1; }
cannam@102 73 void reset() { m_writer = 0; m_reader = 0; }
cannam@102 74
cannam@102 75 int getReadSpace() const {
cannam@102 76 int writer = m_writer, reader = m_reader, space;
cannam@102 77 if (writer > reader) space = writer - reader;
cannam@102 78 else if (writer < reader) space = (writer + m_size) - reader;
cannam@102 79 else space = 0;
cannam@102 80 return space;
cannam@102 81 }
cannam@102 82
cannam@102 83 int getWriteSpace() const {
cannam@102 84 int writer = m_writer;
cannam@102 85 int reader = m_reader;
cannam@102 86 int space = (reader + m_size - writer - 1);
cannam@102 87 if (space >= m_size) space -= m_size;
cannam@102 88 return space;
cannam@102 89 }
cannam@102 90
cannam@102 91 int peek(float *destination, int n) const {
cannam@102 92
cannam@102 93 int available = getReadSpace();
cannam@102 94
cannam@102 95 if (n > available) {
cannam@102 96 for (int i = available; i < n; ++i) {
cannam@102 97 destination[i] = 0.f;
cannam@102 98 }
cannam@102 99 n = available;
cannam@102 100 }
cannam@102 101 if (n == 0) return n;
cannam@102 102
cannam@102 103 int reader = m_reader;
cannam@102 104 int here = m_size - reader;
cannam@102 105 const float *const bufbase = m_buffer + reader;
cannam@102 106
cannam@102 107 if (here >= n) {
cannam@102 108 for (int i = 0; i < n; ++i) {
cannam@102 109 destination[i] = bufbase[i];
cannam@102 110 }
cannam@102 111 } else {
cannam@102 112 for (int i = 0; i < here; ++i) {
cannam@102 113 destination[i] = bufbase[i];
cannam@102 114 }
cannam@102 115 float *const destbase = destination + here;
cannam@102 116 const int nh = n - here;
cannam@102 117 for (int i = 0; i < nh; ++i) {
cannam@102 118 destbase[i] = m_buffer[i];
cannam@102 119 }
cannam@102 120 }
cannam@102 121
cannam@102 122 return n;
cannam@102 123 }
cannam@102 124
cannam@102 125 int skip(int n) {
cannam@102 126
cannam@102 127 int available = getReadSpace();
cannam@102 128 if (n > available) {
cannam@102 129 n = available;
cannam@102 130 }
cannam@102 131 if (n == 0) return n;
cannam@102 132
cannam@102 133 int reader = m_reader;
cannam@102 134 reader += n;
cannam@102 135 while (reader >= m_size) reader -= m_size;
cannam@102 136 m_reader = reader;
cannam@102 137 return n;
cannam@102 138 }
cannam@102 139
cannam@102 140 int write(const float *source, int n) {
cannam@102 141
cannam@102 142 int available = getWriteSpace();
cannam@102 143 if (n > available) {
cannam@102 144 n = available;
cannam@102 145 }
cannam@102 146 if (n == 0) return n;
cannam@102 147
cannam@102 148 int writer = m_writer;
cannam@102 149 int here = m_size - writer;
cannam@102 150 float *const bufbase = m_buffer + writer;
cannam@102 151
cannam@102 152 if (here >= n) {
cannam@102 153 for (int i = 0; i < n; ++i) {
cannam@102 154 bufbase[i] = source[i];
cannam@102 155 }
cannam@102 156 } else {
cannam@102 157 for (int i = 0; i < here; ++i) {
cannam@102 158 bufbase[i] = source[i];
cannam@102 159 }
cannam@102 160 const int nh = n - here;
cannam@102 161 const float *const srcbase = source + here;
cannam@102 162 float *const buf = m_buffer;
cannam@102 163 for (int i = 0; i < nh; ++i) {
cannam@102 164 buf[i] = srcbase[i];
cannam@102 165 }
cannam@102 166 }
cannam@102 167
cannam@102 168 writer += n;
cannam@102 169 while (writer >= m_size) writer -= m_size;
cannam@102 170 m_writer = writer;
cannam@102 171
cannam@102 172 return n;
cannam@102 173 }
cannam@102 174
cannam@102 175 int zero(int n) {
cannam@102 176
cannam@102 177 int available = getWriteSpace();
cannam@102 178 if (n > available) {
cannam@102 179 n = available;
cannam@102 180 }
cannam@102 181 if (n == 0) return n;
cannam@102 182
cannam@102 183 int writer = m_writer;
cannam@102 184 int here = m_size - writer;
cannam@102 185 float *const bufbase = m_buffer + writer;
cannam@102 186
cannam@102 187 if (here >= n) {
cannam@102 188 for (int i = 0; i < n; ++i) {
cannam@102 189 bufbase[i] = 0.f;
cannam@102 190 }
cannam@102 191 } else {
cannam@102 192 for (int i = 0; i < here; ++i) {
cannam@102 193 bufbase[i] = 0.f;
cannam@102 194 }
cannam@102 195 const int nh = n - here;
cannam@102 196 for (int i = 0; i < nh; ++i) {
cannam@102 197 m_buffer[i] = 0.f;
cannam@102 198 }
cannam@102 199 }
cannam@102 200
cannam@102 201 writer += n;
cannam@102 202 while (writer >= m_size) writer -= m_size;
cannam@102 203 m_writer = writer;
cannam@102 204
cannam@102 205 return n;
cannam@102 206 }
cannam@102 207
cannam@102 208 protected:
cannam@102 209 float *m_buffer;
cannam@102 210 int m_writer;
cannam@102 211 int m_reader;
cannam@102 212 int m_size;
cannam@102 213
cannam@102 214 private:
cannam@102 215 RingBuffer(const RingBuffer &); // not provided
cannam@102 216 RingBuffer &operator=(const RingBuffer &); // not provided
cannam@102 217 };
cannam@102 218
cannam@92 219 Plugin *m_plugin;
cannam@92 220 size_t m_inputStepSize;
cannam@92 221 size_t m_inputBlockSize;
cannam@92 222 size_t m_stepSize;
cannam@92 223 size_t m_blockSize;
cannam@92 224 size_t m_channels;
cannam@102 225 vector<RingBuffer *> m_queue;
cannam@102 226 float **m_buffers;
cannam@92 227 float m_inputSampleRate;
cannam@92 228 RealTime m_timestamp;
cannam@92 229 OutputList m_outputs;
cannam@92 230
cannam@92 231 void processBlock(FeatureSet& allFeatureSets, RealTime timestamp);
cannam@92 232 };
cannam@92 233
cannam@92 234 PluginBufferingAdapter::PluginBufferingAdapter(Plugin *plugin) :
cannam@92 235 PluginWrapper(plugin)
cannam@92 236 {
cannam@92 237 m_impl = new Impl(plugin, m_inputSampleRate);
cannam@92 238 }
cannam@92 239
cannam@92 240 PluginBufferingAdapter::~PluginBufferingAdapter()
cannam@92 241 {
cannam@92 242 delete m_impl;
cannam@92 243 }
cannam@92 244
cannam@92 245 bool
cannam@92 246 PluginBufferingAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@92 247 {
cannam@92 248 return m_impl->initialise(channels, stepSize, blockSize);
cannam@92 249 }
cannam@92 250
cannam@92 251 PluginBufferingAdapter::OutputList
cannam@92 252 PluginBufferingAdapter::getOutputDescriptors() const
cannam@92 253 {
cannam@92 254 return m_impl->getOutputDescriptors();
cannam@92 255 }
cannam@92 256
cannam@92 257 PluginBufferingAdapter::FeatureSet
cannam@92 258 PluginBufferingAdapter::process(const float *const *inputBuffers,
cannam@92 259 RealTime timestamp)
cannam@92 260 {
cannam@92 261 return m_impl->process(inputBuffers, timestamp);
cannam@92 262 }
cannam@92 263
cannam@92 264 PluginBufferingAdapter::FeatureSet
cannam@92 265 PluginBufferingAdapter::getRemainingFeatures()
cannam@92 266 {
cannam@92 267 return m_impl->getRemainingFeatures();
cannam@92 268 }
cannam@92 269
cannam@92 270 PluginBufferingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
cannam@92 271 m_plugin(plugin),
cannam@92 272 m_inputStepSize(0),
cannam@92 273 m_inputBlockSize(0),
cannam@92 274 m_stepSize(0),
cannam@92 275 m_blockSize(0),
cannam@92 276 m_channels(0),
cannam@102 277 m_queue(0),
cannam@92 278 m_buffers(0),
cannam@92 279 m_inputSampleRate(inputSampleRate),
cannam@92 280 m_timestamp()
cannam@92 281 {
cannam@92 282 m_outputs = plugin->getOutputDescriptors();
cannam@92 283 }
cannam@92 284
cannam@92 285 PluginBufferingAdapter::Impl::~Impl()
cannam@92 286 {
cannam@92 287 // the adapter will delete the plugin
cannam@102 288
cannam@102 289 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 290 delete m_queue[i];
cannam@102 291 delete[] m_buffers[i];
cannam@102 292 }
cannam@102 293 delete[] m_buffers;
cannam@92 294 }
cannam@92 295
cannam@92 296 size_t
cannam@92 297 PluginBufferingAdapter::getPreferredStepSize() const
cannam@92 298 {
cannam@92 299 return getPreferredBlockSize();
cannam@92 300 }
cannam@92 301
cannam@92 302 bool
cannam@92 303 PluginBufferingAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@92 304 {
cannam@92 305 if (stepSize != blockSize) {
cannam@92 306 std::cerr << "PluginBufferingAdapter::initialise: input stepSize must be equal to blockSize for this adapter (stepSize = " << stepSize << ", blockSize = " << blockSize << ")" << std::endl;
cannam@92 307 return false;
cannam@92 308 }
cannam@92 309
cannam@92 310 m_channels = channels;
cannam@92 311 m_inputStepSize = stepSize;
cannam@92 312 m_inputBlockSize = blockSize;
cannam@92 313
cannam@92 314 // use the step and block sizes which the plugin prefers
cannam@92 315 m_stepSize = m_plugin->getPreferredStepSize();
cannam@92 316 m_blockSize = m_plugin->getPreferredBlockSize();
cannam@92 317
cannam@92 318 // or sensible defaults if it has no preference
cannam@92 319 if (m_blockSize == 0) {
cannam@92 320 m_blockSize = 1024;
cannam@92 321 }
cannam@92 322 if (m_stepSize == 0) {
cannam@92 323 if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@92 324 m_stepSize = m_blockSize/2;
cannam@92 325 } else {
cannam@92 326 m_stepSize = m_blockSize;
cannam@92 327 }
cannam@92 328 } else if (m_stepSize > m_blockSize) {
cannam@92 329 if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@92 330 m_blockSize = m_stepSize * 2;
cannam@92 331 } else {
cannam@92 332 m_blockSize = m_stepSize;
cannam@92 333 }
cannam@92 334 }
cannam@92 335
cannam@92 336 std::cerr << "PluginBufferingAdapter::initialise: stepSize " << m_inputStepSize << " -> " << m_stepSize
cannam@92 337 << ", blockSize " << m_inputBlockSize << " -> " << m_blockSize << std::endl;
cannam@92 338
cannam@92 339 // current implementation breaks if step is greater than block
cannam@92 340 if (m_stepSize > m_blockSize) {
cannam@92 341 std::cerr << "PluginBufferingAdapter::initialise: plugin's preferred stepSize greater than blockSize, giving up!" << std::endl;
cannam@92 342 return false;
cannam@92 343 }
cannam@102 344
cannam@102 345 m_buffers = new float *[m_channels];
cannam@102 346
cannam@102 347 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 348 m_queue.push_back(new RingBuffer(m_blockSize + m_inputBlockSize));
cannam@102 349 m_buffers[i] = new float[m_blockSize];
cannam@102 350 }
cannam@92 351
cannam@92 352 return m_plugin->initialise(m_channels, m_stepSize, m_blockSize);
cannam@92 353 }
cannam@92 354
cannam@92 355 PluginBufferingAdapter::OutputList
cannam@92 356 PluginBufferingAdapter::Impl::getOutputDescriptors() const
cannam@92 357 {
cannam@92 358 OutputList outs = m_plugin->getOutputDescriptors();
cannam@92 359 for (size_t i = 0; i < outs.size(); ++i) {
cannam@92 360 if (outs[i].sampleType == OutputDescriptor::OneSamplePerStep) {
cannam@92 361 outs[i].sampleRate = 1.f / m_stepSize;
cannam@92 362 }
cannam@92 363 outs[i].sampleType = OutputDescriptor::VariableSampleRate;
cannam@92 364 }
cannam@92 365 return outs;
cannam@92 366 }
cannam@92 367
cannam@92 368 PluginBufferingAdapter::FeatureSet
cannam@92 369 PluginBufferingAdapter::Impl::process(const float *const *inputBuffers,
cannam@92 370 RealTime timestamp)
cannam@92 371 {
cannam@92 372 FeatureSet allFeatureSets;
cannam@92 373
cannam@92 374 // queue the new input
cannam@92 375
cannam@102 376 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 377 int written = m_queue[i]->write(inputBuffers[i], m_inputBlockSize);
cannam@102 378 if (written < int(m_inputBlockSize) && i == 0) {
cannam@102 379 std::cerr << "WARNING: PluginBufferingAdapter::Impl::process: "
cannam@102 380 << "Buffer overflow: wrote " << written
cannam@102 381 << " of " << m_inputBlockSize
cannam@102 382 << " input samples (for plugin step size "
cannam@102 383 << m_stepSize << ", block size " << m_blockSize << ")"
cannam@102 384 << std::endl;
cannam@102 385 }
cannam@102 386 }
cannam@92 387
cannam@92 388 // process as much as we can
cannam@102 389
cannam@102 390 while (m_queue[0]->getReadSpace() >= int(m_blockSize)) {
cannam@92 391 processBlock(allFeatureSets, timestamp);
cannam@92 392 }
cannam@92 393
cannam@92 394 return allFeatureSets;
cannam@92 395 }
cannam@92 396
cannam@92 397 PluginBufferingAdapter::FeatureSet
cannam@92 398 PluginBufferingAdapter::Impl::getRemainingFeatures()
cannam@92 399 {
cannam@92 400 FeatureSet allFeatureSets;
cannam@92 401
cannam@92 402 // process remaining samples in queue
cannam@102 403 while (m_queue[0]->getReadSpace() >= int(m_blockSize)) {
cannam@92 404 processBlock(allFeatureSets, m_timestamp);
cannam@92 405 }
cannam@92 406
cannam@92 407 // pad any last samples remaining and process
cannam@102 408 if (m_queue[0]->getReadSpace() > 0) {
cannam@102 409 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 410 m_queue[i]->zero(m_blockSize - m_queue[i]->getReadSpace());
cannam@102 411 }
cannam@92 412 processBlock(allFeatureSets, m_timestamp);
cannam@92 413 }
cannam@92 414
cannam@92 415 // get remaining features
cannam@102 416
cannam@92 417 FeatureSet featureSet = m_plugin->getRemainingFeatures();
cannam@102 418
cannam@92 419 for (map<int, FeatureList>::iterator iter = featureSet.begin();
cannam@102 420 iter != featureSet.end(); ++iter) {
cannam@92 421 FeatureList featureList = iter->second;
cannam@102 422 for (size_t i = 0; i < featureList.size(); ++i) {
cannam@102 423 allFeatureSets[iter->first].push_back(featureList[i]);
cannam@102 424 }
cannam@92 425 }
cannam@92 426
cannam@92 427 return allFeatureSets;
cannam@92 428 }
cannam@92 429
cannam@92 430 void
cannam@102 431 PluginBufferingAdapter::Impl::processBlock(FeatureSet& allFeatureSets,
cannam@102 432 RealTime timestamp)
cannam@92 433 {
cannam@102 434 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 435 m_queue[i]->peek(m_buffers[i], m_blockSize);
cannam@102 436 }
cannam@102 437
cannam@92 438 FeatureSet featureSet = m_plugin->process(m_buffers, m_timestamp);
cannam@92 439
cannam@92 440 for (map<int, FeatureList>::iterator iter = featureSet.begin();
cannam@102 441 iter != featureSet.end(); ++iter) {
cannam@92 442
cannam@92 443 FeatureList featureList = iter->second;
cannam@92 444 int outputNo = iter->first;
cannam@92 445
cannam@102 446 for (size_t i = 0; i < featureList.size(); ++i) {
cannam@92 447
cannam@92 448 // make sure the timestamp is set
cannam@102 449 switch (m_outputs[outputNo].sampleType) {
cannam@102 450
cannam@92 451 case OutputDescriptor::OneSamplePerStep:
cannam@92 452 // use our internal timestamp - OK????
cannam@92 453 featureList[i].timestamp = m_timestamp;
cannam@92 454 break;
cannam@102 455
cannam@92 456 case OutputDescriptor::FixedSampleRate:
cannam@92 457 // use our internal timestamp
cannam@92 458 featureList[i].timestamp = m_timestamp;
cannam@92 459 break;
cannam@102 460
cannam@92 461 case OutputDescriptor::VariableSampleRate:
cannam@92 462 break; // plugin must set timestamp
cannam@102 463
cannam@92 464 default:
cannam@92 465 break;
cannam@92 466 }
cannam@92 467
cannam@92 468 allFeatureSets[outputNo].push_back(featureList[i]);
cannam@92 469 }
cannam@92 470 }
cannam@92 471
cannam@92 472 // step forward
cannam@102 473
cannam@102 474 for (size_t i = 0; i < m_channels; ++i) {
cannam@102 475 m_queue[i]->skip(m_stepSize);
cannam@102 476 }
cannam@92 477
cannam@92 478 // fake up the timestamp each time we step forward
cannam@102 479
cannam@102 480 long frame = RealTime::realTime2Frame(m_timestamp,
cannam@102 481 int(m_inputSampleRate + 0.5));
cannam@102 482 m_timestamp = RealTime::frame2RealTime(frame + m_stepSize,
cannam@102 483 int(m_inputSampleRate + 0.5));
cannam@92 484 }
cannam@92 485
cannam@92 486 }
cannam@92 487
cannam@92 488 }
cannam@92 489
cannam@92 490