cannam@7: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@7:
cannam@7: /*
cannam@7: Vamp feature extraction plugins using Paul Brossier's Aubio library.
cannam@7:
cannam@7: Centre for Digital Music, Queen Mary, University of London.
cannam@7: This file copyright 2006 Chris Cannam.
cannam@7:
piem@112: This file is part of vamp-aubio-plugins.
piem@112:
piem@112: vamp-aubio is free software: you can redistribute it and/or modify
piem@112: it under the terms of the GNU General Public License as published by
piem@112: the Free Software Foundation, either version 3 of the License, or
piem@112: (at your option) any later version.
piem@112:
piem@112: vamp-aubio is distributed in the hope that it will be useful,
piem@112: but WITHOUT ANY WARRANTY; without even the implied warranty of
piem@112: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
piem@112: GNU General Public License for more details.
piem@112:
piem@112: You should have received a copy of the GNU General Public License
piem@112: along with aubio. If not, see .
cannam@7:
cannam@7: */
cannam@7:
cannam@7: #include
cannam@7: #include "Tempo.h"
cannam@7:
cannam@7: using std::string;
cannam@7: using std::vector;
cannam@7: using std::cerr;
cannam@7: using std::endl;
cannam@7:
cannam@7: Tempo::Tempo(float inputSampleRate) :
cannam@7: Plugin(inputSampleRate),
cannam@7: m_ibuf(0),
cannam@35: m_beat(0),
cannam@35: m_bpm(0),
cannam@35: m_onsettype(OnsetComplex),
cannam@35: m_tempo(0),
cannam@7: m_threshold(0.3),
cannam@35: m_silence(-70)
cannam@7: {
cannam@7: }
cannam@7:
cannam@7: Tempo::~Tempo()
cannam@7: {
cannam@7: if (m_ibuf) del_fvec(m_ibuf);
cannam@35: if (m_beat) del_fvec(m_beat);
cannam@35: if (m_tempo) del_aubio_tempo(m_tempo);
cannam@7: }
cannam@7:
cannam@7: string
cannam@13: Tempo::getIdentifier() const
cannam@7: {
cannam@7: return "aubiotempo";
cannam@7: }
cannam@7:
cannam@7: string
cannam@13: Tempo::getName() const
cannam@7: {
cannam@25: return "Aubio Beat Tracker";
cannam@7: }
cannam@7:
cannam@7: string
cannam@13: Tempo::getDescription() const
cannam@13: {
cannam@23: return "Estimate the musical tempo and track beat positions";
cannam@13: }
cannam@13:
cannam@13: string
cannam@7: Tempo::getMaker() const
cannam@7: {
cannam@13: return "Paul Brossier (method by Matthew Davies, plugin by Chris Cannam)";
cannam@7: }
cannam@7:
cannam@7: int
cannam@7: Tempo::getPluginVersion() const
cannam@7: {
cannam@31: return 2;
cannam@7: }
cannam@7:
cannam@7: string
cannam@7: Tempo::getCopyright() const
cannam@7: {
cannam@7: return "GPL";
cannam@7: }
cannam@7:
cannam@7: bool
cannam@7: Tempo::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@7: {
cannam@32: if (channels != 1) {
cannam@32: std::cerr << "Tempo::initialise: channels must be 1" << std::endl;
cannam@32: return false;
cannam@32: }
cannam@32:
cannam@7: m_stepSize = stepSize;
cannam@7: m_blockSize = blockSize;
cannam@7:
cannam@32: m_ibuf = new_fvec(stepSize);
cannam@35: m_beat = new_fvec(2);
cannam@7:
cannam@7: m_delay = Vamp::RealTime::frame2RealTime(3 * stepSize,
cannam@7: lrintf(m_inputSampleRate));
cannam@7:
cannam@37: reset();
cannam@7:
cannam@7: return true;
cannam@7: }
cannam@7:
cannam@7: void
cannam@7: Tempo::reset()
cannam@7: {
cannam@37: if (m_tempo) del_aubio_tempo(m_tempo);
cannam@37:
cannam@37: m_lastBeat = Vamp::RealTime::zeroTime - m_delay - m_delay;
cannam@37:
cannam@37: m_tempo = new_aubio_tempo
cannam@37: (const_cast(getAubioNameForOnsetType(m_onsettype)),
cannam@37: m_blockSize,
cannam@37: m_stepSize,
cannam@37: lrintf(m_inputSampleRate));
cannam@37:
cannam@37: aubio_tempo_set_silence(m_tempo, m_silence);
cannam@37: aubio_tempo_set_threshold(m_tempo, m_threshold);
cannam@7: }
cannam@7:
cannam@7: size_t
cannam@7: Tempo::getPreferredStepSize() const
cannam@7: {
cannam@7: return 512;
cannam@7: }
cannam@7:
cannam@7: size_t
cannam@7: Tempo::getPreferredBlockSize() const
cannam@7: {
cannam@7: return 2 * getPreferredStepSize();
cannam@7: }
cannam@7:
cannam@7: Tempo::ParameterList
cannam@7: Tempo::getParameterDescriptors() const
cannam@7: {
cannam@7: ParameterList list;
cannam@7:
cannam@7: ParameterDescriptor desc;
cannam@13: desc.identifier = "onsettype";
cannam@13: desc.name = "Onset Detection Function Type";
piem@67: desc.description = "Type of onset detection function to use";
cannam@7: desc.minValue = 0;
cannam@35: desc.maxValue = 7;
cannam@35: desc.defaultValue = (int)OnsetComplex;
cannam@7: desc.isQuantized = true;
cannam@7: desc.quantizeStep = 1;
cannam@7: desc.valueNames.push_back("Energy Based");
cannam@7: desc.valueNames.push_back("Spectral Difference");
cannam@7: desc.valueNames.push_back("High-Frequency Content");
cannam@7: desc.valueNames.push_back("Complex Domain");
cannam@7: desc.valueNames.push_back("Phase Deviation");
cannam@7: desc.valueNames.push_back("Kullback-Liebler");
cannam@7: desc.valueNames.push_back("Modified Kullback-Liebler");
cannam@35: desc.valueNames.push_back("Spectral Flux");
cannam@7: list.push_back(desc);
cannam@7:
cannam@7: desc = ParameterDescriptor();
cannam@13: desc.identifier = "peakpickthreshold";
cannam@13: desc.name = "Peak Picker Threshold";
piem@67: desc.description = "Peak picking threshold, the higher the least detection";
cannam@7: desc.minValue = 0;
cannam@7: desc.maxValue = 1;
cannam@7: desc.defaultValue = 0.3;
cannam@7: desc.isQuantized = false;
cannam@7: list.push_back(desc);
cannam@7:
cannam@7: desc = ParameterDescriptor();
cannam@13: desc.identifier = "silencethreshold";
cannam@13: desc.name = "Silence Threshold";
piem@67: desc.description = "Silence threshold, the higher the least detection";
cannam@7: desc.minValue = -120;
cannam@7: desc.maxValue = 0;
cannam@35: desc.defaultValue = -70;
cannam@7: desc.unit = "dB";
cannam@7: desc.isQuantized = false;
cannam@7: list.push_back(desc);
cannam@7:
cannam@7: return list;
cannam@7: }
cannam@7:
cannam@7: float
cannam@7: Tempo::getParameter(std::string param) const
cannam@7: {
cannam@7: if (param == "onsettype") {
cannam@7: return m_onsettype;
cannam@7: } else if (param == "peakpickthreshold") {
cannam@7: return m_threshold;
cannam@7: } else if (param == "silencethreshold") {
cannam@7: return m_silence;
cannam@7: } else {
cannam@7: return 0.0;
cannam@7: }
cannam@7: }
cannam@7:
cannam@7: void
cannam@7: Tempo::setParameter(std::string param, float value)
cannam@7: {
cannam@7: if (param == "onsettype") {
cannam@7: switch (lrintf(value)) {
cannam@35: case 0: m_onsettype = OnsetEnergy; break;
cannam@35: case 1: m_onsettype = OnsetSpecDiff; break;
cannam@35: case 2: m_onsettype = OnsetHFC; break;
cannam@35: case 3: m_onsettype = OnsetComplex; break;
cannam@35: case 4: m_onsettype = OnsetPhase; break;
cannam@35: case 5: m_onsettype = OnsetKL; break;
cannam@35: case 6: m_onsettype = OnsetMKL; break;
cannam@35: case 7: m_onsettype = OnsetSpecFlux; break;
cannam@7: }
cannam@7: } else if (param == "peakpickthreshold") {
cannam@7: m_threshold = value;
cannam@7: } else if (param == "silencethreshold") {
cannam@7: m_silence = value;
cannam@7: }
cannam@7: }
cannam@7:
cannam@7: Tempo::OutputList
cannam@7: Tempo::getOutputDescriptors() const
cannam@7: {
cannam@7: OutputList list;
cannam@7:
cannam@7: OutputDescriptor d;
cannam@13: d.identifier = "beats";
cannam@13: d.name = "Beats";
piem@67: d.description = "List of times at which a beat was detected";
cannam@7: d.unit = "";
cannam@7: d.hasFixedBinCount = true;
cannam@7: d.binCount = 0;
cannam@7: d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@7: d.sampleRate = 0;
cannam@7: list.push_back(d);
cannam@7:
cannam@13: d.identifier = "tempo";
cannam@13: d.name = "Tempo";
piem@68: d.description = "Overall estimated tempo";
cannam@12: d.unit = "bpm";
cannam@12: d.hasFixedBinCount = true;
cannam@12: d.binCount = 1;
cannam@12: d.hasKnownExtents = false;
cannam@12: d.isQuantized = false;
cannam@12: d.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@12: list.push_back(d);
cannam@12:
cannam@7: return list;
cannam@7: }
cannam@7:
cannam@7: Tempo::FeatureSet
cannam@12: Tempo::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
cannam@7: {
cannam@7: for (size_t i = 0; i < m_stepSize; ++i) {
piem@52: fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
cannam@7: }
cannam@7:
cannam@35: aubio_tempo_do(m_tempo, m_ibuf, m_beat);
cannam@7:
cannam@35: bool istactus = m_beat->data[0];
cannam@12:
cannam@35: m_bpm = aubio_tempo_get_bpm(m_tempo);
cannam@7:
cannam@7: FeatureSet returnFeatures;
cannam@7:
cannam@7: if (istactus == true) {
cannam@7: if (timestamp - m_lastBeat >= m_delay) {
cannam@7: Feature onsettime;
cannam@7: onsettime.hasTimestamp = true;
cannam@7: if (timestamp < m_delay) timestamp = m_delay;
cannam@7: onsettime.timestamp = timestamp - m_delay;
cannam@7: returnFeatures[0].push_back(onsettime);
cannam@7: m_lastBeat = timestamp;
cannam@7: }
cannam@7: }
cannam@7:
cannam@35: if (m_bpm >= 30 && m_bpm <= 206) {
cannam@12: Feature tempo;
cannam@12: tempo.hasTimestamp = false;
cannam@35: tempo.values.push_back(m_bpm);
cannam@12: returnFeatures[1].push_back(tempo);
cannam@12: }
cannam@12:
cannam@7: return returnFeatures;
cannam@7: }
cannam@7:
cannam@7: Tempo::FeatureSet
cannam@7: Tempo::getRemainingFeatures()
cannam@7: {
cannam@7: return FeatureSet();
cannam@7: }
cannam@7: