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