annotate plugins/Tempo.cpp @ 12:62414aaaaa7e

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