plugins/Onset.cpp: improve parameters handling, use same defaults as aubioonset
[vamp-aubio-plugins.git] / plugins / Tempo.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 "Tempo.h"
28
29 using std::string;
30 using std::vector;
31 using std::cerr;
32 using std::endl;
33
34 Tempo::Tempo(float inputSampleRate) :
35     Plugin(inputSampleRate),
36     m_ibuf(0),
37     m_beat(0),
38     m_bpm(0),
39     m_onsettype(OnsetComplex),
40     m_tempo(0),
41     m_threshold(0.3),
42     m_silence(-70)
43 {
44 }
45
46 Tempo::~Tempo()
47 {
48     if (m_ibuf) del_fvec(m_ibuf);
49     if (m_beat) del_fvec(m_beat);
50     if (m_tempo) del_aubio_tempo(m_tempo);
51 }
52
53 string
54 Tempo::getIdentifier() const
55 {
56     return "aubiotempo";
57 }
58
59 string
60 Tempo::getName() const
61 {
62     return "Aubio Beat Tracker";
63 }
64
65 string
66 Tempo::getDescription() const
67 {
68     return "Estimate the musical tempo and track beat positions";
69 }
70
71 string
72 Tempo::getMaker() const
73 {
74     return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
75 }
76
77 int
78 Tempo::getPluginVersion() const
79 {
80     return 2;
81 }
82
83 string
84 Tempo::getCopyright() const
85 {
86     return "GPL";
87 }
88
89 bool
90 Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
91 {
92     if (channels != 1) {
93         std::cerr << "Tempo::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_beat = new_fvec(2);
102     
103     m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
104                                              lrintf(m_inputSampleRate));
105
106     reset();
107
108     return true;
109 }
110
111 void
112 Tempo::reset()
113 {
114     if (m_tempo) del_aubio_tempo(m_tempo);
115
116     m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
117
118     m_tempo = new_aubio_tempo
119         (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
120          m_blockSize,
121          m_stepSize,
122          lrintf(m_inputSampleRate));
123
124     aubio_tempo_set_silence(m_tempo, m_silence);
125     aubio_tempo_set_threshold(m_tempo, m_threshold);
126 }
127
128 size_t
129 Tempo::getPreferredStepSize() const
130 {
131     return 512;
132 }
133
134 size_t
135 Tempo::getPreferredBlockSize() const
136 {
137     return 2 * getPreferredStepSize();
138 }
139
140 Tempo::ParameterList
141 Tempo::getParameterDescriptors() const
142 {
143     ParameterList list;
144     
145     ParameterDescriptor desc;
146     desc.identifier = "onsettype";
147     desc.name = "Onset Detection Function Type";
148     desc.description = "Type of onset detection function to use";
149     desc.minValue = 0;
150     desc.maxValue = 7;
151     desc.defaultValue = (int)OnsetComplex;
152     desc.isQuantized = true;
153     desc.quantizeStep = 1;
154     desc.valueNames.push_back("Energy Based");
155     desc.valueNames.push_back("Spectral Difference");
156     desc.valueNames.push_back("High-Frequency Content");
157     desc.valueNames.push_back("Complex Domain");
158     desc.valueNames.push_back("Phase Deviation");
159     desc.valueNames.push_back("Kullback-Liebler");
160     desc.valueNames.push_back("Modified Kullback-Liebler");
161     desc.valueNames.push_back("Spectral Flux");
162     list.push_back(desc);
163
164     desc = ParameterDescriptor();
165     desc.identifier = "peakpickthreshold";
166     desc.name = "Peak Picker Threshold";
167     desc.description = "Peak picking threshold, the higher the least detection";
168     desc.minValue = 0;
169     desc.maxValue = 1;
170     desc.defaultValue = 0.3;
171     desc.isQuantized = false;
172     list.push_back(desc);
173
174     desc = ParameterDescriptor();
175     desc.identifier = "silencethreshold";
176     desc.name = "Silence Threshold";
177     desc.description = "Silence threshold, the higher the least detection";
178     desc.minValue = -120;
179     desc.maxValue = 0;
180     desc.defaultValue = -70;
181     desc.unit = "dB";
182     desc.isQuantized = false;
183     list.push_back(desc);
184
185     return list;
186 }
187
188 float
189 Tempo::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 {
198         return 0.0;
199     }
200 }
201
202 void
203 Tempo::setParameter(std::string param, float value)
204 {
205     if (param == "onsettype") {
206         switch (lrintf(value)) {
207         case 0: m_onsettype = OnsetEnergy; break;
208         case 1: m_onsettype = OnsetSpecDiff; break;
209         case 2: m_onsettype = OnsetHFC; break;
210         case 3: m_onsettype = OnsetComplex; break;
211         case 4: m_onsettype = OnsetPhase; break;
212         case 5: m_onsettype = OnsetKL; break;
213         case 6: m_onsettype = OnsetMKL; break;
214         case 7: m_onsettype = OnsetSpecFlux; break;
215         }
216     } else if (param == "peakpickthreshold") {
217         m_threshold = value;
218     } else if (param == "silencethreshold") {
219         m_silence = value;
220     }
221 }
222
223 Tempo::OutputList
224 Tempo::getOutputDescriptors() const
225 {
226     OutputList list;
227
228     OutputDescriptor d;
229     d.identifier = "beats";
230     d.name = "Beats";
231     d.description = "List of times at which a beat was detected";
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.identifier = "tempo";
240     d.name = "Tempo";
241     d.description = "Overall estimated tempo";
242     d.unit = "bpm";
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 Tempo::FeatureSet
254 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
255 {
256     for (size_t i = 0; i < m_stepSize; ++i) {
257         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
258     }
259
260     aubio_tempo_do(m_tempo, m_ibuf, m_beat);
261
262     bool istactus = m_beat->data[0];
263
264     m_bpm = aubio_tempo_get_bpm(m_tempo);
265
266     FeatureSet returnFeatures;
267
268     if (istactus == true) {
269         if (timestamp - m_lastBeat >= m_delay) {
270             Feature onsettime;
271             onsettime.hasTimestamp = true;
272             if (timestamp < m_delay) timestamp = m_delay;
273             onsettime.timestamp = timestamp - m_delay;
274             returnFeatures[0].push_back(onsettime);
275             m_lastBeat = timestamp;
276         }
277     }
278
279     if (m_bpm >= 30 && m_bpm <= 206) {
280         Feature tempo;
281         tempo.hasTimestamp = false;
282         tempo.values.push_back(m_bpm);
283         returnFeatures[1].push_back(tempo);
284     }
285
286     return returnFeatures;
287 }
288
289 Tempo::FeatureSet
290 Tempo::getRemainingFeatures()
291 {
292     return FeatureSet();
293 }
294