1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
16 #include <ardourext/float_cast.h>
18 #include "OnsetDetect.h"
20 #include "dsp/onsets/DetectionFunction.h"
21 #include "dsp/onsets/PeakPicking.h"
22 #include "dsp/tempotracking/TempoTrack.h"
29 float OnsetDetector::m_preferredStepSecs = 0.01161;
31 class OnsetDetectorData
34 OnsetDetectorData(const DFConfig &config) : dfConfig(config) {
35 df = new DetectionFunction(config);
37 ~OnsetDetectorData() {
42 df = new DetectionFunction(dfConfig);
44 origin = Vamp::RealTime::zeroTime;
48 DetectionFunction *df;
49 vector<double> dfOutput;
50 Vamp::RealTime origin;
54 OnsetDetector::OnsetDetector(float inputSampleRate) :
55 Vamp::Plugin(inputSampleRate),
57 m_dfType(DF_COMPLEXSD),
63 OnsetDetector::~OnsetDetector()
69 OnsetDetector::getIdentifier() const
71 return "qm-onsetdetector";
75 OnsetDetector::getName() const
77 return "Note Onset Detector";
81 OnsetDetector::getDescription() const
83 return "Estimate individual note onset positions";
87 OnsetDetector::getMaker() const
89 return "Queen Mary, University of London";
93 OnsetDetector::getPluginVersion() const
99 OnsetDetector::getCopyright() const
101 return "Plugin by Christian Landone, Chris Duxbury and Juan Pablo Bello. Copyright (c) 2006-2009 QMUL - All Rights Reserved";
104 OnsetDetector::ParameterList
105 OnsetDetector::getParameterDescriptors() const
109 ParameterDescriptor desc;
110 desc.identifier = "dftype";
111 desc.name = "Onset Detection Function Type";
112 desc.description = "Method used to calculate the onset detection function";
115 desc.defaultValue = 3;
116 desc.isQuantized = true;
117 desc.quantizeStep = 1;
118 desc.valueNames.push_back("High-Frequency Content");
119 desc.valueNames.push_back("Spectral Difference");
120 desc.valueNames.push_back("Phase Deviation");
121 desc.valueNames.push_back("Complex Domain");
122 desc.valueNames.push_back("Broadband Energy Rise");
123 list.push_back(desc);
125 desc.identifier = "sensitivity";
126 desc.name = "Onset Detector Sensitivity";
127 desc.description = "Sensitivity of peak-picker for onset detection";
130 desc.defaultValue = 50;
131 desc.isQuantized = true;
132 desc.quantizeStep = 1;
134 desc.valueNames.clear();
135 list.push_back(desc);
137 desc.identifier = "whiten";
138 desc.name = "Adaptive Whitening";
139 desc.description = "Normalize frequency bin magnitudes relative to recent peak levels";
142 desc.defaultValue = 0;
143 desc.isQuantized = true;
144 desc.quantizeStep = 1;
146 list.push_back(desc);
152 OnsetDetector::getParameter(std::string name) const
154 if (name == "dftype") {
156 case DF_HFC: return 0;
157 case DF_SPECDIFF: return 1;
158 case DF_PHASEDEV: return 2;
159 default: case DF_COMPLEXSD: return 3;
160 case DF_BROADBAND: return 4;
162 } else if (name == "sensitivity") {
163 return m_sensitivity;
164 } else if (name == "whiten") {
165 return m_whiten ? 1.0 : 0.0;
171 OnsetDetector::setParameter(std::string name, float value)
173 if (name == "dftype") {
174 int dfType = m_dfType;
175 switch (lrintf(value)) {
176 case 0: dfType = DF_HFC; break;
177 case 1: dfType = DF_SPECDIFF; break;
178 case 2: dfType = DF_PHASEDEV; break;
179 default: case 3: dfType = DF_COMPLEXSD; break;
180 case 4: dfType = DF_BROADBAND; break;
182 if (dfType == m_dfType) return;
185 } else if (name == "sensitivity") {
186 if (m_sensitivity == value) return;
187 m_sensitivity = value;
189 } else if (name == "whiten") {
190 if (m_whiten == (value > 0.5)) return;
191 m_whiten = (value > 0.5);
196 OnsetDetector::ProgramList
197 OnsetDetector::getPrograms() const
199 ProgramList programs;
200 programs.push_back("");
201 programs.push_back("General purpose");
202 programs.push_back("Soft onsets");
203 programs.push_back("Percussive onsets");
208 OnsetDetector::getCurrentProgram() const
210 if (m_program == "") return "";
211 else return m_program;
215 OnsetDetector::selectProgram(std::string program)
217 if (program == "General purpose") {
218 setParameter("dftype", 3); // complex
219 setParameter("sensitivity", 50);
220 setParameter("whiten", 0);
221 } else if (program == "Soft onsets") {
222 setParameter("dftype", 3); // complex
223 setParameter("sensitivity", 40);
224 setParameter("whiten", 1);
225 } else if (program == "Percussive onsets") {
226 setParameter("dftype", 4); // broadband energy rise
227 setParameter("sensitivity", 40);
228 setParameter("whiten", 0);
236 OnsetDetector::initialise(size_t channels, size_t stepSize, size_t blockSize)
243 if (channels < getMinChannelCount() ||
244 channels > getMaxChannelCount()) {
245 std::cerr << "OnsetDetector::initialise: Unsupported channel count: "
246 << channels << std::endl;
250 if (stepSize != getPreferredStepSize()) {
251 std::cerr << "WARNING: OnsetDetector::initialise: Possibly sub-optimal step size for this sample rate: "
252 << stepSize << " (wanted " << (getPreferredStepSize()) << ")" << std::endl;
255 if (blockSize != getPreferredBlockSize()) {
256 std::cerr << "WARNING: OnsetDetector::initialise: Possibly sub-optimal block size for this sample rate: "
257 << blockSize << " (wanted " << (getPreferredBlockSize()) << ")" << std::endl;
261 dfConfig.DFType = m_dfType;
262 dfConfig.stepSize = stepSize;
263 dfConfig.frameLength = blockSize;
264 dfConfig.dbRise = 6.0 - m_sensitivity / 16.6667;
265 dfConfig.adaptiveWhitening = m_whiten;
266 dfConfig.whiteningRelaxCoeff = -1;
267 dfConfig.whiteningFloor = -1;
269 m_d = new OnsetDetectorData(dfConfig);
274 OnsetDetector::reset()
276 if (m_d) m_d->reset();
280 OnsetDetector::getPreferredStepSize() const
282 size_t step = size_t(m_inputSampleRate * m_preferredStepSecs + 0.0001);
283 if (step < 1) step = 1;
284 // std::cerr << "OnsetDetector::getPreferredStepSize: input sample rate is " << m_inputSampleRate << ", step size is " << step << std::endl;
289 OnsetDetector::getPreferredBlockSize() const
291 return getPreferredStepSize() * 2;
294 OnsetDetector::OutputList
295 OnsetDetector::getOutputDescriptors() const
299 float stepSecs = m_preferredStepSecs;
300 // if (m_d) stepSecs = m_d->dfConfig.stepSecs;
302 OutputDescriptor onsets;
303 onsets.identifier = "onsets";
304 onsets.name = "Note Onsets";
305 onsets.description = "Perceived note onset positions";
307 onsets.hasFixedBinCount = true;
309 onsets.sampleType = OutputDescriptor::VariableSampleRate;
310 onsets.sampleRate = 1.0 / stepSecs;
313 df.identifier = "detection_fn";
314 df.name = "Onset Detection Function";
315 df.description = "Probability function of note onset likelihood";
317 df.hasFixedBinCount = true;
319 df.hasKnownExtents = false;
320 df.isQuantized = false;
321 df.sampleType = OutputDescriptor::OneSamplePerStep;
323 OutputDescriptor sdf;
324 sdf.identifier = "smoothed_df";
325 sdf.name = "Smoothed Detection Function";
326 sdf.description = "Smoothed probability function used for peak-picking";
328 sdf.hasFixedBinCount = true;
330 sdf.hasKnownExtents = false;
331 sdf.isQuantized = false;
333 sdf.sampleType = OutputDescriptor::VariableSampleRate;
335 //!!! SV doesn't seem to handle these correctly in getRemainingFeatures
336 // sdf.sampleType = OutputDescriptor::FixedSampleRate;
337 sdf.sampleRate = 1.0 / stepSecs;
339 list.push_back(onsets);
346 OnsetDetector::FeatureSet
347 OnsetDetector::process(const float *const *inputBuffers,
348 Vamp::RealTime timestamp)
351 cerr << "ERROR: OnsetDetector::process: "
352 << "OnsetDetector has not been initialised"
357 size_t len = m_d->dfConfig.frameLength / 2 + 1;
360 // for (size_t i = 0; i < len; ++i) {
361 //// std::cerr << inputBuffers[0][i] << " ";
362 // mean += inputBuffers[0][i];
364 //// std::cerr << std::endl;
367 // std::cerr << "OnsetDetector::process(" << timestamp << "): "
368 // << "dftype " << m_dfType << ", sens " << m_sensitivity
369 // << ", len " << len << ", mean " << mean << std::endl;
371 double *reals = new double[len];
372 double *imags = new double[len];
374 // We only support a single input channel
376 for (size_t i = 0; i < len; ++i) {
377 reals[i] = inputBuffers[0][i*2];
378 imags[i] = inputBuffers[0][i*2+1];
381 double output = m_d->df->processFrequencyDomain(reals, imags);
386 if (m_d->dfOutput.empty()) m_d->origin = timestamp;
388 m_d->dfOutput.push_back(output);
390 FeatureSet returnFeatures;
393 feature.hasTimestamp = false;
394 feature.values.push_back(output);
396 // std::cerr << "df: " << output << std::endl;
398 returnFeatures[1].push_back(feature); // detection function is output 1
399 return returnFeatures;
402 OnsetDetector::FeatureSet
403 OnsetDetector::getRemainingFeatures()
406 cerr << "ERROR: OnsetDetector::getRemainingFeatures: "
407 << "OnsetDetector has not been initialised"
412 if (m_dfType == DF_BROADBAND) {
413 for (size_t i = 0; i < m_d->dfOutput.size(); ++i) {
414 if (m_d->dfOutput[i] < ((110 - m_sensitivity) *
415 m_d->dfConfig.frameLength) / 200) {
416 m_d->dfOutput[i] = 0;
421 double aCoeffs[] = { 1.0000, -0.5949, 0.2348 };
422 double bCoeffs[] = { 0.1600, 0.3200, 0.1600 };
424 FeatureSet returnFeatures;
426 PPickParams ppParams;
427 ppParams.length = m_d->dfOutput.size();
428 // tau and cutoff appear to be unused in PeakPicking, but I've
429 // inserted some moderately plausible values rather than leave
430 // them unset. The QuadThresh values come from trial and error.
431 // The rest of these are copied from ttParams in the BeatTracker
432 // code: I don't claim to know whether they're good or not --cc
433 ppParams.tau = m_d->dfConfig.stepSize / m_inputSampleRate;
435 ppParams.cutoff = m_inputSampleRate/4;
437 ppParams.LPACoeffs = aCoeffs;
438 ppParams.LPBCoeffs = bCoeffs;
439 ppParams.WinT.post = 8;
440 ppParams.WinT.pre = 7;
441 ppParams.QuadThresh.a = (100 - m_sensitivity) / 1000.0;
442 ppParams.QuadThresh.b = 0;
443 ppParams.QuadThresh.c = (100 - m_sensitivity) / 1500.0;
445 PeakPicking peakPicker(ppParams);
447 double *ppSrc = new double[ppParams.length];
448 for (unsigned int i = 0; i < ppParams.length; ++i) {
449 ppSrc[i] = m_d->dfOutput[i];
453 peakPicker.process(ppSrc, ppParams.length, onsets);
455 for (size_t i = 0; i < onsets.size(); ++i) {
457 size_t index = onsets[i];
459 if (m_dfType != DF_BROADBAND) {
460 double prevDiff = 0.0;
462 double diff = ppSrc[index] - ppSrc[index-1];
463 if (diff < prevDiff * 0.9) break;
469 size_t frame = index * m_d->dfConfig.stepSize;
472 feature.hasTimestamp = true;
473 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime
474 (frame, lrintf(m_inputSampleRate));
476 returnFeatures[0].push_back(feature); // onsets are output 0
479 for (unsigned int i = 0; i < ppParams.length; ++i) {
482 // feature.hasTimestamp = false;
483 feature.hasTimestamp = true;
484 size_t frame = i * m_d->dfConfig.stepSize;
485 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime
486 (frame, lrintf(m_inputSampleRate));
488 feature.values.push_back(ppSrc[i]);
489 returnFeatures[2].push_back(feature); // smoothed df is output 2
492 return returnFeatures;