annotate plugins/OnsetDetect.cpp @ 88:d47b22cf47bc

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