annotate plugins/Tempo.cpp @ 15:30153569c1a6

* FLOOR -> floor
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Apr 2007 10:14:16 +0000
parents a5e23a7501a2
children 1a7563c56d88
rev   line source
cannam@7 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@7 2
cannam@7 3 /*
cannam@7 4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
cannam@7 5
cannam@7 6 Centre for Digital Music, Queen Mary, University of London.
cannam@7 7 This file copyright 2006 Chris Cannam.
cannam@7 8
cannam@7 9 This program is free software; you can redistribute it and/or
cannam@7 10 modify it under the terms of the GNU General Public License as
cannam@7 11 published by the Free Software Foundation; either version 2 of the
cannam@7 12 License, or (at your option) any later version. See the file
cannam@7 13 COPYING included with this distribution for more information.
cannam@7 14
cannam@7 15 */
cannam@7 16
cannam@7 17 #include <math.h>
cannam@7 18 #include "Tempo.h"
cannam@7 19
cannam@7 20 using std::string;
cannam@7 21 using std::vector;
cannam@7 22 using std::cerr;
cannam@7 23 using std::endl;
cannam@7 24
cannam@14 25 //#define HAVE_AUBIO_LOCKED_TEMPO_HACK
cannam@12 26
cannam@7 27 Tempo::Tempo(float inputSampleRate) :
cannam@7 28 Plugin(inputSampleRate),
cannam@7 29 m_ibuf(0),
cannam@7 30 m_fftgrain(0),
cannam@7 31 m_onset(0),
cannam@7 32 m_pv(0),
cannam@7 33 m_peakpick(0),
cannam@7 34 m_onsetdet(0),
cannam@7 35 m_onsettype(aubio_onset_specdiff),
cannam@7 36 m_beattracking(0),
cannam@7 37 m_dfframe(0),
cannam@7 38 m_btout(0),
cannam@7 39 m_btcounter(0),
cannam@7 40 m_threshold(0.3),
cannam@7 41 m_silence(-90),
cannam@7 42 m_channelCount(1)
cannam@7 43 {
cannam@7 44 }
cannam@7 45
cannam@7 46 Tempo::~Tempo()
cannam@7 47 {
cannam@7 48 if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
cannam@7 49 if (m_ibuf) del_fvec(m_ibuf);
cannam@7 50 if (m_onset) del_fvec(m_onset);
cannam@7 51 if (m_fftgrain) del_cvec(m_fftgrain);
cannam@7 52 if (m_pv) del_aubio_pvoc(m_pv);
cannam@7 53 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
cannam@7 54 if (m_beattracking) del_aubio_beattracking(m_beattracking);
cannam@7 55 if (m_dfframe) del_fvec(m_dfframe);
cannam@7 56 if (m_btout) del_fvec(m_btout);
cannam@7 57 }
cannam@7 58
cannam@7 59 string
cannam@13 60 Tempo::getIdentifier() const
cannam@7 61 {
cannam@7 62 return "aubiotempo";
cannam@7 63 }
cannam@7 64
cannam@7 65 string
cannam@13 66 Tempo::getName() const
cannam@7 67 {
cannam@7 68 return "Aubio Tempo Detector";
cannam@7 69 }
cannam@7 70
cannam@7 71 string
cannam@13 72 Tempo::getDescription() const
cannam@13 73 {
cannam@13 74 return "Estimate the musical tempo by tracking note onset timings";
cannam@13 75 }
cannam@13 76
cannam@13 77 string
cannam@7 78 Tempo::getMaker() const
cannam@7 79 {
cannam@13 80 return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
cannam@7 81 }
cannam@7 82
cannam@7 83 int
cannam@7 84 Tempo::getPluginVersion() const
cannam@7 85 {
cannam@7 86 return 1;
cannam@7 87 }
cannam@7 88
cannam@7 89 string
cannam@7 90 Tempo::getCopyright() const
cannam@7 91 {
cannam@7 92 return "GPL";
cannam@7 93 }
cannam@7 94
cannam@7 95 bool
cannam@7 96 Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@7 97 {
cannam@7 98 m_channelCount = channels;
cannam@7 99 m_stepSize = stepSize;
cannam@7 100 m_blockSize = blockSize;
cannam@7 101
cannam@7 102 m_ibuf = new_fvec(stepSize, channels);
cannam@7 103 m_onset = new_fvec(1, channels);
cannam@7 104 m_fftgrain = new_cvec(blockSize, channels);
cannam@7 105 m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
cannam@7 106 m_peakpick = new_aubio_peakpicker(m_threshold);
cannam@7 107
cannam@7 108 m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
cannam@7 109
cannam@7 110 m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
cannam@7 111 lrintf(m_inputSampleRate));
cannam@7 112
cannam@7 113 m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
cannam@7 114
cannam@7 115 m_winlen = 512*512/stepSize;
cannam@7 116 m_dfframe = new_fvec(m_winlen,channels);
cannam@7 117 m_btstep = m_winlen/4;
cannam@7 118 m_btout = new_fvec(m_btstep,channels);
cannam@7 119 m_beattracking = new_aubio_beattracking(m_winlen,channels);
cannam@7 120
cannam@7 121 return true;
cannam@7 122 }
cannam@7 123
cannam@7 124 void
cannam@7 125 Tempo::reset()
cannam@7 126 {
cannam@7 127 }
cannam@7 128
cannam@7 129 size_t
cannam@7 130 Tempo::getPreferredStepSize() const
cannam@7 131 {
cannam@7 132 return 512;
cannam@7 133 }
cannam@7 134
cannam@7 135 size_t
cannam@7 136 Tempo::getPreferredBlockSize() const
cannam@7 137 {
cannam@7 138 return 2 * getPreferredStepSize();
cannam@7 139 }
cannam@7 140
cannam@7 141 Tempo::ParameterList
cannam@7 142 Tempo::getParameterDescriptors() const
cannam@7 143 {
cannam@7 144 ParameterList list;
cannam@7 145
cannam@7 146 ParameterDescriptor desc;
cannam@13 147 desc.identifier = "onsettype";
cannam@13 148 desc.name = "Onset Detection Function Type";
cannam@7 149 desc.minValue = 0;
cannam@7 150 desc.maxValue = 6;
cannam@7 151 desc.defaultValue = (int)aubio_onset_complex;
cannam@7 152 desc.isQuantized = true;
cannam@7 153 desc.quantizeStep = 1;
cannam@7 154 desc.valueNames.push_back("Energy Based");
cannam@7 155 desc.valueNames.push_back("Spectral Difference");
cannam@7 156 desc.valueNames.push_back("High-Frequency Content");
cannam@7 157 desc.valueNames.push_back("Complex Domain");
cannam@7 158 desc.valueNames.push_back("Phase Deviation");
cannam@7 159 desc.valueNames.push_back("Kullback-Liebler");
cannam@7 160 desc.valueNames.push_back("Modified Kullback-Liebler");
cannam@7 161 list.push_back(desc);
cannam@7 162
cannam@7 163 desc = ParameterDescriptor();
cannam@13 164 desc.identifier = "peakpickthreshold";
cannam@13 165 desc.name = "Peak Picker Threshold";
cannam@7 166 desc.minValue = 0;
cannam@7 167 desc.maxValue = 1;
cannam@7 168 desc.defaultValue = 0.3;
cannam@7 169 desc.isQuantized = false;
cannam@7 170 list.push_back(desc);
cannam@7 171
cannam@7 172 desc = ParameterDescriptor();
cannam@13 173 desc.identifier = "silencethreshold";
cannam@13 174 desc.name = "Silence Threshold";
cannam@7 175 desc.minValue = -120;
cannam@7 176 desc.maxValue = 0;
cannam@7 177 desc.defaultValue = -90;
cannam@7 178 desc.unit = "dB";
cannam@7 179 desc.isQuantized = false;
cannam@7 180 list.push_back(desc);
cannam@7 181
cannam@7 182 return list;
cannam@7 183 }
cannam@7 184
cannam@7 185 float
cannam@7 186 Tempo::getParameter(std::string param) const
cannam@7 187 {
cannam@7 188 if (param == "onsettype") {
cannam@7 189 return m_onsettype;
cannam@7 190 } else if (param == "peakpickthreshold") {
cannam@7 191 return m_threshold;
cannam@7 192 } else if (param == "silencethreshold") {
cannam@7 193 return m_silence;
cannam@7 194 } else {
cannam@7 195 return 0.0;
cannam@7 196 }
cannam@7 197 }
cannam@7 198
cannam@7 199 void
cannam@7 200 Tempo::setParameter(std::string param, float value)
cannam@7 201 {
cannam@7 202 if (param == "onsettype") {
cannam@7 203 switch (lrintf(value)) {
cannam@7 204 case 0: m_onsettype = aubio_onset_energy; break;
cannam@7 205 case 1: m_onsettype = aubio_onset_specdiff; break;
cannam@7 206 case 2: m_onsettype = aubio_onset_hfc; break;
cannam@7 207 case 3: m_onsettype = aubio_onset_complex; break;
cannam@7 208 case 4: m_onsettype = aubio_onset_phase; break;
cannam@7 209 case 5: m_onsettype = aubio_onset_kl; break;
cannam@7 210 case 6: m_onsettype = aubio_onset_mkl; break;
cannam@7 211 }
cannam@7 212 } else if (param == "peakpickthreshold") {
cannam@7 213 m_threshold = value;
cannam@7 214 } else if (param == "silencethreshold") {
cannam@7 215 m_silence = value;
cannam@7 216 }
cannam@7 217 }
cannam@7 218
cannam@7 219 Tempo::OutputList
cannam@7 220 Tempo::getOutputDescriptors() const
cannam@7 221 {
cannam@7 222 OutputList list;
cannam@7 223
cannam@7 224 OutputDescriptor d;
cannam@13 225 d.identifier = "beats";
cannam@13 226 d.name = "Beats";
cannam@7 227 d.unit = "";
cannam@7 228 d.hasFixedBinCount = true;
cannam@7 229 d.binCount = 0;
cannam@7 230 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@7 231 d.sampleRate = 0;
cannam@7 232 list.push_back(d);
cannam@7 233
cannam@12 234 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
cannam@13 235 d.identifier = "tempo";
cannam@13 236 d.name = "Tempo";
cannam@12 237 d.unit = "bpm";
cannam@12 238 d.hasFixedBinCount = true;
cannam@12 239 d.binCount = 1;
cannam@12 240 d.hasKnownExtents = false;
cannam@12 241 d.isQuantized = false;
cannam@12 242 d.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@12 243 list.push_back(d);
cannam@12 244 #endif
cannam@12 245
cannam@7 246 return list;
cannam@7 247 }
cannam@7 248
cannam@7 249 Tempo::FeatureSet
cannam@12 250 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
cannam@7 251 {
cannam@7 252 for (size_t i = 0; i < m_stepSize; ++i) {
cannam@7 253 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@7 254 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
cannam@7 255 }
cannam@7 256 }
cannam@7 257
cannam@7 258 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
cannam@7 259 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
cannam@7 260
cannam@12 261 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
cannam@12 262 float locked_tempo = 0;
cannam@12 263 #endif
cannam@12 264
cannam@7 265 if ( m_btcounter == m_btstep - 1 ) {
cannam@12 266 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
cannam@12 267 aubio_beattracking_do(m_beattracking,m_dfframe,m_btout,&locked_tempo);
cannam@12 268 #else
cannam@7 269 aubio_beattracking_do(m_beattracking,m_dfframe,m_btout);
cannam@12 270 #endif
cannam@7 271 /* rotate dfframe */
cannam@7 272 for (size_t i = 0 ; i < m_winlen - m_btstep; i++ )
cannam@7 273 m_dfframe->data[0][i] = m_dfframe->data[0][i+m_btstep];
cannam@7 274 for (size_t i = m_winlen - m_btstep ; i < m_winlen; i++ )
cannam@7 275 m_dfframe->data[0][i] = 0.;
cannam@7 276
cannam@7 277 m_btcounter = -1;
cannam@7 278 }
cannam@7 279 m_btcounter++;
cannam@7 280 bool isonset = aubio_peakpick_pimrt_wt( m_onset, m_peakpick,
cannam@7 281 &(m_dfframe->data[0][m_winlen - m_btstep + m_btcounter]));
cannam@7 282 bool istactus = 0;
cannam@7 283
cannam@7 284 /* check if any of the predicted beat correspond to the current time */
cannam@7 285 for (size_t i = 1; i < m_btout->data[0][0]; i++ ) {
cannam@7 286 if (m_btcounter == m_btout->data[0][i]) {
cannam@7 287 if (aubio_silence_detection(m_ibuf, m_silence)) {
cannam@7 288 isonset = false;
cannam@7 289 istactus = false;
cannam@7 290 } else {
cannam@7 291 istactus = true;
cannam@7 292 }
cannam@7 293 }
cannam@7 294 }
cannam@7 295
cannam@7 296 FeatureSet returnFeatures;
cannam@7 297
cannam@7 298 if (istactus == true) {
cannam@7 299 if (timestamp - m_lastBeat >= m_delay) {
cannam@7 300 Feature onsettime;
cannam@7 301 onsettime.hasTimestamp = true;
cannam@7 302 if (timestamp < m_delay) timestamp = m_delay;
cannam@7 303 onsettime.timestamp = timestamp - m_delay;
cannam@7 304 returnFeatures[0].push_back(onsettime);
cannam@7 305 m_lastBeat = timestamp;
cannam@7 306 }
cannam@7 307 }
cannam@7 308
cannam@12 309 #ifdef HAVE_AUBIO_LOCKED_TEMPO_HACK
cannam@12 310 if (locked_tempo >= 30 && locked_tempo <= 206) {
cannam@12 311 if (locked_tempo > 145) locked_tempo /= 2;
cannam@12 312 std::cerr << "Locked tempo: " << locked_tempo << std::endl;
cannam@12 313 Feature tempo;
cannam@12 314 tempo.hasTimestamp = false;
cannam@12 315 tempo.values.push_back(locked_tempo);
cannam@12 316 returnFeatures[1].push_back(tempo);
cannam@12 317 }
cannam@12 318 #endif
cannam@12 319
cannam@7 320 return returnFeatures;
cannam@7 321 }
cannam@7 322
cannam@7 323 Tempo::FeatureSet
cannam@7 324 Tempo::getRemainingFeatures()
cannam@7 325 {
cannam@7 326 return FeatureSet();
cannam@7 327 }
cannam@7 328