vamp-aubio.cat: added Mfcc
[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 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 Onset::Onset(float inputSampleRate) :
26     Plugin(inputSampleRate),
27     m_ibuf(0),
28     m_onset(0),
29     m_onsetdet(0),
30     m_onsettype(OnsetComplex),
31     m_threshold(0.3),
32     m_silence(-70),
33     m_minioi(4)
34 {
35 }
36
37 Onset::~Onset()
38 {
39     if (m_onsetdet) del_aubio_onset(m_onsetdet);
40     if (m_ibuf) del_fvec(m_ibuf);
41     if (m_onset) del_fvec(m_onset);
42 }
43
44 string
45 Onset::getIdentifier() const
46 {
47     return "aubioonset";
48 }
49
50 string
51 Onset::getName() const
52 {
53     return "Aubio Onset Detector";
54 }
55
56 string
57 Onset::getDescription() const
58 {
59     return "Estimate note onset times";
60 }
61
62 string
63 Onset::getMaker() const
64 {
65     return "Paul Brossier (plugin by Chris Cannam)";
66 }
67
68 int
69 Onset::getPluginVersion() const
70 {
71     return 2;
72 }
73
74 string
75 Onset::getCopyright() const
76 {
77     return "GPL";
78 }
79
80 bool
81 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
82 {
83     if (channels != 1) {
84         std::cerr << "Onset::initialise: channels must be 1" << std::endl;
85         return false;
86     }
87
88     m_stepSize = stepSize;
89     m_blockSize = blockSize;
90
91     m_ibuf = new_fvec(stepSize);
92     m_onset = new_fvec(1);
93
94     reset();
95
96     return true;
97 }
98
99 void
100 Onset::reset()
101 {
102     if (m_onsetdet) del_aubio_onset(m_onsetdet);
103
104     m_onsetdet = new_aubio_onset
105         (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
106          m_blockSize,
107          m_stepSize,
108          lrintf(m_inputSampleRate));
109     
110     aubio_onset_set_threshold(m_onsetdet, m_threshold);
111     aubio_onset_set_silence(m_onsetdet, m_silence);
112     aubio_onset_set_minioi(m_onsetdet, m_minioi);
113
114     m_delay = Vamp::RealTime::frame2RealTime(4 * m_stepSize,
115                                              lrintf(m_inputSampleRate));
116
117     m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
118 }
119
120 size_t
121 Onset::getPreferredStepSize() const
122 {
123     return 512;
124 }
125
126 size_t
127 Onset::getPreferredBlockSize() const
128 {
129     return 2 * getPreferredStepSize();
130 }
131
132 Onset::ParameterList
133 Onset::getParameterDescriptors() const
134 {
135     ParameterList list;
136     
137     ParameterDescriptor desc;
138     desc.identifier = "onsettype";
139     desc.name = "Onset Detection Function Type";
140     desc.description = "Type of onset detection function to use";
141     desc.minValue = 0;
142     desc.maxValue = 7;
143     desc.defaultValue = (int)OnsetComplex;
144     desc.isQuantized = true;
145     desc.quantizeStep = 1;
146     desc.valueNames.push_back("Energy Based");
147     desc.valueNames.push_back("Spectral Difference");
148     desc.valueNames.push_back("High-Frequency Content");
149     desc.valueNames.push_back("Complex Domain");
150     desc.valueNames.push_back("Phase Deviation");
151     desc.valueNames.push_back("Kullback-Liebler");
152     desc.valueNames.push_back("Modified Kullback-Liebler");
153     desc.valueNames.push_back("Spectral Flux");
154     list.push_back(desc);
155
156     desc = ParameterDescriptor();
157     desc.identifier = "peakpickthreshold";
158     desc.name = "Peak Picker Threshold";
159     desc.description = "Threshold used for peak picking, the higher the more detections";
160     desc.minValue = 0;
161     desc.maxValue = 1;
162     desc.defaultValue = 0.3;
163     desc.isQuantized = false;
164     list.push_back(desc);
165
166     desc = ParameterDescriptor();
167     desc.identifier = "silencethreshold";
168     desc.name = "Silence Threshold";
169     desc.description = "Silence threshold, the higher the least detection";
170     desc.minValue = -120;
171     desc.maxValue = 0;
172     desc.defaultValue = -70;
173     desc.unit = "dB";
174     desc.isQuantized = false;
175     list.push_back(desc);
176
177     desc = ParameterDescriptor();
178     desc.identifier = "minioi";
179     desc.name = "Minimum Inter-Onset Interval";
180     desc.description = "Time interval below which two consecutive onsets should be merged";
181     desc.minValue = 0;
182     desc.maxValue = 40;
183     desc.defaultValue = 4;
184     desc.unit = "ms";
185     desc.isQuantized = true;
186     desc.quantizeStep = 1;
187     list.push_back(desc);
188
189     return list;
190 }
191
192 float
193 Onset::getParameter(std::string param) const
194 {
195     if (param == "onsettype") {
196         return m_onsettype;
197     } else if (param == "peakpickthreshold") {
198         return m_threshold;
199     } else if (param == "silencethreshold") {
200         return m_silence;
201     } else if (param == "minioi") {
202         return m_minioi;
203     } else {
204         return 0.0;
205     }
206 }
207
208 void
209 Onset::setParameter(std::string param, float value)
210 {
211     if (param == "onsettype") {
212         switch (lrintf(value)) {
213         case 0: m_onsettype = OnsetEnergy; break;
214         case 1: m_onsettype = OnsetSpecDiff; break;
215         case 2: m_onsettype = OnsetHFC; break;
216         case 3: m_onsettype = OnsetComplex; break;
217         case 4: m_onsettype = OnsetPhase; break;
218         case 5: m_onsettype = OnsetKL; break;
219         case 6: m_onsettype = OnsetMKL; break;
220         case 7: m_onsettype = OnsetSpecFlux; break;
221         }
222     } else if (param == "peakpickthreshold") {
223         m_threshold = value;
224     } else if (param == "silencethreshold") {
225         m_silence = value;
226     } else if (param == "minioi") {
227         m_minioi = value;
228     }
229 }
230
231 Onset::OutputList
232 Onset::getOutputDescriptors() const
233 {
234     OutputList list;
235
236     OutputDescriptor d;
237     d.identifier = "onsets";
238     d.name = "Onsets";
239     d.description = "List of times at which a note onset was detected";
240     d.unit = "";
241     d.hasFixedBinCount = true;
242     d.binCount = 0;
243     d.sampleType = OutputDescriptor::VariableSampleRate;
244     d.sampleRate = 0;
245     list.push_back(d);
246
247     d.identifier = "odf";
248     d.name = "Onset detection function";
249     d.description = "Output of the onset detection function";
250     d.binCount = 1;
251     d.isQuantized = true;
252     d.quantizeStep = 1.0;
253     d.sampleType = OutputDescriptor::OneSamplePerStep;
254     list.push_back(d);
255
256     d.identifier = "todf";
257     d.name = "Thresholded Onset detection function";
258     d.description = "Output of the thresholded onset detection function";
259     d.binCount = 1;
260     d.isQuantized = true;
261     d.quantizeStep = 1.0;
262     d.sampleType = OutputDescriptor::OneSamplePerStep;
263     list.push_back(d);
264
265     return list;
266 }
267
268 Onset::FeatureSet
269 Onset::process(const float *const *inputBuffers,
270                Vamp::RealTime timestamp)
271 {
272     for (size_t i = 0; i < m_stepSize; ++i) {
273         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
274     }
275
276     aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
277
278     bool isonset = m_onset->data[0];
279
280     FeatureSet returnFeatures;
281
282     if (isonset) {
283         if (timestamp - m_lastOnset >= m_delay) {
284             Feature onsettime;
285             onsettime.hasTimestamp = true;
286             if (timestamp < m_delay) timestamp = m_delay;
287             onsettime.timestamp = timestamp - m_delay;
288             returnFeatures[0].push_back(onsettime);
289             m_lastOnset = timestamp;
290         }
291     }
292
293     Feature odf;
294     odf.hasTimestamp = false;
295     odf.values.push_back(aubio_onset_get_descriptor(m_onsetdet));
296     returnFeatures[1].push_back(odf);
297
298     Feature todf;
299     todf.hasTimestamp = false;
300     todf.values.push_back(aubio_onset_get_thresholded_descriptor(m_onsetdet));
301     returnFeatures[2].push_back(todf);
302
303     return returnFeatures;
304 }
305
306 Onset::FeatureSet
307 Onset::getRemainingFeatures()
308 {
309     return FeatureSet();
310 }
311