adamstark@46: //======================================================================= adamstark@46: /** @file BTrack.h adamstark@47: * @brief BTrack - a real-time beat tracker adamstark@46: * @author Adam Stark adamstark@46: * @copyright Copyright (C) 2008-2014 Queen Mary University of London adamstark@46: * adamstark@46: * This program is free software: you can redistribute it and/or modify adamstark@46: * it under the terms of the GNU General Public License as published by adamstark@46: * the Free Software Foundation, either version 3 of the License, or adamstark@46: * (at your option) any later version. adamstark@46: * adamstark@46: * This program is distributed in the hope that it will be useful, adamstark@46: * but WITHOUT ANY WARRANTY; without even the implied warranty of adamstark@46: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the adamstark@46: * GNU General Public License for more details. adamstark@46: * adamstark@46: * You should have received a copy of the GNU General Public License adamstark@46: * along with this program. If not, see . adamstark@46: */ adamstark@46: //======================================================================= adamstark@46: adamstark@46: #ifndef __BTRACK_H adamstark@46: #define __BTRACK_H adamstark@46: adamstark@55: #include "OnsetDetectionFunction.h" adamstark@63: #include adamstark@55: adamstark@60: //======================================================================= adamstark@60: /** The main beat tracking class and the interface to the BTrack adamstark@60: * beat tracking algorithm. The algorithm can process either adamstark@60: * audio frames or onset detection function samples and also adamstark@60: * contains some static functions for calculating beat times in seconds adamstark@60: */ adamstark@46: class BTrack { adamstark@46: adamstark@46: public: adamstark@51: adamstark@58: //======================================================================= adamstark@61: /** Constructor assuming hop size of 512 and frame size of 1024 */ adamstark@56: BTrack(); adamstark@51: adamstark@61: /** Constructor assuming frame size will be double the hopSize adamstark@60: * @param hopSize the hop size in audio samples adamstark@55: */ adamstark@57: BTrack(int hopSize_); adamstark@55: adamstark@61: /** Constructor taking both hopSize and frameSize adamstark@60: * @param hopSize the hop size in audio samples adamstark@60: * @param frameSize the frame size in audio samples adamstark@55: */ adamstark@57: BTrack(int hopSize_,int frameSize_); adamstark@55: adamstark@58: //======================================================================= adamstark@65: /** Updates the hop and frame size used by the beat tracker adamstark@65: * @param hopSize the hop size in audio samples adamstark@65: * @param frameSize the frame size in audio samples adamstark@65: */ adamstark@65: void updateHopAndFrameSize(int hopSize_,int frameSize_); adamstark@65: adamstark@65: //======================================================================= adamstark@60: /** Process a single audio frame adamstark@60: * @param frame a pointer to an array containing an audio frame. The number of samples should adamstark@60: * match the frame size that the algorithm was initialised with. adamstark@60: */ adamstark@56: void processAudioFrame(double *frame); adamstark@55: adamstark@60: /** Add new onset detection function sample to buffer and apply beat tracking adamstark@60: * @param sample an onset detection function sample adamstark@60: */ adamstark@56: void processOnsetDetectionFunctionSample(double sample); adamstark@51: adamstark@58: //======================================================================= adamstark@57: /** @returns the current hop size being used by the beat tracker */ adamstark@57: int getHopSize(); adamstark@57: adamstark@58: /** @returns true if a beat should occur in the current audio frame */ adamstark@58: bool beatDueInCurrentFrame(); adamstark@58: adamstark@58: /** @returns the current tempo estimate being used by the beat tracker */ adamstark@58: double getCurrentTempoEstimate(); adamstark@58: adamstark@58: /** @returns the most recent value of the cumulative score function */ adamstark@58: double getLatestCumulativeScoreValue(); adamstark@58: adamstark@58: //======================================================================= adamstark@60: /** Set the tempo of the beat tracker adamstark@60: * @param tempo the tempo in beats per minute (bpm) adamstark@60: */ adamstark@57: void setTempo(double tempo); adamstark@51: adamstark@61: /** Fix tempo to roughly around some value, so that the algorithm will only try to track adamstark@60: * tempi around the given tempo adamstark@60: * @param tempo the tempo in beats per minute (bpm) adamstark@60: */ adamstark@57: void fixTempo(double tempo); adamstark@51: adamstark@61: /** Tell the algorithm to not fix the tempo anymore */ adamstark@57: void doNotFixTempo(); adamstark@55: adamstark@58: //======================================================================= adamstark@61: /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency. adamstark@60: * This version uses a long to represent the frame number adamstark@60: * @param frameNumber the index of the current frame adamstark@60: * @param hopSize the hop size in audio samples adamstark@60: * @param fs the sampling frequency in Hz adamstark@60: * @returns a beat time in seconds adamstark@60: */ adamstark@55: static double getBeatTimeInSeconds(long frameNumber,int hopSize,int fs); adamstark@55: adamstark@61: /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency. adamstark@60: * This version uses an int to represent the frame number adamstark@60: * @param frameNumber the index of the current frame adamstark@60: * @param hopSize the hop size in audio samples adamstark@60: * @param fs the sampling frequency in Hz adamstark@60: * @returns a beat time in seconds adamstark@60: */ adamstark@55: static double getBeatTimeInSeconds(int frameNumber,int hopSize,int fs); adamstark@55: adamstark@58: adamstark@46: private: adamstark@51: adamstark@60: /** Initialises the algorithm, setting internal parameters and creating weighting vectors adamstark@60: * @param hopSize_ the hop size in audio samples adamstark@60: * @param frameSize_ the frame size in audio samples adamstark@60: */ adamstark@57: void initialise(int hopSize_,int frameSize_); adamstark@56: adamstark@60: /** Initialise with hop size and set all array sizes accordingly adamstark@60: * @param hopSize_ the hop size in audio samples adamstark@60: */ adamstark@57: void setHopSize(int hopSize_); adamstark@56: adamstark@60: /** Resamples the onset detection function from an arbitrary number of samples to 512 */ adamstark@57: void resampleOnsetDetectionFunction(); adamstark@51: adamstark@60: /** Updates the cumulative score function with a new onset detection function sample adamstark@60: * @param odfSample an onset detection function sample adamstark@60: */ adamstark@59: void updateCumulativeScore(double odfSample); adamstark@51: adamstark@61: /** Predicts the next beat, based upon the internal program state */ adamstark@57: void predictBeat(); adamstark@51: adamstark@51: /** Calculates the current tempo expressed as the beat period in detection function samples */ adamstark@57: void calculateTempo(); adamstark@51: adamstark@61: /** Calculates an adaptive threshold which is used to remove low level energy from detection adamstark@51: * function and emphasise peaks adamstark@60: * @param x a pointer to an array containing onset detection function samples adamstark@60: * @param N the length of the array, x adamstark@51: */ adamstark@57: void adaptiveThreshold(double *x,int N); adamstark@51: adamstark@61: /** Calculates the mean of values in an array between index locations [startIndex,endIndex] adamstark@60: * @param array a pointer to an array that contains the values we wish to find the mean from adamstark@60: * @param startIndex the start index from which we would like to calculate the mean adamstark@60: * @param endIndex the final index to which we would like to calculate the mean adamstark@60: * @returns the mean of the sub-section of the array adamstark@60: */ adamstark@59: double calculateMeanOfArray(double *array,int startIndex,int endIndex); adamstark@51: adamstark@61: /** Normalises a given array adamstark@60: * @param array a pointer to the array we wish to normalise adamstark@60: * @param N the length of the array adamstark@60: */ adamstark@57: void normaliseArray(double *array,int N); adamstark@51: adamstark@61: /** Calculates the balanced autocorrelation of the smoothed onset detection function adamstark@60: * @param onsetDetectionFunction a pointer to an array containing the onset detection function adamstark@60: */ adamstark@60: void calculateBalancedACF(double *onsetDetectionFunction); adamstark@51: adamstark@61: /** Calculates the output of the comb filter bank */ adamstark@57: void calculateOutputOfCombFilterBank(); adamstark@46: adamstark@58: //======================================================================= adamstark@58: adamstark@61: /** An OnsetDetectionFunction instance for calculating onset detection functions */ adamstark@58: OnsetDetectionFunction odf; adamstark@58: adamstark@58: //======================================================================= adamstark@46: // buffers adamstark@63: adamstark@63: std::vector onsetDF; /**< to hold onset detection function */ adamstark@63: std::vector cumulativeScore; /**< to hold cumulative score */ adamstark@63: adamstark@58: double resampledOnsetDF[512]; /**< to hold resampled detection function */ adamstark@46: adamstark@58: double acf[512]; /**< to hold autocorrelation function */ adamstark@46: adamstark@58: double weightingVector[128]; /**< to hold weighting vector */ adamstark@46: adamstark@58: double combFilterBankOutput[128]; /**< to hold comb filter output */ adamstark@58: double tempoObservationVector[41]; /**< to hold tempo version of comb filter output */ adamstark@46: adamstark@58: double delta[41]; /**< to hold final tempo candidate array */ adamstark@58: double prevDelta[41]; /**< previous delta */ adamstark@58: double prevDeltaFixed[41]; /**< fixed tempo version of previous delta */ adamstark@46: adamstark@58: double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */ adamstark@46: adamstark@58: adamstark@60: //======================================================================= adamstark@56: // parameters adamstark@60: adamstark@60: adamstark@60: double tightness; /**< the tightness of the weighting used to calculate cumulative score */ adamstark@60: adamstark@60: double alpha; /**< the mix between the current detection function sample and the cumulative score's "momentum" */ adamstark@60: adamstark@60: double beatPeriod; /**< the beat period, in detection function samples */ adamstark@60: adamstark@60: double tempo; /**< the tempo in beats per minute */ adamstark@46: adamstark@58: double estimatedTempo; /**< the current tempo estimation being used by the algorithm */ adamstark@58: adamstark@58: double latestCumulativeScoreValue; /**< holds the latest value of the cumulative score function */ adamstark@58: adamstark@59: double tempoToLagFactor; /**< factor for converting between lag and tempo */ adamstark@46: adamstark@59: int m0; /**< indicates when the next point to predict the next beat is */ adamstark@58: adamstark@58: int beatCounter; /**< keeps track of when the next beat is - will be zero when the beat is due, and is set elsewhere in the algorithm to be positive once a beat prediction is made */ adamstark@46: adamstark@58: int hopSize; /**< the hop size being used by the algorithm */ adamstark@58: adamstark@58: int onsetDFBufferSize; /**< the onset detection function buffer size */ adamstark@46: adamstark@58: bool tempoFixed; /**< indicates whether the tempo should be fixed or not */ adamstark@57: adamstark@58: bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */ adamstark@46: adamstark@46: }; adamstark@46: adamstark@46: #endif