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