annotate src/BTrack.h @ 24:deb49a2590f3 develop

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