annotate plugins/OnsetDetect.cpp @ 50:df7a0bc46592

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