81c4a4968c5c541f919f387bc232e21eba6ca79f
[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::getName() const
61 {
62     return "aubiotempo";
63 }
64
65 string
66 Tempo::getDescription() const
67 {
68     return "Aubio Tempo Detector";
69 }
70
71 string
72 Tempo::getMaker() const
73 {
74     return "Paul Brossier (plugin by Chris Cannam)";
75 }
76
77 int
78 Tempo::getPluginVersion() const
79 {
80     return 1;
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     m_channelCount = channels;
93     m_stepSize = stepSize;
94     m_blockSize = blockSize;
95
96     m_ibuf = new_fvec(stepSize, channels);
97     m_onset = new_fvec(1, channels);
98     m_fftgrain = new_cvec(blockSize, channels);
99     m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
100     m_peakpick = new_aubio_peakpicker(m_threshold);
101
102     m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
103     
104     m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
105                                              lrintf(m_inputSampleRate));
106
107     m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
108
109     m_winlen = 512*512/stepSize;
110     m_dfframe = new_fvec(m_winlen,channels);
111     m_btstep = m_winlen/4;
112     m_btout = new_fvec(m_btstep,channels);
113     m_beattracking = new_aubio_beattracking(m_winlen,channels);
114
115     return true;
116 }
117
118 void
119 Tempo::reset()
120 {
121 }
122
123 size_t
124 Tempo::getPreferredStepSize() const
125 {
126     return 512;
127 }
128
129 size_t
130 Tempo::getPreferredBlockSize() const
131 {
132     return 2 * getPreferredStepSize();
133 }
134
135 Tempo::ParameterList
136 Tempo::getParameterDescriptors() const
137 {
138     ParameterList list;
139     
140     ParameterDescriptor desc;
141     desc.name = "onsettype";
142     desc.description = "Onset Detection Function Type";
143     desc.minValue = 0;
144     desc.maxValue = 6;
145     desc.defaultValue = (int)aubio_onset_complex;
146     desc.isQuantized = true;
147     desc.quantizeStep = 1;
148     desc.valueNames.push_back("Energy Based");
149     desc.valueNames.push_back("Spectral Difference");
150     desc.valueNames.push_back("High-Frequency Content");
151     desc.valueNames.push_back("Complex Domain");
152     desc.valueNames.push_back("Phase Deviation");
153     desc.valueNames.push_back("Kullback-Liebler");
154     desc.valueNames.push_back("Modified Kullback-Liebler");
155     list.push_back(desc);
156
157     desc = ParameterDescriptor();
158     desc.name = "peakpickthreshold";
159     desc.description = "Peak Picker Threshold";
160     desc.minValue = 0;
161     desc.maxValue = 1;
162     desc.defaultValue = 0.3;
163     desc.isQuantized = false;
164     list.push_back(desc);
165
166     desc = ParameterDescriptor();
167     desc.name = "silencethreshold";
168     desc.description = "Silence Threshold";
169     desc.minValue = -120;
170     desc.maxValue = 0;
171     desc.defaultValue = -90;
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 = aubio_onset_energy; break;
199         case 1: m_onsettype = aubio_onset_specdiff; break;
200         case 2: m_onsettype = aubio_onset_hfc; break;
201         case 3: m_onsettype = aubio_onset_complex; break;
202         case 4: m_onsettype = aubio_onset_phase; break;
203         case 5: m_onsettype = aubio_onset_kl; break;
204         case 6: m_onsettype = aubio_onset_mkl; break;
205         }
206     } else if (param == "peakpickthreshold") {
207         m_threshold = value;
208     } else if (param == "silencethreshold") {
209         m_silence = value;
210     }
211 }
212
213 Tempo::OutputList
214 Tempo::getOutputDescriptors() const
215 {
216     OutputList list;
217
218     OutputDescriptor d;
219     d.name = "beats";
220     d.unit = "";
221     d.description = "Beats";
222     d.hasFixedBinCount = true;
223     d.binCount = 0;
224     d.sampleType = OutputDescriptor::VariableSampleRate;
225     d.sampleRate = 0;
226     list.push_back(d);
227
228 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
229     d.name = "tempo";
230     d.unit = "bpm";
231     d.description = "Tempo";
232     d.hasFixedBinCount = true;
233     d.binCount = 1;
234     d.hasKnownExtents = false;
235     d.isQuantized = false;
236     d.sampleType = OutputDescriptor::OneSamplePerStep;
237     list.push_back(d);
238 #endif
239
240     return list;
241 }
242
243 Tempo::FeatureSet
244 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
245 {
246     for (size_t i = 0; i < m_stepSize; ++i) {
247         for (size_t j = 0; j < m_channelCount; ++j) {
248             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
249         }
250     }
251
252     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
253     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
254
255 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
256     float locked_tempo = 0;
257 #endif
258
259     if ( m_btcounter == m_btstep - 1 ) {
260 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
261         aubio_beattracking_do(m_beattracking,m_dfframe,m_btout,&locked_tempo);
262 #else
263         aubio_beattracking_do(m_beattracking,m_dfframe,m_btout);
264 #endif
265         /* rotate dfframe */
266         for (size_t i = 0 ; i < m_winlen - m_btstep; i++ ) 
267                 m_dfframe->data[0][i] = m_dfframe->data[0][i+m_btstep];
268         for (size_t i = m_winlen - m_btstep ; i < m_winlen; i++ ) 
269                 m_dfframe->data[0][i] = 0.;
270                 
271         m_btcounter = -1;
272     }
273     m_btcounter++;
274     bool isonset = aubio_peakpick_pimrt_wt( m_onset, m_peakpick, 
275         &(m_dfframe->data[0][m_winlen - m_btstep + m_btcounter]));
276     bool istactus = 0;
277
278     /* check if any of the predicted beat correspond to the current time */
279     for (size_t i = 1; i < m_btout->data[0][0]; i++ ) { 
280             if (m_btcounter == m_btout->data[0][i]) {
281                     if (aubio_silence_detection(m_ibuf, m_silence)) {
282                             isonset  = false;
283                             istactus = false;
284                     } else {
285                             istactus = true;
286                     }
287             }
288     }
289
290     FeatureSet returnFeatures;
291
292     if (istactus == true) {
293         if (timestamp - m_lastBeat >= m_delay) {
294             Feature onsettime;
295             onsettime.hasTimestamp = true;
296             if (timestamp < m_delay) timestamp = m_delay;
297             onsettime.timestamp = timestamp - m_delay;
298             returnFeatures[0].push_back(onsettime);
299             m_lastBeat = timestamp;
300         }
301     }
302
303 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
304     if (locked_tempo >= 30 && locked_tempo <= 206) {
305         if (locked_tempo > 145) locked_tempo /= 2;
306         std::cerr << "Locked tempo: " << locked_tempo << std::endl;
307         Feature tempo;
308         tempo.hasTimestamp = false;
309         tempo.values.push_back(locked_tempo);
310         returnFeatures[1].push_back(tempo);
311     }
312 #endif
313
314     return returnFeatures;
315 }
316
317 Tempo::FeatureSet
318 Tempo::getRemainingFeatures()
319 {
320     return FeatureSet();
321 }
322