annotate src/BTrack.h @ 96:c58f01834337

Merge branch 'release/1.0.4'
author Adam Stark <adamstark.uk@gmail.com>
date Sat, 18 Jun 2016 10:50:06 +0100
parents 4aa362058011
children 6a4dd7478954
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@61 114 /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency.
adamstark@60 115 * This version uses an int to represent the frame number
adamstark@60 116 * @param frameNumber the index of the current frame
adamstark@60 117 * @param hopSize the hop size in audio samples
adamstark@60 118 * @param fs the sampling frequency in Hz
adamstark@60 119 * @returns a beat time in seconds
adamstark@60 120 */
adamstark@91 121 static double getBeatTimeInSeconds (int frameNumber, int hopSize, int fs);
adamstark@55 122
adamstark@58 123
adamstark@46 124 private:
adamstark@51 125
adamstark@60 126 /** Initialises the algorithm, setting internal parameters and creating weighting vectors
adamstark@60 127 * @param hopSize_ the hop size in audio samples
adamstark@60 128 * @param frameSize_ the frame size in audio samples
adamstark@60 129 */
adamstark@91 130 void initialise (int hopSize_, int frameSize_);
adamstark@56 131
adamstark@60 132 /** Initialise with hop size and set all array sizes accordingly
adamstark@60 133 * @param hopSize_ the hop size in audio samples
adamstark@60 134 */
adamstark@91 135 void setHopSize (int hopSize_);
adamstark@56 136
adamstark@60 137 /** Resamples the onset detection function from an arbitrary number of samples to 512 */
adamstark@57 138 void resampleOnsetDetectionFunction();
adamstark@51 139
adamstark@60 140 /** Updates the cumulative score function with a new onset detection function sample
adamstark@60 141 * @param odfSample an onset detection function sample
adamstark@60 142 */
adamstark@91 143 void updateCumulativeScore (double odfSample);
adamstark@51 144
adamstark@61 145 /** Predicts the next beat, based upon the internal program state */
adamstark@57 146 void predictBeat();
adamstark@51 147
adamstark@51 148 /** Calculates the current tempo expressed as the beat period in detection function samples */
adamstark@57 149 void calculateTempo();
adamstark@51 150
adamstark@61 151 /** Calculates an adaptive threshold which is used to remove low level energy from detection
adamstark@51 152 * function and emphasise peaks
adamstark@60 153 * @param x a pointer to an array containing onset detection function samples
adamstark@60 154 * @param N the length of the array, x
adamstark@51 155 */
adamstark@91 156 void adaptiveThreshold (double* x, int N);
adamstark@51 157
adamstark@61 158 /** Calculates the mean of values in an array between index locations [startIndex,endIndex]
adamstark@60 159 * @param array a pointer to an array that contains the values we wish to find the mean from
adamstark@60 160 * @param startIndex the start index from which we would like to calculate the mean
adamstark@60 161 * @param endIndex the final index to which we would like to calculate the mean
adamstark@60 162 * @returns the mean of the sub-section of the array
adamstark@60 163 */
adamstark@93 164 double calculateMeanOfArray (double* array, int startIndex, int endIndex);
adamstark@51 165
adamstark@61 166 /** Normalises a given array
adamstark@60 167 * @param array a pointer to the array we wish to normalise
adamstark@60 168 * @param N the length of the array
adamstark@60 169 */
adamstark@91 170 void normaliseArray (double* array, int N);
adamstark@51 171
adamstark@61 172 /** Calculates the balanced autocorrelation of the smoothed onset detection function
adamstark@60 173 * @param onsetDetectionFunction a pointer to an array containing the onset detection function
adamstark@60 174 */
adamstark@91 175 void calculateBalancedACF (double* onsetDetectionFunction);
adamstark@51 176
adamstark@61 177 /** Calculates the output of the comb filter bank */
adamstark@57 178 void calculateOutputOfCombFilterBank();
adamstark@46 179
adamstark@58 180 //=======================================================================
adamstark@58 181
adamstark@61 182 /** An OnsetDetectionFunction instance for calculating onset detection functions */
adamstark@58 183 OnsetDetectionFunction odf;
adamstark@58 184
adamstark@58 185 //=======================================================================
adamstark@46 186 // buffers
adamstark@63 187
adamstark@89 188 CircularBuffer onsetDF; /**< to hold onset detection function */
adamstark@89 189 CircularBuffer cumulativeScore; /**< to hold cumulative score */
adamstark@63 190
adamstark@58 191 double resampledOnsetDF[512]; /**< to hold resampled detection function */
adamstark@58 192 double acf[512]; /**< to hold autocorrelation function */
adamstark@58 193 double weightingVector[128]; /**< to hold weighting vector */
adamstark@58 194 double combFilterBankOutput[128]; /**< to hold comb filter output */
adamstark@58 195 double tempoObservationVector[41]; /**< to hold tempo version of comb filter output */
adamstark@58 196 double delta[41]; /**< to hold final tempo candidate array */
adamstark@58 197 double prevDelta[41]; /**< previous delta */
adamstark@58 198 double prevDeltaFixed[41]; /**< fixed tempo version of previous delta */
adamstark@58 199 double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */
adamstark@58 200
adamstark@60 201 //=======================================================================
adamstark@56 202 // parameters
adamstark@60 203
adamstark@93 204 double tightness; /**< the tightness of the weighting used to calculate cumulative score */
adamstark@93 205 double alpha; /**< the mix between the current detection function sample and the cumulative score's "momentum" */
adamstark@93 206 double beatPeriod; /**< the beat period, in detection function samples */
adamstark@93 207 double tempo; /**< the tempo in beats per minute */
adamstark@93 208 double estimatedTempo; /**< the current tempo estimation being used by the algorithm */
adamstark@93 209 double latestCumulativeScoreValue; /**< holds the latest value of the cumulative score function */
adamstark@93 210 double tempoToLagFactor; /**< factor for converting between lag and tempo */
adamstark@93 211 int m0; /**< indicates when the next point to predict the next beat is */
adamstark@93 212 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@93 213 int hopSize; /**< the hop size being used by the algorithm */
adamstark@93 214 int onsetDFBufferSize; /**< the onset detection function buffer size */
adamstark@93 215 bool tempoFixed; /**< indicates whether the tempo should be fixed or not */
adamstark@93 216 bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */
adamstark@93 217 int FFTLengthForACFCalculation; /**< the FFT length for the auto-correlation function calculation */
adamstark@60 218
adamstark@93 219 #ifdef USE_FFTW
adamstark@88 220 fftw_plan acfForwardFFT; /**< forward fftw plan for calculating auto-correlation function */
adamstark@88 221 fftw_plan acfBackwardFFT; /**< inverse fftw plan for calculating auto-correlation function */
adamstark@92 222 fftw_complex* complexIn; /**< to hold complex fft values for input */
adamstark@92 223 fftw_complex* complexOut; /**< to hold complex fft values for output */
adamstark@93 224 #endif
adamstark@93 225
adamstark@93 226 #ifdef USE_KISS_FFT
adamstark@93 227 kiss_fft_cfg cfgForwards; /**< Kiss FFT configuration */
adamstark@93 228 kiss_fft_cfg cfgBackwards; /**< Kiss FFT configuration */
adamstark@93 229 kiss_fft_cpx* fftIn; /**< FFT input samples, in complex form */
adamstark@93 230 kiss_fft_cpx* fftOut; /**< FFT output samples, in complex form */
adamstark@93 231 #endif
adamstark@46 232
adamstark@46 233 };
adamstark@46 234
adamstark@46 235 #endif