annotate plugins/Tempo.cpp @ 198:3a76aa26b578 tip master

wscript: check for 64bit using sys.maxsize (closes #3)
author Paul Brossier <piem@piem.org>
date Mon, 04 Dec 2017 01:42:19 +0100
parents f80b207ccd15
children
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
piem@112 9 This file is part of vamp-aubio-plugins.
piem@112 10
piem@112 11 vamp-aubio is free software: you can redistribute it and/or modify
piem@112 12 it under the terms of the GNU General Public License as published by
piem@112 13 the Free Software Foundation, either version 3 of the License, or
piem@112 14 (at your option) any later version.
piem@112 15
piem@112 16 vamp-aubio is distributed in the hope that it will be useful,
piem@112 17 but WITHOUT ANY WARRANTY; without even the implied warranty of
piem@112 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
piem@112 19 GNU General Public License for more details.
piem@112 20
piem@112 21 You should have received a copy of the GNU General Public License
piem@112 22 along with aubio. If not, see <http://www.gnu.org/licenses/>.
cannam@7 23
cannam@7 24 */
cannam@7 25
cannam@7 26 #include <math.h>
cannam@7 27 #include "Tempo.h"
cannam@7 28
cannam@7 29 using std::string;
cannam@7 30 using std::vector;
cannam@7 31 using std::cerr;
cannam@7 32 using std::endl;
cannam@7 33
cannam@7 34 Tempo::Tempo(float inputSampleRate) :
cannam@7 35 Plugin(inputSampleRate),
cannam@7 36 m_ibuf(0),
cannam@35 37 m_beat(0),
cannam@35 38 m_bpm(0),
cannam@35 39 m_onsettype(OnsetComplex),
cannam@35 40 m_tempo(0),
cannam@7 41 m_threshold(0.3),
cannam@35 42 m_silence(-70)
cannam@7 43 {
cannam@7 44 }
cannam@7 45
cannam@7 46 Tempo::~Tempo()
cannam@7 47 {
cannam@7 48 if (m_ibuf) del_fvec(m_ibuf);
cannam@35 49 if (m_beat) del_fvec(m_beat);
cannam@35 50 if (m_tempo) del_aubio_tempo(m_tempo);
cannam@7 51 }
cannam@7 52
cannam@7 53 string
cannam@13 54 Tempo::getIdentifier() const
cannam@7 55 {
cannam@7 56 return "aubiotempo";
cannam@7 57 }
cannam@7 58
cannam@7 59 string
cannam@13 60 Tempo::getName() const
cannam@7 61 {
cannam@25 62 return "Aubio Beat Tracker";
cannam@7 63 }
cannam@7 64
cannam@7 65 string
cannam@13 66 Tempo::getDescription() const
cannam@13 67 {
cannam@23 68 return "Estimate the musical tempo and track beat positions";
cannam@13 69 }
cannam@13 70
cannam@13 71 string
cannam@7 72 Tempo::getMaker() const
cannam@7 73 {
cannam@13 74 return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
cannam@7 75 }
cannam@7 76
cannam@7 77 int
cannam@7 78 Tempo::getPluginVersion() const
cannam@7 79 {
cannam@31 80 return 2;
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@32 92 if (channels != 1) {
cannam@32 93 std::cerr << "Tempo::initialise: channels must be 1" << std::endl;
cannam@32 94 return false;
cannam@32 95 }
cannam@32 96
cannam@7 97 m_stepSize = stepSize;
cannam@7 98 m_blockSize = blockSize;
cannam@7 99
cannam@32 100 m_ibuf = new_fvec(stepSize);
cannam@35 101 m_beat = new_fvec(2);
cannam@7 102
cannam@7 103 m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
cannam@7 104 lrintf(m_inputSampleRate));
cannam@7 105
cannam@37 106 reset();
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@37 114 if (m_tempo) del_aubio_tempo(m_tempo);
cannam@37 115
cannam@37 116 m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
cannam@37 117
cannam@37 118 m_tempo = new_aubio_tempo
cannam@37 119 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
cannam@37 120 m_blockSize,
cannam@37 121 m_stepSize,
cannam@37 122 lrintf(m_inputSampleRate));
cannam@37 123
cannam@37 124 aubio_tempo_set_silence(m_tempo, m_silence);
cannam@37 125 aubio_tempo_set_threshold(m_tempo, m_threshold);
cannam@7 126 }
cannam@7 127
cannam@7 128 size_t
cannam@7 129 Tempo::getPreferredStepSize() const
cannam@7 130 {
cannam@7 131 return 512;
cannam@7 132 }
cannam@7 133
cannam@7 134 size_t
cannam@7 135 Tempo::getPreferredBlockSize() const
cannam@7 136 {
cannam@7 137 return 2 * getPreferredStepSize();
cannam@7 138 }
cannam@7 139
cannam@7 140 Tempo::ParameterList
cannam@7 141 Tempo::getParameterDescriptors() const
cannam@7 142 {
cannam@7 143 ParameterList list;
cannam@7 144
cannam@7 145 ParameterDescriptor desc;
cannam@13 146 desc.identifier = "onsettype";
cannam@13 147 desc.name = "Onset Detection Function Type";
piem@67 148 desc.description = "Type of onset detection function to use";
cannam@7 149 desc.minValue = 0;
cannam@35 150 desc.maxValue = 7;
cannam@35 151 desc.defaultValue = (int)OnsetComplex;
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@35 161 desc.valueNames.push_back("Spectral Flux");
cannam@7 162 list.push_back(desc);
cannam@7 163
cannam@7 164 desc = ParameterDescriptor();
cannam@13 165 desc.identifier = "peakpickthreshold";
cannam@13 166 desc.name = "Peak Picker Threshold";
piem@67 167 desc.description = "Peak picking threshold, the higher the least detection";
cannam@7 168 desc.minValue = 0;
cannam@7 169 desc.maxValue = 1;
cannam@7 170 desc.defaultValue = 0.3;
cannam@7 171 desc.isQuantized = false;
cannam@7 172 list.push_back(desc);
cannam@7 173
cannam@7 174 desc = ParameterDescriptor();
cannam@13 175 desc.identifier = "silencethreshold";
cannam@13 176 desc.name = "Silence Threshold";
piem@67 177 desc.description = "Silence threshold, the higher the least detection";
cannam@7 178 desc.minValue = -120;
cannam@7 179 desc.maxValue = 0;
cannam@35 180 desc.defaultValue = -70;
cannam@7 181 desc.unit = "dB";
cannam@7 182 desc.isQuantized = false;
cannam@7 183 list.push_back(desc);
cannam@7 184
cannam@7 185 return list;
cannam@7 186 }
cannam@7 187
cannam@7 188 float
cannam@7 189 Tempo::getParameter(std::string param) const
cannam@7 190 {
cannam@7 191 if (param == "onsettype") {
cannam@7 192 return m_onsettype;
cannam@7 193 } else if (param == "peakpickthreshold") {
cannam@7 194 return m_threshold;
cannam@7 195 } else if (param == "silencethreshold") {
cannam@7 196 return m_silence;
cannam@7 197 } else {
cannam@7 198 return 0.0;
cannam@7 199 }
cannam@7 200 }
cannam@7 201
cannam@7 202 void
cannam@7 203 Tempo::setParameter(std::string param, float value)
cannam@7 204 {
cannam@7 205 if (param == "onsettype") {
cannam@7 206 switch (lrintf(value)) {
cannam@35 207 case 0: m_onsettype = OnsetEnergy; break;
cannam@35 208 case 1: m_onsettype = OnsetSpecDiff; break;
cannam@35 209 case 2: m_onsettype = OnsetHFC; break;
cannam@35 210 case 3: m_onsettype = OnsetComplex; break;
cannam@35 211 case 4: m_onsettype = OnsetPhase; break;
cannam@35 212 case 5: m_onsettype = OnsetKL; break;
cannam@35 213 case 6: m_onsettype = OnsetMKL; break;
cannam@35 214 case 7: m_onsettype = OnsetSpecFlux; break;
cannam@7 215 }
cannam@7 216 } else if (param == "peakpickthreshold") {
cannam@7 217 m_threshold = value;
cannam@7 218 } else if (param == "silencethreshold") {
cannam@7 219 m_silence = value;
cannam@7 220 }
cannam@7 221 }
cannam@7 222
cannam@7 223 Tempo::OutputList
cannam@7 224 Tempo::getOutputDescriptors() const
cannam@7 225 {
cannam@7 226 OutputList list;
cannam@7 227
cannam@7 228 OutputDescriptor d;
cannam@13 229 d.identifier = "beats";
cannam@13 230 d.name = "Beats";
piem@67 231 d.description = "List of times at which a beat was detected";
cannam@7 232 d.unit = "";
cannam@7 233 d.hasFixedBinCount = true;
cannam@7 234 d.binCount = 0;
cannam@7 235 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@7 236 d.sampleRate = 0;
cannam@7 237 list.push_back(d);
cannam@7 238
cannam@13 239 d.identifier = "tempo";
cannam@13 240 d.name = "Tempo";
piem@68 241 d.description = "Overall estimated tempo";
cannam@12 242 d.unit = "bpm";
cannam@12 243 d.hasFixedBinCount = true;
cannam@12 244 d.binCount = 1;
cannam@12 245 d.hasKnownExtents = false;
cannam@12 246 d.isQuantized = false;
cannam@12 247 d.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@12 248 list.push_back(d);
cannam@12 249
cannam@7 250 return list;
cannam@7 251 }
cannam@7 252
cannam@7 253 Tempo::FeatureSet
cannam@12 254 Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
cannam@7 255 {
cannam@7 256 for (size_t i = 0; i < m_stepSize; ++i) {
piem@52 257 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
cannam@7 258 }
cannam@7 259
cannam@35 260 aubio_tempo_do(m_tempo, m_ibuf, m_beat);
cannam@7 261
cannam@35 262 bool istactus = m_beat->data[0];
cannam@12 263
cannam@35 264 m_bpm = aubio_tempo_get_bpm(m_tempo);
cannam@7 265
cannam@7 266 FeatureSet returnFeatures;
cannam@7 267
cannam@7 268 if (istactus == true) {
cannam@7 269 if (timestamp - m_lastBeat >= m_delay) {
cannam@7 270 Feature onsettime;
cannam@7 271 onsettime.hasTimestamp = true;
cannam@7 272 if (timestamp < m_delay) timestamp = m_delay;
cannam@7 273 onsettime.timestamp = timestamp - m_delay;
cannam@7 274 returnFeatures[0].push_back(onsettime);
cannam@7 275 m_lastBeat = timestamp;
cannam@7 276 }
cannam@7 277 }
cannam@7 278
cannam@35 279 if (m_bpm >= 30 && m_bpm <= 206) {
cannam@12 280 Feature tempo;
cannam@12 281 tempo.hasTimestamp = false;
cannam@35 282 tempo.values.push_back(m_bpm);
cannam@12 283 returnFeatures[1].push_back(tempo);
cannam@12 284 }
cannam@12 285
cannam@7 286 return returnFeatures;
cannam@7 287 }
cannam@7 288
cannam@7 289 Tempo::FeatureSet
cannam@7 290 Tempo::getRemainingFeatures()
cannam@7 291 {
cannam@7 292 return FeatureSet();
cannam@7 293 }
cannam@7 294