annotate src/BTrack.h @ 98:3b24b01fbe15

More tidying...
author Adam Stark <adamstark.uk@gmail.com>
date Sat, 12 Aug 2017 13:54:35 +0100
parents 6a4dd7478954
children b91eb3b6de86
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@97 28 #include <array>
adamstark@55 29
adamstark@60 30 //=======================================================================
adamstark@60 31 /** The main beat tracking class and the interface to the BTrack
adamstark@60 32 * beat tracking algorithm. The algorithm can process either
adamstark@60 33 * audio frames or onset detection function samples and also
adamstark@60 34 * contains some static functions for calculating beat times in seconds
adamstark@60 35 */
adamstark@46 36 class BTrack {
adamstark@46 37
adamstark@46 38 public:
adamstark@51 39
adamstark@58 40 //=======================================================================
adamstark@61 41 /** Constructor assuming hop size of 512 and frame size of 1024 */
adamstark@56 42 BTrack();
adamstark@51 43
adamstark@61 44 /** Constructor assuming frame size will be double the hopSize
adamstark@60 45 * @param hopSize the hop size in audio samples
adamstark@55 46 */
adamstark@91 47 BTrack (int hopSize_);
adamstark@55 48
adamstark@61 49 /** Constructor taking both hopSize and frameSize
adamstark@60 50 * @param hopSize the hop size in audio samples
adamstark@60 51 * @param frameSize the frame size in audio samples
adamstark@55 52 */
adamstark@91 53 BTrack (int hopSize_, int frameSize_);
adamstark@55 54
adamstark@88 55 /** Destructor */
adamstark@88 56 ~BTrack();
adamstark@88 57
adamstark@58 58 //=======================================================================
adamstark@65 59 /** Updates the hop and frame size used by the beat tracker
adamstark@65 60 * @param hopSize the hop size in audio samples
adamstark@65 61 * @param frameSize the frame size in audio samples
adamstark@65 62 */
adamstark@91 63 void updateHopAndFrameSize (int hopSize_, int frameSize_);
adamstark@65 64
adamstark@65 65 //=======================================================================
adamstark@60 66 /** Process a single audio frame
adamstark@60 67 * @param frame a pointer to an array containing an audio frame. The number of samples should
adamstark@60 68 * match the frame size that the algorithm was initialised with.
adamstark@60 69 */
adamstark@93 70 void processAudioFrame (double* frame);
adamstark@55 71
adamstark@60 72 /** Add new onset detection function sample to buffer and apply beat tracking
adamstark@60 73 * @param sample an onset detection function sample
adamstark@60 74 */
adamstark@91 75 void processOnsetDetectionFunctionSample (double sample);
adamstark@51 76
adamstark@58 77 //=======================================================================
adamstark@57 78 /** @returns the current hop size being used by the beat tracker */
adamstark@57 79 int getHopSize();
adamstark@57 80
adamstark@58 81 /** @returns true if a beat should occur in the current audio frame */
adamstark@58 82 bool beatDueInCurrentFrame();
adamstark@58 83
adamstark@58 84 /** @returns the current tempo estimate being used by the beat tracker */
adamstark@58 85 double getCurrentTempoEstimate();
adamstark@58 86
adamstark@58 87 /** @returns the most recent value of the cumulative score function */
adamstark@58 88 double getLatestCumulativeScoreValue();
adamstark@58 89
adamstark@58 90 //=======================================================================
adamstark@60 91 /** Set the tempo of the beat tracker
adamstark@60 92 * @param tempo the tempo in beats per minute (bpm)
adamstark@60 93 */
adamstark@91 94 void setTempo (double tempo);
adamstark@51 95
adamstark@61 96 /** Fix tempo to roughly around some value, so that the algorithm will only try to track
adamstark@60 97 * tempi around the given tempo
adamstark@60 98 * @param tempo the tempo in beats per minute (bpm)
adamstark@60 99 */
adamstark@91 100 void fixTempo (double tempo);
adamstark@51 101
adamstark@61 102 /** Tell the algorithm to not fix the tempo anymore */
adamstark@57 103 void doNotFixTempo();
adamstark@55 104
adamstark@58 105 //=======================================================================
adamstark@61 106 /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency.
adamstark@60 107 * This version uses a long to represent the frame number
adamstark@60 108 * @param frameNumber the index of the current frame
adamstark@60 109 * @param hopSize the hop size in audio samples
adamstark@60 110 * @param fs the sampling frequency in Hz
adamstark@60 111 * @returns a beat time in seconds
adamstark@60 112 */
adamstark@91 113 static double getBeatTimeInSeconds (long frameNumber, int hopSize, int fs);
adamstark@55 114
adamstark@61 115 /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency.
adamstark@60 116 * This version uses an int to represent the frame number
adamstark@60 117 * @param frameNumber the index of the current frame
adamstark@60 118 * @param hopSize the hop size in audio samples
adamstark@60 119 * @param fs the sampling frequency in Hz
adamstark@60 120 * @returns a beat time in seconds
adamstark@60 121 */
adamstark@91 122 static double getBeatTimeInSeconds (int frameNumber, int hopSize, int fs);
adamstark@55 123
adamstark@58 124
adamstark@46 125 private:
adamstark@51 126
adamstark@60 127 /** Initialises the algorithm, setting internal parameters and creating weighting vectors
adamstark@60 128 * @param hopSize_ the hop size in audio samples
adamstark@60 129 * @param frameSize_ the frame size in audio samples
adamstark@60 130 */
adamstark@91 131 void initialise (int hopSize_, int frameSize_);
adamstark@56 132
adamstark@60 133 /** Initialise with hop size and set all array sizes accordingly
adamstark@60 134 * @param hopSize_ the hop size in audio samples
adamstark@60 135 */
adamstark@91 136 void setHopSize (int hopSize_);
adamstark@56 137
adamstark@60 138 /** Resamples the onset detection function from an arbitrary number of samples to 512 */
adamstark@57 139 void resampleOnsetDetectionFunction();
adamstark@51 140
adamstark@60 141 /** Updates the cumulative score function with a new onset detection function sample
adamstark@60 142 * @param odfSample an onset detection function sample
adamstark@60 143 */
adamstark@91 144 void updateCumulativeScore (double odfSample);
adamstark@51 145
adamstark@61 146 /** Predicts the next beat, based upon the internal program state */
adamstark@57 147 void predictBeat();
adamstark@51 148
adamstark@51 149 /** Calculates the current tempo expressed as the beat period in detection function samples */
adamstark@57 150 void calculateTempo();
adamstark@51 151
adamstark@61 152 /** Calculates an adaptive threshold which is used to remove low level energy from detection
adamstark@51 153 * function and emphasise peaks
adamstark@60 154 * @param x a pointer to an array containing onset detection function samples
adamstark@60 155 * @param N the length of the array, x
adamstark@51 156 */
adamstark@97 157 void adaptiveThreshold (std::vector<double>& x, int N);
adamstark@51 158
adamstark@61 159 /** Calculates the mean of values in an array between index locations [startIndex,endIndex]
adamstark@60 160 * @param array a pointer to an array that contains the values we wish to find the mean from
adamstark@60 161 * @param startIndex the start index from which we would like to calculate the mean
adamstark@60 162 * @param endIndex the final index to which we would like to calculate the mean
adamstark@60 163 * @returns the mean of the sub-section of the array
adamstark@60 164 */
adamstark@97 165 double calculateMeanOfArray (std::vector<double>& array, int startIndex, int endIndex);
adamstark@51 166
adamstark@61 167 /** Normalises a given array
adamstark@97 168 * @param array the array we wish to normalise
adamstark@60 169 */
adamstark@97 170 void normaliseArray (std::vector<double>& array);
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@97 188 CircularBuffer onsetDF; /**< to hold onset detection function */
adamstark@97 189 CircularBuffer cumulativeScore; /**< to hold cumulative score */
adamstark@63 190
adamstark@97 191 std::vector<double> resampledOnsetDF; /**< to hold resampled detection function */
adamstark@97 192 std::vector<double> acf; /**< to hold autocorrelation function */
adamstark@97 193 std::vector<double> weightingVector; /**< to hold weighting vector */
adamstark@97 194 std::vector<double> combFilterBankOutput; /**< to hold comb filter output */
adamstark@97 195 std::vector<double> tempoObservationVector; /**< to hold tempo version of comb filter output */
adamstark@97 196 std::vector<double> delta; /**< to hold final tempo candidate array */
adamstark@97 197 std::vector<double> prevDelta; /**< previous delta */
adamstark@97 198 std::vector<double> prevDeltaFixed; /**< fixed tempo version of previous delta */
adamstark@97 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