annotate plugins/Tempo.cpp @ 67:f435d3b3ba8b

plugins/Tempo.cpp: add parameters and output descriptions
author Paul Brossier <piem@piem.org>
date Mon, 26 Jan 2015 14:46:15 +0100
parents b80056ac0503
children 43e22cc4992d
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";
piem@67 139 desc.description = "Type of onset detection function to use";
cannam@7 140 desc.minValue = 0;
cannam@35 141 desc.maxValue = 7;
cannam@35 142 desc.defaultValue = (int)OnsetComplex;
cannam@7 143 desc.isQuantized = true;
cannam@7 144 desc.quantizeStep = 1;
cannam@7 145 desc.valueNames.push_back("Energy Based");
cannam@7 146 desc.valueNames.push_back("Spectral Difference");
cannam@7 147 desc.valueNames.push_back("High-Frequency Content");
cannam@7 148 desc.valueNames.push_back("Complex Domain");
cannam@7 149 desc.valueNames.push_back("Phase Deviation");
cannam@7 150 desc.valueNames.push_back("Kullback-Liebler");
cannam@7 151 desc.valueNames.push_back("Modified Kullback-Liebler");
cannam@35 152 desc.valueNames.push_back("Spectral Flux");
cannam@7 153 list.push_back(desc);
cannam@7 154
cannam@7 155 desc = ParameterDescriptor();
cannam@13 156 desc.identifier = "peakpickthreshold";
cannam@13 157 desc.name = "Peak Picker Threshold";
piem@67 158 desc.description = "Peak picking threshold, the higher the least detection";
cannam@7 159 desc.minValue = 0;
cannam@7 160 desc.maxValue = 1;
cannam@7 161 desc.defaultValue = 0.3;
cannam@7 162 desc.isQuantized = false;
cannam@7 163 list.push_back(desc);
cannam@7 164
cannam@7 165 desc = ParameterDescriptor();
cannam@13 166 desc.identifier = "silencethreshold";
cannam@13 167 desc.name = "Silence Threshold";
piem@67 168 desc.description = "Silence threshold, the higher the least detection";
cannam@7 169 desc.minValue = -120;
cannam@7 170 desc.maxValue = 0;
cannam@35 171 desc.defaultValue = -70;
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@35 198 case 0: m_onsettype = OnsetEnergy; break;
cannam@35 199 case 1: m_onsettype = OnsetSpecDiff; break;
cannam@35 200 case 2: m_onsettype = OnsetHFC; break;
cannam@35 201 case 3: m_onsettype = OnsetComplex; break;
cannam@35 202 case 4: m_onsettype = OnsetPhase; break;
cannam@35 203 case 5: m_onsettype = OnsetKL; break;
cannam@35 204 case 6: m_onsettype = OnsetMKL; break;
cannam@35 205 case 7: m_onsettype = OnsetSpecFlux; break;
cannam@7 206 }
cannam@7 207 } else if (param == "peakpickthreshold") {
cannam@7 208 m_threshold = value;
cannam@7 209 } else if (param == "silencethreshold") {
cannam@7 210 m_silence = value;
cannam@7 211 }
cannam@7 212 }
cannam@7 213
cannam@7 214 Tempo::OutputList
cannam@7 215 Tempo::getOutputDescriptors() const
cannam@7 216 {
cannam@7 217 OutputList list;
cannam@7 218
cannam@7 219 OutputDescriptor d;
cannam@13 220 d.identifier = "beats";
cannam@13 221 d.name = "Beats";
piem@67 222 d.description = "List of times at which a beat was detected";
cannam@7 223 d.unit = "";
cannam@7 224 d.hasFixedBinCount = true;
cannam@7 225 d.binCount = 0;
cannam@7 226 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@7 227 d.sampleRate = 0;
cannam@7 228 list.push_back(d);
cannam@7 229
cannam@13 230 d.identifier = "tempo";
cannam@13 231 d.name = "Tempo";
piem@67 232 d.desc = "Overall estimated tempo";
cannam@12 233 d.unit = "bpm";
cannam@12 234 d.hasFixedBinCount = true;
cannam@12 235 d.binCount = 1;
cannam@12 236 d.hasKnownExtents = false;
cannam@12 237 d.isQuantized = false;
cannam@12 238 d.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@12 239 list.push_back(d);
cannam@12 240
cannam@7 241 return list;
cannam@7 242 }
cannam@7 243
cannam@7 244 Tempo::FeatureSet
cannam@12 245 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
cannam@7 246 {
cannam@7 247 for (size_t i = 0; i < m_stepSize; ++i) {
piem@52 248 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
cannam@7 249 }
cannam@7 250
cannam@35 251 aubio_tempo_do(m_tempo, m_ibuf, m_beat);
cannam@7 252
cannam@35 253 bool istactus = m_beat->data[0];
cannam@12 254
cannam@35 255 m_bpm = aubio_tempo_get_bpm(m_tempo);
cannam@7 256
cannam@7 257 FeatureSet returnFeatures;
cannam@7 258
cannam@7 259 if (istactus == true) {
cannam@7 260 if (timestamp - m_lastBeat >= m_delay) {
cannam@7 261 Feature onsettime;
cannam@7 262 onsettime.hasTimestamp = true;
cannam@7 263 if (timestamp < m_delay) timestamp = m_delay;
cannam@7 264 onsettime.timestamp = timestamp - m_delay;
cannam@7 265 returnFeatures[0].push_back(onsettime);
cannam@7 266 m_lastBeat = timestamp;
cannam@7 267 }
cannam@7 268 }
cannam@7 269
cannam@35 270 if (m_bpm >= 30 && m_bpm <= 206) {
cannam@12 271 Feature tempo;
cannam@12 272 tempo.hasTimestamp = false;
cannam@35 273 tempo.values.push_back(m_bpm);
cannam@12 274 returnFeatures[1].push_back(tempo);
cannam@12 275 }
cannam@12 276
cannam@7 277 return returnFeatures;
cannam@7 278 }
cannam@7 279
cannam@7 280 Tempo::FeatureSet
cannam@7 281 Tempo::getRemainingFeatures()
cannam@7 282 {
cannam@7 283 return FeatureSet();
cannam@7 284 }
cannam@7 285