vamp-aubio.n3: update links and download locations, patch from Chris Cannam
[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(OnsetComplex),
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)OnsetComplex;
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     list.push_back(desc);
164
165     desc = ParameterDescriptor();
166     desc.identifier = "peakpickthreshold";
167     desc.name = "Peak Picker Threshold";
168     desc.description = "Threshold used for peak picking, the higher the more detections";
169     desc.minValue = 0;
170     desc.maxValue = 1;
171     desc.defaultValue = 0.3;
172     desc.isQuantized = false;
173     list.push_back(desc);
174
175     desc = ParameterDescriptor();
176     desc.identifier = "silencethreshold";
177     desc.name = "Silence Threshold";
178     desc.description = "Silence threshold, the higher the least detection";
179     desc.minValue = -120;
180     desc.maxValue = 0;
181     desc.defaultValue = -70;
182     desc.unit = "dB";
183     desc.isQuantized = false;
184     list.push_back(desc);
185
186     desc = ParameterDescriptor();
187     desc.identifier = "minioi";
188     desc.name = "Minimum Inter-Onset Interval";
189     desc.description = "Time interval below which two consecutive onsets should be merged";
190     desc.minValue = 0;
191     desc.maxValue = 40;
192     desc.defaultValue = 4;
193     desc.unit = "ms";
194     desc.isQuantized = true;
195     desc.quantizeStep = 1;
196     list.push_back(desc);
197
198     return list;
199 }
200
201 float
202 Onset::getParameter(std::string param) const
203 {
204     if (param == "onsettype") {
205         return m_onsettype;
206     } else if (param == "peakpickthreshold") {
207         return m_threshold;
208     } else if (param == "silencethreshold") {
209         return m_silence;
210     } else if (param == "minioi") {
211         return m_minioi;
212     } else {
213         return 0.0;
214     }
215 }
216
217 void
218 Onset::setParameter(std::string param, float value)
219 {
220     if (param == "onsettype") {
221         switch (lrintf(value)) {
222         case 0: m_onsettype = OnsetEnergy; break;
223         case 1: m_onsettype = OnsetSpecDiff; break;
224         case 2: m_onsettype = OnsetHFC; break;
225         case 3: m_onsettype = OnsetComplex; break;
226         case 4: m_onsettype = OnsetPhase; break;
227         case 5: m_onsettype = OnsetKL; break;
228         case 6: m_onsettype = OnsetMKL; break;
229         case 7: m_onsettype = OnsetSpecFlux; break;
230         }
231     } else if (param == "peakpickthreshold") {
232         m_threshold = value;
233     } else if (param == "silencethreshold") {
234         m_silence = value;
235     } else if (param == "minioi") {
236         m_minioi = value;
237     }
238 }
239
240 Onset::OutputList
241 Onset::getOutputDescriptors() const
242 {
243     OutputList list;
244
245     OutputDescriptor d;
246     d.identifier = "onsets";
247     d.name = "Onsets";
248     d.description = "List of times at which a note onset was detected";
249     d.unit = "";
250     d.hasFixedBinCount = true;
251     d.binCount = 0;
252     d.sampleType = OutputDescriptor::VariableSampleRate;
253     d.sampleRate = 0;
254     list.push_back(d);
255
256     d.identifier = "odf";
257     d.name = "Onset detection function";
258     d.description = "Output of the 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     d.identifier = "todf";
266     d.name = "Thresholded Onset detection function";
267     d.description = "Output of the thresholded onset detection function";
268     d.binCount = 1;
269     d.isQuantized = true;
270     d.quantizeStep = 1.0;
271     d.sampleType = OutputDescriptor::OneSamplePerStep;
272     list.push_back(d);
273
274     return list;
275 }
276
277 Onset::FeatureSet
278 Onset::process(const float *const *inputBuffers,
279                Vamp::RealTime timestamp)
280 {
281     for (size_t i = 0; i < m_stepSize; ++i) {
282         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
283     }
284
285     aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
286
287     bool isonset = m_onset->data[0];
288
289     FeatureSet returnFeatures;
290
291     if (isonset) {
292         if (timestamp - m_lastOnset >= m_delay) {
293             Feature onsettime;
294             onsettime.hasTimestamp = true;
295             if (timestamp < m_delay) timestamp = m_delay;
296             onsettime.timestamp = timestamp - m_delay;
297             returnFeatures[0].push_back(onsettime);
298             m_lastOnset = timestamp;
299         }
300     }
301
302     Feature odf;
303     odf.hasTimestamp = false;
304     odf.values.push_back(aubio_onset_get_descriptor(m_onsetdet));
305     returnFeatures[1].push_back(odf);
306
307     Feature todf;
308     todf.hasTimestamp = false;
309     todf.values.push_back(aubio_onset_get_thresholded_descriptor(m_onsetdet));
310     returnFeatures[2].push_back(todf);
311
312     return returnFeatures;
313 }
314
315 Onset::FeatureSet
316 Onset::getRemainingFeatures()
317 {
318     return FeatureSet();
319 }
320