* Add silence detector. But I'm not happy with its results: take a look at
[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 //#define HAVE_AUBIO_LOCKED_TEMPO_HACK
26
27 Tempo::Tempo(float inputSampleRate) :
28     Plugin(inputSampleRate),
29     m_ibuf(0),
30     m_fftgrain(0),
31     m_onset(0),
32     m_pv(0),
33     m_peakpick(0),
34     m_onsetdet(0),
35     m_onsettype(aubio_onset_specdiff),
36     m_beattracking(0),
37     m_dfframe(0),
38     m_btout(0),
39     m_btcounter(0),
40     m_threshold(0.3),
41     m_silence(-90),
42     m_channelCount(1)
43 {
44 }
45
46 Tempo::~Tempo()
47 {
48     if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
49     if (m_ibuf) del_fvec(m_ibuf);
50     if (m_onset) del_fvec(m_onset);
51     if (m_fftgrain) del_cvec(m_fftgrain);
52     if (m_pv) del_aubio_pvoc(m_pv);
53     if (m_peakpick) del_aubio_peakpicker(m_peakpick);
54     if (m_beattracking) del_aubio_beattracking(m_beattracking);
55     if (m_dfframe) del_fvec(m_dfframe);
56     if (m_btout) del_fvec(m_btout);
57 }
58
59 string
60 Tempo::getIdentifier() const
61 {
62     return "aubiotempo";
63 }
64
65 string
66 Tempo::getName() const
67 {
68     return "Aubio Tempo Detector";
69 }
70
71 string
72 Tempo::getDescription() const
73 {
74     return "Estimate the musical tempo by tracking note onset timings";
75 }
76
77 string
78 Tempo::getMaker() const
79 {
80     return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
81 }
82
83 int
84 Tempo::getPluginVersion() const
85 {
86     return 1;
87 }
88
89 string
90 Tempo::getCopyright() const
91 {
92     return "GPL";
93 }
94
95 bool
96 Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
97 {
98     m_channelCount = channels;
99     m_stepSize = stepSize;
100     m_blockSize = blockSize;
101
102     m_ibuf = new_fvec(stepSize, channels);
103     m_onset = new_fvec(1, channels);
104     m_fftgrain = new_cvec(blockSize, channels);
105     m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
106     m_peakpick = new_aubio_peakpicker(m_threshold);
107
108     m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
109     
110     m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
111                                              lrintf(m_inputSampleRate));
112
113     m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
114
115     m_winlen = 512*512/stepSize;
116     m_dfframe = new_fvec(m_winlen,channels);
117     m_btstep = m_winlen/4;
118     m_btout = new_fvec(m_btstep,channels);
119     m_beattracking = new_aubio_beattracking(m_winlen,channels);
120
121     return true;
122 }
123
124 void
125 Tempo::reset()
126 {
127 }
128
129 size_t
130 Tempo::getPreferredStepSize() const
131 {
132     return 512;
133 }
134
135 size_t
136 Tempo::getPreferredBlockSize() const
137 {
138     return 2 * getPreferredStepSize();
139 }
140
141 Tempo::ParameterList
142 Tempo::getParameterDescriptors() const
143 {
144     ParameterList list;
145     
146     ParameterDescriptor desc;
147     desc.identifier = "onsettype";
148     desc.name = "Onset Detection Function Type";
149     desc.minValue = 0;
150     desc.maxValue = 6;
151     desc.defaultValue = (int)aubio_onset_complex;
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     list.push_back(desc);
162
163     desc = ParameterDescriptor();
164     desc.identifier = "peakpickthreshold";
165     desc.name = "Peak Picker Threshold";
166     desc.minValue = 0;
167     desc.maxValue = 1;
168     desc.defaultValue = 0.3;
169     desc.isQuantized = false;
170     list.push_back(desc);
171
172     desc = ParameterDescriptor();
173     desc.identifier = "silencethreshold";
174     desc.name = "Silence Threshold";
175     desc.minValue = -120;
176     desc.maxValue = 0;
177     desc.defaultValue = -90;
178     desc.unit = "dB";
179     desc.isQuantized = false;
180     list.push_back(desc);
181
182     return list;
183 }
184
185 float
186 Tempo::getParameter(std::string param) const
187 {
188     if (param == "onsettype") {
189         return m_onsettype;
190     } else if (param == "peakpickthreshold") {
191         return m_threshold;
192     } else if (param == "silencethreshold") {
193         return m_silence;
194     } else {
195         return 0.0;
196     }
197 }
198
199 void
200 Tempo::setParameter(std::string param, float value)
201 {
202     if (param == "onsettype") {
203         switch (lrintf(value)) {
204         case 0: m_onsettype = aubio_onset_energy; break;
205         case 1: m_onsettype = aubio_onset_specdiff; break;
206         case 2: m_onsettype = aubio_onset_hfc; break;
207         case 3: m_onsettype = aubio_onset_complex; break;
208         case 4: m_onsettype = aubio_onset_phase; break;
209         case 5: m_onsettype = aubio_onset_kl; break;
210         case 6: m_onsettype = aubio_onset_mkl; break;
211         }
212     } else if (param == "peakpickthreshold") {
213         m_threshold = value;
214     } else if (param == "silencethreshold") {
215         m_silence = value;
216     }
217 }
218
219 Tempo::OutputList
220 Tempo::getOutputDescriptors() const
221 {
222     OutputList list;
223
224     OutputDescriptor d;
225     d.identifier = "beats";
226     d.name = "Beats";
227     d.unit = "";
228     d.hasFixedBinCount = true;
229     d.binCount = 0;
230     d.sampleType = OutputDescriptor::VariableSampleRate;
231     d.sampleRate = 0;
232     list.push_back(d);
233
234 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
235     d.identifier = "tempo";
236     d.name = "Tempo";
237     d.unit = "bpm";
238     d.hasFixedBinCount = true;
239     d.binCount = 1;
240     d.hasKnownExtents = false;
241     d.isQuantized = false;
242     d.sampleType = OutputDescriptor::OneSamplePerStep;
243     list.push_back(d);
244 #endif
245
246     return list;
247 }
248
249 Tempo::FeatureSet
250 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
251 {
252     for (size_t i = 0; i < m_stepSize; ++i) {
253         for (size_t j = 0; j < m_channelCount; ++j) {
254             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
255         }
256     }
257
258     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
259     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
260
261 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
262     float locked_tempo = 0;
263 #endif
264
265     if ( m_btcounter == m_btstep - 1 ) {
266 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
267         aubio_beattracking_do(m_beattracking,m_dfframe,m_btout,&locked_tempo);
268 #else
269         aubio_beattracking_do(m_beattracking,m_dfframe,m_btout);
270 #endif
271         /* rotate dfframe */
272         for (size_t i = 0 ; i < m_winlen - m_btstep; i++ ) 
273                 m_dfframe->data[0][i] = m_dfframe->data[0][i+m_btstep];
274         for (size_t i = m_winlen - m_btstep ; i < m_winlen; i++ ) 
275                 m_dfframe->data[0][i] = 0.;
276                 
277         m_btcounter = -1;
278     }
279     m_btcounter++;
280     bool isonset = aubio_peakpick_pimrt_wt( m_onset, m_peakpick, 
281         &(m_dfframe->data[0][m_winlen - m_btstep + m_btcounter]));
282     bool istactus = 0;
283
284     /* check if any of the predicted beat correspond to the current time */
285     for (size_t i = 1; i < m_btout->data[0][0]; i++ ) { 
286             if (m_btcounter == m_btout->data[0][i]) {
287                     if (aubio_silence_detection(m_ibuf, m_silence)) {
288                             isonset  = false;
289                             istactus = false;
290                     } else {
291                             istactus = true;
292                     }
293             }
294     }
295
296     FeatureSet returnFeatures;
297
298     if (istactus == true) {
299         if (timestamp - m_lastBeat >= m_delay) {
300             Feature onsettime;
301             onsettime.hasTimestamp = true;
302             if (timestamp < m_delay) timestamp = m_delay;
303             onsettime.timestamp = timestamp - m_delay;
304             returnFeatures[0].push_back(onsettime);
305             m_lastBeat = timestamp;
306         }
307     }
308
309 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
310     if (locked_tempo >= 30 && locked_tempo <= 206) {
311         if (locked_tempo > 145) locked_tempo /= 2;
312         std::cerr << "Locked tempo: " << locked_tempo << std::endl;
313         Feature tempo;
314         tempo.hasTimestamp = false;
315         tempo.values.push_back(locked_tempo);
316         returnFeatures[1].push_back(tempo);
317     }
318 #endif
319
320     return returnFeatures;
321 }
322
323 Tempo::FeatureSet
324 Tempo::getRemainingFeatures()
325 {
326     return FeatureSet();
327 }
328