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@89: #include "CircularBuffer.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@108: 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@108: BTrack (int hopSize, int frameSize);
adamstark@55:
adamstark@88: /** Destructor */
adamstark@88: ~BTrack();
adamstark@88:
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@111: 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@93: 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@91: 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@91: 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@91: 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@91: static double getBeatTimeInSeconds (long frameNumber, int hopSize, int fs);
adamstark@55:
adamstark@46: private:
adamstark@51:
adamstark@60: /** Initialises the algorithm, setting internal parameters and creating weighting vectors
adamstark@108: * @param hopSize the hop size in audio samples
adamstark@60: */
adamstark@108: void initialise (int hopSize);
adamstark@56:
adamstark@60: /** Initialise with hop size and set all array sizes accordingly
adamstark@108: * @param hopSize the hop size in audio samples
adamstark@60: */
adamstark@108: 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@100: * @param onsetDetectionFunctionSample an onset detection function sample
adamstark@60: */
adamstark@100: void updateCumulativeScore (double onsetDetectionFunctionSample);
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@100: * @param x a vector containing onset detection function samples
adamstark@51: */
adamstark@100: void adaptiveThreshold (std::vector& x);
adamstark@51:
adamstark@100: /** Calculates the mean of values in a vector between index locations [startIndex, endIndex]
adamstark@100: * @param vector a vector 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@100: * @returns the mean of the sub-section of the vector
adamstark@60: */
adamstark@100: double calculateMeanOfVector (std::vector& vector, int startIndex, int endIndex);
adamstark@51:
adamstark@61: /** Normalises a given array
adamstark@100: * @param vector the vector we wish to normalise
adamstark@60: */
adamstark@100: void normaliseVector (std::vector& vector);
adamstark@51:
adamstark@61: /** Calculates the balanced autocorrelation of the smoothed onset detection function
adamstark@100: * @param onsetDetectionFunction a vector containing the onset detection function
adamstark@60: */
adamstark@100: void calculateBalancedACF (std::vector& onsetDetectionFunction);
adamstark@51:
adamstark@61: /** Calculates the output of the comb filter bank */
adamstark@57: void calculateOutputOfCombFilterBank();
adamstark@103:
adamstark@104: /** Calculate a log gaussian transition weighting */
adamstark@103: void createLogGaussianTransitionWeighting (double* weightingArray, int numSamples, double beatPeriod);
adamstark@103:
adamstark@104: /** Calculate a new cumulative score value */
adamstark@104: template
adamstark@104: double calculateNewCumulativeScoreValue (T cumulativeScoreArray, double* logGaussianTransitionWeighting, int startIndex, int endIndex, double onsetDetectionFunctionSample, double alphaWeightingFactor);
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@97: CircularBuffer onsetDF; /**< to hold onset detection function */
adamstark@97: CircularBuffer cumulativeScore; /**< to hold cumulative score */
adamstark@63:
adamstark@97: std::vector resampledOnsetDF; /**< to hold resampled detection function */
adamstark@97: std::vector acf; /**< to hold autocorrelation function */
adamstark@97: std::vector weightingVector; /**< to hold weighting vector */
adamstark@97: std::vector combFilterBankOutput; /**< to hold comb filter output */
adamstark@97: std::vector tempoObservationVector; /**< to hold tempo version of comb filter output */
adamstark@97: std::vector delta; /**< to hold final tempo candidate array */
adamstark@97: std::vector prevDelta; /**< previous delta */
adamstark@97: std::vector prevDeltaFixed; /**< fixed tempo version of previous delta */
adamstark@97: double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */
adamstark@58:
adamstark@60: //=======================================================================
adamstark@56: // parameters
adamstark@60:
adamstark@93: double tightness; /**< the tightness of the weighting used to calculate cumulative score */
adamstark@93: double alpha; /**< the mix between the current detection function sample and the cumulative score's "momentum" */
adamstark@93: double beatPeriod; /**< the beat period, in detection function samples */
adamstark@93: double estimatedTempo; /**< the current tempo estimation being used by the algorithm */
adamstark@105: int timeToNextPrediction; /**< indicates when the next point to predict the next beat is */
adamstark@105: int timeToNextBeat; /**< 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@93: int hopSize; /**< the hop size being used by the algorithm */
adamstark@93: int onsetDFBufferSize; /**< the onset detection function buffer size */
adamstark@93: bool tempoFixed; /**< indicates whether the tempo should be fixed or not */
adamstark@93: bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */
adamstark@93: int FFTLengthForACFCalculation; /**< the FFT length for the auto-correlation function calculation */
adamstark@60:
adamstark@93: #ifdef USE_FFTW
adamstark@88: fftw_plan acfForwardFFT; /**< forward fftw plan for calculating auto-correlation function */
adamstark@88: fftw_plan acfBackwardFFT; /**< inverse fftw plan for calculating auto-correlation function */
adamstark@92: fftw_complex* complexIn; /**< to hold complex fft values for input */
adamstark@92: fftw_complex* complexOut; /**< to hold complex fft values for output */
adamstark@93: #endif
adamstark@93:
adamstark@93: #ifdef USE_KISS_FFT
adamstark@93: kiss_fft_cfg cfgForwards; /**< Kiss FFT configuration */
adamstark@93: kiss_fft_cfg cfgBackwards; /**< Kiss FFT configuration */
adamstark@93: kiss_fft_cpx* fftIn; /**< FFT input samples, in complex form */
adamstark@93: kiss_fft_cpx* fftOut; /**< FFT output samples, in complex form */
adamstark@93: #endif
adamstark@46:
adamstark@46: };
adamstark@46:
adamstark@46: #endif