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@108
|
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@108
|
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@111
|
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@46
|
114 private:
|
adamstark@51
|
115
|
adamstark@60
|
116 /** Initialises the algorithm, setting internal parameters and creating weighting vectors
|
adamstark@108
|
117 * @param hopSize the hop size in audio samples
|
adamstark@60
|
118 */
|
adamstark@108
|
119 void initialise (int hopSize);
|
adamstark@56
|
120
|
adamstark@60
|
121 /** Initialise with hop size and set all array sizes accordingly
|
adamstark@108
|
122 * @param hopSize the hop size in audio samples
|
adamstark@60
|
123 */
|
adamstark@108
|
124 void setHopSize (int hopSize);
|
adamstark@56
|
125
|
adamstark@60
|
126 /** Resamples the onset detection function from an arbitrary number of samples to 512 */
|
adamstark@57
|
127 void resampleOnsetDetectionFunction();
|
adamstark@51
|
128
|
adamstark@60
|
129 /** Updates the cumulative score function with a new onset detection function sample
|
adamstark@100
|
130 * @param onsetDetectionFunctionSample an onset detection function sample
|
adamstark@60
|
131 */
|
adamstark@100
|
132 void updateCumulativeScore (double onsetDetectionFunctionSample);
|
adamstark@51
|
133
|
adamstark@61
|
134 /** Predicts the next beat, based upon the internal program state */
|
adamstark@57
|
135 void predictBeat();
|
adamstark@51
|
136
|
adamstark@51
|
137 /** Calculates the current tempo expressed as the beat period in detection function samples */
|
adamstark@57
|
138 void calculateTempo();
|
adamstark@51
|
139
|
adamstark@61
|
140 /** Calculates an adaptive threshold which is used to remove low level energy from detection
|
adamstark@51
|
141 * function and emphasise peaks
|
adamstark@100
|
142 * @param x a vector containing onset detection function samples
|
adamstark@51
|
143 */
|
adamstark@100
|
144 void adaptiveThreshold (std::vector<double>& x);
|
adamstark@51
|
145
|
adamstark@100
|
146 /** Calculates the mean of values in a vector between index locations [startIndex, endIndex]
|
adamstark@100
|
147 * @param vector a vector that contains the values we wish to find the mean from
|
adamstark@60
|
148 * @param startIndex the start index from which we would like to calculate the mean
|
adamstark@60
|
149 * @param endIndex the final index to which we would like to calculate the mean
|
adamstark@100
|
150 * @returns the mean of the sub-section of the vector
|
adamstark@60
|
151 */
|
adamstark@100
|
152 double calculateMeanOfVector (std::vector<double>& vector, int startIndex, int endIndex);
|
adamstark@51
|
153
|
adamstark@61
|
154 /** Normalises a given array
|
adamstark@100
|
155 * @param vector the vector we wish to normalise
|
adamstark@60
|
156 */
|
adamstark@100
|
157 void normaliseVector (std::vector<double>& vector);
|
adamstark@51
|
158
|
adamstark@61
|
159 /** Calculates the balanced autocorrelation of the smoothed onset detection function
|
adamstark@100
|
160 * @param onsetDetectionFunction a vector containing the onset detection function
|
adamstark@60
|
161 */
|
adamstark@100
|
162 void calculateBalancedACF (std::vector<double>& onsetDetectionFunction);
|
adamstark@51
|
163
|
adamstark@61
|
164 /** Calculates the output of the comb filter bank */
|
adamstark@57
|
165 void calculateOutputOfCombFilterBank();
|
adamstark@103
|
166
|
adamstark@104
|
167 /** Calculate a log gaussian transition weighting */
|
adamstark@103
|
168 void createLogGaussianTransitionWeighting (double* weightingArray, int numSamples, double beatPeriod);
|
adamstark@103
|
169
|
adamstark@104
|
170 /** Calculate a new cumulative score value */
|
adamstark@104
|
171 template <typename T>
|
adamstark@104
|
172 double calculateNewCumulativeScoreValue (T cumulativeScoreArray, double* logGaussianTransitionWeighting, int startIndex, int endIndex, double onsetDetectionFunctionSample, double alphaWeightingFactor);
|
adamstark@46
|
173
|
adamstark@58
|
174 //=======================================================================
|
adamstark@58
|
175
|
adamstark@61
|
176 /** An OnsetDetectionFunction instance for calculating onset detection functions */
|
adamstark@58
|
177 OnsetDetectionFunction odf;
|
adamstark@58
|
178
|
adamstark@58
|
179 //=======================================================================
|
adamstark@46
|
180 // buffers
|
adamstark@63
|
181
|
adamstark@97
|
182 CircularBuffer onsetDF; /**< to hold onset detection function */
|
adamstark@97
|
183 CircularBuffer cumulativeScore; /**< to hold cumulative score */
|
adamstark@63
|
184
|
adamstark@97
|
185 std::vector<double> resampledOnsetDF; /**< to hold resampled detection function */
|
adamstark@97
|
186 std::vector<double> acf; /**< to hold autocorrelation function */
|
adamstark@97
|
187 std::vector<double> weightingVector; /**< to hold weighting vector */
|
adamstark@97
|
188 std::vector<double> combFilterBankOutput; /**< to hold comb filter output */
|
adamstark@97
|
189 std::vector<double> tempoObservationVector; /**< to hold tempo version of comb filter output */
|
adamstark@97
|
190 std::vector<double> delta; /**< to hold final tempo candidate array */
|
adamstark@97
|
191 std::vector<double> prevDelta; /**< previous delta */
|
adamstark@97
|
192 std::vector<double> prevDeltaFixed; /**< fixed tempo version of previous delta */
|
adamstark@97
|
193 double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */
|
adamstark@58
|
194
|
adamstark@60
|
195 //=======================================================================
|
adamstark@56
|
196 // parameters
|
adamstark@60
|
197
|
adamstark@93
|
198 double tightness; /**< the tightness of the weighting used to calculate cumulative score */
|
adamstark@93
|
199 double alpha; /**< the mix between the current detection function sample and the cumulative score's "momentum" */
|
adamstark@93
|
200 double beatPeriod; /**< the beat period, in detection function samples */
|
adamstark@93
|
201 double estimatedTempo; /**< the current tempo estimation being used by the algorithm */
|
adamstark@105
|
202 int timeToNextPrediction; /**< indicates when the next point to predict the next beat is */
|
adamstark@105
|
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 */
|
adamstark@93
|
204 int hopSize; /**< the hop size being used by the algorithm */
|
adamstark@93
|
205 int onsetDFBufferSize; /**< the onset detection function buffer size */
|
adamstark@93
|
206 bool tempoFixed; /**< indicates whether the tempo should be fixed or not */
|
adamstark@93
|
207 bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */
|
adamstark@93
|
208 int FFTLengthForACFCalculation; /**< the FFT length for the auto-correlation function calculation */
|
adamstark@60
|
209
|
adamstark@93
|
210 #ifdef USE_FFTW
|
adamstark@88
|
211 fftw_plan acfForwardFFT; /**< forward fftw plan for calculating auto-correlation function */
|
adamstark@88
|
212 fftw_plan acfBackwardFFT; /**< inverse fftw plan for calculating auto-correlation function */
|
adamstark@92
|
213 fftw_complex* complexIn; /**< to hold complex fft values for input */
|
adamstark@92
|
214 fftw_complex* complexOut; /**< to hold complex fft values for output */
|
adamstark@93
|
215 #endif
|
adamstark@93
|
216
|
adamstark@93
|
217 #ifdef USE_KISS_FFT
|
adamstark@93
|
218 kiss_fft_cfg cfgForwards; /**< Kiss FFT configuration */
|
adamstark@93
|
219 kiss_fft_cfg cfgBackwards; /**< Kiss FFT configuration */
|
adamstark@93
|
220 kiss_fft_cpx* fftIn; /**< FFT input samples, in complex form */
|
adamstark@93
|
221 kiss_fft_cpx* fftOut; /**< FFT output samples, in complex form */
|
adamstark@93
|
222 #endif
|
adamstark@46
|
223
|
adamstark@46
|
224 };
|
adamstark@46
|
225
|
adamstark@46
|
226 #endif
|