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