c@27: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@27: c@27: /* c@27: QM Vamp Plugin Set c@27: c@27: Centre for Digital Music, Queen Mary, University of London. c@135: c@135: This program is free software; you can redistribute it and/or c@135: modify it under the terms of the GNU General Public License as c@135: published by the Free Software Foundation; either version 2 of the c@135: License, or (at your option) any later version. See the file c@135: COPYING included with this distribution for more information. c@27: */ c@27: c@27: #include "BeatTrack.h" c@27: c@27: #include c@27: #include c@27: #include c@86: #include c@27: c@27: using std::string; c@27: using std::vector; c@27: using std::cerr; c@27: using std::endl; c@27: c@86: float BeatTracker::m_stepSecs = 0.01161; // 512 samples at 44100 c@86: c@86: #define METHOD_OLD 0 c@86: #define METHOD_NEW 1 c@27: c@27: class BeatTrackerData c@27: { c@27: public: c@27: BeatTrackerData(const DFConfig &config) : dfConfig(config) { luis@144: df = new DetectionFunction(config); c@27: } c@27: ~BeatTrackerData() { luis@144: delete df; c@27: } c@27: void reset() { luis@144: delete df; luis@144: df = new DetectionFunction(dfConfig); luis@144: dfOutput.clear(); c@85: origin = Vamp::RealTime::zeroTime; c@27: } c@27: c@27: DFConfig dfConfig; c@27: DetectionFunction *df; c@27: vector dfOutput; c@85: Vamp::RealTime origin; c@27: }; luis@144: c@27: c@27: BeatTracker::BeatTracker(float inputSampleRate) : c@27: Vamp::Plugin(inputSampleRate), c@27: m_d(0), c@86: m_method(METHOD_NEW), c@30: m_dfType(DF_COMPLEXSD), luis@144: m_alpha(0.9), // MEPD new exposed parameter for beat tracker, default value = 0.9 (as old version) c@148: m_tightness(4.), luis@144: m_inputtempo(120.), // MEPD new exposed parameter for beat tracker, default value = 120. (as old version) c@178: m_constraintempo(false), // MEPD new exposed parameter for beat tracker, default value = false (as old version) luis@144: // calling the beat tracker with these default parameters will give the same output as the previous existing version c@178: m_whiten(false) luis@144: c@27: { c@27: } c@27: c@27: BeatTracker::~BeatTracker() c@27: { c@27: delete m_d; c@27: } c@27: c@27: string c@27: BeatTracker::getIdentifier() const c@27: { c@27: return "qm-tempotracker"; c@27: } c@27: c@27: string c@27: BeatTracker::getName() const c@27: { c@27: return "Tempo and Beat Tracker"; c@27: } c@27: c@27: string c@27: BeatTracker::getDescription() const c@27: { c@27: return "Estimate beat locations and tempo"; c@27: } c@27: c@27: string c@27: BeatTracker::getMaker() const c@27: { c@50: return "Queen Mary, University of London"; c@27: } c@27: c@27: int c@27: BeatTracker::getPluginVersion() const c@27: { c@145: return 6; c@27: } c@27: c@27: string c@27: BeatTracker::getCopyright() const c@27: { c@149: return "Plugin by Christian Landone and Matthew Davies. Copyright (c) 2006-2013 QMUL - All Rights Reserved"; c@27: } c@27: c@27: BeatTracker::ParameterList c@27: BeatTracker::getParameterDescriptors() const c@27: { c@27: ParameterList list; c@27: c@27: ParameterDescriptor desc; c@86: c@86: desc.identifier = "method"; c@86: desc.name = "Beat Tracking Method"; c@119: desc.description = "Basic method to use "; c@86: desc.minValue = 0; c@86: desc.maxValue = 1; c@86: desc.defaultValue = METHOD_NEW; c@86: desc.isQuantized = true; c@86: desc.quantizeStep = 1; c@86: desc.valueNames.push_back("Old"); c@86: desc.valueNames.push_back("New"); c@86: list.push_back(desc); c@86: c@27: desc.identifier = "dftype"; c@27: desc.name = "Onset Detection Function Type"; c@27: desc.description = "Method used to calculate the onset detection function"; c@27: desc.minValue = 0; c@31: desc.maxValue = 4; c@27: desc.defaultValue = 3; c@86: desc.valueNames.clear(); c@27: desc.valueNames.push_back("High-Frequency Content"); c@27: desc.valueNames.push_back("Spectral Difference"); c@27: desc.valueNames.push_back("Phase Deviation"); c@27: desc.valueNames.push_back("Complex Domain"); c@27: desc.valueNames.push_back("Broadband Energy Rise"); c@27: list.push_back(desc); c@27: c@30: desc.identifier = "whiten"; c@30: desc.name = "Adaptive Whitening"; c@30: desc.description = "Normalize frequency bin magnitudes relative to recent peak levels"; c@30: desc.minValue = 0; c@30: desc.maxValue = 1; c@30: desc.defaultValue = 0; c@30: desc.isQuantized = true; c@30: desc.quantizeStep = 1; c@30: desc.unit = ""; c@30: desc.valueNames.clear(); c@30: list.push_back(desc); c@30: luis@144: // MEPD new exposed parameter - used in the dynamic programming part of the beat tracker luis@144: //Alpha Parameter of Beat Tracker luis@144: desc.identifier = "alpha"; luis@144: desc.name = "Alpha"; luis@144: desc.description = "Inertia - Flexibility Trade Off"; luis@144: desc.minValue = 0.1; luis@144: desc.maxValue = 0.99; luis@144: desc.defaultValue = 0.90; luis@144: desc.unit = ""; luis@144: desc.isQuantized = false; luis@144: list.push_back(desc); luis@144: c@148: // We aren't exposing tightness as a parameter, it's fixed at 4 luis@144: luis@144: // MEPD new exposed parameter - used in the periodicity estimation luis@144: //User input tempo luis@144: desc.identifier = "inputtempo"; c@148: desc.name = "Tempo Hint"; c@151: desc.description = "User-defined tempo on which to centre the tempo preference function"; luis@144: desc.minValue = 50; luis@144: desc.maxValue = 250; luis@144: desc.defaultValue = 120; luis@144: desc.unit = "BPM"; luis@144: desc.isQuantized = true; luis@144: list.push_back(desc); luis@144: luis@144: // MEPD new exposed parameter - used in periodicity estimation luis@144: desc.identifier = "constraintempo"; luis@144: desc.name = "Constrain Tempo"; c@148: desc.description = "Constrain more tightly around the tempo hint, using a Gaussian weighting instead of Rayleigh"; luis@144: desc.minValue = 0; luis@144: desc.maxValue = 1; luis@144: desc.defaultValue = 0; luis@144: desc.isQuantized = true; luis@144: desc.quantizeStep = 1; luis@144: desc.unit = ""; luis@144: desc.valueNames.clear(); luis@144: list.push_back(desc); luis@144: luis@144: luis@144: c@27: return list; c@27: } c@27: c@27: float c@27: BeatTracker::getParameter(std::string name) const c@27: { c@27: if (name == "dftype") { c@27: switch (m_dfType) { c@27: case DF_HFC: return 0; c@27: case DF_SPECDIFF: return 1; c@27: case DF_PHASEDEV: return 2; c@27: default: case DF_COMPLEXSD: return 3; c@27: case DF_BROADBAND: return 4; c@27: } c@86: } else if (name == "method") { c@86: return m_method; c@30: } else if (name == "whiten") { luis@144: return m_whiten ? 1.0 : 0.0; luis@144: } else if (name == "alpha") { luis@144: return m_alpha; luis@144: } else if (name == "inputtempo") { luis@144: return m_inputtempo; luis@144: } else if (name == "constraintempo") { luis@144: return m_constraintempo ? 1.0 : 0.0; c@27: } c@27: return 0.0; c@27: } c@27: c@27: void c@27: BeatTracker::setParameter(std::string name, float value) c@27: { c@27: if (name == "dftype") { c@27: switch (lrintf(value)) { c@27: case 0: m_dfType = DF_HFC; break; c@27: case 1: m_dfType = DF_SPECDIFF; break; c@27: case 2: m_dfType = DF_PHASEDEV; break; c@27: default: case 3: m_dfType = DF_COMPLEXSD; break; c@27: case 4: m_dfType = DF_BROADBAND; break; c@27: } c@86: } else if (name == "method") { c@86: m_method = lrintf(value); c@30: } else if (name == "whiten") { c@30: m_whiten = (value > 0.5); luis@144: } else if (name == "alpha") { luis@144: m_alpha = value; luis@144: } else if (name == "inputtempo") { luis@144: m_inputtempo = value; luis@144: } else if (name == "constraintempo") { luis@144: m_constraintempo = (value > 0.5); c@27: } c@27: } c@27: c@27: bool c@27: BeatTracker::initialise(size_t channels, size_t stepSize, size_t blockSize) c@27: { c@27: if (m_d) { luis@144: delete m_d; luis@144: m_d = 0; c@27: } c@27: c@27: if (channels < getMinChannelCount() || luis@144: channels > getMaxChannelCount()) { c@27: std::cerr << "BeatTracker::initialise: Unsupported channel count: " c@27: << channels << std::endl; c@27: return false; c@27: } c@27: c@28: if (stepSize != getPreferredStepSize()) { c@28: std::cerr << "ERROR: BeatTracker::initialise: Unsupported step size for this sample rate: " c@28: << stepSize << " (wanted " << (getPreferredStepSize()) << ")" << std::endl; c@27: return false; c@27: } c@27: c@28: if (blockSize != getPreferredBlockSize()) { c@29: std::cerr << "WARNING: BeatTracker::initialise: Sub-optimal block size for this sample rate: " c@28: << blockSize << " (wanted " << getPreferredBlockSize() << ")" << std::endl; c@28: // return false; c@27: } c@27: c@27: DFConfig dfConfig; c@27: dfConfig.DFType = m_dfType; c@27: dfConfig.stepSize = stepSize; c@27: dfConfig.frameLength = blockSize; c@27: dfConfig.dbRise = 3; c@30: dfConfig.adaptiveWhitening = m_whiten; c@30: dfConfig.whiteningRelaxCoeff = -1; c@30: dfConfig.whiteningFloor = -1; luis@144: c@27: m_d = new BeatTrackerData(dfConfig); c@27: return true; c@27: } c@27: c@27: void c@27: BeatTracker::reset() c@27: { c@27: if (m_d) m_d->reset(); c@27: } c@27: c@27: size_t c@27: BeatTracker::getPreferredStepSize() const c@27: { c@27: size_t step = size_t(m_inputSampleRate * m_stepSecs + 0.0001); c@27: // std::cerr << "BeatTracker::getPreferredStepSize: input sample rate is " << m_inputSampleRate << ", step size is " << step << std::endl; c@27: return step; c@27: } c@27: c@27: size_t c@27: BeatTracker::getPreferredBlockSize() const c@27: { c@28: size_t theoretical = getPreferredStepSize() * 2; c@28: c@52: // I think this is not necessarily going to be a power of two, and c@52: // the host might have a problem with that, but I'm not sure we c@52: // can do much about it here c@28: return theoretical; c@27: } c@27: c@27: BeatTracker::OutputList c@27: BeatTracker::getOutputDescriptors() const c@27: { c@27: OutputList list; c@27: c@27: OutputDescriptor beat; c@27: beat.identifier = "beats"; c@27: beat.name = "Beats"; c@27: beat.description = "Estimated metrical beat locations"; c@27: beat.unit = ""; c@27: beat.hasFixedBinCount = true; c@27: beat.binCount = 0; c@27: beat.sampleType = OutputDescriptor::VariableSampleRate; c@27: beat.sampleRate = 1.0 / m_stepSecs; c@27: c@27: OutputDescriptor df; c@27: df.identifier = "detection_fn"; c@27: df.name = "Onset Detection Function"; c@27: df.description = "Probability function of note onset likelihood"; c@27: df.unit = ""; c@27: df.hasFixedBinCount = true; c@27: df.binCount = 1; c@27: df.hasKnownExtents = false; c@27: df.isQuantized = false; c@27: df.sampleType = OutputDescriptor::OneSamplePerStep; c@27: c@27: OutputDescriptor tempo; c@27: tempo.identifier = "tempo"; c@27: tempo.name = "Tempo"; c@27: tempo.description = "Locked tempo estimates"; c@27: tempo.unit = "bpm"; c@27: tempo.hasFixedBinCount = true; c@27: tempo.binCount = 1; c@31: tempo.hasKnownExtents = false; c@31: tempo.isQuantized = false; c@27: tempo.sampleType = OutputDescriptor::VariableSampleRate; c@27: tempo.sampleRate = 1.0 / m_stepSecs; c@27: c@27: list.push_back(beat); c@27: list.push_back(df); c@27: list.push_back(tempo); c@27: c@27: return list; c@27: } c@27: c@27: BeatTracker::FeatureSet c@27: BeatTracker::process(const float *const *inputBuffers, c@85: Vamp::RealTime timestamp) c@27: { c@27: if (!m_d) { luis@144: cerr << "ERROR: BeatTracker::process: " luis@144: << "BeatTracker has not been initialised" luis@144: << endl; luis@144: return FeatureSet(); c@27: } c@27: c@153: size_t len = m_d->dfConfig.frameLength / 2 + 1; c@27: c@153: double *reals = new double[len]; c@153: double *imags = new double[len]; c@27: c@27: // We only support a single input channel c@27: c@27: for (size_t i = 0; i < len; ++i) { c@153: reals[i] = inputBuffers[0][i*2]; c@153: imags[i] = inputBuffers[0][i*2+1]; c@27: } c@27: c@153: double output = m_d->df->processFrequencyDomain(reals, imags); c@27: c@153: delete[] reals; c@153: delete[] imags; c@27: c@85: if (m_d->dfOutput.empty()) m_d->origin = timestamp; c@85: c@27: m_d->dfOutput.push_back(output); c@27: c@27: FeatureSet returnFeatures; c@27: c@27: Feature feature; c@27: feature.hasTimestamp = false; c@27: feature.values.push_back(output); c@27: c@27: returnFeatures[1].push_back(feature); // detection function is output 1 c@27: return returnFeatures; c@27: } c@27: c@27: BeatTracker::FeatureSet c@27: BeatTracker::getRemainingFeatures() c@27: { c@27: if (!m_d) { luis@144: cerr << "ERROR: BeatTracker::getRemainingFeatures: " luis@144: << "BeatTracker has not been initialised" luis@144: << endl; luis@144: return FeatureSet(); c@27: } c@27: c@86: if (m_method == METHOD_OLD) return beatTrackOld(); c@86: else return beatTrackNew(); c@86: } c@86: c@86: BeatTracker::FeatureSet c@86: BeatTracker::beatTrackOld() c@86: { c@27: double aCoeffs[] = { 1.0000, -0.5949, 0.2348 }; c@27: double bCoeffs[] = { 0.1600, 0.3200, 0.1600 }; c@27: c@27: TTParams ttParams; c@27: ttParams.winLength = 512; c@27: ttParams.lagLength = 128; c@27: ttParams.LPOrd = 2; c@27: ttParams.LPACoeffs = aCoeffs; c@27: ttParams.LPBCoeffs = bCoeffs; c@27: ttParams.alpha = 9; c@27: ttParams.WinT.post = 8; c@27: ttParams.WinT.pre = 7; c@27: c@27: TempoTrack tempoTracker(ttParams); c@27: c@87: vector tempi; c@87: vector beats = tempoTracker.process(m_d->dfOutput, &tempi); c@27: c@27: FeatureSet returnFeatures; c@27: c@27: char label[100]; c@27: c@27: for (size_t i = 0; i < beats.size(); ++i) { c@27: luis@144: size_t frame = beats[i] * m_d->dfConfig.stepSize; c@27: luis@144: Feature feature; luis@144: feature.hasTimestamp = true; luis@144: feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime luis@144: (frame, lrintf(m_inputSampleRate)); c@27: luis@144: float bpm = 0.0; luis@144: int frameIncrement = 0; c@27: luis@144: if (i < beats.size() - 1) { c@27: luis@144: frameIncrement = (beats[i+1] - beats[i]) * m_d->dfConfig.stepSize; c@27: luis@144: // one beat is frameIncrement frames, so there are luis@144: // samplerate/frameIncrement bps, so luis@144: // 60*samplerate/frameIncrement bpm c@27: luis@144: if (frameIncrement > 0) { luis@144: bpm = (60.0 * m_inputSampleRate) / frameIncrement; luis@144: bpm = int(bpm * 100.0 + 0.5) / 100.0; c@27: sprintf(label, "%.2f bpm", bpm); c@27: feature.label = label; luis@144: } luis@144: } c@27: luis@144: returnFeatures[0].push_back(feature); // beats are output 0 c@27: } c@27: c@27: double prevTempo = 0.0; c@27: c@87: for (size_t i = 0; i < tempi.size(); ++i) { c@27: c@27: size_t frame = i * m_d->dfConfig.stepSize * ttParams.lagLength; c@27: c@27: // std::cerr << "unit " << i << ", step size " << m_d->dfConfig.stepSize << ", hop " << ttParams.lagLength << ", frame = " << frame << std::endl; luis@144: c@87: if (tempi[i] > 1 && int(tempi[i] * 100) != int(prevTempo * 100)) { c@27: Feature feature; c@27: feature.hasTimestamp = true; c@85: feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime c@27: (frame, lrintf(m_inputSampleRate)); c@87: feature.values.push_back(tempi[i]); c@87: sprintf(label, "%.2f bpm", tempi[i]); c@27: feature.label = label; c@27: returnFeatures[2].push_back(feature); // tempo is output 2 c@87: prevTempo = tempi[i]; c@27: } c@27: } c@27: c@27: return returnFeatures; c@27: } c@27: c@86: BeatTracker::FeatureSet c@86: BeatTracker::beatTrackNew() c@86: { c@86: vector df; c@86: vector beatPeriod; c@87: vector tempi; c@86: c@120: size_t nonZeroCount = m_d->dfOutput.size(); c@120: while (nonZeroCount > 0) { c@120: if (m_d->dfOutput[nonZeroCount-1] > 0.0) { c@120: break; c@120: } c@120: --nonZeroCount; c@120: } c@120: c@147: // std::cerr << "Note: nonZeroCount was " << m_d->dfOutput.size() << ", is now " << nonZeroCount << std::endl; c@120: c@120: for (size_t i = 2; i < nonZeroCount; ++i) { // discard first two elts c@86: df.push_back(m_d->dfOutput[i]); c@86: beatPeriod.push_back(0.0); c@86: } c@86: if (df.empty()) return FeatureSet(); c@86: c@88: TempoTrackV2 tt(m_inputSampleRate, m_d->dfConfig.stepSize); c@86: luis@144: luis@144: // MEPD - note this function is now passed 2 new parameters, m_inputtempo and m_constraintempo luis@144: tt.calculateBeatPeriod(df, beatPeriod, tempi, m_inputtempo, m_constraintempo); c@86: c@86: vector beats; luis@144: luis@144: // MEPD - note this function is now passed 2 new parameters, m_alpha and m_tightness luis@144: tt.calculateBeats(df, beatPeriod, beats, m_alpha, m_tightness); luis@144: c@86: FeatureSet returnFeatures; c@86: c@86: char label[100]; c@86: c@86: for (size_t i = 0; i < beats.size(); ++i) { c@86: luis@144: size_t frame = beats[i] * m_d->dfConfig.stepSize; c@86: luis@144: Feature feature; luis@144: feature.hasTimestamp = true; luis@144: feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime luis@144: (frame, lrintf(m_inputSampleRate)); c@86: luis@144: float bpm = 0.0; luis@144: int frameIncrement = 0; c@86: luis@144: if (i+1 < beats.size()) { c@86: luis@144: frameIncrement = (beats[i+1] - beats[i]) * m_d->dfConfig.stepSize; c@86: luis@144: // one beat is frameIncrement frames, so there are luis@144: // samplerate/frameIncrement bps, so luis@144: // 60*samplerate/frameIncrement bpm luis@144: luis@144: if (frameIncrement > 0) { luis@144: bpm = (60.0 * m_inputSampleRate) / frameIncrement; luis@144: bpm = int(bpm * 100.0 + 0.5) / 100.0; c@86: sprintf(label, "%.2f bpm", bpm); c@86: feature.label = label; luis@144: } luis@144: } c@86: luis@144: returnFeatures[0].push_back(feature); // beats are output 0 c@86: } c@86: c@87: double prevTempo = 0.0; c@87: c@87: for (size_t i = 0; i < tempi.size(); ++i) { c@87: luis@144: size_t frame = i * m_d->dfConfig.stepSize; luis@144: c@87: if (tempi[i] > 1 && int(tempi[i] * 100) != int(prevTempo * 100)) { c@87: Feature feature; c@87: feature.hasTimestamp = true; c@87: feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime c@87: (frame, lrintf(m_inputSampleRate)); c@87: feature.values.push_back(tempi[i]); c@87: sprintf(label, "%.2f bpm", tempi[i]); c@87: feature.label = label; c@87: returnFeatures[2].push_back(feature); // tempo is output 2 c@87: prevTempo = tempi[i]; c@87: } c@87: } c@87: c@86: return returnFeatures; c@86: }