annotate plugins/BeatTrack.cpp @ 135:dcf5800f0f00

* Add GPL
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 13 Dec 2010 14:03:46 +0000
parents 0258a32639e6
children 67b6ea887d5d
rev   line source
c@27 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@27 2
c@27 3 /*
c@27 4 QM Vamp Plugin Set
c@27 5
c@27 6 Centre for Digital Music, Queen Mary, University of London.
c@135 7
c@135 8 This program is free software; you can redistribute it and/or
c@135 9 modify it under the terms of the GNU General Public License as
c@135 10 published by the Free Software Foundation; either version 2 of the
c@135 11 License, or (at your option) any later version. See the file
c@135 12 COPYING included with this distribution for more information.
c@27 13 */
c@27 14
c@27 15 #include "BeatTrack.h"
c@27 16
c@27 17 #include <dsp/onsets/DetectionFunction.h>
c@27 18 #include <dsp/onsets/PeakPicking.h>
c@27 19 #include <dsp/tempotracking/TempoTrack.h>
c@86 20 #include <dsp/tempotracking/TempoTrackV2.h>
c@27 21
c@27 22 using std::string;
c@27 23 using std::vector;
c@27 24 using std::cerr;
c@27 25 using std::endl;
c@27 26
c@86 27 float BeatTracker::m_stepSecs = 0.01161; // 512 samples at 44100
c@86 28
c@86 29 #define METHOD_OLD 0
c@86 30 #define METHOD_NEW 1
c@27 31
c@27 32 class BeatTrackerData
c@27 33 {
c@27 34 public:
c@27 35 BeatTrackerData(const DFConfig &config) : dfConfig(config) {
c@27 36 df = new DetectionFunction(config);
c@27 37 }
c@27 38 ~BeatTrackerData() {
c@27 39 delete df;
c@27 40 }
c@27 41 void reset() {
c@27 42 delete df;
c@27 43 df = new DetectionFunction(dfConfig);
c@27 44 dfOutput.clear();
c@85 45 origin = Vamp::RealTime::zeroTime;
c@27 46 }
c@27 47
c@27 48 DFConfig dfConfig;
c@27 49 DetectionFunction *df;
c@27 50 vector<double> dfOutput;
c@85 51 Vamp::RealTime origin;
c@27 52 };
c@27 53
c@27 54
c@27 55 BeatTracker::BeatTracker(float inputSampleRate) :
c@27 56 Vamp::Plugin(inputSampleRate),
c@27 57 m_d(0),
c@86 58 m_method(METHOD_NEW),
c@30 59 m_dfType(DF_COMPLEXSD),
c@30 60 m_whiten(false)
c@27 61 {
c@27 62 }
c@27 63
c@27 64 BeatTracker::~BeatTracker()
c@27 65 {
c@27 66 delete m_d;
c@27 67 }
c@27 68
c@27 69 string
c@27 70 BeatTracker::getIdentifier() const
c@27 71 {
c@27 72 return "qm-tempotracker";
c@27 73 }
c@27 74
c@27 75 string
c@27 76 BeatTracker::getName() const
c@27 77 {
c@27 78 return "Tempo and Beat Tracker";
c@27 79 }
c@27 80
c@27 81 string
c@27 82 BeatTracker::getDescription() const
c@27 83 {
c@27 84 return "Estimate beat locations and tempo";
c@27 85 }
c@27 86
c@27 87 string
c@27 88 BeatTracker::getMaker() const
c@27 89 {
c@50 90 return "Queen Mary, University of London";
c@27 91 }
c@27 92
c@27 93 int
c@27 94 BeatTracker::getPluginVersion() const
c@27 95 {
c@131 96 return 5;
c@27 97 }
c@27 98
c@27 99 string
c@27 100 BeatTracker::getCopyright() const
c@27 101 {
c@89 102 return "Plugin by Christian Landone and Matthew Davies. Copyright (c) 2006-2009 QMUL - All Rights Reserved";
c@27 103 }
c@27 104
c@27 105 BeatTracker::ParameterList
c@27 106 BeatTracker::getParameterDescriptors() const
c@27 107 {
c@27 108 ParameterList list;
c@27 109
c@27 110 ParameterDescriptor desc;
c@86 111
c@86 112 desc.identifier = "method";
c@86 113 desc.name = "Beat Tracking Method";
c@119 114 desc.description = "Basic method to use ";
c@86 115 desc.minValue = 0;
c@86 116 desc.maxValue = 1;
c@86 117 desc.defaultValue = METHOD_NEW;
c@86 118 desc.isQuantized = true;
c@86 119 desc.quantizeStep = 1;
c@86 120 desc.valueNames.push_back("Old");
c@86 121 desc.valueNames.push_back("New");
c@86 122 list.push_back(desc);
c@86 123
c@27 124 desc.identifier = "dftype";
c@27 125 desc.name = "Onset Detection Function Type";
c@27 126 desc.description = "Method used to calculate the onset detection function";
c@27 127 desc.minValue = 0;
c@31 128 desc.maxValue = 4;
c@27 129 desc.defaultValue = 3;
c@86 130 desc.valueNames.clear();
c@27 131 desc.valueNames.push_back("High-Frequency Content");
c@27 132 desc.valueNames.push_back("Spectral Difference");
c@27 133 desc.valueNames.push_back("Phase Deviation");
c@27 134 desc.valueNames.push_back("Complex Domain");
c@27 135 desc.valueNames.push_back("Broadband Energy Rise");
c@27 136 list.push_back(desc);
c@27 137
c@30 138 desc.identifier = "whiten";
c@30 139 desc.name = "Adaptive Whitening";
c@30 140 desc.description = "Normalize frequency bin magnitudes relative to recent peak levels";
c@30 141 desc.minValue = 0;
c@30 142 desc.maxValue = 1;
c@30 143 desc.defaultValue = 0;
c@30 144 desc.isQuantized = true;
c@30 145 desc.quantizeStep = 1;
c@30 146 desc.unit = "";
c@30 147 desc.valueNames.clear();
c@30 148 list.push_back(desc);
c@30 149
c@27 150 return list;
c@27 151 }
c@27 152
c@27 153 float
c@27 154 BeatTracker::getParameter(std::string name) const
c@27 155 {
c@27 156 if (name == "dftype") {
c@27 157 switch (m_dfType) {
c@27 158 case DF_HFC: return 0;
c@27 159 case DF_SPECDIFF: return 1;
c@27 160 case DF_PHASEDEV: return 2;
c@27 161 default: case DF_COMPLEXSD: return 3;
c@27 162 case DF_BROADBAND: return 4;
c@27 163 }
c@86 164 } else if (name == "method") {
c@86 165 return m_method;
c@30 166 } else if (name == "whiten") {
c@30 167 return m_whiten ? 1.0 : 0.0;
c@27 168 }
c@27 169 return 0.0;
c@27 170 }
c@27 171
c@27 172 void
c@27 173 BeatTracker::setParameter(std::string name, float value)
c@27 174 {
c@27 175 if (name == "dftype") {
c@27 176 switch (lrintf(value)) {
c@27 177 case 0: m_dfType = DF_HFC; break;
c@27 178 case 1: m_dfType = DF_SPECDIFF; break;
c@27 179 case 2: m_dfType = DF_PHASEDEV; break;
c@27 180 default: case 3: m_dfType = DF_COMPLEXSD; break;
c@27 181 case 4: m_dfType = DF_BROADBAND; break;
c@27 182 }
c@86 183 } else if (name == "method") {
c@86 184 m_method = lrintf(value);
c@30 185 } else if (name == "whiten") {
c@30 186 m_whiten = (value > 0.5);
c@27 187 }
c@27 188 }
c@27 189
c@27 190 bool
c@27 191 BeatTracker::initialise(size_t channels, size_t stepSize, size_t blockSize)
c@27 192 {
c@27 193 if (m_d) {
c@27 194 delete m_d;
c@27 195 m_d = 0;
c@27 196 }
c@27 197
c@27 198 if (channels < getMinChannelCount() ||
c@27 199 channels > getMaxChannelCount()) {
c@27 200 std::cerr << "BeatTracker::initialise: Unsupported channel count: "
c@27 201 << channels << std::endl;
c@27 202 return false;
c@27 203 }
c@27 204
c@28 205 if (stepSize != getPreferredStepSize()) {
c@28 206 std::cerr << "ERROR: BeatTracker::initialise: Unsupported step size for this sample rate: "
c@28 207 << stepSize << " (wanted " << (getPreferredStepSize()) << ")" << std::endl;
c@27 208 return false;
c@27 209 }
c@27 210
c@28 211 if (blockSize != getPreferredBlockSize()) {
c@29 212 std::cerr << "WARNING: BeatTracker::initialise: Sub-optimal block size for this sample rate: "
c@28 213 << blockSize << " (wanted " << getPreferredBlockSize() << ")" << std::endl;
c@28 214 // return false;
c@27 215 }
c@27 216
c@27 217 DFConfig dfConfig;
c@27 218 dfConfig.DFType = m_dfType;
c@27 219 dfConfig.stepSize = stepSize;
c@27 220 dfConfig.frameLength = blockSize;
c@27 221 dfConfig.dbRise = 3;
c@30 222 dfConfig.adaptiveWhitening = m_whiten;
c@30 223 dfConfig.whiteningRelaxCoeff = -1;
c@30 224 dfConfig.whiteningFloor = -1;
c@27 225
c@27 226 m_d = new BeatTrackerData(dfConfig);
c@27 227 return true;
c@27 228 }
c@27 229
c@27 230 void
c@27 231 BeatTracker::reset()
c@27 232 {
c@27 233 if (m_d) m_d->reset();
c@27 234 }
c@27 235
c@27 236 size_t
c@27 237 BeatTracker::getPreferredStepSize() const
c@27 238 {
c@27 239 size_t step = size_t(m_inputSampleRate * m_stepSecs + 0.0001);
c@27 240 // std::cerr << "BeatTracker::getPreferredStepSize: input sample rate is " << m_inputSampleRate << ", step size is " << step << std::endl;
c@27 241 return step;
c@27 242 }
c@27 243
c@27 244 size_t
c@27 245 BeatTracker::getPreferredBlockSize() const
c@27 246 {
c@28 247 size_t theoretical = getPreferredStepSize() * 2;
c@28 248
c@52 249 // I think this is not necessarily going to be a power of two, and
c@52 250 // the host might have a problem with that, but I'm not sure we
c@52 251 // can do much about it here
c@28 252 return theoretical;
c@27 253 }
c@27 254
c@27 255 BeatTracker::OutputList
c@27 256 BeatTracker::getOutputDescriptors() const
c@27 257 {
c@27 258 OutputList list;
c@27 259
c@27 260 OutputDescriptor beat;
c@27 261 beat.identifier = "beats";
c@27 262 beat.name = "Beats";
c@27 263 beat.description = "Estimated metrical beat locations";
c@27 264 beat.unit = "";
c@27 265 beat.hasFixedBinCount = true;
c@27 266 beat.binCount = 0;
c@27 267 beat.sampleType = OutputDescriptor::VariableSampleRate;
c@27 268 beat.sampleRate = 1.0 / m_stepSecs;
c@27 269
c@27 270 OutputDescriptor df;
c@27 271 df.identifier = "detection_fn";
c@27 272 df.name = "Onset Detection Function";
c@27 273 df.description = "Probability function of note onset likelihood";
c@27 274 df.unit = "";
c@27 275 df.hasFixedBinCount = true;
c@27 276 df.binCount = 1;
c@27 277 df.hasKnownExtents = false;
c@27 278 df.isQuantized = false;
c@27 279 df.sampleType = OutputDescriptor::OneSamplePerStep;
c@27 280
c@27 281 OutputDescriptor tempo;
c@27 282 tempo.identifier = "tempo";
c@27 283 tempo.name = "Tempo";
c@27 284 tempo.description = "Locked tempo estimates";
c@27 285 tempo.unit = "bpm";
c@27 286 tempo.hasFixedBinCount = true;
c@27 287 tempo.binCount = 1;
c@31 288 tempo.hasKnownExtents = false;
c@31 289 tempo.isQuantized = false;
c@27 290 tempo.sampleType = OutputDescriptor::VariableSampleRate;
c@27 291 tempo.sampleRate = 1.0 / m_stepSecs;
c@27 292
c@27 293 list.push_back(beat);
c@27 294 list.push_back(df);
c@27 295 list.push_back(tempo);
c@27 296
c@27 297 return list;
c@27 298 }
c@27 299
c@27 300 BeatTracker::FeatureSet
c@27 301 BeatTracker::process(const float *const *inputBuffers,
c@85 302 Vamp::RealTime timestamp)
c@27 303 {
c@27 304 if (!m_d) {
c@27 305 cerr << "ERROR: BeatTracker::process: "
c@27 306 << "BeatTracker has not been initialised"
c@27 307 << endl;
c@27 308 return FeatureSet();
c@27 309 }
c@27 310
c@27 311 size_t len = m_d->dfConfig.frameLength / 2;
c@27 312
c@27 313 double *magnitudes = new double[len];
c@27 314 double *phases = new double[len];
c@27 315
c@27 316 // We only support a single input channel
c@27 317
c@27 318 for (size_t i = 0; i < len; ++i) {
c@27 319
c@27 320 magnitudes[i] = sqrt(inputBuffers[0][i*2 ] * inputBuffers[0][i*2 ] +
c@27 321 inputBuffers[0][i*2+1] * inputBuffers[0][i*2+1]);
c@27 322
c@27 323 phases[i] = atan2(-inputBuffers[0][i*2+1], inputBuffers[0][i*2]);
c@27 324 }
c@27 325
c@27 326 double output = m_d->df->process(magnitudes, phases);
c@27 327
c@27 328 delete[] magnitudes;
c@27 329 delete[] phases;
c@27 330
c@85 331 if (m_d->dfOutput.empty()) m_d->origin = timestamp;
c@85 332
c@27 333 m_d->dfOutput.push_back(output);
c@27 334
c@27 335 FeatureSet returnFeatures;
c@27 336
c@27 337 Feature feature;
c@27 338 feature.hasTimestamp = false;
c@27 339 feature.values.push_back(output);
c@27 340
c@27 341 returnFeatures[1].push_back(feature); // detection function is output 1
c@27 342 return returnFeatures;
c@27 343 }
c@27 344
c@27 345 BeatTracker::FeatureSet
c@27 346 BeatTracker::getRemainingFeatures()
c@27 347 {
c@27 348 if (!m_d) {
c@27 349 cerr << "ERROR: BeatTracker::getRemainingFeatures: "
c@27 350 << "BeatTracker has not been initialised"
c@27 351 << endl;
c@27 352 return FeatureSet();
c@27 353 }
c@27 354
c@86 355 if (m_method == METHOD_OLD) return beatTrackOld();
c@86 356 else return beatTrackNew();
c@86 357 }
c@86 358
c@86 359 BeatTracker::FeatureSet
c@86 360 BeatTracker::beatTrackOld()
c@86 361 {
c@27 362 double aCoeffs[] = { 1.0000, -0.5949, 0.2348 };
c@27 363 double bCoeffs[] = { 0.1600, 0.3200, 0.1600 };
c@27 364
c@27 365 TTParams ttParams;
c@27 366 ttParams.winLength = 512;
c@27 367 ttParams.lagLength = 128;
c@27 368 ttParams.LPOrd = 2;
c@27 369 ttParams.LPACoeffs = aCoeffs;
c@27 370 ttParams.LPBCoeffs = bCoeffs;
c@27 371 ttParams.alpha = 9;
c@27 372 ttParams.WinT.post = 8;
c@27 373 ttParams.WinT.pre = 7;
c@27 374
c@27 375 TempoTrack tempoTracker(ttParams);
c@27 376
c@87 377 vector<double> tempi;
c@87 378 vector<int> beats = tempoTracker.process(m_d->dfOutput, &tempi);
c@27 379
c@27 380 FeatureSet returnFeatures;
c@27 381
c@27 382 char label[100];
c@27 383
c@27 384 for (size_t i = 0; i < beats.size(); ++i) {
c@27 385
c@27 386 size_t frame = beats[i] * m_d->dfConfig.stepSize;
c@27 387
c@27 388 Feature feature;
c@27 389 feature.hasTimestamp = true;
c@85 390 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime
c@27 391 (frame, lrintf(m_inputSampleRate));
c@27 392
c@27 393 float bpm = 0.0;
c@27 394 int frameIncrement = 0;
c@27 395
c@27 396 if (i < beats.size() - 1) {
c@27 397
c@27 398 frameIncrement = (beats[i+1] - beats[i]) * m_d->dfConfig.stepSize;
c@27 399
c@27 400 // one beat is frameIncrement frames, so there are
c@27 401 // samplerate/frameIncrement bps, so
c@27 402 // 60*samplerate/frameIncrement bpm
c@27 403
c@27 404 if (frameIncrement > 0) {
c@27 405 bpm = (60.0 * m_inputSampleRate) / frameIncrement;
c@27 406 bpm = int(bpm * 100.0 + 0.5) / 100.0;
c@27 407 sprintf(label, "%.2f bpm", bpm);
c@27 408 feature.label = label;
c@27 409 }
c@27 410 }
c@27 411
c@27 412 returnFeatures[0].push_back(feature); // beats are output 0
c@27 413 }
c@27 414
c@27 415 double prevTempo = 0.0;
c@27 416
c@87 417 for (size_t i = 0; i < tempi.size(); ++i) {
c@27 418
c@27 419 size_t frame = i * m_d->dfConfig.stepSize * ttParams.lagLength;
c@27 420
c@27 421 // std::cerr << "unit " << i << ", step size " << m_d->dfConfig.stepSize << ", hop " << ttParams.lagLength << ", frame = " << frame << std::endl;
c@27 422
c@87 423 if (tempi[i] > 1 && int(tempi[i] * 100) != int(prevTempo * 100)) {
c@27 424 Feature feature;
c@27 425 feature.hasTimestamp = true;
c@85 426 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime
c@27 427 (frame, lrintf(m_inputSampleRate));
c@87 428 feature.values.push_back(tempi[i]);
c@87 429 sprintf(label, "%.2f bpm", tempi[i]);
c@27 430 feature.label = label;
c@27 431 returnFeatures[2].push_back(feature); // tempo is output 2
c@87 432 prevTempo = tempi[i];
c@27 433 }
c@27 434 }
c@27 435
c@27 436 return returnFeatures;
c@27 437 }
c@27 438
c@86 439 BeatTracker::FeatureSet
c@86 440 BeatTracker::beatTrackNew()
c@86 441 {
c@86 442 vector<double> df;
c@86 443 vector<double> beatPeriod;
c@87 444 vector<double> tempi;
c@86 445
c@120 446 size_t nonZeroCount = m_d->dfOutput.size();
c@120 447 while (nonZeroCount > 0) {
c@120 448 if (m_d->dfOutput[nonZeroCount-1] > 0.0) {
c@120 449 break;
c@120 450 }
c@120 451 --nonZeroCount;
c@120 452 }
c@120 453
c@120 454 std::cerr << "Note: nonZeroCount was " << m_d->dfOutput.size() << ", is now " << nonZeroCount << std::endl;
c@120 455
c@120 456 for (size_t i = 2; i < nonZeroCount; ++i) { // discard first two elts
c@86 457 df.push_back(m_d->dfOutput[i]);
c@86 458 beatPeriod.push_back(0.0);
c@86 459 }
c@86 460 if (df.empty()) return FeatureSet();
c@86 461
c@88 462 TempoTrackV2 tt(m_inputSampleRate, m_d->dfConfig.stepSize);
c@86 463
c@87 464 tt.calculateBeatPeriod(df, beatPeriod, tempi);
c@86 465
c@86 466 vector<double> beats;
c@86 467 tt.calculateBeats(df, beatPeriod, beats);
c@86 468
c@86 469 FeatureSet returnFeatures;
c@86 470
c@86 471 char label[100];
c@86 472
c@86 473 for (size_t i = 0; i < beats.size(); ++i) {
c@86 474
c@87 475 size_t frame = beats[i] * m_d->dfConfig.stepSize;
c@86 476
c@86 477 Feature feature;
c@86 478 feature.hasTimestamp = true;
c@86 479 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime
c@86 480 (frame, lrintf(m_inputSampleRate));
c@86 481
c@86 482 float bpm = 0.0;
c@86 483 int frameIncrement = 0;
c@86 484
c@87 485 if (i+1 < beats.size()) {
c@86 486
c@87 487 frameIncrement = (beats[i+1] - beats[i]) * m_d->dfConfig.stepSize;
c@86 488
c@86 489 // one beat is frameIncrement frames, so there are
c@86 490 // samplerate/frameIncrement bps, so
c@86 491 // 60*samplerate/frameIncrement bpm
c@86 492
c@86 493 if (frameIncrement > 0) {
c@86 494 bpm = (60.0 * m_inputSampleRate) / frameIncrement;
c@86 495 bpm = int(bpm * 100.0 + 0.5) / 100.0;
c@86 496 sprintf(label, "%.2f bpm", bpm);
c@86 497 feature.label = label;
c@86 498 }
c@86 499 }
c@86 500
c@86 501 returnFeatures[0].push_back(feature); // beats are output 0
c@86 502 }
c@86 503
c@87 504 double prevTempo = 0.0;
c@87 505
c@87 506 for (size_t i = 0; i < tempi.size(); ++i) {
c@87 507
c@87 508 size_t frame = i * m_d->dfConfig.stepSize;
c@87 509
c@87 510 if (tempi[i] > 1 && int(tempi[i] * 100) != int(prevTempo * 100)) {
c@87 511 Feature feature;
c@87 512 feature.hasTimestamp = true;
c@87 513 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime
c@87 514 (frame, lrintf(m_inputSampleRate));
c@87 515 feature.values.push_back(tempi[i]);
c@87 516 sprintf(label, "%.2f bpm", tempi[i]);
c@87 517 feature.label = label;
c@87 518 returnFeatures[2].push_back(feature); // tempo is output 2
c@87 519 prevTempo = tempi[i];
c@87 520 }
c@87 521 }
c@87 522
c@86 523 return returnFeatures;
c@86 524 }
c@86 525