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),
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);
60 Tempo::getIdentifier() const
66 Tempo::getName() const
68 return "Aubio Beat Tracker";
72 Tempo::getDescription() const
74 return "Estimate the musical tempo and track beat positions";
78 Tempo::getMaker() const
80 return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
84 Tempo::getPluginVersion() const
90 Tempo::getCopyright() const
96 Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
98 m_channelCount = channels;
99 m_stepSize = stepSize;
100 m_blockSize = blockSize;
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);
108 m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
110 m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
111 lrintf(m_inputSampleRate));
113 m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
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);
130 Tempo::getPreferredStepSize() const
136 Tempo::getPreferredBlockSize() const
138 return 2 * getPreferredStepSize();
142 Tempo::getParameterDescriptors() const
146 ParameterDescriptor desc;
147 desc.identifier = "onsettype";
148 desc.name = "Onset Detection Function Type";
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);
163 desc = ParameterDescriptor();
164 desc.identifier = "peakpickthreshold";
165 desc.name = "Peak Picker Threshold";
168 desc.defaultValue = 0.3;
169 desc.isQuantized = false;
170 list.push_back(desc);
172 desc = ParameterDescriptor();
173 desc.identifier = "silencethreshold";
174 desc.name = "Silence Threshold";
175 desc.minValue = -120;
177 desc.defaultValue = -90;
179 desc.isQuantized = false;
180 list.push_back(desc);
186 Tempo::getParameter(std::string param) const
188 if (param == "onsettype") {
190 } else if (param == "peakpickthreshold") {
192 } else if (param == "silencethreshold") {
200 Tempo::setParameter(std::string param, float value)
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;
212 } else if (param == "peakpickthreshold") {
214 } else if (param == "silencethreshold") {
220 Tempo::getOutputDescriptors() const
225 d.identifier = "beats";
228 d.hasFixedBinCount = true;
230 d.sampleType = OutputDescriptor::VariableSampleRate;
234 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
235 d.identifier = "tempo";
238 d.hasFixedBinCount = true;
240 d.hasKnownExtents = false;
241 d.isQuantized = false;
242 d.sampleType = OutputDescriptor::OneSamplePerStep;
250 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
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);
258 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
259 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
261 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
262 float locked_tempo = 0;
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);
269 aubio_beattracking_do(m_beattracking,m_dfframe,m_btout);
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.;
280 bool isonset = aubio_peakpick_pimrt_wt( m_onset, m_peakpick,
281 &(m_dfframe->data[0][m_winlen - m_btstep + m_btcounter]));
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)) {
296 FeatureSet returnFeatures;
298 if (istactus == true) {
299 if (timestamp - m_lastBeat >= m_delay) {
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;
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;
314 tempo.hasTimestamp = false;
315 tempo.values.push_back(locked_tempo);
316 returnFeatures[1].push_back(tempo);
320 return returnFeatures;
324 Tempo::getRemainingFeatures()