annotate src/BTrack.h @ 106:6f5ba0250afd

Fixed some comments
author Adam Stark <adamstark.uk@gmail.com>
date Tue, 12 Sep 2017 01:00:53 +0100
parents 3647be01027b
children a6701b3f636f
rev   line source
adamstark@46 1 //=======================================================================
adamstark@46 2 /** @file BTrack.h
adamstark@47 3 * @brief BTrack - a real-time beat tracker
adamstark@46 4 * @author Adam Stark
adamstark@46 5 * @copyright Copyright (C) 2008-2014 Queen Mary University of London
adamstark@46 6 *
adamstark@46 7 * This program is free software: you can redistribute it and/or modify
adamstark@46 8 * it under the terms of the GNU General Public License as published by
adamstark@46 9 * the Free Software Foundation, either version 3 of the License, or
adamstark@46 10 * (at your option) any later version.
adamstark@46 11 *
adamstark@46 12 * This program is distributed in the hope that it will be useful,
adamstark@46 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
adamstark@46 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
adamstark@46 15 * GNU General Public License for more details.
adamstark@46 16 *
adamstark@46 17 * You should have received a copy of the GNU General Public License
adamstark@46 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
adamstark@46 19 */
adamstark@46 20 //=======================================================================
adamstark@46 21
adamstark@46 22 #ifndef __BTRACK_H
adamstark@46 23 #define __BTRACK_H
adamstark@46 24
adamstark@55 25 #include "OnsetDetectionFunction.h"
adamstark@89 26 #include "CircularBuffer.h"
adamstark@63 27 #include <vector>
adamstark@55 28
adamstark@60 29 //=======================================================================
adamstark@60 30 /** The main beat tracking class and the interface to the BTrack
adamstark@60 31 * beat tracking algorithm. The algorithm can process either
adamstark@60 32 * audio frames or onset detection function samples and also
adamstark@60 33 * contains some static functions for calculating beat times in seconds
adamstark@60 34 */
adamstark@46 35 class BTrack {
adamstark@46 36
adamstark@46 37 public:
adamstark@51 38
adamstark@58 39 //=======================================================================
adamstark@61 40 /** Constructor assuming hop size of 512 and frame size of 1024 */
adamstark@56 41 BTrack();
adamstark@51 42
adamstark@61 43 /** Constructor assuming frame size will be double the hopSize
adamstark@60 44 * @param hopSize the hop size in audio samples
adamstark@55 45 */
adamstark@91 46 BTrack (int hopSize_);
adamstark@55 47
adamstark@61 48 /** Constructor taking both hopSize and frameSize
adamstark@60 49 * @param hopSize the hop size in audio samples
adamstark@60 50 * @param frameSize the frame size in audio samples
adamstark@55 51 */
adamstark@91 52 BTrack (int hopSize_, int frameSize_);
adamstark@55 53
adamstark@88 54 /** Destructor */
adamstark@88 55 ~BTrack();
adamstark@88 56
adamstark@58 57 //=======================================================================
adamstark@65 58 /** Updates the hop and frame size used by the beat tracker
adamstark@65 59 * @param hopSize the hop size in audio samples
adamstark@65 60 * @param frameSize the frame size in audio samples
adamstark@65 61 */
adamstark@91 62 void updateHopAndFrameSize (int hopSize_, int frameSize_);
adamstark@65 63
adamstark@65 64 //=======================================================================
adamstark@60 65 /** Process a single audio frame
adamstark@60 66 * @param frame a pointer to an array containing an audio frame. The number of samples should
adamstark@60 67 * match the frame size that the algorithm was initialised with.
adamstark@60 68 */
adamstark@93 69 void processAudioFrame (double* frame);
adamstark@55 70
adamstark@60 71 /** Add new onset detection function sample to buffer and apply beat tracking
adamstark@60 72 * @param sample an onset detection function sample
adamstark@60 73 */
adamstark@91 74 void processOnsetDetectionFunctionSample (double sample);
adamstark@51 75
adamstark@58 76 //=======================================================================
adamstark@57 77 /** @returns the current hop size being used by the beat tracker */
adamstark@57 78 int getHopSize();
adamstark@57 79
adamstark@58 80 /** @returns true if a beat should occur in the current audio frame */
adamstark@58 81 bool beatDueInCurrentFrame();
adamstark@58 82
adamstark@58 83 /** @returns the current tempo estimate being used by the beat tracker */
adamstark@58 84 double getCurrentTempoEstimate();
adamstark@58 85
adamstark@58 86 /** @returns the most recent value of the cumulative score function */
adamstark@58 87 double getLatestCumulativeScoreValue();
adamstark@58 88
adamstark@58 89 //=======================================================================
adamstark@60 90 /** Set the tempo of the beat tracker
adamstark@60 91 * @param tempo the tempo in beats per minute (bpm)
adamstark@60 92 */
adamstark@91 93 void setTempo (double tempo);
adamstark@51 94
adamstark@61 95 /** Fix tempo to roughly around some value, so that the algorithm will only try to track
adamstark@60 96 * tempi around the given tempo
adamstark@60 97 * @param tempo the tempo in beats per minute (bpm)
adamstark@60 98 */
adamstark@91 99 void fixTempo (double tempo);
adamstark@51 100
adamstark@61 101 /** Tell the algorithm to not fix the tempo anymore */
adamstark@57 102 void doNotFixTempo();
adamstark@55 103
adamstark@58 104 //=======================================================================
adamstark@61 105 /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency.
adamstark@60 106 * This version uses a long to represent the frame number
adamstark@60 107 * @param frameNumber the index of the current frame
adamstark@60 108 * @param hopSize the hop size in audio samples
adamstark@60 109 * @param fs the sampling frequency in Hz
adamstark@60 110 * @returns a beat time in seconds
adamstark@60 111 */
adamstark@91 112 static double getBeatTimeInSeconds (long frameNumber, int hopSize, int fs);
adamstark@55 113
adamstark@46 114 private:
adamstark@51 115
adamstark@60 116 /** Initialises the algorithm, setting internal parameters and creating weighting vectors
adamstark@60 117 * @param hopSize_ the hop size in audio samples
adamstark@60 118 * @param frameSize_ the frame size in audio samples
adamstark@60 119 */
adamstark@91 120 void initialise (int hopSize_, int frameSize_);
adamstark@56 121
adamstark@60 122 /** Initialise with hop size and set all array sizes accordingly
adamstark@60 123 * @param hopSize_ the hop size in audio samples
adamstark@60 124 */
adamstark@91 125 void setHopSize (int hopSize_);
adamstark@56 126
adamstark@60 127 /** Resamples the onset detection function from an arbitrary number of samples to 512 */
adamstark@57 128 void resampleOnsetDetectionFunction();
adamstark@51 129
adamstark@60 130 /** Updates the cumulative score function with a new onset detection function sample
adamstark@100 131 * @param onsetDetectionFunctionSample an onset detection function sample
adamstark@60 132 */
adamstark@100 133 void updateCumulativeScore (double onsetDetectionFunctionSample);
adamstark@51 134
adamstark@61 135 /** Predicts the next beat, based upon the internal program state */
adamstark@57 136 void predictBeat();
adamstark@51 137
adamstark@51 138 /** Calculates the current tempo expressed as the beat period in detection function samples */
adamstark@57 139 void calculateTempo();
adamstark@51 140
adamstark@61 141 /** Calculates an adaptive threshold which is used to remove low level energy from detection
adamstark@51 142 * function and emphasise peaks
adamstark@100 143 * @param x a vector containing onset detection function samples
adamstark@51 144 */
adamstark@100 145 void adaptiveThreshold (std::vector<double>& x);
adamstark@51 146
adamstark@100 147 /** Calculates the mean of values in a vector between index locations [startIndex, endIndex]
adamstark@100 148 * @param vector a vector that contains the values we wish to find the mean from
adamstark@60 149 * @param startIndex the start index from which we would like to calculate the mean
adamstark@60 150 * @param endIndex the final index to which we would like to calculate the mean
adamstark@100 151 * @returns the mean of the sub-section of the vector
adamstark@60 152 */
adamstark@100 153 double calculateMeanOfVector (std::vector<double>& vector, int startIndex, int endIndex);
adamstark@51 154
adamstark@61 155 /** Normalises a given array
adamstark@100 156 * @param vector the vector we wish to normalise
adamstark@60 157 */
adamstark@100 158 void normaliseVector (std::vector<double>& vector);
adamstark@51 159
adamstark@61 160 /** Calculates the balanced autocorrelation of the smoothed onset detection function
adamstark@100 161 * @param onsetDetectionFunction a vector containing the onset detection function
adamstark@60 162 */
adamstark@100 163 void calculateBalancedACF (std::vector<double>& onsetDetectionFunction);
adamstark@51 164
adamstark@61 165 /** Calculates the output of the comb filter bank */
adamstark@57 166 void calculateOutputOfCombFilterBank();
adamstark@103 167
adamstark@104 168 /** Calculate a log gaussian transition weighting */
adamstark@103 169 void createLogGaussianTransitionWeighting (double* weightingArray, int numSamples, double beatPeriod);
adamstark@103 170
adamstark@104 171 /** Calculate a new cumulative score value */
adamstark@104 172 template <typename T>
adamstark@104 173 double calculateNewCumulativeScoreValue (T cumulativeScoreArray, double* logGaussianTransitionWeighting, int startIndex, int endIndex, double onsetDetectionFunctionSample, double alphaWeightingFactor);
adamstark@46 174
adamstark@58 175 //=======================================================================
adamstark@58 176
adamstark@61 177 /** An OnsetDetectionFunction instance for calculating onset detection functions */
adamstark@58 178 OnsetDetectionFunction odf;
adamstark@58 179
adamstark@58 180 //=======================================================================
adamstark@46 181 // buffers
adamstark@63 182
adamstark@97 183 CircularBuffer onsetDF; /**< to hold onset detection function */
adamstark@97 184 CircularBuffer cumulativeScore; /**< to hold cumulative score */
adamstark@63 185
adamstark@97 186 std::vector<double> resampledOnsetDF; /**< to hold resampled detection function */
adamstark@97 187 std::vector<double> acf; /**< to hold autocorrelation function */
adamstark@97 188 std::vector<double> weightingVector; /**< to hold weighting vector */
adamstark@97 189 std::vector<double> combFilterBankOutput; /**< to hold comb filter output */
adamstark@97 190 std::vector<double> tempoObservationVector; /**< to hold tempo version of comb filter output */
adamstark@97 191 std::vector<double> delta; /**< to hold final tempo candidate array */
adamstark@97 192 std::vector<double> prevDelta; /**< previous delta */
adamstark@97 193 std::vector<double> prevDeltaFixed; /**< fixed tempo version of previous delta */
adamstark@97 194 double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */
adamstark@58 195
adamstark@60 196 //=======================================================================
adamstark@56 197 // parameters
adamstark@60 198
adamstark@93 199 double tightness; /**< the tightness of the weighting used to calculate cumulative score */
adamstark@93 200 double alpha; /**< the mix between the current detection function sample and the cumulative score's "momentum" */
adamstark@93 201 double beatPeriod; /**< the beat period, in detection function samples */
adamstark@93 202 double estimatedTempo; /**< the current tempo estimation being used by the algorithm */
adamstark@105 203 int timeToNextPrediction; /**< indicates when the next point to predict the next beat is */
adamstark@105 204 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 205 int hopSize; /**< the hop size being used by the algorithm */
adamstark@93 206 int onsetDFBufferSize; /**< the onset detection function buffer size */
adamstark@93 207 bool tempoFixed; /**< indicates whether the tempo should be fixed or not */
adamstark@93 208 bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */
adamstark@93 209 int FFTLengthForACFCalculation; /**< the FFT length for the auto-correlation function calculation */
adamstark@60 210
adamstark@93 211 #ifdef USE_FFTW
adamstark@88 212 fftw_plan acfForwardFFT; /**< forward fftw plan for calculating auto-correlation function */
adamstark@88 213 fftw_plan acfBackwardFFT; /**< inverse fftw plan for calculating auto-correlation function */
adamstark@92 214 fftw_complex* complexIn; /**< to hold complex fft values for input */
adamstark@92 215 fftw_complex* complexOut; /**< to hold complex fft values for output */
adamstark@93 216 #endif
adamstark@93 217
adamstark@93 218 #ifdef USE_KISS_FFT
adamstark@93 219 kiss_fft_cfg cfgForwards; /**< Kiss FFT configuration */
adamstark@93 220 kiss_fft_cfg cfgBackwards; /**< Kiss FFT configuration */
adamstark@93 221 kiss_fft_cpx* fftIn; /**< FFT input samples, in complex form */
adamstark@93 222 kiss_fft_cpx* fftOut; /**< FFT output samples, in complex form */
adamstark@93 223 #endif
adamstark@46 224
adamstark@46 225 };
adamstark@46 226
adamstark@46 227 #endif