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@55
|
26
|
adamstark@46
|
27 class BTrack {
|
adamstark@46
|
28
|
adamstark@46
|
29 public:
|
adamstark@51
|
30
|
adamstark@58
|
31 //=======================================================================
|
adamstark@55
|
32 /** constructor assuming hop size of 512 and frame size of 1024 */
|
adamstark@56
|
33 BTrack();
|
adamstark@51
|
34
|
adamstark@55
|
35 /** constructor assuming frame size will be double hopSize
|
adamstark@55
|
36 * @param hopSize the step size in audio samples by which we will receive audio frames
|
adamstark@55
|
37 */
|
adamstark@57
|
38 BTrack(int hopSize_);
|
adamstark@55
|
39
|
adamstark@55
|
40 /** constructor taking both hopSize and frameSize
|
adamstark@55
|
41 * @param hopSize the step size in audio samples by which we will receive audio frames
|
adamstark@55
|
42 * @param frameSize the audio frame size in audio samples
|
adamstark@55
|
43 */
|
adamstark@57
|
44 BTrack(int hopSize_,int frameSize_);
|
adamstark@55
|
45
|
adamstark@58
|
46 //=======================================================================
|
adamstark@55
|
47 /** Process a single audio frame */
|
adamstark@56
|
48 void processAudioFrame(double *frame);
|
adamstark@55
|
49
|
adamstark@55
|
50 /** Add new onset detection function sample to buffer and apply beat tracking */
|
adamstark@56
|
51 void processOnsetDetectionFunctionSample(double sample);
|
adamstark@51
|
52
|
adamstark@58
|
53 //=======================================================================
|
adamstark@57
|
54 /** @returns the current hop size being used by the beat tracker */
|
adamstark@57
|
55 int getHopSize();
|
adamstark@57
|
56
|
adamstark@58
|
57 /** @returns true if a beat should occur in the current audio frame */
|
adamstark@58
|
58 bool beatDueInCurrentFrame();
|
adamstark@58
|
59
|
adamstark@58
|
60 /** @returns the current tempo estimate being used by the beat tracker */
|
adamstark@58
|
61 double getCurrentTempoEstimate();
|
adamstark@58
|
62
|
adamstark@58
|
63 /** @returns the most recent value of the cumulative score function */
|
adamstark@58
|
64 double getLatestCumulativeScoreValue();
|
adamstark@58
|
65
|
adamstark@58
|
66 //=======================================================================
|
adamstark@51
|
67 /** Set the tempo of the beat tracker */
|
adamstark@57
|
68 void setTempo(double tempo);
|
adamstark@51
|
69
|
adamstark@51
|
70 /** fix tempo to roughly around some value */
|
adamstark@57
|
71 void fixTempo(double tempo);
|
adamstark@51
|
72
|
adamstark@51
|
73 /** do not fix the tempo anymore */
|
adamstark@57
|
74 void doNotFixTempo();
|
adamstark@55
|
75
|
adamstark@58
|
76 //=======================================================================
|
adamstark@55
|
77 static double getBeatTimeInSeconds(long frameNumber,int hopSize,int fs);
|
adamstark@55
|
78
|
adamstark@55
|
79 static double getBeatTimeInSeconds(int frameNumber,int hopSize,int fs);
|
adamstark@55
|
80
|
adamstark@58
|
81
|
adamstark@46
|
82 private:
|
adamstark@51
|
83
|
adamstark@57
|
84 void initialise(int hopSize_,int frameSize_);
|
adamstark@56
|
85
|
adamstark@56
|
86 /** Initialise with hop size and set all frame sizes accordingly */
|
adamstark@57
|
87 void setHopSize(int hopSize_);
|
adamstark@56
|
88
|
adamstark@51
|
89 /** Convert detection function from N samples to 512 */
|
adamstark@57
|
90 void resampleOnsetDetectionFunction();
|
adamstark@51
|
91
|
adamstark@51
|
92 /** update the cumulative score */
|
adamstark@57
|
93 void updateCumulativeScore(double df_sample);
|
adamstark@51
|
94
|
adamstark@51
|
95 /** predicts the next beat */
|
adamstark@57
|
96 void predictBeat();
|
adamstark@51
|
97
|
adamstark@51
|
98 /** Calculates the current tempo expressed as the beat period in detection function samples */
|
adamstark@57
|
99 void calculateTempo();
|
adamstark@51
|
100
|
adamstark@51
|
101 /** calculates an adaptive threshold which is used to remove low level energy from detection
|
adamstark@51
|
102 * function and emphasise peaks
|
adamstark@51
|
103 */
|
adamstark@57
|
104 void adaptiveThreshold(double *x,int N);
|
adamstark@51
|
105
|
adamstark@51
|
106 /** calculates the mean of values in an array from index locations [start,end] */
|
adamstark@57
|
107 double calculateMeanOfArray(double *array,int start,int end);
|
adamstark@51
|
108
|
adamstark@51
|
109 /** normalises a given array */
|
adamstark@57
|
110 void normaliseArray(double *array,int N);
|
adamstark@51
|
111
|
adamstark@51
|
112 /** calculates the balanced autocorrelation of the smoothed detection function */
|
adamstark@57
|
113 void calculateBalancedACF(double *df_thresh);
|
adamstark@51
|
114
|
adamstark@57
|
115 /** calculates the output of the comb filter bank */
|
adamstark@57
|
116 void calculateOutputOfCombFilterBank();
|
adamstark@46
|
117
|
adamstark@58
|
118 //=======================================================================
|
adamstark@58
|
119
|
adamstark@58
|
120 OnsetDetectionFunction odf;
|
adamstark@58
|
121
|
adamstark@58
|
122 //=======================================================================
|
adamstark@46
|
123 // buffers
|
adamstark@58
|
124 double *onsetDF; /**< to hold detection function */
|
adamstark@58
|
125 double resampledOnsetDF[512]; /**< to hold resampled detection function */
|
adamstark@58
|
126 double *cumulativeScore; /**< to hold cumulative score */
|
adamstark@46
|
127
|
adamstark@58
|
128 double acf[512]; /**< to hold autocorrelation function */
|
adamstark@46
|
129
|
adamstark@58
|
130 double weightingVector[128]; /**< to hold weighting vector */
|
adamstark@46
|
131
|
adamstark@58
|
132 double combFilterBankOutput[128]; /**< to hold comb filter output */
|
adamstark@58
|
133 double tempoObservationVector[41]; /**< to hold tempo version of comb filter output */
|
adamstark@46
|
134
|
adamstark@58
|
135 double delta[41]; /**< to hold final tempo candidate array */
|
adamstark@58
|
136 double prevDelta[41]; /**< previous delta */
|
adamstark@58
|
137 double prevDeltaFixed[41]; /**< fixed tempo version of previous delta */
|
adamstark@46
|
138
|
adamstark@58
|
139 double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */
|
adamstark@46
|
140
|
adamstark@58
|
141
|
adamstark@46
|
142
|
adamstark@56
|
143 // parameters
|
adamstark@56
|
144 double tightness;
|
adamstark@56
|
145 double alpha;
|
adamstark@57
|
146 double beatPeriod;
|
adamstark@56
|
147 double tempo;
|
adamstark@46
|
148
|
adamstark@58
|
149 double estimatedTempo; /**< the current tempo estimation being used by the algorithm */
|
adamstark@58
|
150
|
adamstark@58
|
151 double latestCumulativeScoreValue; /**< holds the latest value of the cumulative score function */
|
adamstark@58
|
152
|
adamstark@56
|
153 double p_fact;
|
adamstark@46
|
154
|
adamstark@58
|
155 int m0; // indicates when the next point to predict the next beat is
|
adamstark@58
|
156
|
adamstark@58
|
157 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
|
158
|
adamstark@58
|
159 int hopSize; /**< the hop size being used by the algorithm */
|
adamstark@58
|
160
|
adamstark@58
|
161 int onsetDFBufferSize; /**< the onset detection function buffer size */
|
adamstark@46
|
162
|
adamstark@58
|
163 bool tempoFixed; /**< indicates whether the tempo should be fixed or not */
|
adamstark@57
|
164
|
adamstark@58
|
165 bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */
|
adamstark@46
|
166
|
adamstark@46
|
167 };
|
adamstark@46
|
168
|
adamstark@46
|
169 #endif
|