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