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