1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
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.
25 //#define HAVE_AUBIO_LOCKED_TEMPO_HACK
27 Tempo::Tempo(float inputSampleRate) :
28 Plugin(inputSampleRate),
35 m_onsettype(aubio_onset_specdiff),
47 if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
48 if (m_ibuf) del_fvec(m_ibuf);
49 if (m_onset) del_fvec(m_onset);
50 if (m_fftgrain) del_cvec(m_fftgrain);
51 if (m_pv) del_aubio_pvoc(m_pv);
52 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
53 if (m_beattracking) del_aubio_beattracking(m_beattracking);
54 if (m_dfframe) del_fvec(m_dfframe);
55 if (m_btout) del_fvec(m_btout);
59 Tempo::getIdentifier() const
65 Tempo::getName() const
67 return "Aubio Beat Tracker";
71 Tempo::getDescription() const
73 return "Estimate the musical tempo and track beat positions";
77 Tempo::getMaker() const
79 return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
83 Tempo::getPluginVersion() const
89 Tempo::getCopyright() const
95 Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
98 std::cerr << "Tempo::initialise: channels must be 1" << std::endl;
102 m_stepSize = stepSize;
103 m_blockSize = blockSize;
105 m_ibuf = new_fvec(stepSize);
106 m_onset = new_fvec(1);
107 m_fftgrain = new_cvec(blockSize);
108 m_pv = new_aubio_pvoc(blockSize, stepSize);
109 m_peakpick = new_aubio_peakpicker(m_threshold);
111 m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize);
113 m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
114 lrintf(m_inputSampleRate));
116 m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
118 m_winlen = 512*512/stepSize;
119 m_dfframe = new_fvec(m_winlen,channels);
120 m_btstep = m_winlen/4;
121 m_btout = new_fvec(m_btstep,channels);
122 m_beattracking = new_aubio_beattracking(m_winlen,channels);
133 Tempo::getPreferredStepSize() const
139 Tempo::getPreferredBlockSize() const
141 return 2 * getPreferredStepSize();
145 Tempo::getParameterDescriptors() const
149 ParameterDescriptor desc;
150 desc.identifier = "onsettype";
151 desc.name = "Onset Detection Function Type";
154 desc.defaultValue = (int)aubio_onset_complex;
155 desc.isQuantized = true;
156 desc.quantizeStep = 1;
157 desc.valueNames.push_back("Energy Based");
158 desc.valueNames.push_back("Spectral Difference");
159 desc.valueNames.push_back("High-Frequency Content");
160 desc.valueNames.push_back("Complex Domain");
161 desc.valueNames.push_back("Phase Deviation");
162 desc.valueNames.push_back("Kullback-Liebler");
163 desc.valueNames.push_back("Modified Kullback-Liebler");
164 list.push_back(desc);
166 desc = ParameterDescriptor();
167 desc.identifier = "peakpickthreshold";
168 desc.name = "Peak Picker Threshold";
171 desc.defaultValue = 0.3;
172 desc.isQuantized = false;
173 list.push_back(desc);
175 desc = ParameterDescriptor();
176 desc.identifier = "silencethreshold";
177 desc.name = "Silence Threshold";
178 desc.minValue = -120;
180 desc.defaultValue = -90;
182 desc.isQuantized = false;
183 list.push_back(desc);
189 Tempo::getParameter(std::string param) const
191 if (param == "onsettype") {
193 } else if (param == "peakpickthreshold") {
195 } else if (param == "silencethreshold") {
203 Tempo::setParameter(std::string param, float value)
205 if (param == "onsettype") {
206 switch (lrintf(value)) {
207 case 0: m_onsettype = aubio_onset_energy; break;
208 case 1: m_onsettype = aubio_onset_specdiff; break;
209 case 2: m_onsettype = aubio_onset_hfc; break;
210 case 3: m_onsettype = aubio_onset_complex; break;
211 case 4: m_onsettype = aubio_onset_phase; break;
212 case 5: m_onsettype = aubio_onset_kl; break;
213 case 6: m_onsettype = aubio_onset_mkl; break;
215 } else if (param == "peakpickthreshold") {
217 } else if (param == "silencethreshold") {
223 Tempo::getOutputDescriptors() const
228 d.identifier = "beats";
231 d.hasFixedBinCount = true;
233 d.sampleType = OutputDescriptor::VariableSampleRate;
237 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
238 d.identifier = "tempo";
241 d.hasFixedBinCount = true;
243 d.hasKnownExtents = false;
244 d.isQuantized = false;
245 d.sampleType = OutputDescriptor::OneSamplePerStep;
253 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
255 for (size_t i = 0; i < m_stepSize; ++i) {
256 for (size_t j = 0; j < m_channelCount; ++j) {
257 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
261 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
262 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
264 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
265 float locked_tempo = 0;
268 if ( m_btcounter == m_btstep - 1 ) {
269 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
270 aubio_beattracking_do(m_beattracking,m_dfframe,m_btout,&locked_tempo);
272 aubio_beattracking_do(m_beattracking,m_dfframe,m_btout);
275 for (size_t i = 0 ; i < m_winlen - m_btstep; i++ )
276 m_dfframe->data[0][i] = m_dfframe->data[0][i+m_btstep];
277 for (size_t i = m_winlen - m_btstep ; i < m_winlen; i++ )
278 m_dfframe->data[0][i] = 0.;
283 bool isonset = aubio_peakpick_pimrt_wt( m_onset, m_peakpick,
284 &(m_dfframe->data[0][m_winlen - m_btstep + m_btcounter]));
287 /* check if any of the predicted beat correspond to the current time */
288 for (size_t i = 1; i < m_btout->data[0][0]; i++ ) {
289 if (m_btcounter == m_btout->data[0][i]) {
290 if (aubio_silence_detection(m_ibuf, m_silence)) {
299 FeatureSet returnFeatures;
301 if (istactus == true) {
302 if (timestamp - m_lastBeat >= m_delay) {
304 onsettime.hasTimestamp = true;
305 if (timestamp < m_delay) timestamp = m_delay;
306 onsettime.timestamp = timestamp - m_delay;
307 returnFeatures[0].push_back(onsettime);
308 m_lastBeat = timestamp;
312 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
313 if (locked_tempo >= 30 && locked_tempo <= 206) {
314 if (locked_tempo > 145) locked_tempo /= 2;
315 std::cerr << "Locked tempo: " << locked_tempo << std::endl;
317 tempo.hasTimestamp = false;
318 tempo.values.push_back(locked_tempo);
319 returnFeatures[1].push_back(tempo);
323 return returnFeatures;
327 Tempo::getRemainingFeatures()