add dBTP Vamp plugin
[ardour.git] / libs / vamp-plugins / Onset.cpp
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     Vamp feature extraction plugins using Paul Brossier's Aubio library.
5
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006 Chris Cannam.
8
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14
15 */
16
17 #ifdef COMPILER_MSVC
18 #include <ardourext/float_cast.h>
19 #endif
20 #include <math.h>
21 #include "Onset.h"
22
23 using std::string;
24 using std::vector;
25 using std::cerr;
26 using std::endl;
27
28 #ifdef HAVE_AUBIO4
29 const char *getAubioNameForOnsetType(OnsetType t)
30 {
31     // In the same order as the enum elements in the header
32     static const char *const names[] = {
33         "energy", "specdiff", "hfc", "complex", "phase", "kl", "mkl", "specflux"
34     };
35     return names[(int)t];
36 }
37 #endif
38
39 Onset::Onset(float inputSampleRate) :
40     Plugin(inputSampleRate),
41     m_ibuf(0),
42     m_onset(0),
43 #ifdef HAVE_AUBIO4
44     m_onsetdet(0),
45     m_onsettype(OnsetComplex),
46     m_minioi(4),
47     m_silence(-70),
48 #else
49     m_fftgrain(0),
50     m_pv(0),
51     m_peakpick(0),
52     m_onsetdet(0),
53     m_onsettype(aubio_onset_complex),
54     m_channelCount(1),
55     m_silence(-90),
56 #endif
57     m_threshold(0.3)
58 {
59 }
60
61 Onset::~Onset()
62 {
63 #ifdef HAVE_AUBIO4
64     if (m_onsetdet) del_aubio_onset(m_onsetdet);
65 #else
66     if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
67     if (m_fftgrain) del_cvec(m_fftgrain);
68     if (m_pv) del_aubio_pvoc(m_pv);
69     if (m_peakpick) del_aubio_peakpicker(m_peakpick);
70 #endif
71     if (m_ibuf) del_fvec(m_ibuf);
72     if (m_onset) del_fvec(m_onset);
73 }
74
75 string
76 Onset::getIdentifier() const
77 {
78     return "aubioonset";
79 }
80
81 string
82 Onset::getName() const
83 {
84     return "Aubio Onset Detector";
85 }
86
87 string
88 Onset::getDescription() const
89 {
90     return "Estimate note onset times";
91 }
92
93 string
94 Onset::getMaker() const
95 {
96     return "Paul Brossier (plugin by Chris Cannam)";
97 }
98
99 int
100 Onset::getPluginVersion() const
101 {
102 #ifdef HAVE_AUBIO4
103     return 2;
104 #else
105     return 1;
106 #endif
107 }
108
109 string
110 Onset::getCopyright() const
111 {
112     return "GPL";
113 }
114
115 bool
116 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
117 {
118     m_stepSize = stepSize;
119     m_blockSize = blockSize;
120
121 #ifdef HAVE_AUBIO4
122     if (channels != 1) {
123         std::cerr << "Onset::initialise: channels must be 1" << std::endl;
124         return false;
125     }
126     m_ibuf = new_fvec(stepSize);
127     m_onset = new_fvec(1);
128     reset();
129 #else
130     m_channelCount = channels;
131
132     m_ibuf = new_fvec(stepSize, channels);
133     m_onset = new_fvec(1, channels);
134     m_fftgrain = new_cvec(blockSize, channels);
135     m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
136     m_peakpick = new_aubio_peakpicker(m_threshold);
137
138     m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
139
140     m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
141                                              lrintf(m_inputSampleRate));
142
143     m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
144
145 #endif
146     return true;
147 }
148
149 void
150 Onset::reset()
151 {
152 #ifdef HAVE_AUBIO4
153     if (m_onsetdet) del_aubio_onset(m_onsetdet);
154
155     m_onsetdet = new_aubio_onset
156         (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
157          m_blockSize,
158          m_stepSize,
159          lrintf(m_inputSampleRate));
160
161     aubio_onset_set_threshold(m_onsetdet, m_threshold);
162     aubio_onset_set_silence(m_onsetdet, m_silence);
163     aubio_onset_set_minioi(m_onsetdet, m_minioi);
164
165     m_delay = Vamp::RealTime::frame2RealTime(4 * m_stepSize,
166                                              lrintf(m_inputSampleRate));
167
168     m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
169 #endif
170 }
171
172 size_t
173 Onset::getPreferredStepSize() const
174 {
175     return 512;
176 }
177
178 size_t
179 Onset::getPreferredBlockSize() const
180 {
181     return 2 * getPreferredStepSize();
182 }
183
184 Onset::ParameterList
185 Onset::getParameterDescriptors() const
186 {
187     ParameterList list;
188
189     ParameterDescriptor desc;
190     desc.identifier = "onsettype";
191     desc.name = "Onset Detection Function Type";
192     desc.minValue = 0;
193 #ifdef HAVE_AUBIO4
194     desc.maxValue = 7;
195     desc.defaultValue = (int)OnsetComplex;
196 #else
197     desc.maxValue = 6;
198     desc.defaultValue = (int)aubio_onset_complex;
199 #endif
200     desc.isQuantized = true;
201     desc.quantizeStep = 1;
202     desc.valueNames.push_back("Energy Based");
203     desc.valueNames.push_back("Spectral Difference");
204     desc.valueNames.push_back("High-Frequency Content");
205     desc.valueNames.push_back("Complex Domain");
206     desc.valueNames.push_back("Phase Deviation");
207     desc.valueNames.push_back("Kullback-Liebler");
208     desc.valueNames.push_back("Modified Kullback-Liebler");
209 #ifdef HAVE_AUBIO4
210     desc.valueNames.push_back("Spectral Flux");
211 #endif
212     list.push_back(desc);
213
214     desc = ParameterDescriptor();
215     desc.identifier = "peakpickthreshold";
216     desc.name = "Peak Picker Threshold";
217     desc.minValue = 0;
218     desc.maxValue = 1;
219     desc.defaultValue = 0.3;
220     desc.isQuantized = false;
221     list.push_back(desc);
222
223     desc = ParameterDescriptor();
224     desc.identifier = "silencethreshold";
225     desc.name = "Silence Threshold";
226     desc.minValue = -120;
227     desc.maxValue = 0;
228 #ifdef HAVE_AUBIO4
229     desc.defaultValue = -70;
230 #else
231     desc.defaultValue = -90;
232 #endif
233     desc.unit = "dB";
234     desc.isQuantized = false;
235     list.push_back(desc);
236
237 #ifdef HAVE_AUBIO4
238     desc = ParameterDescriptor();
239     desc.identifier = "minioi";
240     desc.name = "Minimum Inter-Onset Interval";
241     desc.minValue = 0;
242     desc.maxValue = 40;
243     desc.defaultValue = 4;
244     desc.unit = "ms";
245     desc.isQuantized = true;
246     desc.quantizeStep = 1;
247     list.push_back(desc);
248 #endif
249     return list;
250 }
251
252 float
253 Onset::getParameter(std::string param) const
254 {
255     if (param == "onsettype") {
256         return m_onsettype;
257     } else if (param == "peakpickthreshold") {
258         return m_threshold;
259     } else if (param == "silencethreshold") {
260         return m_silence;
261 #ifdef HAVE_AUBIO4
262     } else if (param == "minioi") {
263         return m_minioi;
264 #endif
265     } else {
266         return 0.0;
267     }
268 }
269
270 void
271 Onset::setParameter(std::string param, float value)
272 {
273     if (param == "onsettype") {
274         switch (lrintf(value)) {
275 #ifdef HAVE_AUBIO4
276         case 0: m_onsettype = OnsetEnergy; break;
277         case 1: m_onsettype = OnsetSpecDiff; break;
278         case 2: m_onsettype = OnsetHFC; break;
279         case 3: m_onsettype = OnsetComplex; break;
280         case 4: m_onsettype = OnsetPhase; break;
281         case 5: m_onsettype = OnsetKL; break;
282         case 6: m_onsettype = OnsetMKL; break;
283         case 7: m_onsettype = OnsetSpecFlux; break;
284 #else
285         case 0: m_onsettype = aubio_onset_energy; break;
286         case 1: m_onsettype = aubio_onset_specdiff; break;
287         case 2: m_onsettype = aubio_onset_hfc; break;
288         case 3: m_onsettype = aubio_onset_complex; break;
289         case 4: m_onsettype = aubio_onset_phase; break;
290         case 5: m_onsettype = aubio_onset_kl; break;
291         case 6: m_onsettype = aubio_onset_mkl; break;
292 #endif
293         }
294     } else if (param == "peakpickthreshold") {
295         m_threshold = value;
296     } else if (param == "silencethreshold") {
297         m_silence = value;
298 #ifdef HAVE_AUBIO4
299     } else if (param == "minioi") {
300         m_minioi = value;
301 #endif
302     }
303 }
304
305 Onset::OutputList
306 Onset::getOutputDescriptors() const
307 {
308     OutputList list;
309
310     OutputDescriptor d;
311     d.identifier = "onsets";
312     d.name = "Onsets";
313     d.unit = "";
314     d.hasFixedBinCount = true;
315     d.binCount = 0;
316     d.sampleType = OutputDescriptor::VariableSampleRate;
317     d.sampleRate = 0;
318     list.push_back(d);
319
320 #ifndef HAVE_AUBIO4
321     d = OutputDescriptor();
322     d.identifier = "detectionfunction";
323     d.name = "Onset Detection Function";
324     d.unit = "";
325     d.hasFixedBinCount = true;
326     d.binCount = m_channelCount;
327     d.hasKnownExtents = false;
328     d.isQuantized = false;
329     d.sampleType = OutputDescriptor::OneSamplePerStep;
330     list.push_back(d);
331 #endif
332     return list;
333 }
334
335 Onset::FeatureSet
336 Onset::process(const float *const *inputBuffers,
337                Vamp::RealTime timestamp)
338 {
339 #ifdef HAVE_AUBIO4
340     for (size_t i = 0; i < m_stepSize; ++i) {
341         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
342     }
343
344     aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
345
346     bool isonset = m_onset->data[0];
347 #else
348     for (size_t i = 0; i < m_stepSize; ++i) {
349         for (size_t j = 0; j < m_channelCount; ++j) {
350             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
351         }
352     }
353
354     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
355     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
356
357     bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
358
359     if (isonset) {
360         if (aubio_silence_detection(m_ibuf, m_silence)) {
361             isonset = false;
362         }
363     }
364 #endif
365
366     FeatureSet returnFeatures;
367
368     if (isonset) {
369         if (timestamp - m_lastOnset >= m_delay) {
370             Feature onsettime;
371             onsettime.hasTimestamp = true;
372             if (timestamp < m_delay) timestamp = m_delay;
373             onsettime.timestamp = timestamp - m_delay;
374             returnFeatures[0].push_back(onsettime);
375             m_lastOnset = timestamp;
376         }
377     }
378 #ifndef HAVE_AUBIO4
379     Feature feature;
380     for (size_t j = 0; j < m_channelCount; ++j) {
381         feature.values.push_back(m_onset->data[j][0]);
382     }
383     returnFeatures[1].push_back(feature);
384 #endif
385
386     return returnFeatures;
387 }
388
389 Onset::FeatureSet
390 Onset::getRemainingFeatures()
391 {
392     return FeatureSet();
393 }
394