annotate plugins/OnsetDetect.cpp @ 30:ff5a09e45209

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