annotate plugins/Tempo.cpp @ 35:bcb23bb4b7aa

Get the rest to build
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 10 Jul 2012 17:20:10 +0100
parents 8a20f3488d88
children 119e22552925
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@7 25 Tempo::Tempo(float inputSampleRate) :
cannam@7 26 Plugin(inputSampleRate),
cannam@7 27 m_ibuf(0),
cannam@35 28 m_beat(0),
cannam@35 29 m_bpm(0),
cannam@35 30 m_onsettype(OnsetComplex),
cannam@35 31 m_tempo(0),
cannam@7 32 m_threshold(0.3),
cannam@35 33 m_silence(-70)
cannam@7 34 {
cannam@7 35 }
cannam@7 36
cannam@7 37 Tempo::~Tempo()
cannam@7 38 {
cannam@7 39 if (m_ibuf) del_fvec(m_ibuf);
cannam@35 40 if (m_beat) del_fvec(m_beat);
cannam@35 41 if (m_tempo) del_aubio_tempo(m_tempo);
cannam@7 42 }
cannam@7 43
cannam@7 44 string
cannam@13 45 Tempo::getIdentifier() const
cannam@7 46 {
cannam@7 47 return "aubiotempo";
cannam@7 48 }
cannam@7 49
cannam@7 50 string
cannam@13 51 Tempo::getName() const
cannam@7 52 {
cannam@25 53 return "Aubio Beat Tracker";
cannam@7 54 }
cannam@7 55
cannam@7 56 string
cannam@13 57 Tempo::getDescription() const
cannam@13 58 {
cannam@23 59 return "Estimate the musical tempo and track beat positions";
cannam@13 60 }
cannam@13 61
cannam@13 62 string
cannam@7 63 Tempo::getMaker() const
cannam@7 64 {
cannam@13 65 return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
cannam@7 66 }
cannam@7 67
cannam@7 68 int
cannam@7 69 Tempo::getPluginVersion() const
cannam@7 70 {
cannam@31 71 return 2;
cannam@7 72 }
cannam@7 73
cannam@7 74 string
cannam@7 75 Tempo::getCopyright() const
cannam@7 76 {
cannam@7 77 return "GPL";
cannam@7 78 }
cannam@7 79
cannam@7 80 bool
cannam@7 81 Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@7 82 {
cannam@32 83 if (channels != 1) {
cannam@32 84 std::cerr << "Tempo::initialise: channels must be 1" << std::endl;
cannam@32 85 return false;
cannam@32 86 }
cannam@32 87
cannam@7 88 m_stepSize = stepSize;
cannam@7 89 m_blockSize = blockSize;
cannam@7 90
cannam@32 91 m_ibuf = new_fvec(stepSize);
cannam@35 92 m_beat = new_fvec(2);
cannam@7 93
cannam@7 94 m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
cannam@7 95 lrintf(m_inputSampleRate));
cannam@7 96
cannam@7 97 m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
cannam@7 98
cannam@35 99 m_tempo = new_aubio_tempo
cannam@35 100 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
cannam@35 101 blockSize,
cannam@35 102 stepSize,
cannam@35 103 lrintf(m_inputSampleRate));
cannam@35 104
cannam@35 105 aubio_tempo_set_silence(m_tempo, m_silence);
cannam@35 106 aubio_tempo_set_threshold(m_tempo, m_threshold);
cannam@7 107
cannam@7 108 return true;
cannam@7 109 }
cannam@7 110
cannam@7 111 void
cannam@7 112 Tempo::reset()
cannam@7 113 {
cannam@7 114 }
cannam@7 115
cannam@7 116 size_t
cannam@7 117 Tempo::getPreferredStepSize() const
cannam@7 118 {
cannam@7 119 return 512;
cannam@7 120 }
cannam@7 121
cannam@7 122 size_t
cannam@7 123 Tempo::getPreferredBlockSize() const
cannam@7 124 {
cannam@7 125 return 2 * getPreferredStepSize();
cannam@7 126 }
cannam@7 127
cannam@7 128 Tempo::ParameterList
cannam@7 129 Tempo::getParameterDescriptors() const
cannam@7 130 {
cannam@7 131 ParameterList list;
cannam@7 132
cannam@7 133 ParameterDescriptor desc;
cannam@13 134 desc.identifier = "onsettype";
cannam@13 135 desc.name = "Onset Detection Function Type";
cannam@7 136 desc.minValue = 0;
cannam@35 137 desc.maxValue = 7;
cannam@35 138 desc.defaultValue = (int)OnsetComplex;
cannam@7 139 desc.isQuantized = true;
cannam@7 140 desc.quantizeStep = 1;
cannam@7 141 desc.valueNames.push_back("Energy Based");
cannam@7 142 desc.valueNames.push_back("Spectral Difference");
cannam@7 143 desc.valueNames.push_back("High-Frequency Content");
cannam@7 144 desc.valueNames.push_back("Complex Domain");
cannam@7 145 desc.valueNames.push_back("Phase Deviation");
cannam@7 146 desc.valueNames.push_back("Kullback-Liebler");
cannam@7 147 desc.valueNames.push_back("Modified Kullback-Liebler");
cannam@35 148 desc.valueNames.push_back("Spectral Flux");
cannam@7 149 list.push_back(desc);
cannam@7 150
cannam@7 151 desc = ParameterDescriptor();
cannam@13 152 desc.identifier = "peakpickthreshold";
cannam@13 153 desc.name = "Peak Picker Threshold";
cannam@7 154 desc.minValue = 0;
cannam@7 155 desc.maxValue = 1;
cannam@7 156 desc.defaultValue = 0.3;
cannam@7 157 desc.isQuantized = false;
cannam@7 158 list.push_back(desc);
cannam@7 159
cannam@7 160 desc = ParameterDescriptor();
cannam@13 161 desc.identifier = "silencethreshold";
cannam@13 162 desc.name = "Silence Threshold";
cannam@7 163 desc.minValue = -120;
cannam@7 164 desc.maxValue = 0;
cannam@35 165 desc.defaultValue = -70;
cannam@7 166 desc.unit = "dB";
cannam@7 167 desc.isQuantized = false;
cannam@7 168 list.push_back(desc);
cannam@7 169
cannam@7 170 return list;
cannam@7 171 }
cannam@7 172
cannam@7 173 float
cannam@7 174 Tempo::getParameter(std::string param) const
cannam@7 175 {
cannam@7 176 if (param == "onsettype") {
cannam@7 177 return m_onsettype;
cannam@7 178 } else if (param == "peakpickthreshold") {
cannam@7 179 return m_threshold;
cannam@7 180 } else if (param == "silencethreshold") {
cannam@7 181 return m_silence;
cannam@7 182 } else {
cannam@7 183 return 0.0;
cannam@7 184 }
cannam@7 185 }
cannam@7 186
cannam@7 187 void
cannam@7 188 Tempo::setParameter(std::string param, float value)
cannam@7 189 {
cannam@7 190 if (param == "onsettype") {
cannam@7 191 switch (lrintf(value)) {
cannam@35 192 case 0: m_onsettype = OnsetEnergy; break;
cannam@35 193 case 1: m_onsettype = OnsetSpecDiff; break;
cannam@35 194 case 2: m_onsettype = OnsetHFC; break;
cannam@35 195 case 3: m_onsettype = OnsetComplex; break;
cannam@35 196 case 4: m_onsettype = OnsetPhase; break;
cannam@35 197 case 5: m_onsettype = OnsetKL; break;
cannam@35 198 case 6: m_onsettype = OnsetMKL; break;
cannam@35 199 case 7: m_onsettype = OnsetSpecFlux; break;
cannam@7 200 }
cannam@7 201 } else if (param == "peakpickthreshold") {
cannam@7 202 m_threshold = value;
cannam@7 203 } else if (param == "silencethreshold") {
cannam@7 204 m_silence = value;
cannam@7 205 }
cannam@7 206 }
cannam@7 207
cannam@7 208 Tempo::OutputList
cannam@7 209 Tempo::getOutputDescriptors() const
cannam@7 210 {
cannam@7 211 OutputList list;
cannam@7 212
cannam@7 213 OutputDescriptor d;
cannam@13 214 d.identifier = "beats";
cannam@13 215 d.name = "Beats";
cannam@7 216 d.unit = "";
cannam@7 217 d.hasFixedBinCount = true;
cannam@7 218 d.binCount = 0;
cannam@7 219 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@7 220 d.sampleRate = 0;
cannam@7 221 list.push_back(d);
cannam@7 222
cannam@13 223 d.identifier = "tempo";
cannam@13 224 d.name = "Tempo";
cannam@12 225 d.unit = "bpm";
cannam@12 226 d.hasFixedBinCount = true;
cannam@12 227 d.binCount = 1;
cannam@12 228 d.hasKnownExtents = false;
cannam@12 229 d.isQuantized = false;
cannam@12 230 d.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@12 231 list.push_back(d);
cannam@12 232
cannam@7 233 return list;
cannam@7 234 }
cannam@7 235
cannam@7 236 Tempo::FeatureSet
cannam@12 237 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
cannam@7 238 {
cannam@7 239 for (size_t i = 0; i < m_stepSize; ++i) {
cannam@35 240 fvec_write_sample(m_ibuf, inputBuffers[0][i], i);
cannam@7 241 }
cannam@7 242
cannam@35 243 aubio_tempo_do(m_tempo, m_ibuf, m_beat);
cannam@7 244
cannam@35 245 bool istactus = m_beat->data[0];
cannam@12 246
cannam@35 247 m_bpm = aubio_tempo_get_bpm(m_tempo);
cannam@7 248
cannam@7 249 FeatureSet returnFeatures;
cannam@7 250
cannam@7 251 if (istactus == true) {
cannam@7 252 if (timestamp - m_lastBeat >= m_delay) {
cannam@7 253 Feature onsettime;
cannam@7 254 onsettime.hasTimestamp = true;
cannam@7 255 if (timestamp < m_delay) timestamp = m_delay;
cannam@7 256 onsettime.timestamp = timestamp - m_delay;
cannam@7 257 returnFeatures[0].push_back(onsettime);
cannam@7 258 m_lastBeat = timestamp;
cannam@7 259 }
cannam@7 260 }
cannam@7 261
cannam@35 262 if (m_bpm >= 30 && m_bpm <= 206) {
cannam@12 263 Feature tempo;
cannam@12 264 tempo.hasTimestamp = false;
cannam@35 265 tempo.values.push_back(m_bpm);
cannam@12 266 returnFeatures[1].push_back(tempo);
cannam@12 267 }
cannam@12 268
cannam@7 269 return returnFeatures;
cannam@7 270 }
cannam@7 271
cannam@7 272 Tempo::FeatureSet
cannam@7 273 Tempo::getRemainingFeatures()
cannam@7 274 {
cannam@7 275 return FeatureSet();
cannam@7 276 }
cannam@7 277