Mercurial > hg > btrack
comparison src/BTrack.h @ 36:5bd9ae503dcf master 1.0.0
flow: Merged <release> '1.0.0' to <master> ('master').
author | Adam Stark <adamstark.uk@gmail.com> |
---|---|
date | Tue, 08 Jul 2014 12:32:27 +0100 |
parents | 7af87d3f2ce2 |
children |
comparison
equal
deleted
inserted
replaced
13:0d29e68fa2ee | 36:5bd9ae503dcf |
---|---|
20 //======================================================================= | 20 //======================================================================= |
21 | 21 |
22 #ifndef __BTRACK_H | 22 #ifndef __BTRACK_H |
23 #define __BTRACK_H | 23 #define __BTRACK_H |
24 | 24 |
25 //#include "fftw3.h" | 25 #include "OnsetDetectionFunction.h" |
26 | 26 #include <vector> |
27 | |
28 //======================================================================= | |
29 /** The main beat tracking class and the interface to the BTrack | |
30 * beat tracking algorithm. The algorithm can process either | |
31 * audio frames or onset detection function samples and also | |
32 * contains some static functions for calculating beat times in seconds | |
33 */ | |
27 class BTrack { | 34 class BTrack { |
28 | 35 |
29 public: | 36 public: |
30 BTrack(); // constructor | 37 |
31 ~BTrack(); // destructor | 38 //======================================================================= |
32 | 39 /** Constructor assuming hop size of 512 and frame size of 1024 */ |
33 void initialise(int fsize); | 40 BTrack(); |
34 void process(float df_sample); | 41 |
35 void plotdfbuffer(); | 42 /** Constructor assuming frame size will be double the hopSize |
36 void updatecumscore(float df_sample); | 43 * @param hopSize the hop size in audio samples |
37 void predictbeat(); | 44 */ |
38 void dfconvert(); | 45 BTrack(int hopSize_); |
39 void calcTempo(); | 46 |
40 void adapt_thresh(float x[],int N); | 47 /** Constructor taking both hopSize and frameSize |
41 float mean_array(float array[],int start,int end); | 48 * @param hopSize the hop size in audio samples |
42 void normalise(float array[],int N); | 49 * @param frameSize the frame size in audio samples |
43 void acf_bal(float df_thresh[]); | 50 */ |
44 void getrcfoutput(); | 51 BTrack(int hopSize_,int frameSize_); |
45 void settempo(float tempo); | 52 |
46 void fixtempo(float tempo); | 53 //======================================================================= |
47 void unfixtempo(); | 54 /** Updates the hop and frame size used by the beat tracker |
48 | 55 * @param hopSize the hop size in audio samples |
49 int playbeat; | 56 * @param frameSize the frame size in audio samples |
50 float cscoreval; | 57 */ |
51 float est_tempo; | 58 void updateHopAndFrameSize(int hopSize_,int frameSize_); |
52 | 59 |
60 //======================================================================= | |
61 /** Process a single audio frame | |
62 * @param frame a pointer to an array containing an audio frame. The number of samples should | |
63 * match the frame size that the algorithm was initialised with. | |
64 */ | |
65 void processAudioFrame(double *frame); | |
66 | |
67 /** Add new onset detection function sample to buffer and apply beat tracking | |
68 * @param sample an onset detection function sample | |
69 */ | |
70 void processOnsetDetectionFunctionSample(double sample); | |
71 | |
72 //======================================================================= | |
73 /** @returns the current hop size being used by the beat tracker */ | |
74 int getHopSize(); | |
75 | |
76 /** @returns true if a beat should occur in the current audio frame */ | |
77 bool beatDueInCurrentFrame(); | |
78 | |
79 /** @returns the current tempo estimate being used by the beat tracker */ | |
80 double getCurrentTempoEstimate(); | |
81 | |
82 /** @returns the most recent value of the cumulative score function */ | |
83 double getLatestCumulativeScoreValue(); | |
84 | |
85 //======================================================================= | |
86 /** Set the tempo of the beat tracker | |
87 * @param tempo the tempo in beats per minute (bpm) | |
88 */ | |
89 void setTempo(double tempo); | |
90 | |
91 /** Fix tempo to roughly around some value, so that the algorithm will only try to track | |
92 * tempi around the given tempo | |
93 * @param tempo the tempo in beats per minute (bpm) | |
94 */ | |
95 void fixTempo(double tempo); | |
96 | |
97 /** Tell the algorithm to not fix the tempo anymore */ | |
98 void doNotFixTempo(); | |
99 | |
100 //======================================================================= | |
101 /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency. | |
102 * This version uses a long to represent the frame number | |
103 * @param frameNumber the index of the current frame | |
104 * @param hopSize the hop size in audio samples | |
105 * @param fs the sampling frequency in Hz | |
106 * @returns a beat time in seconds | |
107 */ | |
108 static double getBeatTimeInSeconds(long frameNumber,int hopSize,int fs); | |
109 | |
110 /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency. | |
111 * This version uses an int to represent the frame number | |
112 * @param frameNumber the index of the current frame | |
113 * @param hopSize the hop size in audio samples | |
114 * @param fs the sampling frequency in Hz | |
115 * @returns a beat time in seconds | |
116 */ | |
117 static double getBeatTimeInSeconds(int frameNumber,int hopSize,int fs); | |
118 | |
119 | |
53 private: | 120 private: |
54 | 121 |
122 /** Initialises the algorithm, setting internal parameters and creating weighting vectors | |
123 * @param hopSize_ the hop size in audio samples | |
124 * @param frameSize_ the frame size in audio samples | |
125 */ | |
126 void initialise(int hopSize_,int frameSize_); | |
127 | |
128 /** Initialise with hop size and set all array sizes accordingly | |
129 * @param hopSize_ the hop size in audio samples | |
130 */ | |
131 void setHopSize(int hopSize_); | |
132 | |
133 /** Resamples the onset detection function from an arbitrary number of samples to 512 */ | |
134 void resampleOnsetDetectionFunction(); | |
135 | |
136 /** Updates the cumulative score function with a new onset detection function sample | |
137 * @param odfSample an onset detection function sample | |
138 */ | |
139 void updateCumulativeScore(double odfSample); | |
140 | |
141 /** Predicts the next beat, based upon the internal program state */ | |
142 void predictBeat(); | |
143 | |
144 /** Calculates the current tempo expressed as the beat period in detection function samples */ | |
145 void calculateTempo(); | |
146 | |
147 /** Calculates an adaptive threshold which is used to remove low level energy from detection | |
148 * function and emphasise peaks | |
149 * @param x a pointer to an array containing onset detection function samples | |
150 * @param N the length of the array, x | |
151 */ | |
152 void adaptiveThreshold(double *x,int N); | |
153 | |
154 /** Calculates the mean of values in an array between index locations [startIndex,endIndex] | |
155 * @param array a pointer to an array that contains the values we wish to find the mean from | |
156 * @param startIndex the start index from which we would like to calculate the mean | |
157 * @param endIndex the final index to which we would like to calculate the mean | |
158 * @returns the mean of the sub-section of the array | |
159 */ | |
160 double calculateMeanOfArray(double *array,int startIndex,int endIndex); | |
161 | |
162 /** Normalises a given array | |
163 * @param array a pointer to the array we wish to normalise | |
164 * @param N the length of the array | |
165 */ | |
166 void normaliseArray(double *array,int N); | |
167 | |
168 /** Calculates the balanced autocorrelation of the smoothed onset detection function | |
169 * @param onsetDetectionFunction a pointer to an array containing the onset detection function | |
170 */ | |
171 void calculateBalancedACF(double *onsetDetectionFunction); | |
172 | |
173 /** Calculates the output of the comb filter bank */ | |
174 void calculateOutputOfCombFilterBank(); | |
175 | |
176 //======================================================================= | |
177 | |
178 /** An OnsetDetectionFunction instance for calculating onset detection functions */ | |
179 OnsetDetectionFunction odf; | |
180 | |
181 //======================================================================= | |
55 // buffers | 182 // buffers |
56 float *dfbuffer; // to hold detection function | 183 |
57 float df512[512]; // to hold resampled detection function | 184 std::vector<double> onsetDF; /**< to hold onset detection function */ |
58 float *cumscore; // to hold cumulative score | 185 std::vector<double> cumulativeScore; /**< to hold cumulative score */ |
59 | 186 |
60 float acf[512]; // to hold autocorrelation function | 187 double resampledOnsetDF[512]; /**< to hold resampled detection function */ |
61 | 188 |
62 float wv[128]; // to hold weighting vector | 189 double acf[512]; /**< to hold autocorrelation function */ |
63 | 190 |
64 float rcf[128]; // to hold comb filter output | 191 double weightingVector[128]; /**< to hold weighting vector */ |
65 float t_obs[41]; // to hold tempo version of comb filter output | 192 |
66 | 193 double combFilterBankOutput[128]; /**< to hold comb filter output */ |
67 float delta[41]; // to hold final tempo candidate array | 194 double tempoObservationVector[41]; /**< to hold tempo version of comb filter output */ |
68 float prev_delta[41]; // previous delta | 195 |
69 float prev_delta_fix[41]; // fixed tempo version of previous delta | 196 double delta[41]; /**< to hold final tempo candidate array */ |
70 | 197 double prevDelta[41]; /**< previous delta */ |
71 float t_tmat[41][41]; // transition matrix | 198 double prevDeltaFixed[41]; /**< fixed tempo version of previous delta */ |
72 | 199 |
73 | 200 double tempoTransitionMatrix[41][41]; /**< tempo transition matrix */ |
74 // parameters | 201 |
75 float tightness; | 202 |
76 float alpha; | 203 //======================================================================= |
77 float bperiod; | 204 // parameters |
78 float tempo; | 205 |
79 | 206 |
80 | 207 double tightness; /**< the tightness of the weighting used to calculate cumulative score */ |
81 float p_fact; | 208 |
82 | 209 double alpha; /**< the mix between the current detection function sample and the cumulative score's "momentum" */ |
83 | 210 |
84 // | 211 double beatPeriod; /**< the beat period, in detection function samples */ |
85 int m0; // indicates when the next point to predict the next beat is | 212 |
86 int beat; | 213 double tempo; /**< the tempo in beats per minute */ |
87 | 214 |
88 int dfbuffer_size; | 215 double estimatedTempo; /**< the current tempo estimation being used by the algorithm */ |
89 | 216 |
90 | 217 double latestCumulativeScoreValue; /**< holds the latest value of the cumulative score function */ |
91 int framesize; | 218 |
92 | 219 double tempoToLagFactor; /**< factor for converting between lag and tempo */ |
93 | 220 |
94 int tempofix; | 221 int m0; /**< indicates when the next point to predict the next beat is */ |
95 | 222 |
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 */ | |
224 | |
225 int hopSize; /**< the hop size being used by the algorithm */ | |
226 | |
227 int onsetDFBufferSize; /**< the onset detection function buffer size */ | |
228 | |
229 bool tempoFixed; /**< indicates whether the tempo should be fixed or not */ | |
230 | |
231 bool beatDueInFrame; /**< indicates whether a beat is due in the current frame */ | |
96 | 232 |
97 }; | 233 }; |
98 | 234 |
99 #endif | 235 #endif |