annotate MatchVampPlugin.cpp @ 1:de792b8c2801

* update from other repo
author cannam
date Wed, 04 Jun 2008 19:32:59 +0000
parents 640f92242cc1
children 80fae79c9472
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 Vamp feature extraction plugin using the MATCH audio alignment
cannam@0 5 algorithm.
cannam@0 6
cannam@0 7 Centre for Digital Music, Queen Mary, University of London.
cannam@0 8 This file copyright 2007 Simon Dixon, Chris Cannam and QMUL.
cannam@0 9
cannam@0 10 This program is free software; you can redistribute it and/or
cannam@0 11 modify it under the terms of the GNU General Public License as
cannam@0 12 published by the Free Software Foundation; either version 2 of the
cannam@0 13 License, or (at your option) any later version. See the file
cannam@0 14 COPYING included with this distribution for more information.
cannam@0 15 */
cannam@0 16
cannam@0 17 #include "MatchVampPlugin.h"
cannam@0 18
cannam@0 19 #include "Matcher.h"
cannam@0 20 #include "MatchFeeder.h"
cannam@0 21 #include "Path.h"
cannam@0 22
cannam@0 23 #include <vamp/vamp.h>
cannam@0 24 #include <vamp-sdk/PluginAdapter.h>
cannam@0 25 #include <vamp-sdk/RealTime.h>
cannam@0 26
cannam@0 27 #include <vector>
cannam@0 28 #include <algorithm>
cannam@0 29
cannam@0 30 //static int extant = 0;
cannam@0 31
cannam@0 32 #ifdef _WIN32
cannam@0 33 HANDLE
cannam@0 34 MatchVampPlugin::m_serialisingMutex;
cannam@0 35 #else
cannam@0 36 pthread_mutex_t
cannam@0 37 MatchVampPlugin::m_serialisingMutex;
cannam@0 38 #endif
cannam@0 39
cannam@0 40 bool
cannam@0 41 MatchVampPlugin::m_serialisingMutexInitialised = false;
cannam@0 42
cannam@0 43 MatchVampPlugin::MatchVampPlugin(float inputSampleRate) :
cannam@0 44 Plugin(inputSampleRate),
cannam@0 45 m_serialise(false),
cannam@0 46 m_begin(true),
cannam@0 47 m_locked(false)
cannam@0 48 {
cannam@0 49 if (!m_serialisingMutexInitialised) {
cannam@0 50 m_serialisingMutexInitialised = true;
cannam@0 51 #ifdef _WIN32
cannam@0 52 m_serialisingMutex = CreateMutex(NULL, FALSE, NULL);
cannam@0 53 #else
cannam@0 54 pthread_mutex_init(&m_serialisingMutex, 0);
cannam@0 55 #endif
cannam@0 56 }
cannam@0 57
cannam@0 58 pm1 = 0;
cannam@0 59 pm2 = 0;
cannam@0 60 feeder = 0;
cannam@0 61 // std::cerr << "MatchVampPlugin::MatchVampPlugin(" << this << "): extant = " << ++extant << std::endl;
cannam@0 62 }
cannam@0 63
cannam@0 64 MatchVampPlugin::~MatchVampPlugin()
cannam@0 65 {
cannam@0 66 // std::cerr << "MatchVampPlugin::~MatchVampPlugin(" << this << "): extant = " << --extant << std::endl;
cannam@0 67
cannam@0 68 delete feeder;
cannam@0 69 delete pm1;
cannam@0 70 delete pm2;
cannam@0 71
cannam@0 72 if (m_locked) {
cannam@0 73 #ifdef _WIN32
cannam@0 74 ReleaseMutex(m_serialisingMutex);
cannam@0 75 #else
cannam@0 76 pthread_mutex_unlock(&m_serialisingMutex);
cannam@0 77 #endif
cannam@0 78 m_locked = false;
cannam@0 79 }
cannam@0 80 }
cannam@0 81
cannam@0 82 string
cannam@0 83 MatchVampPlugin::getIdentifier() const
cannam@0 84 {
cannam@0 85 return "match";
cannam@0 86 }
cannam@0 87
cannam@0 88 string
cannam@0 89 MatchVampPlugin::getName() const
cannam@0 90 {
cannam@0 91 return "Match Performance Aligner";
cannam@0 92 }
cannam@0 93
cannam@0 94 string
cannam@0 95 MatchVampPlugin::getDescription() const
cannam@0 96 {
cannam@0 97 return "Calculate alignment between two performances in separate channel inputs";
cannam@0 98 }
cannam@0 99
cannam@0 100 string
cannam@0 101 MatchVampPlugin::getMaker() const
cannam@0 102 {
cannam@0 103 return "Simon Dixon (plugin by Chris Cannam)";
cannam@0 104 }
cannam@0 105
cannam@0 106 int
cannam@0 107 MatchVampPlugin::getPluginVersion() const
cannam@0 108 {
cannam@0 109 return 1;
cannam@0 110 }
cannam@0 111
cannam@0 112 string
cannam@0 113 MatchVampPlugin::getCopyright() const
cannam@0 114 {
cannam@0 115 return "GPL";
cannam@0 116 }
cannam@0 117
cannam@0 118 MatchVampPlugin::ParameterList
cannam@0 119 MatchVampPlugin::getParameterDescriptors() const
cannam@0 120 {
cannam@0 121 ParameterList list;
cannam@0 122
cannam@0 123 ParameterDescriptor desc;
cannam@0 124 desc.identifier = "serialise";
cannam@0 125 desc.name = "Serialise Plugin Invocations";
cannam@0 126 desc.description = "Reduce potential memory load at the expense of multiprocessor performance by serialising multi-threaded plugin runs";
cannam@0 127 desc.minValue = 0;
cannam@0 128 desc.maxValue = 1;
cannam@0 129 desc.defaultValue = 0;
cannam@0 130 desc.isQuantized = true;
cannam@0 131 desc.quantizeStep = 1;
cannam@0 132 list.push_back(desc);
cannam@0 133
cannam@0 134 return list;
cannam@0 135 }
cannam@0 136
cannam@0 137 float
cannam@0 138 MatchVampPlugin::getParameter(std::string name) const
cannam@0 139 {
cannam@0 140 if (name == "serialise") {
cannam@0 141 return m_serialise ? 1.0 : 0.0;
cannam@0 142 }
cannam@0 143 return 0.0;
cannam@0 144 }
cannam@0 145
cannam@0 146 void
cannam@0 147 MatchVampPlugin::setParameter(std::string name, float value)
cannam@0 148 {
cannam@0 149 if (name == "serialise") {
cannam@0 150 m_serialise = (value > 0.5);
cannam@0 151 std::cerr << "MatchVampPlugin::setParameter: set serialise to " << m_serialise << std::endl;
cannam@0 152 }
cannam@0 153 }
cannam@0 154
cannam@0 155 size_t
cannam@0 156 MatchVampPlugin::getPreferredStepSize() const
cannam@0 157 {
cannam@0 158 if (!pm1) createMatchers();
cannam@0 159 return pm1->getHopSize();
cannam@0 160 }
cannam@0 161
cannam@0 162 size_t
cannam@0 163 MatchVampPlugin::getPreferredBlockSize() const
cannam@0 164 {
cannam@0 165 if (!pm1) createMatchers();
cannam@0 166 return pm1->getFFTSize();
cannam@0 167 }
cannam@0 168
cannam@0 169 void
cannam@0 170 MatchVampPlugin::createMatchers() const
cannam@0 171 {
cannam@0 172 pm1 = new Matcher(m_inputSampleRate, 0);
cannam@0 173 pm2 = new Matcher(m_inputSampleRate, pm1);
cannam@0 174 pm1->setOtherMatcher(pm2);
cannam@0 175 feeder = new MatchFeeder(pm1, pm2);
cannam@0 176 }
cannam@0 177
cannam@0 178 bool
cannam@0 179 MatchVampPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@0 180 {
cannam@0 181 if (!pm1) createMatchers();
cannam@0 182 if (channels < getMinChannelCount() ||
cannam@0 183 channels > getMaxChannelCount()) return false;
cannam@1 184 if (stepSize > blockSize/2 ||
cannam@0 185 blockSize != getPreferredBlockSize()) return false;
cannam@1 186 pm1->setHopSize(stepSize);
cannam@1 187 pm2->setHopSize(stepSize);
cannam@0 188 m_begin = true;
cannam@0 189 m_locked = false;
cannam@0 190 return true;
cannam@0 191 }
cannam@0 192
cannam@0 193 void
cannam@0 194 MatchVampPlugin::reset()
cannam@0 195 {
cannam@0 196 //!!!???
cannam@0 197 }
cannam@0 198
cannam@0 199 MatchVampPlugin::OutputList
cannam@0 200 MatchVampPlugin::getOutputDescriptors() const
cannam@0 201 {
cannam@0 202 OutputList list;
cannam@0 203
cannam@0 204 float outRate = 1.0 / 0.020; //!!! this is the default value of hopTime in Matcher
cannam@0 205
cannam@0 206 OutputDescriptor desc;
cannam@0 207 desc.identifier = "path";
cannam@0 208 desc.name = "Path";
cannam@0 209 desc.description = "Alignment path";
cannam@0 210 desc.unit = "";
cannam@0 211 desc.hasFixedBinCount = true;
cannam@0 212 desc.binCount = 1;
cannam@0 213 desc.hasKnownExtents = false;
cannam@0 214 desc.isQuantized = true;
cannam@0 215 desc.quantizeStep = 1;
cannam@0 216 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 217 desc.sampleRate = outRate;
cannam@0 218 list.push_back(desc);
cannam@0 219
cannam@0 220 desc.identifier = "a_b";
cannam@0 221 desc.name = "A-B Timeline";
cannam@0 222 desc.description = "Timing in performance B corresponding to moments in performance A";
cannam@0 223 desc.unit = "sec";
cannam@0 224 desc.hasFixedBinCount = true;
cannam@0 225 desc.binCount = 1;
cannam@0 226 desc.hasKnownExtents = false;
cannam@0 227 desc.isQuantized = false;
cannam@0 228 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 229 desc.sampleRate = outRate;
cannam@0 230 list.push_back(desc);
cannam@0 231
cannam@0 232 desc.identifier = "b_a";
cannam@0 233 desc.name = "B-A Timeline";
cannam@0 234 desc.description = "Timing in performance A corresponding to moments in performance B";
cannam@0 235 desc.unit = "sec";
cannam@0 236 desc.hasFixedBinCount = true;
cannam@0 237 desc.binCount = 1;
cannam@0 238 desc.hasKnownExtents = false;
cannam@0 239 desc.isQuantized = false;
cannam@0 240 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 241 desc.sampleRate = outRate;
cannam@0 242 list.push_back(desc);
cannam@0 243
cannam@0 244 desc.identifier = "a_b_divergence";
cannam@0 245 desc.name = "A-B Divergence";
cannam@0 246 desc.description = "Difference between timings in performances A and B";
cannam@0 247 desc.unit = "sec";
cannam@0 248 desc.hasFixedBinCount = true;
cannam@0 249 desc.binCount = 1;
cannam@0 250 desc.hasKnownExtents = false;
cannam@0 251 desc.isQuantized = false;
cannam@0 252 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 253 desc.sampleRate = outRate;
cannam@0 254 list.push_back(desc);
cannam@0 255
cannam@0 256 desc.identifier = "a_b_temporatio";
cannam@0 257 desc.name = "A-B Tempo Ratio";
cannam@0 258 desc.description = "Ratio of tempi between performances A and B";
cannam@0 259 desc.unit = "";
cannam@0 260 desc.hasFixedBinCount = true;
cannam@0 261 desc.binCount = 1;
cannam@0 262 desc.hasKnownExtents = false;
cannam@0 263 desc.isQuantized = false;
cannam@0 264 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 265 desc.sampleRate = outRate;
cannam@0 266 list.push_back(desc);
cannam@0 267
cannam@0 268 return list;
cannam@0 269 }
cannam@0 270
cannam@0 271 MatchVampPlugin::FeatureSet
cannam@0 272 MatchVampPlugin::process(const float *const *inputBuffers,
cannam@0 273 Vamp::RealTime timestamp)
cannam@0 274 {
cannam@0 275 if (m_begin) {
cannam@0 276 if (!m_locked && m_serialise) {
cannam@0 277 m_locked = true;
cannam@0 278 #ifdef _WIN32
cannam@0 279 WaitForSingleObject(m_serialisingMutex, INFINITE);
cannam@0 280 #else
cannam@0 281 pthread_mutex_lock(&m_serialisingMutex);
cannam@0 282 #endif
cannam@0 283 }
cannam@0 284 m_begin = false;
cannam@0 285 }
cannam@0 286
cannam@0 287 // std::cerr << timestamp.toString();
cannam@0 288
cannam@0 289 feeder->feed(inputBuffers);
cannam@0 290
cannam@0 291 // std::cerr << ".";
cannam@0 292 // std::cerr << std::endl;
cannam@0 293
cannam@0 294 return FeatureSet();
cannam@0 295 }
cannam@0 296
cannam@0 297 MatchVampPlugin::FeatureSet
cannam@0 298 MatchVampPlugin::getRemainingFeatures()
cannam@0 299 {
cannam@0 300 int x = pm2->getFrameCount() - 1;
cannam@0 301 int y = pm1->getFrameCount() - 1;
cannam@0 302
cannam@0 303 Finder *finder = feeder->getFinder();
cannam@0 304
cannam@0 305 std::vector<int> pathx;
cannam@0 306 std::vector<int> pathy;
cannam@0 307
cannam@0 308 // std::cerr << "initial x,y = " << x << std::endl;
cannam@0 309
cannam@0 310 while (finder->find(y, x) && ((x > 0) || (y > 0))) {
cannam@0 311
cannam@0 312 pathx.push_back(x);
cannam@0 313 pathy.push_back(y);
cannam@0 314
cannam@0 315 // std::cerr << pathx.size() << ": (" << x << "," << y << ")" << std::endl;
cannam@0 316
cannam@0 317 switch (finder->getDistance() & ADVANCE_BOTH){
cannam@0 318 case ADVANCE_THIS: y--; break;
cannam@0 319 case ADVANCE_OTHER: x--; break;
cannam@0 320 case ADVANCE_BOTH: x--; y--; break;
cannam@0 321 default: // this would indicate a bug, but we wouldn't want to hang
cannam@0 322 std::cerr << "WARNING: MatchVampPlugin::getRemainingFeatures: Neither matcher advanced in path backtrack at (" << x << "," << y << ")" << std::endl;
cannam@0 323 if (x > y) x--; else y--; break;
cannam@0 324 }
cannam@0 325 }
cannam@0 326
cannam@0 327 std::reverse(pathx.begin(), pathx.end());
cannam@0 328 std::reverse(pathy.begin(), pathy.end());
cannam@0 329
cannam@0 330 int smoothedLen = Path().smooth(pathx, pathy, pathx.size());
cannam@0 331
cannam@0 332 FeatureSet returnFeatures;
cannam@0 333
cannam@0 334 int prevx = 0;
cannam@0 335 int prevy = 0;
cannam@0 336
cannam@0 337 for (int i = 0; i < smoothedLen; ++i) {
cannam@0 338
cannam@0 339 int x = pathx[i];
cannam@0 340 int y = pathy[i];
cannam@0 341
cannam@0 342 Vamp::RealTime xt = Vamp::RealTime::frame2RealTime
cannam@0 343 (x * pm1->getHopSize(), lrintf(m_inputSampleRate));
cannam@0 344 Vamp::RealTime yt = Vamp::RealTime::frame2RealTime
cannam@0 345 (y * pm2->getHopSize(), lrintf(m_inputSampleRate));
cannam@0 346
cannam@0 347 Feature feature;
cannam@0 348 feature.hasTimestamp = true;
cannam@0 349 feature.timestamp = xt;
cannam@0 350 feature.values.clear();
cannam@0 351 feature.values.push_back(yt.sec + double(yt.nsec)/1.0e9);
cannam@0 352 returnFeatures[0].push_back(feature);
cannam@0 353
cannam@0 354 if (x != prevx) {
cannam@0 355
cannam@0 356 feature.hasTimestamp = true;
cannam@0 357 feature.timestamp = xt;
cannam@0 358 feature.values.clear();
cannam@0 359 feature.values.push_back(yt.sec + yt.msec()/1000.0);
cannam@0 360 returnFeatures[1].push_back(feature);
cannam@0 361
cannam@0 362 Vamp::RealTime diff = yt - xt;
cannam@0 363 feature.values.clear();
cannam@0 364 feature.values.push_back(diff.sec + diff.msec()/1000.0);
cannam@0 365 returnFeatures[3].push_back(feature);
cannam@0 366
cannam@0 367 if (i > 0) {
cannam@0 368 int lookback = 100; //!!! arbitrary
cannam@0 369 if (lookback > i) lookback = i;
cannam@0 370 int xdiff = x - pathx[i-lookback];
cannam@0 371 int ydiff = y - pathy[i-lookback];
cannam@0 372 if (xdiff != 0 && ydiff != 0) {
cannam@0 373 float ratio = float(ydiff)/float(xdiff);
cannam@0 374 if (ratio < 8 && ratio > (1.0/8)) { //!!! just for now, since we aren't dealing properly with silence yet
cannam@0 375 feature.values.clear();
cannam@0 376 feature.values.push_back(ratio);
cannam@0 377 returnFeatures[4].push_back(feature);
cannam@0 378 }
cannam@0 379 }
cannam@0 380 }
cannam@0 381 }
cannam@0 382
cannam@0 383 if (y != prevy) {
cannam@0 384 feature.hasTimestamp = true;
cannam@0 385 feature.timestamp = yt;
cannam@0 386 feature.values.clear();
cannam@0 387 feature.values.push_back(xt.sec + xt.msec()/1000.0);
cannam@0 388 returnFeatures[2].push_back(feature);
cannam@0 389 }
cannam@0 390
cannam@0 391 prevx = x;
cannam@0 392 prevy = y;
cannam@0 393 }
cannam@0 394
cannam@0 395 delete feeder;
cannam@0 396 delete pm1;
cannam@0 397 delete pm2;
cannam@0 398 feeder = 0;
cannam@0 399 pm1 = 0;
cannam@0 400 pm2 = 0;
cannam@0 401
cannam@0 402 if (m_locked) {
cannam@0 403 #ifdef _WIN32
cannam@0 404 ReleaseMutex(m_serialisingMutex);
cannam@0 405 #else
cannam@0 406 pthread_mutex_unlock(&m_serialisingMutex);
cannam@0 407 #endif
cannam@0 408 m_locked = false;
cannam@0 409 }
cannam@0 410
cannam@0 411 return returnFeatures;
cannam@0 412
cannam@0 413
cannam@0 414 /*
cannam@0 415 for (int i = 0; i < smoothedLen; ++i) {
cannam@0 416 std::cerr << i << ": [" << pathx[i] << "," << pathy[i] << "]" << std::endl;
cannam@0 417 }
cannam@0 418
cannam@0 419 std::cerr << std::endl;
cannam@0 420 std::cerr << "File: A" << std::endl;
cannam@0 421 std::cerr << "Marks: -1" << std::endl;
cannam@0 422 std::cerr << "FixedPoints: true 0" << std::endl;
cannam@0 423 std::cerr << "0" << std::endl;
cannam@0 424 std::cerr << "0" << std::endl;
cannam@0 425 std::cerr << "0" << std::endl;
cannam@0 426 std::cerr << "0" << std::endl;
cannam@0 427 std::cerr << "File: B" << std::endl;
cannam@0 428 std::cerr << "Marks: 0" << std::endl;
cannam@0 429 std::cerr << "FixedPoints: true 0" << std::endl;
cannam@0 430 std::cerr << "0.02" << std::endl;
cannam@0 431 std::cerr << "0.02" << std::endl;
cannam@0 432
cannam@0 433 std::cerr << smoothedLen << std::endl;
cannam@0 434 for (int i = 0; i < smoothedLen; ++i) {
cannam@0 435 std::cerr << pathx[i] << std::endl;
cannam@0 436 }
cannam@0 437
cannam@0 438 std::cerr << smoothedLen << std::endl;
cannam@0 439 for (int i = 0; i < smoothedLen; ++i) {
cannam@0 440 std::cerr << pathy[i] << std::endl;
cannam@0 441 }
cannam@0 442 */
cannam@0 443 }
cannam@0 444
cannam@0 445 static Vamp::PluginAdapter<MatchVampPlugin> mvpAdapter;
cannam@0 446
cannam@0 447 const VampPluginDescriptor *vampGetPluginDescriptor(unsigned int version,
cannam@0 448 unsigned int index)
cannam@0 449 {
cannam@0 450 if (version < 1) return 0;
cannam@0 451
cannam@0 452 switch (index) {
cannam@0 453 case 0: return mvpAdapter.getDescriptor();
cannam@0 454 default: return 0;
cannam@0 455 }
cannam@0 456 }