annotate plugins/OnsetDetect.cpp @ 266:d04675d44928 tip master

Refer to SDK from Github
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 02 Jun 2021 14:41:26 +0100
parents 855c91bfc912
children
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 "OnsetDetect.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@27 20
c@27 21 using std::string;
c@27 22 using std::vector;
c@27 23 using std::cerr;
c@27 24 using std::endl;
c@27 25
c@32 26 float OnsetDetector::m_preferredStepSecs = 0.01161;
c@27 27
c@27 28 class OnsetDetectorData
c@27 29 {
c@27 30 public:
c@27 31 OnsetDetectorData(const DFConfig &config) : dfConfig(config) {
c@27 32 df = new DetectionFunction(config);
c@27 33 }
c@27 34 ~OnsetDetectorData() {
c@27 35 delete df;
c@27 36 }
c@27 37 void reset() {
c@27 38 delete df;
c@27 39 df = new DetectionFunction(dfConfig);
c@27 40 dfOutput.clear();
c@85 41 origin = Vamp::RealTime::zeroTime;
c@27 42 }
c@27 43
c@27 44 DFConfig dfConfig;
c@27 45 DetectionFunction *df;
c@27 46 vector<double> dfOutput;
c@85 47 Vamp::RealTime origin;
c@27 48 };
c@27 49
c@27 50
c@27 51 OnsetDetector::OnsetDetector(float inputSampleRate) :
c@27 52 Vamp::Plugin(inputSampleRate),
c@27 53 m_d(0),
c@27 54 m_dfType(DF_COMPLEXSD),
c@30 55 m_sensitivity(50),
c@30 56 m_whiten(false)
c@27 57 {
c@27 58 }
c@27 59
c@27 60 OnsetDetector::~OnsetDetector()
c@27 61 {
c@27 62 delete m_d;
c@27 63 }
c@27 64
c@27 65 string
c@27 66 OnsetDetector::getIdentifier() const
c@27 67 {
c@27 68 return "qm-onsetdetector";
c@27 69 }
c@27 70
c@27 71 string
c@27 72 OnsetDetector::getName() const
c@27 73 {
c@27 74 return "Note Onset Detector";
c@27 75 }
c@27 76
c@27 77 string
c@27 78 OnsetDetector::getDescription() const
c@27 79 {
c@27 80 return "Estimate individual note onset positions";
c@27 81 }
c@27 82
c@27 83 string
c@27 84 OnsetDetector::getMaker() const
c@27 85 {
c@50 86 return "Queen Mary, University of London";
c@27 87 }
c@27 88
c@27 89 int
c@27 90 OnsetDetector::getPluginVersion() const
c@27 91 {
c@131 92 return 3;
c@27 93 }
c@27 94
c@27 95 string
c@27 96 OnsetDetector::getCopyright() const
c@27 97 {
c@118 98 return "Plugin by Christian Landone, Chris Duxbury and Juan Pablo Bello. Copyright (c) 2006-2009 QMUL - All Rights Reserved";
c@27 99 }
c@27 100
c@27 101 OnsetDetector::ParameterList
c@27 102 OnsetDetector::getParameterDescriptors() const
c@27 103 {
c@27 104 ParameterList list;
c@27 105
c@27 106 ParameterDescriptor desc;
c@27 107 desc.identifier = "dftype";
c@27 108 desc.name = "Onset Detection Function Type";
c@27 109 desc.description = "Method used to calculate the onset detection function";
c@27 110 desc.minValue = 0;
c@31 111 desc.maxValue = 4;
c@27 112 desc.defaultValue = 3;
c@27 113 desc.isQuantized = true;
c@27 114 desc.quantizeStep = 1;
c@27 115 desc.valueNames.push_back("High-Frequency Content");
c@27 116 desc.valueNames.push_back("Spectral Difference");
c@27 117 desc.valueNames.push_back("Phase Deviation");
c@27 118 desc.valueNames.push_back("Complex Domain");
c@27 119 desc.valueNames.push_back("Broadband Energy Rise");
c@27 120 list.push_back(desc);
c@27 121
c@27 122 desc.identifier = "sensitivity";
c@27 123 desc.name = "Onset Detector Sensitivity";
c@27 124 desc.description = "Sensitivity of peak-picker for onset detection";
c@27 125 desc.minValue = 0;
c@27 126 desc.maxValue = 100;
c@27 127 desc.defaultValue = 50;
c@27 128 desc.isQuantized = true;
c@27 129 desc.quantizeStep = 1;
c@27 130 desc.unit = "%";
c@27 131 desc.valueNames.clear();
c@27 132 list.push_back(desc);
c@27 133
c@30 134 desc.identifier = "whiten";
c@30 135 desc.name = "Adaptive Whitening";
c@30 136 desc.description = "Normalize frequency bin magnitudes relative to recent peak levels";
c@30 137 desc.minValue = 0;
c@30 138 desc.maxValue = 1;
c@30 139 desc.defaultValue = 0;
c@30 140 desc.isQuantized = true;
c@30 141 desc.quantizeStep = 1;
c@30 142 desc.unit = "";
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 OnsetDetector::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@27 159 } else if (name == "sensitivity") {
c@27 160 return m_sensitivity;
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 OnsetDetector::setParameter(std::string name, float value)
c@27 169 {
c@27 170 if (name == "dftype") {
c@30 171 int dfType = m_dfType;
c@27 172 switch (lrintf(value)) {
c@30 173 case 0: dfType = DF_HFC; break;
c@30 174 case 1: dfType = DF_SPECDIFF; break;
c@30 175 case 2: dfType = DF_PHASEDEV; break;
c@30 176 default: case 3: dfType = DF_COMPLEXSD; break;
c@30 177 case 4: dfType = DF_BROADBAND; break;
c@27 178 }
c@30 179 if (dfType == m_dfType) return;
c@30 180 m_dfType = dfType;
c@30 181 m_program = "";
c@27 182 } else if (name == "sensitivity") {
c@30 183 if (m_sensitivity == value) return;
c@27 184 m_sensitivity = value;
c@30 185 m_program = "";
c@30 186 } else if (name == "whiten") {
c@30 187 if (m_whiten == (value > 0.5)) return;
c@30 188 m_whiten = (value > 0.5);
c@30 189 m_program = "";
c@27 190 }
c@27 191 }
c@27 192
c@29 193 OnsetDetector::ProgramList
c@29 194 OnsetDetector::getPrograms() const
c@29 195 {
c@29 196 ProgramList programs;
c@30 197 programs.push_back("");
c@29 198 programs.push_back("General purpose");
c@29 199 programs.push_back("Soft onsets");
c@29 200 programs.push_back("Percussive onsets");
c@29 201 return programs;
c@29 202 }
c@29 203
c@29 204 std::string
c@29 205 OnsetDetector::getCurrentProgram() const
c@29 206 {
c@30 207 if (m_program == "") return "";
c@29 208 else return m_program;
c@29 209 }
c@29 210
c@29 211 void
c@29 212 OnsetDetector::selectProgram(std::string program)
c@29 213 {
c@29 214 if (program == "General purpose") {
c@29 215 setParameter("dftype", 3); // complex
c@29 216 setParameter("sensitivity", 50);
c@30 217 setParameter("whiten", 0);
c@29 218 } else if (program == "Soft onsets") {
c@31 219 setParameter("dftype", 3); // complex
c@31 220 setParameter("sensitivity", 40);
c@31 221 setParameter("whiten", 1);
c@29 222 } else if (program == "Percussive onsets") {
c@29 223 setParameter("dftype", 4); // broadband energy rise
c@29 224 setParameter("sensitivity", 40);
c@30 225 setParameter("whiten", 0);
c@29 226 } else {
c@29 227 return;
c@29 228 }
c@29 229 m_program = program;
c@29 230 }
c@29 231
c@27 232 bool
c@27 233 OnsetDetector::initialise(size_t channels, size_t stepSize, size_t blockSize)
c@27 234 {
c@27 235 if (m_d) {
c@27 236 delete m_d;
c@27 237 m_d = 0;
c@27 238 }
c@27 239
c@27 240 if (channels < getMinChannelCount() ||
c@27 241 channels > getMaxChannelCount()) {
c@27 242 std::cerr << "OnsetDetector::initialise: Unsupported channel count: "
c@27 243 << channels << std::endl;
c@27 244 return false;
c@27 245 }
c@27 246
c@28 247 if (stepSize != getPreferredStepSize()) {
c@32 248 std::cerr << "WARNING: OnsetDetector::initialise: Possibly sub-optimal step size for this sample rate: "
c@28 249 << stepSize << " (wanted " << (getPreferredStepSize()) << ")" << std::endl;
c@27 250 }
c@27 251
c@28 252 if (blockSize != getPreferredBlockSize()) {
c@32 253 std::cerr << "WARNING: OnsetDetector::initialise: Possibly sub-optimal block size for this sample rate: "
c@28 254 << blockSize << " (wanted " << (getPreferredBlockSize()) << ")" << std::endl;
c@27 255 }
c@27 256
c@27 257 DFConfig dfConfig;
c@27 258 dfConfig.DFType = m_dfType;
c@27 259 dfConfig.stepSize = stepSize;
c@27 260 dfConfig.frameLength = blockSize;
c@27 261 dfConfig.dbRise = 6.0 - m_sensitivity / 16.6667;
c@30 262 dfConfig.adaptiveWhitening = m_whiten;
c@30 263 dfConfig.whiteningRelaxCoeff = -1;
c@30 264 dfConfig.whiteningFloor = -1;
c@27 265
c@27 266 m_d = new OnsetDetectorData(dfConfig);
c@27 267 return true;
c@27 268 }
c@27 269
c@27 270 void
c@27 271 OnsetDetector::reset()
c@27 272 {
c@27 273 if (m_d) m_d->reset();
c@27 274 }
c@27 275
c@27 276 size_t
c@27 277 OnsetDetector::getPreferredStepSize() const
c@27 278 {
c@32 279 size_t step = size_t(m_inputSampleRate * m_preferredStepSecs + 0.0001);
c@95 280 if (step < 1) step = 1;
c@27 281 // std::cerr << "OnsetDetector::getPreferredStepSize: input sample rate is " << m_inputSampleRate << ", step size is " << step << std::endl;
c@27 282 return step;
c@27 283 }
c@27 284
c@27 285 size_t
c@27 286 OnsetDetector::getPreferredBlockSize() const
c@27 287 {
c@27 288 return getPreferredStepSize() * 2;
c@27 289 }
c@27 290
c@27 291 OnsetDetector::OutputList
c@27 292 OnsetDetector::getOutputDescriptors() const
c@27 293 {
c@27 294 OutputList list;
c@27 295
c@32 296 float stepSecs = m_preferredStepSecs;
c@88 297 // if (m_d) stepSecs = m_d->dfConfig.stepSecs;
c@32 298
c@27 299 OutputDescriptor onsets;
c@27 300 onsets.identifier = "onsets";
c@27 301 onsets.name = "Note Onsets";
c@27 302 onsets.description = "Perceived note onset positions";
c@27 303 onsets.unit = "";
c@27 304 onsets.hasFixedBinCount = true;
c@27 305 onsets.binCount = 0;
c@27 306 onsets.sampleType = OutputDescriptor::VariableSampleRate;
c@32 307 onsets.sampleRate = 1.0 / stepSecs;
c@27 308
c@27 309 OutputDescriptor df;
c@27 310 df.identifier = "detection_fn";
c@27 311 df.name = "Onset Detection Function";
c@27 312 df.description = "Probability function of note onset likelihood";
c@27 313 df.unit = "";
c@27 314 df.hasFixedBinCount = true;
c@27 315 df.binCount = 1;
c@27 316 df.hasKnownExtents = false;
c@27 317 df.isQuantized = false;
c@27 318 df.sampleType = OutputDescriptor::OneSamplePerStep;
c@27 319
c@27 320 OutputDescriptor sdf;
c@27 321 sdf.identifier = "smoothed_df";
c@27 322 sdf.name = "Smoothed Detection Function";
c@27 323 sdf.description = "Smoothed probability function used for peak-picking";
c@27 324 sdf.unit = "";
c@27 325 sdf.hasFixedBinCount = true;
c@27 326 sdf.binCount = 1;
c@27 327 sdf.hasKnownExtents = false;
c@27 328 sdf.isQuantized = false;
c@27 329
c@27 330 sdf.sampleType = OutputDescriptor::VariableSampleRate;
c@27 331
c@27 332 //!!! SV doesn't seem to handle these correctly in getRemainingFeatures
c@27 333 // sdf.sampleType = OutputDescriptor::FixedSampleRate;
c@32 334 sdf.sampleRate = 1.0 / stepSecs;
c@27 335
c@27 336 list.push_back(onsets);
c@27 337 list.push_back(df);
c@27 338 list.push_back(sdf);
c@27 339
c@27 340 return list;
c@27 341 }
c@27 342
c@27 343 OnsetDetector::FeatureSet
c@27 344 OnsetDetector::process(const float *const *inputBuffers,
c@29 345 Vamp::RealTime timestamp)
c@27 346 {
c@27 347 if (!m_d) {
c@27 348 cerr << "ERROR: OnsetDetector::process: "
c@27 349 << "OnsetDetector has not been initialised"
c@27 350 << endl;
c@27 351 return FeatureSet();
c@27 352 }
c@27 353
c@153 354 size_t len = m_d->dfConfig.frameLength / 2 + 1;
c@27 355
c@29 356 // float mean = 0.f;
c@29 357 // for (size_t i = 0; i < len; ++i) {
c@29 358 //// std::cerr << inputBuffers[0][i] << " ";
c@29 359 // mean += inputBuffers[0][i];
c@29 360 // }
c@29 361 //// std::cerr << std::endl;
c@29 362 // mean /= len;
c@29 363
c@29 364 // std::cerr << "OnsetDetector::process(" << timestamp << "): "
c@29 365 // << "dftype " << m_dfType << ", sens " << m_sensitivity
c@29 366 // << ", len " << len << ", mean " << mean << std::endl;
c@29 367
c@153 368 double *reals = new double[len];
c@153 369 double *imags = new double[len];
c@27 370
c@27 371 // We only support a single input channel
c@27 372
c@27 373 for (size_t i = 0; i < len; ++i) {
c@153 374 reals[i] = inputBuffers[0][i*2];
c@153 375 imags[i] = inputBuffers[0][i*2+1];
c@27 376 }
c@27 377
c@153 378 double output = m_d->df->processFrequencyDomain(reals, imags);
c@27 379
c@153 380 delete[] reals;
c@153 381 delete[] imags;
c@27 382
c@85 383 if (m_d->dfOutput.empty()) m_d->origin = timestamp;
c@85 384
c@27 385 m_d->dfOutput.push_back(output);
c@27 386
c@27 387 FeatureSet returnFeatures;
c@27 388
c@27 389 Feature feature;
c@27 390 feature.hasTimestamp = false;
c@27 391 feature.values.push_back(output);
c@27 392
c@29 393 // std::cerr << "df: " << output << std::endl;
c@29 394
c@27 395 returnFeatures[1].push_back(feature); // detection function is output 1
c@27 396 return returnFeatures;
c@27 397 }
c@27 398
c@27 399 OnsetDetector::FeatureSet
c@27 400 OnsetDetector::getRemainingFeatures()
c@27 401 {
c@27 402 if (!m_d) {
c@27 403 cerr << "ERROR: OnsetDetector::getRemainingFeatures: "
c@27 404 << "OnsetDetector has not been initialised"
c@27 405 << endl;
c@27 406 return FeatureSet();
c@27 407 }
c@27 408
c@27 409 if (m_dfType == DF_BROADBAND) {
c@27 410 for (size_t i = 0; i < m_d->dfOutput.size(); ++i) {
c@27 411 if (m_d->dfOutput[i] < ((110 - m_sensitivity) *
c@27 412 m_d->dfConfig.frameLength) / 200) {
c@27 413 m_d->dfOutput[i] = 0;
c@27 414 }
c@27 415 }
c@27 416 }
c@27 417
c@27 418 double aCoeffs[] = { 1.0000, -0.5949, 0.2348 };
c@27 419 double bCoeffs[] = { 0.1600, 0.3200, 0.1600 };
c@27 420
c@27 421 FeatureSet returnFeatures;
c@27 422
c@27 423 PPickParams ppParams;
c@27 424 ppParams.length = m_d->dfOutput.size();
c@27 425 // tau and cutoff appear to be unused in PeakPicking, but I've
c@27 426 // inserted some moderately plausible values rather than leave
c@27 427 // them unset. The QuadThresh values come from trial and error.
c@27 428 // The rest of these are copied from ttParams in the BeatTracker
c@27 429 // code: I don't claim to know whether they're good or not --cc
c@27 430 ppParams.tau = m_d->dfConfig.stepSize / m_inputSampleRate;
c@27 431 ppParams.alpha = 9;
c@27 432 ppParams.cutoff = m_inputSampleRate/4;
c@27 433 ppParams.LPOrd = 2;
c@27 434 ppParams.LPACoeffs = aCoeffs;
c@27 435 ppParams.LPBCoeffs = bCoeffs;
c@27 436 ppParams.WinT.post = 8;
c@27 437 ppParams.WinT.pre = 7;
c@27 438 ppParams.QuadThresh.a = (100 - m_sensitivity) / 1000.0;
c@27 439 ppParams.QuadThresh.b = 0;
c@27 440 ppParams.QuadThresh.c = (100 - m_sensitivity) / 1500.0;
c@27 441
c@27 442 PeakPicking peakPicker(ppParams);
c@27 443
c@27 444 double *ppSrc = new double[ppParams.length];
cannam@239 445 for (int i = 0; i < ppParams.length; ++i) {
c@27 446 ppSrc[i] = m_d->dfOutput[i];
c@27 447 }
c@27 448
c@27 449 vector<int> onsets;
c@27 450 peakPicker.process(ppSrc, ppParams.length, onsets);
c@27 451
c@27 452 for (size_t i = 0; i < onsets.size(); ++i) {
c@27 453
c@27 454 size_t index = onsets[i];
c@27 455
c@27 456 if (m_dfType != DF_BROADBAND) {
c@27 457 double prevDiff = 0.0;
c@27 458 while (index > 1) {
c@27 459 double diff = ppSrc[index] - ppSrc[index-1];
c@27 460 if (diff < prevDiff * 0.9) break;
c@27 461 prevDiff = diff;
c@27 462 --index;
c@27 463 }
c@27 464 }
c@27 465
c@27 466 size_t frame = index * m_d->dfConfig.stepSize;
c@27 467
c@27 468 Feature feature;
c@27 469 feature.hasTimestamp = true;
c@85 470 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime
c@27 471 (frame, lrintf(m_inputSampleRate));
c@27 472
c@27 473 returnFeatures[0].push_back(feature); // onsets are output 0
c@27 474 }
c@27 475
cannam@239 476 for (int i = 0; i < ppParams.length; ++i) {
c@27 477
c@27 478 Feature feature;
cannam@239 479
c@27 480 feature.hasTimestamp = true;
c@27 481 size_t frame = i * m_d->dfConfig.stepSize;
c@85 482 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime
c@27 483 (frame, lrintf(m_inputSampleRate));
c@27 484
c@27 485 feature.values.push_back(ppSrc[i]);
c@27 486 returnFeatures[2].push_back(feature); // smoothed df is output 2
c@27 487 }
c@27 488
c@27 489 return returnFeatures;
c@27 490 }
c@27 491