scripts/get_aubio.sh: always pass options
[vamp-aubio-plugins.git] / 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 file is part of vamp-aubio-plugins.
10
11     vamp-aubio is free software: you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
15
16     vamp-aubio is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21     You should have received a copy of the GNU General Public License
22     along with aubio.  If not, see <http://www.gnu.org/licenses/>.
23
24 */
25
26 #include <math.h>
27 #include "Onset.h"
28
29 using std::string;
30 using std::vector;
31 using std::cerr;
32 using std::endl;
33
34 Onset::Onset(float inputSampleRate) :
35     Plugin(inputSampleRate),
36     m_ibuf(0),
37     m_onset(0),
38     m_onsetdet(0),
39     m_onsettype(OnsetDefault),
40     m_threshold(0.3),
41     m_silence(-90),
42     m_minioi(4)
43 {
44
45 }
46
47 Onset::~Onset()
48 {
49     if (m_onsetdet) del_aubio_onset(m_onsetdet);
50     if (m_ibuf) del_fvec(m_ibuf);
51     if (m_onset) del_fvec(m_onset);
52 }
53
54 string
55 Onset::getIdentifier() const
56 {
57     return "aubioonset";
58 }
59
60 string
61 Onset::getName() const
62 {
63     return "Aubio Onset Detector";
64 }
65
66 string
67 Onset::getDescription() const
68 {
69     return "Estimate note onset times";
70 }
71
72 string
73 Onset::getMaker() const
74 {
75     return "Paul Brossier (plugin by Chris Cannam)";
76 }
77
78 int
79 Onset::getPluginVersion() const
80 {
81     return 2;
82 }
83
84 string
85 Onset::getCopyright() const
86 {
87     return "GPL";
88 }
89
90 bool
91 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
92 {
93     if (channels != 1) {
94         std::cerr << "Onset::initialise: channels must be 1" << std::endl;
95         return false;
96     }
97
98     m_stepSize = stepSize;
99     m_blockSize = blockSize;
100
101     m_ibuf = new_fvec(stepSize);
102     m_onset = new_fvec(1);
103
104     reset();
105
106     return true;
107 }
108
109 void
110 Onset::reset()
111 {
112     if (m_onsetdet) del_aubio_onset(m_onsetdet);
113
114     m_onsetdet = new_aubio_onset
115         (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
116          m_blockSize,
117          m_stepSize,
118          lrintf(m_inputSampleRate));
119
120     aubio_onset_set_threshold(m_onsetdet, m_threshold);
121     aubio_onset_set_silence(m_onsetdet, m_silence);
122     aubio_onset_set_minioi(m_onsetdet, m_minioi);
123
124 }
125
126 size_t
127 Onset::getPreferredStepSize() const
128 {
129     return 256;
130 }
131
132 size_t
133 Onset::getPreferredBlockSize() const
134 {
135     return 2 * getPreferredStepSize();
136 }
137
138 Onset::ParameterList
139 Onset::getParameterDescriptors() const
140 {
141     ParameterList list;
142     
143     ParameterDescriptor desc;
144     desc.identifier = "onsettype";
145     desc.name = "Onset Detection Function Type";
146     desc.description = "Type of onset detection function to use";
147     desc.minValue = 0;
148     desc.maxValue = 7;
149     desc.defaultValue = (int)OnsetDefault;
150     desc.isQuantized = true;
151     desc.quantizeStep = 1;
152     desc.valueNames.push_back("Energy Based");
153     desc.valueNames.push_back("Spectral Difference");
154     desc.valueNames.push_back("High-Frequency Content");
155     desc.valueNames.push_back("Complex Domain");
156     desc.valueNames.push_back("Phase Deviation");
157     desc.valueNames.push_back("Kullback-Liebler");
158     desc.valueNames.push_back("Modified Kullback-Liebler");
159     desc.valueNames.push_back("Spectral Flux");
160     desc.valueNames.push_back("Default");
161     list.push_back(desc);
162
163     desc = ParameterDescriptor();
164     desc.identifier = "peakpickthreshold";
165     desc.name = "Peak Picker Threshold";
166     desc.description = "Threshold used for peak picking, the higher the more detections";
167     desc.minValue = 0;
168     desc.maxValue = 1;
169     desc.defaultValue = 0.3;
170     desc.isQuantized = false;
171     list.push_back(desc);
172
173     desc = ParameterDescriptor();
174     desc.identifier = "silencethreshold";
175     desc.name = "Silence Threshold";
176     desc.description = "Silence threshold, the higher the least detection";
177     desc.minValue = -120;
178     desc.maxValue = 0;
179     desc.defaultValue = -90;
180     desc.unit = "dB";
181     desc.isQuantized = false;
182     list.push_back(desc);
183
184     desc = ParameterDescriptor();
185     desc.identifier = "minioi";
186     desc.name = "Minimum Inter-Onset Interval";
187     desc.description = "Time interval below which two consecutive onsets should be merged";
188     desc.minValue = 0;
189     desc.maxValue = 40;
190     desc.defaultValue = 4;
191     desc.unit = "ms";
192     desc.isQuantized = true;
193     desc.quantizeStep = 1;
194     list.push_back(desc);
195
196     return list;
197 }
198
199 float
200 Onset::getParameter(std::string param) const
201 {
202     if (param == "onsettype") {
203         return m_onsettype;
204     } else if (param == "peakpickthreshold") {
205         if (m_onsetdet) {
206             return aubio_onset_get_threshold(m_onsetdet);
207         } else {
208             return m_threshold;
209         }
210     } else if (param == "silencethreshold") {
211         if (m_onsetdet) {
212             return aubio_onset_get_silence(m_onsetdet);
213         } else {
214             return m_silence;
215         }
216     } else if (param == "minioi") {
217         if (m_onsetdet) {
218             return aubio_onset_get_minioi(m_onsetdet);
219         } else {
220             return m_minioi;
221         }
222     } else {
223         return 0.0;
224     }
225 }
226
227 void
228 Onset::setParameter(std::string param, float value)
229 {
230     if (param == "onsettype") {
231         switch (lrintf(value)) {
232         case 0: m_onsettype = OnsetEnergy; break;
233         case 1: m_onsettype = OnsetSpecDiff; break;
234         case 2: m_onsettype = OnsetHFC; break;
235         case 3: m_onsettype = OnsetComplex; break;
236         case 4: m_onsettype = OnsetPhase; break;
237         case 5: m_onsettype = OnsetKL; break;
238         case 6: m_onsettype = OnsetMKL; break;
239         case 7: m_onsettype = OnsetSpecFlux; break;
240         case 8: m_onsettype = OnsetDefault; break;
241         }
242         if (!m_onsetdet) initialise(1, 256, 512);
243     } else if (param == "peakpickthreshold") {
244         m_threshold = value;
245         if (m_onsetdet)
246             aubio_onset_set_threshold(m_onsetdet, m_threshold);
247     } else if (param == "silencethreshold") {
248         m_silence = value;
249         if (m_onsetdet)
250             aubio_onset_set_silence(m_onsetdet, m_silence);
251     } else if (param == "minioi") {
252         m_minioi = value;
253         if (m_onsetdet)
254             aubio_onset_set_minioi(m_onsetdet, m_minioi);
255     }
256 }
257
258 Onset::OutputList
259 Onset::getOutputDescriptors() const
260 {
261     OutputList list;
262
263     OutputDescriptor d;
264     d.identifier = "onsets";
265     d.name = "Onsets";
266     d.description = "List of times at which a note onset was detected";
267     d.unit = "";
268     d.hasFixedBinCount = true;
269     d.binCount = 0;
270     d.sampleType = OutputDescriptor::VariableSampleRate;
271     d.sampleRate = 0;
272     list.push_back(d);
273
274     d.identifier = "odf";
275     d.name = "Onset detection function";
276     d.description = "Output of the onset detection function";
277     d.binCount = 1;
278     d.isQuantized = true;
279     d.quantizeStep = 1.0;
280     d.sampleType = OutputDescriptor::OneSamplePerStep;
281     list.push_back(d);
282
283     d.identifier = "todf";
284     d.name = "Thresholded Onset detection function";
285     d.description = "Output of the thresholded onset detection function";
286     d.binCount = 1;
287     d.isQuantized = true;
288     d.quantizeStep = 1.0;
289     d.sampleType = OutputDescriptor::OneSamplePerStep;
290     list.push_back(d);
291
292     return list;
293 }
294
295 Onset::FeatureSet
296 Onset::process(const float *const *inputBuffers,
297                UNUSED Vamp::RealTime timestamp)
298 {
299     for (size_t i = 0; i < m_stepSize; ++i) {
300         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
301     }
302
303     aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
304
305     smpl_t isonset = m_onset->data[0];
306
307     FeatureSet returnFeatures;
308
309     if (isonset) {
310         Feature onsettime;
311         onsettime.hasTimestamp = true;
312         onsettime.timestamp = Vamp::RealTime::fromSeconds(aubio_onset_get_last_s(m_onsetdet));
313         returnFeatures[0].push_back(onsettime);
314     }
315
316     Feature odf;
317     odf.hasTimestamp = false;
318     odf.values.push_back(aubio_onset_get_descriptor(m_onsetdet));
319     returnFeatures[1].push_back(odf);
320
321     Feature todf;
322     todf.hasTimestamp = false;
323     todf.values.push_back(aubio_onset_get_thresholded_descriptor(m_onsetdet));
324     returnFeatures[2].push_back(todf);
325
326     return returnFeatures;
327 }
328
329 Onset::FeatureSet
330 Onset::getRemainingFeatures()
331 {
332     return FeatureSet();
333 }
334