annotate plugins/BeatTrack.cpp @ 120:52d84f7f6ad3

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