annotate src/BTrack.h @ 64:d3c52c6b3905

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