annotate src/MatchVampPlugin.cpp @ 74:b9aa663a607b refactors

Pull out feature extractor calls from Matcher, remove MatchFeeder, have only the feeder-from-features and use that in MatchVampPlugin
author Chris Cannam
date Wed, 19 Nov 2014 11:59:03 +0000
parents da9ead46abe9
children eb3392bf6150
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"
Chris@74 20 #include "MatchFeatureFeeder.h"
Chris@74 21 #include "FeatureExtractor.h"
cannam@0 22 #include "Path.h"
cannam@0 23
cannam@0 24 #include <vamp/vamp.h>
cannam@0 25 #include <vamp-sdk/PluginAdapter.h>
cannam@0 26 #include <vamp-sdk/RealTime.h>
cannam@0 27
cannam@0 28 #include <vector>
cannam@0 29 #include <algorithm>
cannam@0 30
cannam@0 31 //static int extant = 0;
cannam@0 32
cannam@0 33 #ifdef _WIN32
cannam@0 34 HANDLE
cannam@0 35 MatchVampPlugin::m_serialisingMutex;
cannam@0 36 #else
cannam@0 37 pthread_mutex_t
cannam@0 38 MatchVampPlugin::m_serialisingMutex;
cannam@0 39 #endif
cannam@0 40
cannam@0 41 bool
cannam@0 42 MatchVampPlugin::m_serialisingMutexInitialised = false;
cannam@0 43
Chris@10 44 // We want to ensure our freq map / crossover bin in Matcher.cpp are
Chris@10 45 // always valid with a fixed FFT length in seconds, so must reject low
Chris@10 46 // sample rates
Chris@10 47 static float sampleRateMin = 5000.f;
Chris@10 48
Chris@52 49 static float defaultStepTime = 0.020f;
Chris@15 50
cannam@0 51 MatchVampPlugin::MatchVampPlugin(float inputSampleRate) :
cannam@0 52 Plugin(inputSampleRate),
Chris@52 53 m_stepSize(int(inputSampleRate * defaultStepTime + 0.001)),
Chris@15 54 m_stepTime(defaultStepTime),
Chris@16 55 m_blockSize(2048),
cannam@0 56 m_serialise(false),
cannam@0 57 m_begin(true),
Chris@17 58 m_locked(false),
Chris@32 59 m_smooth(true),
Chris@74 60 m_frameNo(0),
Chris@74 61 m_lastFrameIn1(0),
Chris@74 62 m_lastFrameIn2(0),
Chris@17 63 m_params(inputSampleRate, defaultStepTime, m_blockSize),
Chris@38 64 m_defaultParams(inputSampleRate, defaultStepTime, m_blockSize),
Chris@38 65 m_feParams(inputSampleRate, m_blockSize),
Chris@38 66 m_defaultFeParams(inputSampleRate, m_blockSize)
cannam@0 67 {
Chris@10 68 if (inputSampleRate < sampleRateMin) {
Chris@10 69 std::cerr << "MatchVampPlugin::MatchVampPlugin: input sample rate "
Chris@10 70 << inputSampleRate << " < min supported rate "
Chris@10 71 << sampleRateMin << ", plugin will refuse to initialise"
Chris@10 72 << std::endl;
Chris@10 73 }
Chris@10 74
cannam@0 75 if (!m_serialisingMutexInitialised) {
cannam@0 76 m_serialisingMutexInitialised = true;
cannam@0 77 #ifdef _WIN32
cannam@0 78 m_serialisingMutex = CreateMutex(NULL, FALSE, NULL);
cannam@0 79 #else
cannam@0 80 pthread_mutex_init(&m_serialisingMutex, 0);
cannam@0 81 #endif
cannam@0 82 }
cannam@0 83
Chris@74 84 m_pm1 = 0;
Chris@74 85 m_pm2 = 0;
Chris@74 86 m_fe1 = 0;
Chris@74 87 m_fe2 = 0;
Chris@74 88 m_feeder = 0;
cannam@0 89 // std::cerr << "MatchVampPlugin::MatchVampPlugin(" << this << "): extant = " << ++extant << std::endl;
cannam@0 90 }
cannam@0 91
cannam@0 92 MatchVampPlugin::~MatchVampPlugin()
cannam@0 93 {
cannam@0 94 // std::cerr << "MatchVampPlugin::~MatchVampPlugin(" << this << "): extant = " << --extant << std::endl;
cannam@0 95
Chris@74 96 delete m_feeder;
Chris@74 97 delete m_fe1;
Chris@74 98 delete m_fe2;
Chris@74 99 delete m_pm1;
Chris@74 100 delete m_pm2;
cannam@0 101
cannam@0 102 if (m_locked) {
cannam@0 103 #ifdef _WIN32
cannam@0 104 ReleaseMutex(m_serialisingMutex);
cannam@0 105 #else
cannam@0 106 pthread_mutex_unlock(&m_serialisingMutex);
cannam@0 107 #endif
cannam@0 108 m_locked = false;
cannam@0 109 }
cannam@0 110 }
cannam@0 111
cannam@0 112 string
cannam@0 113 MatchVampPlugin::getIdentifier() const
cannam@0 114 {
cannam@0 115 return "match";
cannam@0 116 }
cannam@0 117
cannam@0 118 string
cannam@0 119 MatchVampPlugin::getName() const
cannam@0 120 {
cannam@0 121 return "Match Performance Aligner";
cannam@0 122 }
cannam@0 123
cannam@0 124 string
cannam@0 125 MatchVampPlugin::getDescription() const
cannam@0 126 {
cannam@0 127 return "Calculate alignment between two performances in separate channel inputs";
cannam@0 128 }
cannam@0 129
cannam@0 130 string
cannam@0 131 MatchVampPlugin::getMaker() const
cannam@0 132 {
cannam@0 133 return "Simon Dixon (plugin by Chris Cannam)";
cannam@0 134 }
cannam@0 135
cannam@0 136 int
cannam@0 137 MatchVampPlugin::getPluginVersion() const
cannam@0 138 {
Chris@19 139 return 2;
cannam@0 140 }
cannam@0 141
cannam@0 142 string
cannam@0 143 MatchVampPlugin::getCopyright() const
cannam@0 144 {
cannam@0 145 return "GPL";
cannam@0 146 }
cannam@0 147
cannam@0 148 MatchVampPlugin::ParameterList
cannam@0 149 MatchVampPlugin::getParameterDescriptors() const
cannam@0 150 {
cannam@0 151 ParameterList list;
cannam@0 152
cannam@0 153 ParameterDescriptor desc;
Chris@18 154
cannam@0 155 desc.identifier = "serialise";
cannam@0 156 desc.name = "Serialise Plugin Invocations";
cannam@0 157 desc.description = "Reduce potential memory load at the expense of multiprocessor performance by serialising multi-threaded plugin runs";
cannam@0 158 desc.minValue = 0;
cannam@0 159 desc.maxValue = 1;
cannam@0 160 desc.defaultValue = 0;
cannam@0 161 desc.isQuantized = true;
cannam@0 162 desc.quantizeStep = 1;
cannam@0 163 list.push_back(desc);
cannam@0 164
Chris@18 165 desc.identifier = "framenorm";
Chris@18 166 desc.name = "Frame Normalisation";
Chris@18 167 desc.description = "Type of normalisation to use for frequency-domain audio features";
Chris@18 168 desc.minValue = 0;
Chris@18 169 desc.maxValue = 2;
Chris@38 170 desc.defaultValue = (int)m_defaultFeParams.frameNorm;
Chris@18 171 desc.isQuantized = true;
Chris@18 172 desc.quantizeStep = 1;
Chris@18 173 desc.valueNames.clear();
Chris@18 174 desc.valueNames.push_back("None");
Chris@18 175 desc.valueNames.push_back("Sum To 1");
Chris@18 176 desc.valueNames.push_back("Long-Term Average");
Chris@18 177 list.push_back(desc);
Chris@18 178 desc.valueNames.clear();
Chris@18 179
Chris@18 180 desc.identifier = "distnorm";
Chris@18 181 desc.name = "Distance Normalisation";
Chris@18 182 desc.description = "Type of normalisation to use for distance metric";
Chris@18 183 desc.minValue = 0;
Chris@18 184 desc.maxValue = 2;
Chris@18 185 desc.defaultValue = (int)m_defaultParams.distanceNorm;
Chris@18 186 desc.isQuantized = true;
Chris@18 187 desc.quantizeStep = 1;
Chris@18 188 desc.valueNames.clear();
Chris@18 189 desc.valueNames.push_back("None");
Chris@18 190 desc.valueNames.push_back("Sum of Frames");
Chris@18 191 desc.valueNames.push_back("Log Sum of Frames");
Chris@18 192 list.push_back(desc);
Chris@18 193 desc.valueNames.clear();
Chris@18 194
Chris@18 195 desc.identifier = "usespecdiff";
Chris@18 196 desc.name = "Use Spectral Difference";
Chris@18 197 desc.description = "Whether to use half-wave rectified spectral difference instead of straight spectrum";
Chris@18 198 desc.minValue = 0;
Chris@18 199 desc.maxValue = 1;
Chris@38 200 desc.defaultValue = m_defaultFeParams.useSpectralDifference ? 1 : 0;
Chris@18 201 desc.isQuantized = true;
Chris@18 202 desc.quantizeStep = 1;
Chris@18 203 list.push_back(desc);
Chris@18 204
Chris@18 205 desc.identifier = "usechroma";
Chris@18 206 desc.name = "Use Chroma Frequency Map";
Chris@18 207 desc.description = "Whether to use a chroma frequency map instead of the default warped spectrogram";
Chris@18 208 desc.minValue = 0;
Chris@18 209 desc.maxValue = 1;
Chris@38 210 desc.defaultValue = m_defaultFeParams.useChromaFrequencyMap ? 1 : 0;
Chris@18 211 desc.isQuantized = true;
Chris@18 212 desc.quantizeStep = 1;
Chris@18 213 list.push_back(desc);
Chris@18 214
Chris@25 215 desc.identifier = "gradientlimit";
Chris@25 216 desc.name = "Gradient Limit";
Chris@18 217 desc.description = "Limit of number of frames that will be accepted from one source without a frame from the other source being accepted";
Chris@18 218 desc.minValue = 1;
Chris@18 219 desc.maxValue = 10;
Chris@18 220 desc.defaultValue = m_defaultParams.maxRunCount;
Chris@18 221 desc.isQuantized = true;
Chris@18 222 desc.quantizeStep = 1;
Chris@18 223 list.push_back(desc);
Chris@18 224
Chris@25 225 desc.identifier = "zonewidth";
Chris@25 226 desc.name = "Search Zone Width";
Chris@25 227 desc.description = "Width of the search zone (error margin) either side of the ongoing match position, in seconds";
Chris@25 228 desc.minValue = 1;
Chris@25 229 desc.maxValue = 60;
Chris@52 230 desc.defaultValue = (float)m_defaultParams.blockTime;
Chris@25 231 desc.isQuantized = true;
Chris@25 232 desc.quantizeStep = 1;
Chris@25 233 desc.unit = "s";
Chris@25 234 list.push_back(desc);
Chris@25 235
Chris@32 236 desc.identifier = "smooth";
Chris@32 237 desc.name = "Smooth Path";
Chris@32 238 desc.description = "Smooth the path by replacing steps with diagonals";
Chris@32 239 desc.minValue = 0;
Chris@32 240 desc.maxValue = 1;
Chris@32 241 desc.defaultValue = 1;
Chris@32 242 desc.isQuantized = true;
Chris@32 243 desc.quantizeStep = 1;
Chris@32 244 desc.unit = "";
Chris@32 245 list.push_back(desc);
Chris@32 246
cannam@0 247 return list;
cannam@0 248 }
cannam@0 249
cannam@0 250 float
cannam@0 251 MatchVampPlugin::getParameter(std::string name) const
cannam@0 252 {
cannam@0 253 if (name == "serialise") {
cannam@0 254 return m_serialise ? 1.0 : 0.0;
Chris@18 255 } else if (name == "framenorm") {
Chris@38 256 return (int)m_feParams.frameNorm;
Chris@18 257 } else if (name == "distnorm") {
Chris@18 258 return (int)m_params.distanceNorm;
Chris@18 259 } else if (name == "usespecdiff") {
Chris@38 260 return m_feParams.useSpectralDifference ? 1.0 : 0.0;
Chris@18 261 } else if (name == "usechroma") {
Chris@38 262 return m_feParams.useChromaFrequencyMap ? 1.0 : 0.0;
Chris@25 263 } else if (name == "gradientlimit") {
Chris@18 264 return m_params.maxRunCount;
Chris@25 265 } else if (name == "zonewidth") {
Chris@52 266 return (float)m_params.blockTime;
Chris@32 267 } else if (name == "smooth") {
Chris@32 268 return m_smooth ? 1.0 : 0.0;
cannam@0 269 }
Chris@18 270
cannam@0 271 return 0.0;
cannam@0 272 }
cannam@0 273
cannam@0 274 void
cannam@0 275 MatchVampPlugin::setParameter(std::string name, float value)
cannam@0 276 {
cannam@0 277 if (name == "serialise") {
cannam@0 278 m_serialise = (value > 0.5);
Chris@18 279 } else if (name == "framenorm") {
Chris@38 280 m_feParams.frameNorm = (FeatureExtractor::FrameNormalisation)(int(value + 0.1));
Chris@18 281 } else if (name == "distnorm") {
Chris@26 282 m_params.distanceNorm = (DistanceMetric::DistanceNormalisation)(int(value + 0.1));
Chris@18 283 } else if (name == "usespecdiff") {
Chris@38 284 m_feParams.useSpectralDifference = (value > 0.5);
Chris@18 285 } else if (name == "usechroma") {
Chris@38 286 m_feParams.useChromaFrequencyMap = (value > 0.5);
Chris@25 287 } else if (name == "gradientlimit") {
Chris@18 288 m_params.maxRunCount = int(value + 0.1);
Chris@25 289 } else if (name == "zonewidth") {
Chris@25 290 m_params.blockTime = value;
Chris@32 291 } else if (name == "smooth") {
Chris@32 292 m_smooth = (value > 0.5);
cannam@0 293 }
cannam@0 294 }
cannam@0 295
cannam@0 296 size_t
cannam@0 297 MatchVampPlugin::getPreferredStepSize() const
cannam@0 298 {
Chris@52 299 return int(m_inputSampleRate * defaultStepTime + 0.001);
cannam@0 300 }
cannam@0 301
cannam@0 302 size_t
cannam@0 303 MatchVampPlugin::getPreferredBlockSize() const
cannam@0 304 {
Chris@15 305 return 2048;
cannam@0 306 }
cannam@0 307
cannam@0 308 void
Chris@17 309 MatchVampPlugin::createMatchers()
cannam@0 310 {
Chris@17 311 m_params.hopTime = m_stepTime;
Chris@17 312 m_params.fftSize = m_blockSize;
Chris@38 313 m_feParams.fftSize = m_blockSize;
Chris@74 314 m_fe1 = new FeatureExtractor(m_feParams);
Chris@74 315 m_fe2 = new FeatureExtractor(m_feParams);
Chris@74 316 m_pm1 = new Matcher(m_params, 0, m_fe1->getFeatureSize());
Chris@74 317 m_pm2 = new Matcher(m_params, m_pm1, m_fe2->getFeatureSize());
Chris@74 318 m_pm1->setOtherMatcher(m_pm2);
Chris@74 319 m_feeder = new MatchFeatureFeeder(m_pm1, m_pm2);
cannam@0 320 }
cannam@0 321
cannam@0 322 bool
cannam@0 323 MatchVampPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@0 324 {
Chris@10 325 if (m_inputSampleRate < sampleRateMin) {
Chris@10 326 std::cerr << "MatchVampPlugin::MatchVampPlugin: input sample rate "
Chris@10 327 << m_inputSampleRate << " < min supported rate "
Chris@10 328 << sampleRateMin << std::endl;
Chris@10 329 return false;
Chris@10 330 }
cannam@0 331 if (channels < getMinChannelCount() ||
cannam@0 332 channels > getMaxChannelCount()) return false;
cannam@1 333 if (stepSize > blockSize/2 ||
cannam@0 334 blockSize != getPreferredBlockSize()) return false;
Chris@15 335
cannam@6 336 m_stepSize = stepSize;
Chris@15 337 m_stepTime = float(stepSize) / m_inputSampleRate;
Chris@15 338 m_blockSize = blockSize;
Chris@15 339
Chris@15 340 createMatchers();
cannam@0 341 m_begin = true;
cannam@0 342 m_locked = false;
Chris@15 343
cannam@0 344 return true;
cannam@0 345 }
cannam@0 346
cannam@0 347 void
cannam@0 348 MatchVampPlugin::reset()
cannam@0 349 {
Chris@74 350 delete m_feeder;
Chris@74 351 delete m_fe1;
Chris@74 352 delete m_fe2;
Chris@74 353 delete m_pm1;
Chris@74 354 delete m_pm2;
Chris@74 355
Chris@74 356 m_feeder = 0;
Chris@74 357 m_fe1 = 0;
Chris@74 358 m_fe2 = 0;
Chris@74 359 m_pm1 = 0;
Chris@74 360 m_pm2 = 0;
Chris@74 361
Chris@74 362 m_frameNo = 0;
Chris@74 363 m_lastFrameIn1 = 0;
Chris@74 364 m_lastFrameIn2 = 0;
cannam@6 365
cannam@6 366 createMatchers();
cannam@6 367 m_begin = true;
cannam@6 368 m_locked = false;
cannam@0 369 }
cannam@0 370
cannam@0 371 MatchVampPlugin::OutputList
cannam@0 372 MatchVampPlugin::getOutputDescriptors() const
cannam@0 373 {
cannam@0 374 OutputList list;
cannam@0 375
Chris@52 376 float outRate = 1.0f / m_stepTime;
cannam@0 377
cannam@0 378 OutputDescriptor desc;
cannam@0 379 desc.identifier = "path";
cannam@0 380 desc.name = "Path";
cannam@0 381 desc.description = "Alignment path";
cannam@0 382 desc.unit = "";
cannam@0 383 desc.hasFixedBinCount = true;
cannam@0 384 desc.binCount = 1;
cannam@0 385 desc.hasKnownExtents = false;
cannam@0 386 desc.isQuantized = true;
cannam@0 387 desc.quantizeStep = 1;
cannam@0 388 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 389 desc.sampleRate = outRate;
Chris@16 390 m_pathOutNo = list.size();
cannam@0 391 list.push_back(desc);
cannam@0 392
cannam@0 393 desc.identifier = "a_b";
cannam@0 394 desc.name = "A-B Timeline";
cannam@0 395 desc.description = "Timing in performance B corresponding to moments in performance A";
cannam@0 396 desc.unit = "sec";
cannam@0 397 desc.hasFixedBinCount = true;
cannam@0 398 desc.binCount = 1;
cannam@0 399 desc.hasKnownExtents = false;
cannam@0 400 desc.isQuantized = false;
cannam@0 401 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 402 desc.sampleRate = outRate;
Chris@16 403 m_abOutNo = list.size();
cannam@0 404 list.push_back(desc);
cannam@0 405
cannam@0 406 desc.identifier = "b_a";
cannam@0 407 desc.name = "B-A Timeline";
cannam@0 408 desc.description = "Timing in performance A corresponding to moments in performance B";
cannam@0 409 desc.unit = "sec";
cannam@0 410 desc.hasFixedBinCount = true;
cannam@0 411 desc.binCount = 1;
cannam@0 412 desc.hasKnownExtents = false;
cannam@0 413 desc.isQuantized = false;
cannam@0 414 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 415 desc.sampleRate = outRate;
Chris@16 416 m_baOutNo = list.size();
cannam@0 417 list.push_back(desc);
cannam@0 418
cannam@0 419 desc.identifier = "a_b_divergence";
cannam@0 420 desc.name = "A-B Divergence";
cannam@0 421 desc.description = "Difference between timings in performances A and B";
cannam@0 422 desc.unit = "sec";
cannam@0 423 desc.hasFixedBinCount = true;
cannam@0 424 desc.binCount = 1;
cannam@0 425 desc.hasKnownExtents = false;
cannam@0 426 desc.isQuantized = false;
cannam@0 427 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 428 desc.sampleRate = outRate;
Chris@16 429 m_abDivOutNo = list.size();
cannam@0 430 list.push_back(desc);
cannam@0 431
cannam@0 432 desc.identifier = "a_b_temporatio";
cannam@0 433 desc.name = "A-B Tempo Ratio";
cannam@0 434 desc.description = "Ratio of tempi between performances A and B";
cannam@0 435 desc.unit = "";
cannam@0 436 desc.hasFixedBinCount = true;
cannam@0 437 desc.binCount = 1;
cannam@0 438 desc.hasKnownExtents = false;
cannam@0 439 desc.isQuantized = false;
cannam@0 440 desc.sampleType = OutputDescriptor::VariableSampleRate;
cannam@0 441 desc.sampleRate = outRate;
Chris@16 442 m_abRatioOutNo = list.size();
cannam@0 443 list.push_back(desc);
cannam@0 444
Chris@38 445 int featureSize = FeatureExtractor(m_feParams).getFeatureSize();
Chris@38 446
Chris@15 447 desc.identifier = "a_features";
Chris@15 448 desc.name = "A Features";
Chris@15 449 desc.description = "Spectral features extracted from performance A";
Chris@15 450 desc.unit = "";
Chris@15 451 desc.hasFixedBinCount = true;
Chris@38 452 desc.binCount = featureSize;
Chris@15 453 desc.hasKnownExtents = false;
Chris@15 454 desc.isQuantized = false;
Chris@16 455 desc.sampleType = OutputDescriptor::FixedSampleRate;
Chris@15 456 desc.sampleRate = outRate;
Chris@16 457 m_aFeaturesOutNo = list.size();
Chris@16 458 list.push_back(desc);
Chris@16 459
Chris@16 460 desc.identifier = "b_features";
Chris@16 461 desc.name = "B Features";
Chris@16 462 desc.description = "Spectral features extracted from performance B";
Chris@16 463 desc.unit = "";
Chris@16 464 desc.hasFixedBinCount = true;
Chris@38 465 desc.binCount = featureSize;
Chris@16 466 desc.hasKnownExtents = false;
Chris@16 467 desc.isQuantized = false;
Chris@16 468 desc.sampleType = OutputDescriptor::FixedSampleRate;
Chris@16 469 desc.sampleRate = outRate;
Chris@16 470 m_bFeaturesOutNo = list.size();
Chris@15 471 list.push_back(desc);
Chris@15 472
cannam@0 473 return list;
cannam@0 474 }
cannam@0 475
Chris@74 476 bool
Chris@74 477 MatchVampPlugin::aboveThreshold(const float *frame)
Chris@74 478 {
Chris@74 479 float threshold = 1e-5f;
Chris@74 480 float rms = 0.f;
Chris@74 481 for (int i = 0; i < m_blockSize/2 + 2; ++i) {
Chris@74 482 rms += frame[i] * frame[i];
Chris@74 483 }
Chris@74 484 rms = sqrtf(rms / (m_blockSize/2 + 2));
Chris@74 485 return (rms > threshold);
Chris@74 486 }
Chris@74 487
cannam@0 488 MatchVampPlugin::FeatureSet
cannam@0 489 MatchVampPlugin::process(const float *const *inputBuffers,
cannam@0 490 Vamp::RealTime timestamp)
cannam@0 491 {
cannam@0 492 if (m_begin) {
cannam@0 493 if (!m_locked && m_serialise) {
cannam@0 494 m_locked = true;
cannam@0 495 #ifdef _WIN32
cannam@0 496 WaitForSingleObject(m_serialisingMutex, INFINITE);
cannam@0 497 #else
cannam@0 498 pthread_mutex_lock(&m_serialisingMutex);
cannam@0 499 #endif
cannam@0 500 }
Chris@10 501 m_startTime = timestamp;
cannam@0 502 m_begin = false;
cannam@0 503 }
cannam@0 504
cannam@0 505 // std::cerr << timestamp.toString();
cannam@0 506
Chris@74 507 if (aboveThreshold(inputBuffers[0])) m_lastFrameIn1 = m_frameNo;
Chris@74 508 if (aboveThreshold(inputBuffers[1])) m_lastFrameIn2 = m_frameNo;
Chris@74 509
Chris@74 510 vector<double> f1 = m_fe1->process(inputBuffers[0]);
Chris@74 511 vector<double> f2 = m_fe2->process(inputBuffers[1]);
Chris@74 512
Chris@74 513 m_feeder->feed(f1, f2);
Chris@16 514
Chris@16 515 FeatureSet returnFeatures;
Chris@16 516
Chris@16 517 Feature f;
Chris@16 518 f.hasTimestamp = false;
Chris@16 519
Chris@74 520 f.values.clear();
Chris@74 521 for (int j = 0; j < (int)f1.size(); ++j) {
Chris@74 522 f.values.push_back(float(f1[j]));
Chris@16 523 }
Chris@74 524 returnFeatures[m_aFeaturesOutNo].push_back(f);
Chris@16 525
Chris@74 526 f.values.clear();
Chris@74 527 for (int j = 0; j < (int)f2.size(); ++j) {
Chris@74 528 f.values.push_back(float(f2[j]));
Chris@16 529 }
Chris@74 530 returnFeatures[m_bFeaturesOutNo].push_back(f);
cannam@0 531
cannam@0 532 // std::cerr << ".";
cannam@0 533 // std::cerr << std::endl;
cannam@0 534
Chris@74 535 ++m_frameNo;
Chris@74 536
Chris@16 537 return returnFeatures;
cannam@0 538 }
cannam@0 539
cannam@0 540 MatchVampPlugin::FeatureSet
cannam@0 541 MatchVampPlugin::getRemainingFeatures()
cannam@0 542 {
Chris@74 543 m_feeder->finish();
Chris@74 544
Chris@63 545 FeatureSet returnFeatures;
Chris@63 546
Chris@74 547 Finder *finder = m_feeder->getFinder();
cannam@0 548 std::vector<int> pathx;
cannam@0 549 std::vector<int> pathy;
Chris@32 550 int len = finder->retrievePath(m_smooth, pathx, pathy);
cannam@0 551
cannam@0 552 int prevx = 0;
cannam@0 553 int prevy = 0;
cannam@0 554
Chris@30 555 for (int i = 0; i < len; ++i) {
cannam@0 556
cannam@0 557 int x = pathx[i];
cannam@0 558 int y = pathy[i];
cannam@0 559
cannam@0 560 Vamp::RealTime xt = Vamp::RealTime::frame2RealTime
Chris@15 561 (x * m_stepSize, lrintf(m_inputSampleRate));
cannam@0 562 Vamp::RealTime yt = Vamp::RealTime::frame2RealTime
Chris@15 563 (y * m_stepSize, lrintf(m_inputSampleRate));
cannam@0 564
cannam@0 565 Feature feature;
cannam@0 566 feature.hasTimestamp = true;
Chris@10 567 feature.timestamp = m_startTime + xt;
cannam@0 568 feature.values.clear();
Chris@52 569 feature.values.push_back(float(yt.sec + double(yt.nsec)/1.0e9));
Chris@16 570 returnFeatures[m_pathOutNo].push_back(feature);
cannam@0 571
cannam@0 572 if (x != prevx) {
cannam@0 573
cannam@0 574 feature.hasTimestamp = true;
Chris@10 575 feature.timestamp = m_startTime + xt;
cannam@0 576 feature.values.clear();
Chris@52 577 feature.values.push_back(float(yt.sec + yt.msec()/1000.0));
Chris@16 578 returnFeatures[m_abOutNo].push_back(feature);
cannam@0 579
cannam@0 580 Vamp::RealTime diff = yt - xt;
cannam@0 581 feature.values.clear();
Chris@52 582 feature.values.push_back(float(diff.sec + diff.msec()/1000.0));
Chris@16 583 returnFeatures[m_abDivOutNo].push_back(feature);
cannam@0 584
cannam@0 585 if (i > 0) {
cannam@0 586 int lookback = 100; //!!! arbitrary
cannam@0 587 if (lookback > i) lookback = i;
cannam@0 588 int xdiff = x - pathx[i-lookback];
cannam@0 589 int ydiff = y - pathy[i-lookback];
cannam@0 590 if (xdiff != 0 && ydiff != 0) {
cannam@0 591 float ratio = float(ydiff)/float(xdiff);
cannam@0 592 if (ratio < 8 && ratio > (1.0/8)) { //!!! just for now, since we aren't dealing properly with silence yet
cannam@0 593 feature.values.clear();
cannam@0 594 feature.values.push_back(ratio);
Chris@16 595 returnFeatures[m_abRatioOutNo].push_back(feature);
cannam@0 596 }
cannam@0 597 }
cannam@0 598 }
cannam@0 599 }
cannam@0 600
cannam@0 601 if (y != prevy) {
cannam@0 602 feature.hasTimestamp = true;
Chris@10 603 feature.timestamp = m_startTime + yt;
cannam@0 604 feature.values.clear();
Chris@52 605 feature.values.push_back(float(xt.sec + xt.msec()/1000.0));
Chris@16 606 returnFeatures[m_baOutNo].push_back(feature);
cannam@0 607 }
cannam@0 608
cannam@0 609 prevx = x;
cannam@0 610 prevy = y;
cannam@0 611 }
cannam@0 612
Chris@74 613 delete m_feeder;
Chris@74 614 delete m_pm1;
Chris@74 615 delete m_pm2;
Chris@74 616 m_feeder = 0;
Chris@74 617 m_pm1 = 0;
Chris@74 618 m_pm2 = 0;
cannam@0 619
cannam@0 620 if (m_locked) {
cannam@0 621 #ifdef _WIN32
cannam@0 622 ReleaseMutex(m_serialisingMutex);
cannam@0 623 #else
cannam@0 624 pthread_mutex_unlock(&m_serialisingMutex);
cannam@0 625 #endif
cannam@0 626 m_locked = false;
cannam@0 627 }
cannam@0 628
cannam@0 629 return returnFeatures;
cannam@0 630
cannam@0 631
cannam@0 632 /*
Chris@30 633 for (int i = 0; i < len; ++i) {
cannam@0 634 std::cerr << i << ": [" << pathx[i] << "," << pathy[i] << "]" << std::endl;
cannam@0 635 }
cannam@0 636
cannam@0 637 std::cerr << std::endl;
cannam@0 638 std::cerr << "File: A" << std::endl;
cannam@0 639 std::cerr << "Marks: -1" << std::endl;
cannam@0 640 std::cerr << "FixedPoints: true 0" << std::endl;
cannam@0 641 std::cerr << "0" << std::endl;
cannam@0 642 std::cerr << "0" << std::endl;
cannam@0 643 std::cerr << "0" << std::endl;
cannam@0 644 std::cerr << "0" << std::endl;
cannam@0 645 std::cerr << "File: B" << std::endl;
cannam@0 646 std::cerr << "Marks: 0" << std::endl;
cannam@0 647 std::cerr << "FixedPoints: true 0" << std::endl;
cannam@0 648 std::cerr << "0.02" << std::endl;
cannam@0 649 std::cerr << "0.02" << std::endl;
cannam@0 650
Chris@30 651 std::cerr << len << std::endl;
Chris@30 652 for (int i = 0; i < len; ++i) {
cannam@0 653 std::cerr << pathx[i] << std::endl;
cannam@0 654 }
cannam@0 655
Chris@30 656 std::cerr << len << std::endl;
Chris@30 657 for (int i = 0; i < len; ++i) {
cannam@0 658 std::cerr << pathy[i] << std::endl;
cannam@0 659 }
cannam@0 660 */
cannam@0 661 }
cannam@0 662
cannam@0 663 static Vamp::PluginAdapter<MatchVampPlugin> mvpAdapter;
cannam@0 664
cannam@0 665 const VampPluginDescriptor *vampGetPluginDescriptor(unsigned int version,
cannam@0 666 unsigned int index)
cannam@0 667 {
cannam@0 668 if (version < 1) return 0;
cannam@0 669
cannam@0 670 switch (index) {
cannam@0 671 case 0: return mvpAdapter.getDescriptor();
cannam@0 672 default: return 0;
cannam@0 673 }
cannam@0 674 }