annotate src/MatchVampPlugin.cpp @ 103:593054bf6476 feature_conditioner

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