...
[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_fftgrain(0),
29     m_onset(0),
30     m_pv(0),
31     m_peakpick(0),
32     m_onsetdet(0),
33     m_onsettype(aubio_onset_specdiff),
34     m_beattracking(0),
35     m_dfframe(0),
36     m_btout(0),
37     m_btcounter(0),
38     m_threshold(0.3),
39     m_silence(-90),
40     m_channelCount(1)
41 {
42 }
43
44 Tempo::~Tempo()
45 {
46     if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
47     if (m_ibuf) del_fvec(m_ibuf);
48     if (m_onset) del_fvec(m_onset);
49     if (m_fftgrain) del_cvec(m_fftgrain);
50     if (m_pv) del_aubio_pvoc(m_pv);
51     if (m_peakpick) del_aubio_peakpicker(m_peakpick);
52     if (m_beattracking) del_aubio_beattracking(m_beattracking);
53     if (m_dfframe) del_fvec(m_dfframe);
54     if (m_btout) del_fvec(m_btout);
55 }
56
57 string
58 Tempo::getName() const
59 {
60     return "aubiotempo";
61 }
62
63 string
64 Tempo::getDescription() const
65 {
66     return "Aubio Tempo Detector";
67 }
68
69 string
70 Tempo::getMaker() const
71 {
72     return "Paul Brossier (plugin by Chris Cannam)";
73 }
74
75 int
76 Tempo::getPluginVersion() const
77 {
78     return 1;
79 }
80
81 string
82 Tempo::getCopyright() const
83 {
84     return "GPL";
85 }
86
87 bool
88 Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
89 {
90     m_channelCount = channels;
91     m_stepSize = stepSize;
92     m_blockSize = blockSize;
93
94     m_ibuf = new_fvec(stepSize, channels);
95     m_onset = new_fvec(1, channels);
96     m_fftgrain = new_cvec(blockSize, channels);
97     m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
98     m_peakpick = new_aubio_peakpicker(m_threshold);
99
100     m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
101     
102     m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
103                                              lrintf(m_inputSampleRate));
104
105     m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
106
107     m_winlen = 512*512/stepSize;
108     m_dfframe = new_fvec(m_winlen,channels);
109     m_btstep = m_winlen/4;
110     m_btout = new_fvec(m_btstep,channels);
111     m_beattracking = new_aubio_beattracking(m_winlen,channels);
112
113     return true;
114 }
115
116 void
117 Tempo::reset()
118 {
119 }
120
121 size_t
122 Tempo::getPreferredStepSize() const
123 {
124     return 512;
125 }
126
127 size_t
128 Tempo::getPreferredBlockSize() const
129 {
130     return 2 * getPreferredStepSize();
131 }
132
133 Tempo::ParameterList
134 Tempo::getParameterDescriptors() const
135 {
136     ParameterList list;
137     
138     ParameterDescriptor desc;
139     desc.name = "onsettype";
140     desc.description = "Onset Detection Function Type";
141     desc.minValue = 0;
142     desc.maxValue = 6;
143     desc.defaultValue = (int)aubio_onset_complex;
144     desc.isQuantized = true;
145     desc.quantizeStep = 1;
146     desc.valueNames.push_back("Energy Based");
147     desc.valueNames.push_back("Spectral Difference");
148     desc.valueNames.push_back("High-Frequency Content");
149     desc.valueNames.push_back("Complex Domain");
150     desc.valueNames.push_back("Phase Deviation");
151     desc.valueNames.push_back("Kullback-Liebler");
152     desc.valueNames.push_back("Modified Kullback-Liebler");
153     list.push_back(desc);
154
155     desc = ParameterDescriptor();
156     desc.name = "peakpickthreshold";
157     desc.description = "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.name = "silencethreshold";
166     desc.description = "Silence Threshold";
167     desc.minValue = -120;
168     desc.maxValue = 0;
169     desc.defaultValue = -90;
170     desc.unit = "dB";
171     desc.isQuantized = false;
172     list.push_back(desc);
173
174     return list;
175 }
176
177 float
178 Tempo::getParameter(std::string param) const
179 {
180     if (param == "onsettype") {
181         return m_onsettype;
182     } else if (param == "peakpickthreshold") {
183         return m_threshold;
184     } else if (param == "silencethreshold") {
185         return m_silence;
186     } else {
187         return 0.0;
188     }
189 }
190
191 void
192 Tempo::setParameter(std::string param, float value)
193 {
194     if (param == "onsettype") {
195         switch (lrintf(value)) {
196         case 0: m_onsettype = aubio_onset_energy; break;
197         case 1: m_onsettype = aubio_onset_specdiff; break;
198         case 2: m_onsettype = aubio_onset_hfc; break;
199         case 3: m_onsettype = aubio_onset_complex; break;
200         case 4: m_onsettype = aubio_onset_phase; break;
201         case 5: m_onsettype = aubio_onset_kl; break;
202         case 6: m_onsettype = aubio_onset_mkl; break;
203         }
204     } else if (param == "peakpickthreshold") {
205         m_threshold = value;
206     } else if (param == "silencethreshold") {
207         m_silence = value;
208     }
209 }
210
211 Tempo::OutputList
212 Tempo::getOutputDescriptors() const
213 {
214     OutputList list;
215
216     OutputDescriptor d;
217     d.name = "beats";
218     d.unit = "";
219     d.description = "Beats";
220     d.hasFixedBinCount = true;
221     d.binCount = 0;
222     d.sampleType = OutputDescriptor::VariableSampleRate;
223     d.sampleRate = 0;
224     list.push_back(d);
225
226     return list;
227 }
228
229 Tempo::FeatureSet
230 Tempo::process(float **inputBuffers, Vamp::RealTime timestamp)
231 {
232     for (size_t i = 0; i < m_stepSize; ++i) {
233         for (size_t j = 0; j < m_channelCount; ++j) {
234             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
235         }
236     }
237
238     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
239     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
240
241     if ( m_btcounter == m_btstep - 1 ) {
242         aubio_beattracking_do(m_beattracking,m_dfframe,m_btout);
243         /* rotate dfframe */
244         for (size_t i = 0 ; i < m_winlen - m_btstep; i++ ) 
245                 m_dfframe->data[0][i] = m_dfframe->data[0][i+m_btstep];
246         for (size_t i = m_winlen - m_btstep ; i < m_winlen; i++ ) 
247                 m_dfframe->data[0][i] = 0.;
248                 
249         m_btcounter = -1;
250     }
251     m_btcounter++;
252     bool isonset = aubio_peakpick_pimrt_wt( m_onset, m_peakpick, 
253         &(m_dfframe->data[0][m_winlen - m_btstep + m_btcounter]));
254     bool istactus = 0;
255
256
257     /* check if any of the predicted beat correspond to the current time */
258     for (size_t i = 1; i < m_btout->data[0][0]; i++ ) { 
259             if (m_btcounter == m_btout->data[0][i]) {
260                     if (aubio_silence_detection(m_ibuf, m_silence)) {
261                             isonset  = false;
262                             istactus = false;
263                     } else {
264                             istactus = true;
265                     }
266             }
267     }
268
269     FeatureSet returnFeatures;
270
271     if (istactus == true) {
272         if (timestamp - m_lastBeat >= m_delay) {
273             Feature onsettime;
274             onsettime.hasTimestamp = true;
275             if (timestamp < m_delay) timestamp = m_delay;
276             onsettime.timestamp = timestamp - m_delay;
277             returnFeatures[0].push_back(onsettime);
278             m_lastBeat = timestamp;
279         }
280     }
281
282     return returnFeatures;
283 }
284
285 Tempo::FeatureSet
286 Tempo::getRemainingFeatures()
287 {
288     return FeatureSet();
289 }
290