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