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