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