Mercurial > hg > btrack
comparison src/BTrack.h @ 117:ca2d83d29814 tip master
Merge branch 'release/1.0.5'
author | Adam Stark <adamstark.uk@gmail.com> |
---|---|
date | Fri, 18 Aug 2023 20:07:33 +0200 |
parents | 8fb1610c9192 |
children |
comparison
equal
deleted
inserted
replaced
96:c58f01834337 | 117:ca2d83d29814 |
---|---|
41 BTrack(); | 41 BTrack(); |
42 | 42 |
43 /** Constructor assuming frame size will be double the hopSize | 43 /** Constructor assuming frame size will be double the hopSize |
44 * @param hopSize the hop size in audio samples | 44 * @param hopSize the hop size in audio samples |
45 */ | 45 */ |
46 BTrack (int hopSize_); | 46 BTrack (int hopSize); |
47 | 47 |
48 /** Constructor taking both hopSize and frameSize | 48 /** Constructor taking both hopSize and frameSize |
49 * @param hopSize the hop size in audio samples | 49 * @param hopSize the hop size in audio samples |
50 * @param frameSize the frame size in audio samples | 50 * @param frameSize the frame size in audio samples |
51 */ | 51 */ |
52 BTrack (int hopSize_, int frameSize_); | 52 BTrack (int hopSize, int frameSize); |
53 | 53 |
54 /** Destructor */ | 54 /** Destructor */ |
55 ~BTrack(); | 55 ~BTrack(); |
56 | 56 |
57 //======================================================================= | 57 //======================================================================= |
58 /** Updates the hop and frame size used by the beat tracker | 58 /** Updates the hop and frame size used by the beat tracker |
59 * @param hopSize the hop size in audio samples | 59 * @param hopSize the hop size in audio samples |
60 * @param frameSize the frame size in audio samples | 60 * @param frameSize the frame size in audio samples |
61 */ | 61 */ |
62 void updateHopAndFrameSize (int hopSize_, int frameSize_); | 62 void updateHopAndFrameSize (int hopSize, int frameSize); |
63 | 63 |
64 //======================================================================= | 64 //======================================================================= |
65 /** Process a single audio frame | 65 /** Process a single audio frame |
66 * @param frame a pointer to an array containing an audio frame. The number of samples should | 66 * @param frame a pointer to an array containing an audio frame. The number of samples should |
67 * match the frame size that the algorithm was initialised with. | 67 * match the frame size that the algorithm was initialised with. |
109 * @param fs the sampling frequency in Hz | 109 * @param fs the sampling frequency in Hz |
110 * @returns a beat time in seconds | 110 * @returns a beat time in seconds |
111 */ | 111 */ |
112 static double getBeatTimeInSeconds (long frameNumber, int hopSize, int fs); | 112 static double getBeatTimeInSeconds (long frameNumber, int hopSize, int fs); |
113 | 113 |
114 /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency. | |
115 * This version uses an int to represent the frame number | |
116 * @param frameNumber the index of the current frame | |
117 * @param hopSize the hop size in audio samples | |
118 * @param fs the sampling frequency in Hz | |
119 * @returns a beat time in seconds | |
120 */ | |
121 static double getBeatTimeInSeconds (int frameNumber, int hopSize, int fs); | |
122 | |
123 | |
124 private: | 114 private: |
125 | 115 |
126 /** Initialises the algorithm, setting internal parameters and creating weighting vectors | 116 /** Initialises the algorithm, setting internal parameters and creating weighting vectors |
127 * @param hopSize_ the hop size in audio samples | 117 * @param hopSize the hop size in audio samples |
128 * @param frameSize_ the frame size in audio samples | 118 */ |
129 */ | 119 void initialise (int hopSize); |
130 void initialise (int hopSize_, int frameSize_); | |
131 | 120 |
132 /** Initialise with hop size and set all array sizes accordingly | 121 /** Initialise with hop size and set all array sizes accordingly |
133 * @param hopSize_ the hop size in audio samples | 122 * @param hopSize the hop size in audio samples |
134 */ | 123 */ |
135 void setHopSize (int hopSize_); | 124 void setHopSize (int hopSize); |
136 | 125 |
137 /** Resamples the onset detection function from an arbitrary number of samples to 512 */ | 126 /** Resamples the onset detection function from an arbitrary number of samples to 512 */ |
138 void resampleOnsetDetectionFunction(); | 127 void resampleOnsetDetectionFunction(); |
139 | 128 |
140 /** Updates the cumulative score function with a new onset detection function sample | 129 /** Updates the cumulative score function with a new onset detection function sample |
141 * @param odfSample an onset detection function sample | 130 * @param onsetDetectionFunctionSample an onset detection function sample |
142 */ | 131 */ |
143 void updateCumulativeScore (double odfSample); | 132 void updateCumulativeScore (double onsetDetectionFunctionSample); |
144 | 133 |
145 /** Predicts the next beat, based upon the internal program state */ | 134 /** Predicts the next beat, based upon the internal program state */ |
146 void predictBeat(); | 135 void predictBeat(); |
147 | 136 |
148 /** Calculates the current tempo expressed as the beat period in detection function samples */ | 137 /** Calculates the current tempo expressed as the beat period in detection function samples */ |
149 void calculateTempo(); | 138 void calculateTempo(); |
150 | 139 |
151 /** Calculates an adaptive threshold which is used to remove low level energy from detection | 140 /** Calculates an adaptive threshold which is used to remove low level energy from detection |
152 * function and emphasise peaks | 141 * function and emphasise peaks |
153 * @param x a pointer to an array containing onset detection function samples | 142 * @param x a vector containing onset detection function samples |
154 * @param N the length of the array, x | 143 */ |
155 */ | 144 void adaptiveThreshold (std::vector<double>& x); |
156 void adaptiveThreshold (double* x, int N); | 145 |
157 | 146 /** Calculates the mean of values in a vector between index locations [startIndex, endIndex] |
158 /** Calculates the mean of values in an array between index locations [startIndex,endIndex] | 147 * @param vector a vector that contains the values we wish to find the mean from |
159 * @param array a pointer to an array that contains the values we wish to find the mean from | |
160 * @param startIndex the start index from which we would like to calculate the mean | 148 * @param startIndex the start index from which we would like to calculate the mean |
161 * @param endIndex the final index to which we would like to calculate the mean | 149 * @param endIndex the final index to which we would like to calculate the mean |
162 * @returns the mean of the sub-section of the array | 150 * @returns the mean of the sub-section of the vector |
163 */ | 151 */ |
164 double calculateMeanOfArray (double* array, int startIndex, int endIndex); | 152 double calculateMeanOfVector (std::vector<double>& vector, int startIndex, int endIndex); |
165 | 153 |
166 /** Normalises a given array | 154 /** Normalises a given array |
167 * @param array a pointer to the array we wish to normalise | 155 * @param vector the vector we wish to normalise |
168 * @param N the length of the array | 156 */ |
169 */ | 157 void normaliseVector (std::vector<double>& vector); |
170 void normaliseArray (double* array, int N); | |
171 | 158 |
172 /** Calculates the balanced autocorrelation of the smoothed onset detection function | 159 /** Calculates the balanced autocorrelation of the smoothed onset detection function |
173 * @param onsetDetectionFunction a pointer to an array containing the onset detection function | 160 * @param onsetDetectionFunction a vector containing the onset detection function |
174 */ | 161 */ |
175 void calculateBalancedACF (double* onsetDetectionFunction); | 162 void calculateBalancedACF (std::vector<double>& onsetDetectionFunction); |
176 | 163 |
177 /** Calculates the output of the comb filter bank */ | 164 /** Calculates the output of the comb filter bank */ |
178 void calculateOutputOfCombFilterBank(); | 165 void calculateOutputOfCombFilterBank(); |
166 | |
167 /** Calculate a log gaussian transition weighting */ | |
168 void createLogGaussianTransitionWeighting (double* weightingArray, int numSamples, double beatPeriod); | |
169 | |
170 /** Calculate a new cumulative score value */ | |
171 template <typename T> | |
172 double calculateNewCumulativeScoreValue (T cumulativeScoreArray, double* logGaussianTransitionWeighting, int startIndex, int endIndex, double onsetDetectionFunctionSample, double alphaWeightingFactor); | |
179 | 173 |
180 //======================================================================= | 174 //======================================================================= |
181 | 175 |
182 /** An OnsetDetectionFunction instance for calculating onset detection functions */ | 176 /** An OnsetDetectionFunction instance for calculating onset detection functions */ |
183 OnsetDetectionFunction odf; | 177 OnsetDetectionFunction odf; |
184 | 178 |
185 //======================================================================= | 179 //======================================================================= |
186 // buffers | 180 // buffers |
187 | 181 |
188 CircularBuffer onsetDF; /**< to hold onset detection function */ | 182 CircularBuffer onsetDF; /**< to hold onset detection function */ |
189 CircularBuffer cumulativeScore; /**< to hold cumulative score */ | 183 CircularBuffer cumulativeScore; /**< to hold cumulative score */ |
190 | 184 |
191 double resampledOnsetDF[512]; /**< to hold resampled detection function */ | 185 std::vector<double> resampledOnsetDF; /**< to hold resampled detection function */ |
192 double acf[512]; /**< to hold autocorrelation function */ | 186 std::vector<double> acf; /**< to hold autocorrelation function */ |
193 double weightingVector[128]; /**< to hold weighting vector */ | 187 std::vector<double> weightingVector; /**< to hold weighting vector */ |
194 double combFilterBankOutput[128]; /**< to hold comb filter output */ | 188 std::vector<double> combFilterBankOutput; /**< to hold comb filter output */ |
195 double tempoObservationVector[41]; /**< to hold tempo version of comb filter output */ | 189 std::vector<double> tempoObservationVector; /**< to hold tempo version of comb filter output */ |
196 double delta[41]; /**< to hold final tempo candidate array */ | 190 std::vector<double> delta; /**< to hold final tempo candidate array */ |
197 double prevDelta[41]; /**< previous delta */ | 191 std::vector<double> prevDelta; /**< previous delta */ |
198 double prevDeltaFixed[41]; /**< fixed tempo version of previous delta */ | 192 std::vector<double> prevDeltaFixed; /**< fixed tempo version of previous delta */ |
199 double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */ | 193 double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */ |
200 | 194 |
201 //======================================================================= | 195 //======================================================================= |
202 // parameters | 196 // parameters |
203 | 197 |
204 double tightness; /**< the tightness of the weighting used to calculate cumulative score */ | 198 double tightness; /**< the tightness of the weighting used to calculate cumulative score */ |
205 double alpha; /**< the mix between the current detection function sample and the cumulative score's "momentum" */ | 199 double alpha; /**< the mix between the current detection function sample and the cumulative score's "momentum" */ |
206 double beatPeriod; /**< the beat period, in detection function samples */ | 200 double beatPeriod; /**< the beat period, in detection function samples */ |
207 double tempo; /**< the tempo in beats per minute */ | |
208 double estimatedTempo; /**< the current tempo estimation being used by the algorithm */ | 201 double estimatedTempo; /**< the current tempo estimation being used by the algorithm */ |
209 double latestCumulativeScoreValue; /**< holds the latest value of the cumulative score function */ | 202 int timeToNextPrediction; /**< indicates when the next point to predict the next beat is */ |
210 double tempoToLagFactor; /**< factor for converting between lag and tempo */ | 203 int timeToNextBeat; /**< 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 */ |
211 int m0; /**< indicates when the next point to predict the next beat is */ | |
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 */ | |
213 int hopSize; /**< the hop size being used by the algorithm */ | 204 int hopSize; /**< the hop size being used by the algorithm */ |
214 int onsetDFBufferSize; /**< the onset detection function buffer size */ | 205 int onsetDFBufferSize; /**< the onset detection function buffer size */ |
215 bool tempoFixed; /**< indicates whether the tempo should be fixed or not */ | 206 bool tempoFixed; /**< indicates whether the tempo should be fixed or not */ |
216 bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */ | 207 bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */ |
217 int FFTLengthForACFCalculation; /**< the FFT length for the auto-correlation function calculation */ | 208 int FFTLengthForACFCalculation; /**< the FFT length for the auto-correlation function calculation */ |