annotate plugins/Tempo.cpp @ 52:b80056ac0503

plugins/: use fvec_set_sample
author Paul Brossier <piem@piem.org>
date Mon, 30 Dec 2013 22:49:06 -0400
parents 119e22552925
children f435d3b3ba8b
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@37 97 reset();
cannam@7 98
cannam@7 99 return true;
cannam@7 100 }
cannam@7 101
cannam@7 102 void
cannam@7 103 Tempo::reset()
cannam@7 104 {
cannam@37 105 if (m_tempo) del_aubio_tempo(m_tempo);
cannam@37 106
cannam@37 107 m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
cannam@37 108
cannam@37 109 m_tempo = new_aubio_tempo
cannam@37 110 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
cannam@37 111 m_blockSize,
cannam@37 112 m_stepSize,
cannam@37 113 lrintf(m_inputSampleRate));
cannam@37 114
cannam@37 115 aubio_tempo_set_silence(m_tempo, m_silence);
cannam@37 116 aubio_tempo_set_threshold(m_tempo, m_threshold);
cannam@7 117 }
cannam@7 118
cannam@7 119 size_t
cannam@7 120 Tempo::getPreferredStepSize() const
cannam@7 121 {
cannam@7 122 return 512;
cannam@7 123 }
cannam@7 124
cannam@7 125 size_t
cannam@7 126 Tempo::getPreferredBlockSize() const
cannam@7 127 {
cannam@7 128 return 2 * getPreferredStepSize();
cannam@7 129 }
cannam@7 130
cannam@7 131 Tempo::ParameterList
cannam@7 132 Tempo::getParameterDescriptors() const
cannam@7 133 {
cannam@7 134 ParameterList list;
cannam@7 135
cannam@7 136 ParameterDescriptor desc;
cannam@13 137 desc.identifier = "onsettype";
cannam@13 138 desc.name = "Onset Detection Function Type";
cannam@7 139 desc.minValue = 0;
cannam@35 140 desc.maxValue = 7;
cannam@35 141 desc.defaultValue = (int)OnsetComplex;
cannam@7 142 desc.isQuantized = true;
cannam@7 143 desc.quantizeStep = 1;
cannam@7 144 desc.valueNames.push_back("Energy Based");
cannam@7 145 desc.valueNames.push_back("Spectral Difference");
cannam@7 146 desc.valueNames.push_back("High-Frequency Content");
cannam@7 147 desc.valueNames.push_back("Complex Domain");
cannam@7 148 desc.valueNames.push_back("Phase Deviation");
cannam@7 149 desc.valueNames.push_back("Kullback-Liebler");
cannam@7 150 desc.valueNames.push_back("Modified Kullback-Liebler");
cannam@35 151 desc.valueNames.push_back("Spectral Flux");
cannam@7 152 list.push_back(desc);
cannam@7 153
cannam@7 154 desc = ParameterDescriptor();
cannam@13 155 desc.identifier = "peakpickthreshold";
cannam@13 156 desc.name = "Peak Picker Threshold";
cannam@7 157 desc.minValue = 0;
cannam@7 158 desc.maxValue = 1;
cannam@7 159 desc.defaultValue = 0.3;
cannam@7 160 desc.isQuantized = false;
cannam@7 161 list.push_back(desc);
cannam@7 162
cannam@7 163 desc = ParameterDescriptor();
cannam@13 164 desc.identifier = "silencethreshold";
cannam@13 165 desc.name = "Silence Threshold";
cannam@7 166 desc.minValue = -120;
cannam@7 167 desc.maxValue = 0;
cannam@35 168 desc.defaultValue = -70;
cannam@7 169 desc.unit = "dB";
cannam@7 170 desc.isQuantized = false;
cannam@7 171 list.push_back(desc);
cannam@7 172
cannam@7 173 return list;
cannam@7 174 }
cannam@7 175
cannam@7 176 float
cannam@7 177 Tempo::getParameter(std::string param) const
cannam@7 178 {
cannam@7 179 if (param == "onsettype") {
cannam@7 180 return m_onsettype;
cannam@7 181 } else if (param == "peakpickthreshold") {
cannam@7 182 return m_threshold;
cannam@7 183 } else if (param == "silencethreshold") {
cannam@7 184 return m_silence;
cannam@7 185 } else {
cannam@7 186 return 0.0;
cannam@7 187 }
cannam@7 188 }
cannam@7 189
cannam@7 190 void
cannam@7 191 Tempo::setParameter(std::string param, float value)
cannam@7 192 {
cannam@7 193 if (param == "onsettype") {
cannam@7 194 switch (lrintf(value)) {
cannam@35 195 case 0: m_onsettype = OnsetEnergy; break;
cannam@35 196 case 1: m_onsettype = OnsetSpecDiff; break;
cannam@35 197 case 2: m_onsettype = OnsetHFC; break;
cannam@35 198 case 3: m_onsettype = OnsetComplex; break;
cannam@35 199 case 4: m_onsettype = OnsetPhase; break;
cannam@35 200 case 5: m_onsettype = OnsetKL; break;
cannam@35 201 case 6: m_onsettype = OnsetMKL; break;
cannam@35 202 case 7: m_onsettype = OnsetSpecFlux; break;
cannam@7 203 }
cannam@7 204 } else if (param == "peakpickthreshold") {
cannam@7 205 m_threshold = value;
cannam@7 206 } else if (param == "silencethreshold") {
cannam@7 207 m_silence = value;
cannam@7 208 }
cannam@7 209 }
cannam@7 210
cannam@7 211 Tempo::OutputList
cannam@7 212 Tempo::getOutputDescriptors() const
cannam@7 213 {
cannam@7 214 OutputList list;
cannam@7 215
cannam@7 216 OutputDescriptor d;
cannam@13 217 d.identifier = "beats";
cannam@13 218 d.name = "Beats";
cannam@7 219 d.unit = "";
cannam@7 220 d.hasFixedBinCount = true;
cannam@7 221 d.binCount = 0;
cannam@7 222 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@7 223 d.sampleRate = 0;
cannam@7 224 list.push_back(d);
cannam@7 225
cannam@13 226 d.identifier = "tempo";
cannam@13 227 d.name = "Tempo";
cannam@12 228 d.unit = "bpm";
cannam@12 229 d.hasFixedBinCount = true;
cannam@12 230 d.binCount = 1;
cannam@12 231 d.hasKnownExtents = false;
cannam@12 232 d.isQuantized = false;
cannam@12 233 d.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@12 234 list.push_back(d);
cannam@12 235
cannam@7 236 return list;
cannam@7 237 }
cannam@7 238
cannam@7 239 Tempo::FeatureSet
cannam@12 240 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
cannam@7 241 {
cannam@7 242 for (size_t i = 0; i < m_stepSize; ++i) {
piem@52 243 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
cannam@7 244 }
cannam@7 245
cannam@35 246 aubio_tempo_do(m_tempo, m_ibuf, m_beat);
cannam@7 247
cannam@35 248 bool istactus = m_beat->data[0];
cannam@12 249
cannam@35 250 m_bpm = aubio_tempo_get_bpm(m_tempo);
cannam@7 251
cannam@7 252 FeatureSet returnFeatures;
cannam@7 253
cannam@7 254 if (istactus == true) {
cannam@7 255 if (timestamp - m_lastBeat >= m_delay) {
cannam@7 256 Feature onsettime;
cannam@7 257 onsettime.hasTimestamp = true;
cannam@7 258 if (timestamp < m_delay) timestamp = m_delay;
cannam@7 259 onsettime.timestamp = timestamp - m_delay;
cannam@7 260 returnFeatures[0].push_back(onsettime);
cannam@7 261 m_lastBeat = timestamp;
cannam@7 262 }
cannam@7 263 }
cannam@7 264
cannam@35 265 if (m_bpm >= 30 && m_bpm <= 206) {
cannam@12 266 Feature tempo;
cannam@12 267 tempo.hasTimestamp = false;
cannam@35 268 tempo.values.push_back(m_bpm);
cannam@12 269 returnFeatures[1].push_back(tempo);
cannam@12 270 }
cannam@12 271
cannam@7 272 return returnFeatures;
cannam@7 273 }
cannam@7 274
cannam@7 275 Tempo::FeatureSet
cannam@7 276 Tempo::getRemainingFeatures()
cannam@7 277 {
cannam@7 278 return FeatureSet();
cannam@7 279 }
cannam@7 280