annotate src/BTrack.h @ 28:7af87d3f2ce2 develop

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