8d91587f089e49e9e1f906f317aa54d66205c8bb
[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     m_onsetdet = new_aubio_onset
95         (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
96          blockSize,
97          stepSize,
98          lrintf(m_inputSampleRate));
99     
100     aubio_onset_set_threshold(m_onsetdet, m_threshold);
101     aubio_onset_set_silence(m_onsetdet, m_silence);
102     aubio_onset_set_minioi(m_onsetdet, m_minioi);
103
104     m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
105                                              lrintf(m_inputSampleRate));
106
107     m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
108
109     return true;
110 }
111
112 void
113 Onset::reset()
114 {
115 }
116
117 size_t
118 Onset::getPreferredStepSize() const
119 {
120     return 512;
121 }
122
123 size_t
124 Onset::getPreferredBlockSize() const
125 {
126     return 2 * getPreferredStepSize();
127 }
128
129 Onset::ParameterList
130 Onset::getParameterDescriptors() const
131 {
132     ParameterList list;
133     
134     ParameterDescriptor desc;
135     desc.identifier = "onsettype";
136     desc.name = "Onset Detection Function Type";
137     desc.minValue = 0;
138     desc.maxValue = 7;
139     desc.defaultValue = (int)OnsetComplex;
140     desc.isQuantized = true;
141     desc.quantizeStep = 1;
142     desc.valueNames.push_back("Energy Based");
143     desc.valueNames.push_back("Spectral Difference");
144     desc.valueNames.push_back("High-Frequency Content");
145     desc.valueNames.push_back("Complex Domain");
146     desc.valueNames.push_back("Phase Deviation");
147     desc.valueNames.push_back("Kullback-Liebler");
148     desc.valueNames.push_back("Modified Kullback-Liebler");
149     desc.valueNames.push_back("Spectral Flux");
150     list.push_back(desc);
151
152     desc = ParameterDescriptor();
153     desc.identifier = "peakpickthreshold";
154     desc.name = "Peak Picker Threshold";
155     desc.minValue = 0;
156     desc.maxValue = 1;
157     desc.defaultValue = 0.3;
158     desc.isQuantized = false;
159     list.push_back(desc);
160
161     desc = ParameterDescriptor();
162     desc.identifier = "silencethreshold";
163     desc.name = "Silence Threshold";
164     desc.minValue = -120;
165     desc.maxValue = 0;
166     desc.defaultValue = -70;
167     desc.unit = "dB";
168     desc.isQuantized = false;
169     list.push_back(desc);
170
171     desc = ParameterDescriptor();
172     desc.identifier = "minioi";
173     desc.name = "Minimum Inter-Onset Interval";
174     desc.minValue = 0;
175     desc.maxValue = 40;
176     desc.defaultValue = 4;
177     desc.unit = "ms";
178     desc.isQuantized = true;
179     desc.quantizeStep = 1;
180     list.push_back(desc);
181
182     return list;
183 }
184
185 float
186 Onset::getParameter(std::string param) const
187 {
188     if (param == "onsettype") {
189         return m_onsettype;
190     } else if (param == "peakpickthreshold") {
191         return m_threshold;
192     } else if (param == "silencethreshold") {
193         return m_silence;
194     } else if (param == "minioi") {
195         return m_minioi;
196     } else {
197         return 0.0;
198     }
199 }
200
201 void
202 Onset::setParameter(std::string param, float value)
203 {
204     if (param == "onsettype") {
205         switch (lrintf(value)) {
206         case 0: m_onsettype = OnsetEnergy; break;
207         case 1: m_onsettype = OnsetSpecDiff; break;
208         case 2: m_onsettype = OnsetHFC; break;
209         case 3: m_onsettype = OnsetComplex; break;
210         case 4: m_onsettype = OnsetPhase; break;
211         case 5: m_onsettype = OnsetKL; break;
212         case 6: m_onsettype = OnsetMKL; break;
213         case 7: m_onsettype = OnsetSpecFlux; break;
214         }
215     } else if (param == "peakpickthreshold") {
216         m_threshold = value;
217     } else if (param == "silencethreshold") {
218         m_silence = value;
219     } else if (param == "minioi") {
220         m_minioi = value;
221     }
222 }
223
224 Onset::OutputList
225 Onset::getOutputDescriptors() const
226 {
227     OutputList list;
228
229     OutputDescriptor d;
230     d.identifier = "onsets";
231     d.name = "Onsets";
232     d.unit = "";
233     d.hasFixedBinCount = true;
234     d.binCount = 0;
235     d.sampleType = OutputDescriptor::VariableSampleRate;
236     d.sampleRate = 0;
237     list.push_back(d);
238
239     d = OutputDescriptor();
240     d.identifier = "detectionfunction";
241     d.name = "Onset Detection Function";
242     d.unit = "";
243     d.hasFixedBinCount = true;
244     d.binCount = 1;
245     d.hasKnownExtents = false;
246     d.isQuantized = false;
247     d.sampleType = OutputDescriptor::OneSamplePerStep;
248     list.push_back(d);
249
250     return list;
251 }
252
253 Onset::FeatureSet
254 Onset::process(const float *const *inputBuffers,
255                Vamp::RealTime timestamp)
256 {
257     for (size_t i = 0; i < m_stepSize; ++i) {
258         fvec_write_sample(m_ibuf, inputBuffers[0][i], i);
259     }
260
261     aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
262
263     bool isonset = m_onset->data[0];
264
265     FeatureSet returnFeatures;
266
267     if (isonset) {
268         if (timestamp - m_lastOnset >= m_delay) {
269             Feature onsettime;
270             onsettime.hasTimestamp = true;
271             if (timestamp < m_delay) timestamp = m_delay;
272             onsettime.timestamp = timestamp - m_delay;
273             returnFeatures[0].push_back(onsettime);
274             m_lastOnset = timestamp;
275         }
276     }
277 /*!!! todo!
278     Feature feature;
279     for (size_t j = 0; j < m_channelCount; ++j) {
280         feature.values.push_back(m_onset->data[j][0]);
281     }
282     returnFeatures[1].push_back(feature);
283 */
284     return returnFeatures;
285 }
286
287 Onset::FeatureSet
288 Onset::getRemainingFeatures()
289 {
290     return FeatureSet();
291 }
292