INSTALL.mingw32: build from current tree
[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 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 "Tempo.h"
19
20 using std::string;
21 using std::vector;
22 using std::cerr;
23 using std::endl;
24
25 Tempo::Tempo(float inputSampleRate) :
26     Plugin(inputSampleRate),
27     m_ibuf(0),
28     m_beat(0),
29     m_bpm(0),
30     m_onsettype(OnsetComplex),
31     m_tempo(0),
32     m_threshold(0.3),
33     m_silence(-70)
34 {
35 }
36
37 Tempo::~Tempo()
38 {
39     if (m_ibuf) del_fvec(m_ibuf);
40     if (m_beat) del_fvec(m_beat);
41     if (m_tempo) del_aubio_tempo(m_tempo);
42 }
43
44 string
45 Tempo::getIdentifier() const
46 {
47     return "aubiotempo";
48 }
49
50 string
51 Tempo::getName() const
52 {
53     return "Aubio Beat Tracker";
54 }
55
56 string
57 Tempo::getDescription() const
58 {
59     return "Estimate the musical tempo and track beat positions";
60 }
61
62 string
63 Tempo::getMaker() const
64 {
65     return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
66 }
67
68 int
69 Tempo::getPluginVersion() const
70 {
71     return 2;
72 }
73
74 string
75 Tempo::getCopyright() const
76 {
77     return "GPL";
78 }
79
80 bool
81 Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
82 {
83     if (channels != 1) {
84         std::cerr << "Tempo::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_beat = new_fvec(2);
93     
94     m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
95                                              lrintf(m_inputSampleRate));
96
97     reset();
98
99     return true;
100 }
101
102 void
103 Tempo::reset()
104 {
105     if (m_tempo) del_aubio_tempo(m_tempo);
106
107     m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
108
109     m_tempo = new_aubio_tempo
110         (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
111          m_blockSize,
112          m_stepSize,
113          lrintf(m_inputSampleRate));
114
115     aubio_tempo_set_silence(m_tempo, m_silence);
116     aubio_tempo_set_threshold(m_tempo, m_threshold);
117 }
118
119 size_t
120 Tempo::getPreferredStepSize() const
121 {
122     return 512;
123 }
124
125 size_t
126 Tempo::getPreferredBlockSize() const
127 {
128     return 2 * getPreferredStepSize();
129 }
130
131 Tempo::ParameterList
132 Tempo::getParameterDescriptors() const
133 {
134     ParameterList list;
135     
136     ParameterDescriptor desc;
137     desc.identifier = "onsettype";
138     desc.name = "Onset Detection Function Type";
139     desc.description = "Type of onset detection function to use";
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.description = "Peak picking threshold, the higher the least detection";
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.description = "Silence threshold, the higher the least detection";
169     desc.minValue = -120;
170     desc.maxValue = 0;
171     desc.defaultValue = -70;
172     desc.unit = "dB";
173     desc.isQuantized = false;
174     list.push_back(desc);
175
176     return list;
177 }
178
179 float
180 Tempo::getParameter(std::string param) const
181 {
182     if (param == "onsettype") {
183         return m_onsettype;
184     } else if (param == "peakpickthreshold") {
185         return m_threshold;
186     } else if (param == "silencethreshold") {
187         return m_silence;
188     } else {
189         return 0.0;
190     }
191 }
192
193 void
194 Tempo::setParameter(std::string param, float value)
195 {
196     if (param == "onsettype") {
197         switch (lrintf(value)) {
198         case 0: m_onsettype = OnsetEnergy; break;
199         case 1: m_onsettype = OnsetSpecDiff; break;
200         case 2: m_onsettype = OnsetHFC; break;
201         case 3: m_onsettype = OnsetComplex; break;
202         case 4: m_onsettype = OnsetPhase; break;
203         case 5: m_onsettype = OnsetKL; break;
204         case 6: m_onsettype = OnsetMKL; break;
205         case 7: m_onsettype = OnsetSpecFlux; break;
206         }
207     } else if (param == "peakpickthreshold") {
208         m_threshold = value;
209     } else if (param == "silencethreshold") {
210         m_silence = value;
211     }
212 }
213
214 Tempo::OutputList
215 Tempo::getOutputDescriptors() const
216 {
217     OutputList list;
218
219     OutputDescriptor d;
220     d.identifier = "beats";
221     d.name = "Beats";
222     d.description = "List of times at which a beat was detected";
223     d.unit = "";
224     d.hasFixedBinCount = true;
225     d.binCount = 0;
226     d.sampleType = OutputDescriptor::VariableSampleRate;
227     d.sampleRate = 0;
228     list.push_back(d);
229
230     d.identifier = "tempo";
231     d.name = "Tempo";
232     d.description = "Overall estimated tempo";
233     d.unit = "bpm";
234     d.hasFixedBinCount = true;
235     d.binCount = 1;
236     d.hasKnownExtents = false;
237     d.isQuantized = false;
238     d.sampleType = OutputDescriptor::OneSamplePerStep;
239     list.push_back(d);
240
241     return list;
242 }
243
244 Tempo::FeatureSet
245 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
246 {
247     for (size_t i = 0; i < m_stepSize; ++i) {
248         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
249     }
250
251     aubio_tempo_do(m_tempo, m_ibuf, m_beat);
252
253     bool istactus = m_beat->data[0];
254
255     m_bpm = aubio_tempo_get_bpm(m_tempo);
256
257     FeatureSet returnFeatures;
258
259     if (istactus == true) {
260         if (timestamp - m_lastBeat >= 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_lastBeat = timestamp;
267         }
268     }
269
270     if (m_bpm >= 30 && m_bpm <= 206) {
271         Feature tempo;
272         tempo.hasTimestamp = false;
273         tempo.values.push_back(m_bpm);
274         returnFeatures[1].push_back(tempo);
275     }
276
277     return returnFeatures;
278 }
279
280 Tempo::FeatureSet
281 Tempo::getRemainingFeatures()
282 {
283     return FeatureSet();
284 }
285