(Start to) remove the channel counts from everywhere: they should always be 1 anyway...
[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(aubio_onset_complex),
34     m_threshold(0.3),
35     m_silence(-90)
36 {
37 }
38
39 Onset::~Onset()
40 {
41     if (m_onsetdet) aubio_onsetdetection_free(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(m_threshold);
101
102     m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize);
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 = 6;
139     desc.defaultValue = (int)aubio_onset_complex;
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     list.push_back(desc);
150
151     desc = ParameterDescriptor();
152     desc.identifier = "peakpickthreshold";
153     desc.name = "Peak Picker Threshold";
154     desc.minValue = 0;
155     desc.maxValue = 1;
156     desc.defaultValue = 0.3;
157     desc.isQuantized = false;
158     list.push_back(desc);
159
160     desc = ParameterDescriptor();
161     desc.identifier = "silencethreshold";
162     desc.name = "Silence Threshold";
163     desc.minValue = -120;
164     desc.maxValue = 0;
165     desc.defaultValue = -90;
166     desc.unit = "dB";
167     desc.isQuantized = false;
168     list.push_back(desc);
169
170     return list;
171 }
172
173 float
174 Onset::getParameter(std::string param) const
175 {
176     if (param == "onsettype") {
177         return m_onsettype;
178     } else if (param == "peakpickthreshold") {
179         return m_threshold;
180     } else if (param == "silencethreshold") {
181         return m_silence;
182     } else {
183         return 0.0;
184     }
185 }
186
187 void
188 Onset::setParameter(std::string param, float value)
189 {
190     if (param == "onsettype") {
191         switch (lrintf(value)) {
192         case 0: m_onsettype = aubio_onset_energy; break;
193         case 1: m_onsettype = aubio_onset_specdiff; break;
194         case 2: m_onsettype = aubio_onset_hfc; break;
195         case 3: m_onsettype = aubio_onset_complex; break;
196         case 4: m_onsettype = aubio_onset_phase; break;
197         case 5: m_onsettype = aubio_onset_kl; break;
198         case 6: m_onsettype = aubio_onset_mkl; break;
199         }
200     } else if (param == "peakpickthreshold") {
201         m_threshold = value;
202     } else if (param == "silencethreshold") {
203         m_silence = value;
204     }
205 }
206
207 Onset::OutputList
208 Onset::getOutputDescriptors() const
209 {
210     OutputList list;
211
212     OutputDescriptor d;
213     d.identifier = "onsets";
214     d.name = "Onsets";
215     d.unit = "";
216     d.hasFixedBinCount = true;
217     d.binCount = 0;
218     d.sampleType = OutputDescriptor::VariableSampleRate;
219     d.sampleRate = 0;
220     list.push_back(d);
221
222     d = OutputDescriptor();
223     d.identifier = "detectionfunction";
224     d.name = "Onset Detection Function";
225     d.unit = "";
226     d.hasFixedBinCount = true;
227     d.binCount = 1;
228     d.hasKnownExtents = false;
229     d.isQuantized = false;
230     d.sampleType = OutputDescriptor::OneSamplePerStep;
231     list.push_back(d);
232
233     return list;
234 }
235
236 Onset::FeatureSet
237 Onset::process(const float *const *inputBuffers,
238                Vamp::RealTime timestamp)
239 {
240     for (size_t i = 0; i < m_stepSize; ++i) {
241         for (size_t j = 0; j < m_channelCount; ++j) {
242             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
243         }
244     }
245
246     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
247     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
248
249     bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
250
251     if (isonset) {
252         if (aubio_silence_detection(m_ibuf, m_silence)) {
253             isonset = false;
254         }
255     }
256
257     FeatureSet returnFeatures;
258
259     if (isonset) {
260         if (timestamp - m_lastOnset >= m_delay) {
261             Feature onsettime;
262             onsettime.hasTimestamp = true;
263             if (timestamp < m_delay) timestamp = m_delay;
264             onsettime.timestamp = timestamp - m_delay;
265             returnFeatures[0].push_back(onsettime);
266             m_lastOnset = timestamp;
267         }
268     }
269     Feature feature;
270     for (size_t j = 0; j < m_channelCount; ++j) {
271         feature.values.push_back(m_onset->data[j][0]);
272     }
273     returnFeatures[1].push_back(feature);
274
275     return returnFeatures;
276 }
277
278 Onset::FeatureSet
279 Onset::getRemainingFeatures()
280 {
281     return FeatureSet();
282 }
283