adamstark@46
|
1 //=======================================================================
|
adamstark@46
|
2 /** @file BTrack.cpp
|
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 #include <cmath>
|
adamstark@52
|
23 #include <algorithm>
|
adamstark@46
|
24 #include "BTrack.h"
|
adamstark@46
|
25 #include "samplerate.h"
|
adamstark@46
|
26
|
adamstark@55
|
27 //=======================================================================
|
adamstark@57
|
28 BTrack::BTrack() : odf(512,1024,ComplexSpectralDifferenceHWR,HanningWindow)
|
adamstark@55
|
29 {
|
adamstark@55
|
30 initialise(512, 1024);
|
adamstark@55
|
31 }
|
adamstark@46
|
32
|
adamstark@51
|
33 //=======================================================================
|
adamstark@57
|
34 BTrack::BTrack(int hopSize_) : odf(hopSize_,2*hopSize_,ComplexSpectralDifferenceHWR,HanningWindow)
|
adamstark@46
|
35 {
|
adamstark@57
|
36 initialise(hopSize_, 2*hopSize_);
|
adamstark@55
|
37 }
|
adamstark@55
|
38
|
adamstark@55
|
39 //=======================================================================
|
adamstark@57
|
40 BTrack::BTrack(int hopSize_,int frameSize_) : odf(hopSize_,frameSize_,ComplexSpectralDifferenceHWR,HanningWindow)
|
adamstark@55
|
41 {
|
adamstark@57
|
42 initialise(hopSize_, frameSize_);
|
adamstark@55
|
43 }
|
adamstark@55
|
44
|
adamstark@55
|
45 //=======================================================================
|
adamstark@55
|
46 double BTrack::getBeatTimeInSeconds(long frameNumber,int hopSize,int fs)
|
adamstark@55
|
47 {
|
adamstark@55
|
48 double hop = (double) hopSize;
|
adamstark@55
|
49 double samplingFrequency = (double) fs;
|
adamstark@55
|
50 double frameNum = (double) frameNumber;
|
adamstark@55
|
51
|
adamstark@55
|
52 return ((hop / samplingFrequency) * frameNum);
|
adamstark@55
|
53 }
|
adamstark@55
|
54
|
adamstark@55
|
55 //=======================================================================
|
adamstark@55
|
56 double BTrack::getBeatTimeInSeconds(int frameNumber,int hopSize,int fs)
|
adamstark@55
|
57 {
|
adamstark@55
|
58 long frameNum = (long) frameNumber;
|
adamstark@55
|
59
|
adamstark@55
|
60 return getBeatTimeInSeconds(frameNum, hopSize, fs);
|
adamstark@55
|
61 }
|
adamstark@55
|
62
|
adamstark@55
|
63
|
adamstark@55
|
64
|
adamstark@55
|
65 //=======================================================================
|
adamstark@57
|
66 void BTrack::initialise(int hopSize_, int frameSize_)
|
adamstark@55
|
67 {
|
adamstark@55
|
68 double rayparam = 43;
|
adamstark@54
|
69 double pi = 3.14159265;
|
adamstark@46
|
70
|
adamstark@46
|
71
|
adamstark@46
|
72 // initialise parameters
|
adamstark@46
|
73 tightness = 5;
|
adamstark@46
|
74 alpha = 0.9;
|
adamstark@46
|
75 tempo = 120;
|
adamstark@58
|
76 estimatedTempo = 120.0;
|
adamstark@59
|
77 tempoToLagFactor = 60.*44100./512.;
|
adamstark@46
|
78
|
adamstark@46
|
79 m0 = 10;
|
adamstark@58
|
80 beatCounter = -1;
|
adamstark@46
|
81
|
adamstark@57
|
82 beatDueInFrame = false;
|
adamstark@46
|
83
|
adamstark@58
|
84
|
adamstark@46
|
85 // create rayleigh weighting vector
|
adamstark@46
|
86 for (int n = 0;n < 128;n++)
|
adamstark@46
|
87 {
|
adamstark@58
|
88 weightingVector[n] = ((double) n / pow(rayparam,2)) * exp((-1*pow((double)-n,2)) / (2*pow(rayparam,2)));
|
adamstark@46
|
89 }
|
adamstark@46
|
90
|
adamstark@46
|
91 // initialise prev_delta
|
adamstark@46
|
92 for (int i = 0;i < 41;i++)
|
adamstark@46
|
93 {
|
adamstark@58
|
94 prevDelta[i] = 1;
|
adamstark@46
|
95 }
|
adamstark@46
|
96
|
adamstark@54
|
97 double t_mu = 41/2;
|
adamstark@54
|
98 double m_sig;
|
adamstark@54
|
99 double x;
|
adamstark@46
|
100 // create tempo transition matrix
|
adamstark@46
|
101 m_sig = 41/8;
|
adamstark@46
|
102 for (int i = 0;i < 41;i++)
|
adamstark@46
|
103 {
|
adamstark@46
|
104 for (int j = 0;j < 41;j++)
|
adamstark@46
|
105 {
|
adamstark@46
|
106 x = j+1;
|
adamstark@46
|
107 t_mu = i+1;
|
adamstark@58
|
108 tempoTransitionMatrix[i][j] = (1 / (m_sig * sqrt(2*pi))) * exp( (-1*pow((x-t_mu),2)) / (2*pow(m_sig,2)) );
|
adamstark@46
|
109 }
|
adamstark@55
|
110 }
|
adamstark@46
|
111
|
adamstark@46
|
112 // tempo is not fixed
|
adamstark@58
|
113 tempoFixed = false;
|
adamstark@58
|
114
|
adamstark@58
|
115 // initialise latest cumulative score value
|
adamstark@58
|
116 // in case it is requested before any processing takes place
|
adamstark@58
|
117 latestCumulativeScoreValue = 0;
|
adamstark@55
|
118
|
adamstark@55
|
119 // initialise algorithm given the hopsize
|
adamstark@57
|
120 setHopSize(hopSize_);
|
adamstark@46
|
121 }
|
adamstark@46
|
122
|
adamstark@51
|
123 //=======================================================================
|
adamstark@57
|
124 void BTrack::setHopSize(int hopSize_)
|
adamstark@46
|
125 {
|
adamstark@57
|
126 hopSize = hopSize_;
|
adamstark@58
|
127 onsetDFBufferSize = (512*512)/hopSize; // calculate df buffer size
|
adamstark@46
|
128
|
adamstark@57
|
129 beatPeriod = round(60/((((double) hopSize)/44100)*tempo));
|
adamstark@63
|
130
|
adamstark@63
|
131 // set size of onset detection function buffer
|
adamstark@63
|
132 onsetDF.resize(onsetDFBufferSize);
|
adamstark@63
|
133
|
adamstark@63
|
134 // set size of cumulative score buffer
|
adamstark@63
|
135 cumulativeScore.resize(onsetDFBufferSize);
|
adamstark@46
|
136
|
adamstark@46
|
137 // initialise df_buffer to zeros
|
adamstark@58
|
138 for (int i = 0;i < onsetDFBufferSize;i++)
|
adamstark@46
|
139 {
|
adamstark@58
|
140 onsetDF[i] = 0;
|
adamstark@58
|
141 cumulativeScore[i] = 0;
|
adamstark@46
|
142
|
adamstark@46
|
143
|
adamstark@57
|
144 if ((i % ((int) round(beatPeriod))) == 0)
|
adamstark@46
|
145 {
|
adamstark@58
|
146 onsetDF[i] = 1;
|
adamstark@46
|
147 }
|
adamstark@46
|
148 }
|
adamstark@46
|
149 }
|
adamstark@46
|
150
|
adamstark@51
|
151 //=======================================================================
|
adamstark@57
|
152 bool BTrack::beatDueInCurrentFrame()
|
adamstark@57
|
153 {
|
adamstark@57
|
154 return beatDueInFrame;
|
adamstark@57
|
155 }
|
adamstark@57
|
156
|
adamstark@57
|
157 //=======================================================================
|
adamstark@57
|
158 int BTrack::getHopSize()
|
adamstark@57
|
159 {
|
adamstark@57
|
160 return hopSize;
|
adamstark@57
|
161 }
|
adamstark@57
|
162
|
adamstark@57
|
163 //=======================================================================
|
adamstark@58
|
164 double BTrack::getLatestCumulativeScoreValue()
|
adamstark@58
|
165 {
|
adamstark@58
|
166 return latestCumulativeScoreValue;
|
adamstark@58
|
167 }
|
adamstark@58
|
168
|
adamstark@58
|
169 //=======================================================================
|
adamstark@55
|
170 void BTrack::processAudioFrame(double *frame)
|
adamstark@55
|
171 {
|
adamstark@55
|
172 // calculate the onset detection function sample for the frame
|
adamstark@59
|
173 double sample = odf.calculateOnsetDetectionFunctionSample(frame);
|
adamstark@55
|
174
|
adamstark@56
|
175
|
adamstark@55
|
176
|
adamstark@55
|
177 // process the new onset detection function sample in the beat tracking algorithm
|
adamstark@55
|
178 processOnsetDetectionFunctionSample(sample);
|
adamstark@55
|
179 }
|
adamstark@55
|
180
|
adamstark@55
|
181 //=======================================================================
|
adamstark@55
|
182 void BTrack::processOnsetDetectionFunctionSample(double newSample)
|
adamstark@56
|
183 {
|
adamstark@56
|
184 // we need to ensure that the onset
|
adamstark@56
|
185 // detection function sample is positive
|
adamstark@56
|
186 newSample = fabs(newSample);
|
adamstark@56
|
187
|
adamstark@56
|
188 // add a tiny constant to the sample to stop it from ever going
|
adamstark@56
|
189 // to zero. this is to avoid problems further down the line
|
adamstark@56
|
190 newSample = newSample + 0.0001;
|
adamstark@56
|
191
|
adamstark@46
|
192 m0--;
|
adamstark@58
|
193 beatCounter--;
|
adamstark@57
|
194 beatDueInFrame = false;
|
adamstark@46
|
195
|
adamstark@46
|
196 // move all samples back one step
|
adamstark@58
|
197 for (int i=0;i < (onsetDFBufferSize-1);i++)
|
adamstark@46
|
198 {
|
adamstark@58
|
199 onsetDF[i] = onsetDF[i+1];
|
adamstark@46
|
200 }
|
adamstark@46
|
201
|
adamstark@46
|
202 // add new sample at the end
|
adamstark@58
|
203 onsetDF[onsetDFBufferSize-1] = newSample;
|
adamstark@46
|
204
|
adamstark@46
|
205 // update cumulative score
|
adamstark@57
|
206 updateCumulativeScore(newSample);
|
adamstark@46
|
207
|
adamstark@46
|
208 // if we are halfway between beats
|
adamstark@46
|
209 if (m0 == 0)
|
adamstark@46
|
210 {
|
adamstark@57
|
211 predictBeat();
|
adamstark@46
|
212 }
|
adamstark@46
|
213
|
adamstark@46
|
214 // if we are at a beat
|
adamstark@58
|
215 if (beatCounter == 0)
|
adamstark@46
|
216 {
|
adamstark@57
|
217 beatDueInFrame = true; // indicate a beat should be output
|
adamstark@46
|
218
|
adamstark@46
|
219 // recalculate the tempo
|
adamstark@57
|
220 resampleOnsetDetectionFunction();
|
adamstark@57
|
221 calculateTempo();
|
adamstark@46
|
222 }
|
adamstark@46
|
223 }
|
adamstark@46
|
224
|
adamstark@51
|
225 //=======================================================================
|
adamstark@57
|
226 void BTrack::setTempo(double tempo)
|
adamstark@46
|
227 {
|
adamstark@46
|
228
|
adamstark@46
|
229 /////////// TEMPO INDICATION RESET //////////////////
|
adamstark@46
|
230
|
adamstark@46
|
231 // firstly make sure tempo is between 80 and 160 bpm..
|
adamstark@46
|
232 while (tempo > 160)
|
adamstark@46
|
233 {
|
adamstark@46
|
234 tempo = tempo/2;
|
adamstark@46
|
235 }
|
adamstark@46
|
236
|
adamstark@46
|
237 while (tempo < 80)
|
adamstark@46
|
238 {
|
adamstark@46
|
239 tempo = tempo * 2;
|
adamstark@46
|
240 }
|
adamstark@46
|
241
|
adamstark@46
|
242 // convert tempo from bpm value to integer index of tempo probability
|
adamstark@46
|
243 int tempo_index = (int) round((tempo - 80)/2);
|
adamstark@46
|
244
|
adamstark@46
|
245 // now set previous tempo observations to zero
|
adamstark@46
|
246 for (int i=0;i < 41;i++)
|
adamstark@46
|
247 {
|
adamstark@58
|
248 prevDelta[i] = 0;
|
adamstark@46
|
249 }
|
adamstark@46
|
250
|
adamstark@46
|
251 // set desired tempo index to 1
|
adamstark@58
|
252 prevDelta[tempo_index] = 1;
|
adamstark@46
|
253
|
adamstark@46
|
254
|
adamstark@46
|
255 /////////// CUMULATIVE SCORE ARTIFICAL TEMPO UPDATE //////////////////
|
adamstark@46
|
256
|
adamstark@46
|
257 // calculate new beat period
|
adamstark@57
|
258 int new_bperiod = (int) round(60/((((double) hopSize)/44100)*tempo));
|
adamstark@46
|
259
|
adamstark@46
|
260 int bcounter = 1;
|
adamstark@46
|
261 // initialise df_buffer to zeros
|
adamstark@58
|
262 for (int i = (onsetDFBufferSize-1);i >= 0;i--)
|
adamstark@46
|
263 {
|
adamstark@46
|
264 if (bcounter == 1)
|
adamstark@46
|
265 {
|
adamstark@58
|
266 cumulativeScore[i] = 150;
|
adamstark@58
|
267 onsetDF[i] = 150;
|
adamstark@46
|
268 }
|
adamstark@46
|
269 else
|
adamstark@46
|
270 {
|
adamstark@58
|
271 cumulativeScore[i] = 10;
|
adamstark@58
|
272 onsetDF[i] = 10;
|
adamstark@46
|
273 }
|
adamstark@46
|
274
|
adamstark@46
|
275 bcounter++;
|
adamstark@46
|
276
|
adamstark@46
|
277 if (bcounter > new_bperiod)
|
adamstark@46
|
278 {
|
adamstark@46
|
279 bcounter = 1;
|
adamstark@46
|
280 }
|
adamstark@46
|
281 }
|
adamstark@46
|
282
|
adamstark@46
|
283 /////////// INDICATE THAT THIS IS A BEAT //////////////////
|
adamstark@46
|
284
|
adamstark@46
|
285 // beat is now
|
adamstark@58
|
286 beatCounter = 0;
|
adamstark@46
|
287
|
adamstark@46
|
288 // offbeat is half of new beat period away
|
adamstark@54
|
289 m0 = (int) round(((double) new_bperiod)/2);
|
adamstark@46
|
290 }
|
adamstark@46
|
291
|
adamstark@51
|
292 //=======================================================================
|
adamstark@57
|
293 void BTrack::fixTempo(double tempo)
|
adamstark@46
|
294 {
|
adamstark@46
|
295 // firstly make sure tempo is between 80 and 160 bpm..
|
adamstark@46
|
296 while (tempo > 160)
|
adamstark@46
|
297 {
|
adamstark@46
|
298 tempo = tempo/2;
|
adamstark@46
|
299 }
|
adamstark@46
|
300
|
adamstark@46
|
301 while (tempo < 80)
|
adamstark@46
|
302 {
|
adamstark@46
|
303 tempo = tempo * 2;
|
adamstark@46
|
304 }
|
adamstark@46
|
305
|
adamstark@46
|
306 // convert tempo from bpm value to integer index of tempo probability
|
adamstark@46
|
307 int tempo_index = (int) round((tempo - 80)/2);
|
adamstark@46
|
308
|
adamstark@46
|
309 // now set previous fixed previous tempo observation values to zero
|
adamstark@46
|
310 for (int i=0;i < 41;i++)
|
adamstark@46
|
311 {
|
adamstark@58
|
312 prevDeltaFixed[i] = 0;
|
adamstark@46
|
313 }
|
adamstark@46
|
314
|
adamstark@46
|
315 // set desired tempo index to 1
|
adamstark@58
|
316 prevDeltaFixed[tempo_index] = 1;
|
adamstark@46
|
317
|
adamstark@46
|
318 // set the tempo fix flag
|
adamstark@58
|
319 tempoFixed = true;
|
adamstark@46
|
320 }
|
adamstark@46
|
321
|
adamstark@51
|
322 //=======================================================================
|
adamstark@57
|
323 void BTrack::doNotFixTempo()
|
adamstark@46
|
324 {
|
adamstark@46
|
325 // set the tempo fix flag
|
adamstark@58
|
326 tempoFixed = false;
|
adamstark@46
|
327 }
|
adamstark@46
|
328
|
adamstark@51
|
329 //=======================================================================
|
adamstark@57
|
330 void BTrack::resampleOnsetDetectionFunction()
|
adamstark@46
|
331 {
|
adamstark@46
|
332 float output[512];
|
adamstark@58
|
333 float input[onsetDFBufferSize];
|
adamstark@54
|
334
|
adamstark@58
|
335 for (int i = 0;i < onsetDFBufferSize;i++)
|
adamstark@54
|
336 {
|
adamstark@58
|
337 input[i] = (float) onsetDF[i];
|
adamstark@54
|
338 }
|
adamstark@46
|
339
|
adamstark@58
|
340 double src_ratio = 512.0/((double) onsetDFBufferSize);
|
adamstark@58
|
341 int BUFFER_LEN = onsetDFBufferSize;
|
adamstark@46
|
342 int output_len;
|
adamstark@46
|
343 SRC_DATA src_data ;
|
adamstark@46
|
344
|
adamstark@46
|
345 //output_len = (int) floor (((double) BUFFER_LEN) * src_ratio) ;
|
adamstark@46
|
346 output_len = 512;
|
adamstark@46
|
347
|
adamstark@54
|
348 src_data.data_in = input;
|
adamstark@46
|
349 src_data.input_frames = BUFFER_LEN;
|
adamstark@46
|
350
|
adamstark@46
|
351 src_data.src_ratio = src_ratio;
|
adamstark@46
|
352
|
adamstark@46
|
353 src_data.data_out = output;
|
adamstark@46
|
354 src_data.output_frames = output_len;
|
adamstark@46
|
355
|
adamstark@46
|
356 src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1);
|
adamstark@46
|
357
|
adamstark@46
|
358 for (int i = 0;i < output_len;i++)
|
adamstark@46
|
359 {
|
adamstark@58
|
360 resampledOnsetDF[i] = (double) src_data.data_out[i];
|
adamstark@46
|
361 }
|
adamstark@46
|
362 }
|
adamstark@46
|
363
|
adamstark@51
|
364 //=======================================================================
|
adamstark@57
|
365 void BTrack::calculateTempo()
|
adamstark@46
|
366 {
|
adamstark@46
|
367 // adaptive threshold on input
|
adamstark@58
|
368 adaptiveThreshold(resampledOnsetDF,512);
|
adamstark@46
|
369
|
adamstark@46
|
370 // calculate auto-correlation function of detection function
|
adamstark@58
|
371 calculateBalancedACF(resampledOnsetDF);
|
adamstark@46
|
372
|
adamstark@46
|
373 // calculate output of comb filterbank
|
adamstark@57
|
374 calculateOutputOfCombFilterBank();
|
adamstark@46
|
375
|
adamstark@46
|
376
|
adamstark@46
|
377 // adaptive threshold on rcf
|
adamstark@58
|
378 adaptiveThreshold(combFilterBankOutput,128);
|
adamstark@46
|
379
|
adamstark@46
|
380
|
adamstark@46
|
381 int t_index;
|
adamstark@46
|
382 int t_index2;
|
adamstark@59
|
383 // calculate tempo observation vector from beat period observation vector
|
adamstark@46
|
384 for (int i = 0;i < 41;i++)
|
adamstark@46
|
385 {
|
adamstark@59
|
386 t_index = (int) round(tempoToLagFactor / ((double) ((2*i)+80)));
|
adamstark@59
|
387 t_index2 = (int) round(tempoToLagFactor / ((double) ((4*i)+160)));
|
adamstark@46
|
388
|
adamstark@46
|
389
|
adamstark@58
|
390 tempoObservationVector[i] = combFilterBankOutput[t_index-1] + combFilterBankOutput[t_index2-1];
|
adamstark@46
|
391 }
|
adamstark@46
|
392
|
adamstark@46
|
393
|
adamstark@54
|
394 double maxval;
|
adamstark@54
|
395 double maxind;
|
adamstark@54
|
396 double curval;
|
adamstark@46
|
397
|
adamstark@46
|
398 // if tempo is fixed then always use a fixed set of tempi as the previous observation probability function
|
adamstark@58
|
399 if (tempoFixed)
|
adamstark@46
|
400 {
|
adamstark@46
|
401 for (int k = 0;k < 41;k++)
|
adamstark@46
|
402 {
|
adamstark@58
|
403 prevDelta[k] = prevDeltaFixed[k];
|
adamstark@46
|
404 }
|
adamstark@46
|
405 }
|
adamstark@46
|
406
|
adamstark@46
|
407 for (int j=0;j < 41;j++)
|
adamstark@46
|
408 {
|
adamstark@46
|
409 maxval = -1;
|
adamstark@46
|
410 for (int i = 0;i < 41;i++)
|
adamstark@46
|
411 {
|
adamstark@58
|
412 curval = prevDelta[i]*tempoTransitionMatrix[i][j];
|
adamstark@46
|
413
|
adamstark@46
|
414 if (curval > maxval)
|
adamstark@46
|
415 {
|
adamstark@46
|
416 maxval = curval;
|
adamstark@46
|
417 }
|
adamstark@46
|
418 }
|
adamstark@46
|
419
|
adamstark@58
|
420 delta[j] = maxval*tempoObservationVector[j];
|
adamstark@46
|
421 }
|
adamstark@46
|
422
|
adamstark@46
|
423
|
adamstark@57
|
424 normaliseArray(delta,41);
|
adamstark@46
|
425
|
adamstark@46
|
426 maxind = -1;
|
adamstark@46
|
427 maxval = -1;
|
adamstark@46
|
428
|
adamstark@46
|
429 for (int j=0;j < 41;j++)
|
adamstark@46
|
430 {
|
adamstark@46
|
431 if (delta[j] > maxval)
|
adamstark@46
|
432 {
|
adamstark@46
|
433 maxval = delta[j];
|
adamstark@46
|
434 maxind = j;
|
adamstark@46
|
435 }
|
adamstark@46
|
436
|
adamstark@58
|
437 prevDelta[j] = delta[j];
|
adamstark@46
|
438 }
|
adamstark@46
|
439
|
adamstark@57
|
440 beatPeriod = round((60.0*44100.0)/(((2*maxind)+80)*((double) hopSize)));
|
adamstark@46
|
441
|
adamstark@57
|
442 if (beatPeriod > 0)
|
adamstark@46
|
443 {
|
adamstark@58
|
444 estimatedTempo = 60.0/((((double) hopSize) / 44100.0)*beatPeriod);
|
adamstark@46
|
445 }
|
adamstark@46
|
446 }
|
adamstark@46
|
447
|
adamstark@51
|
448 //=======================================================================
|
adamstark@57
|
449 void BTrack::adaptiveThreshold(double *x,int N)
|
adamstark@46
|
450 {
|
adamstark@46
|
451 int i = 0;
|
adamstark@46
|
452 int k,t = 0;
|
adamstark@54
|
453 double x_thresh[N];
|
adamstark@46
|
454
|
adamstark@46
|
455 int p_post = 7;
|
adamstark@46
|
456 int p_pre = 8;
|
adamstark@46
|
457
|
adamstark@52
|
458 t = std::min(N,p_post); // what is smaller, p_post of df size. This is to avoid accessing outside of arrays
|
adamstark@46
|
459
|
adamstark@46
|
460 // find threshold for first 't' samples, where a full average cannot be computed yet
|
adamstark@46
|
461 for (i = 0;i <= t;i++)
|
adamstark@46
|
462 {
|
adamstark@52
|
463 k = std::min((i+p_pre),N);
|
adamstark@57
|
464 x_thresh[i] = calculateMeanOfArray(x,1,k);
|
adamstark@46
|
465 }
|
adamstark@46
|
466 // find threshold for bulk of samples across a moving average from [i-p_pre,i+p_post]
|
adamstark@46
|
467 for (i = t+1;i < N-p_post;i++)
|
adamstark@46
|
468 {
|
adamstark@57
|
469 x_thresh[i] = calculateMeanOfArray(x,i-p_pre,i+p_post);
|
adamstark@46
|
470 }
|
adamstark@46
|
471 // for last few samples calculate threshold, again, not enough samples to do as above
|
adamstark@46
|
472 for (i = N-p_post;i < N;i++)
|
adamstark@46
|
473 {
|
adamstark@52
|
474 k = std::max((i-p_post),1);
|
adamstark@57
|
475 x_thresh[i] = calculateMeanOfArray(x,k,N);
|
adamstark@46
|
476 }
|
adamstark@46
|
477
|
adamstark@46
|
478 // subtract the threshold from the detection function and check that it is not less than 0
|
adamstark@46
|
479 for (i = 0;i < N;i++)
|
adamstark@46
|
480 {
|
adamstark@46
|
481 x[i] = x[i] - x_thresh[i];
|
adamstark@46
|
482 if (x[i] < 0)
|
adamstark@46
|
483 {
|
adamstark@46
|
484 x[i] = 0;
|
adamstark@46
|
485 }
|
adamstark@46
|
486 }
|
adamstark@46
|
487 }
|
adamstark@46
|
488
|
adamstark@51
|
489 //=======================================================================
|
adamstark@57
|
490 void BTrack::calculateOutputOfCombFilterBank()
|
adamstark@46
|
491 {
|
adamstark@46
|
492 int numelem;
|
adamstark@46
|
493
|
adamstark@46
|
494 for (int i = 0;i < 128;i++)
|
adamstark@46
|
495 {
|
adamstark@58
|
496 combFilterBankOutput[i] = 0;
|
adamstark@46
|
497 }
|
adamstark@46
|
498
|
adamstark@46
|
499 numelem = 4;
|
adamstark@46
|
500
|
adamstark@46
|
501 for (int i = 2;i <= 127;i++) // max beat period
|
adamstark@46
|
502 {
|
adamstark@46
|
503 for (int a = 1;a <= numelem;a++) // number of comb elements
|
adamstark@46
|
504 {
|
adamstark@46
|
505 for (int b = 1-a;b <= a-1;b++) // general state using normalisation of comb elements
|
adamstark@46
|
506 {
|
adamstark@58
|
507 combFilterBankOutput[i-1] = combFilterBankOutput[i-1] + (acf[(a*i+b)-1]*weightingVector[i-1])/(2*a-1); // calculate value for comb filter row
|
adamstark@46
|
508 }
|
adamstark@46
|
509 }
|
adamstark@46
|
510 }
|
adamstark@46
|
511 }
|
adamstark@46
|
512
|
adamstark@51
|
513 //=======================================================================
|
adamstark@60
|
514 void BTrack::calculateBalancedACF(double *onsetDetectionFunction)
|
adamstark@46
|
515 {
|
adamstark@46
|
516 int l, n = 0;
|
adamstark@54
|
517 double sum, tmp;
|
adamstark@46
|
518
|
adamstark@46
|
519 // for l lags from 0-511
|
adamstark@46
|
520 for (l = 0;l < 512;l++)
|
adamstark@46
|
521 {
|
adamstark@46
|
522 sum = 0;
|
adamstark@46
|
523
|
adamstark@46
|
524 // for n samples from 0 - (512-lag)
|
adamstark@46
|
525 for (n = 0;n < (512-l);n++)
|
adamstark@46
|
526 {
|
adamstark@60
|
527 tmp = onsetDetectionFunction[n] * onsetDetectionFunction[n+l]; // multiply current sample n by sample (n+l)
|
adamstark@46
|
528 sum = sum + tmp; // add to sum
|
adamstark@46
|
529 }
|
adamstark@46
|
530
|
adamstark@46
|
531 acf[l] = sum / (512-l); // weight by number of mults and add to acf buffer
|
adamstark@46
|
532 }
|
adamstark@46
|
533 }
|
adamstark@46
|
534
|
adamstark@51
|
535 //=======================================================================
|
adamstark@59
|
536 double BTrack::calculateMeanOfArray(double *array,int startIndex,int endIndex)
|
adamstark@46
|
537 {
|
adamstark@46
|
538 int i;
|
adamstark@47
|
539 double sum = 0;
|
adamstark@47
|
540
|
adamstark@59
|
541 int length = endIndex - startIndex;
|
adamstark@46
|
542
|
adamstark@46
|
543 // find sum
|
adamstark@59
|
544 for (i = startIndex;i < endIndex;i++)
|
adamstark@46
|
545 {
|
adamstark@46
|
546 sum = sum + array[i];
|
adamstark@46
|
547 }
|
adamstark@46
|
548
|
adamstark@47
|
549 if (length > 0)
|
adamstark@47
|
550 {
|
adamstark@47
|
551 return sum / length; // average and return
|
adamstark@47
|
552 }
|
adamstark@47
|
553 else
|
adamstark@47
|
554 {
|
adamstark@47
|
555 return 0;
|
adamstark@47
|
556 }
|
adamstark@46
|
557 }
|
adamstark@46
|
558
|
adamstark@51
|
559 //=======================================================================
|
adamstark@57
|
560 void BTrack::normaliseArray(double *array,int N)
|
adamstark@46
|
561 {
|
adamstark@46
|
562 double sum = 0;
|
adamstark@46
|
563
|
adamstark@46
|
564 for (int i = 0;i < N;i++)
|
adamstark@46
|
565 {
|
adamstark@46
|
566 if (array[i] > 0)
|
adamstark@46
|
567 {
|
adamstark@46
|
568 sum = sum + array[i];
|
adamstark@46
|
569 }
|
adamstark@46
|
570 }
|
adamstark@46
|
571
|
adamstark@46
|
572 if (sum > 0)
|
adamstark@46
|
573 {
|
adamstark@46
|
574 for (int i = 0;i < N;i++)
|
adamstark@46
|
575 {
|
adamstark@46
|
576 array[i] = array[i] / sum;
|
adamstark@46
|
577 }
|
adamstark@46
|
578 }
|
adamstark@46
|
579 }
|
adamstark@46
|
580
|
adamstark@51
|
581 //=======================================================================
|
adamstark@59
|
582 void BTrack::updateCumulativeScore(double odfSample)
|
adamstark@46
|
583 {
|
adamstark@46
|
584 int start, end, winsize;
|
adamstark@54
|
585 double max;
|
adamstark@46
|
586
|
adamstark@58
|
587 start = onsetDFBufferSize - round(2*beatPeriod);
|
adamstark@58
|
588 end = onsetDFBufferSize - round(beatPeriod/2);
|
adamstark@46
|
589 winsize = end-start+1;
|
adamstark@46
|
590
|
adamstark@54
|
591 double w1[winsize];
|
adamstark@57
|
592 double v = -2*beatPeriod;
|
adamstark@54
|
593 double wcumscore;
|
adamstark@46
|
594
|
adamstark@46
|
595
|
adamstark@46
|
596 // create window
|
adamstark@46
|
597 for (int i = 0;i < winsize;i++)
|
adamstark@46
|
598 {
|
adamstark@57
|
599 w1[i] = exp((-1*pow(tightness*log(-v/beatPeriod),2))/2);
|
adamstark@46
|
600 v = v+1;
|
adamstark@46
|
601 }
|
adamstark@46
|
602
|
adamstark@46
|
603 // calculate new cumulative score value
|
adamstark@46
|
604 max = 0;
|
adamstark@46
|
605 int n = 0;
|
adamstark@46
|
606 for (int i=start;i <= end;i++)
|
adamstark@46
|
607 {
|
adamstark@58
|
608 wcumscore = cumulativeScore[i]*w1[n];
|
adamstark@46
|
609
|
adamstark@46
|
610 if (wcumscore > max)
|
adamstark@46
|
611 {
|
adamstark@46
|
612 max = wcumscore;
|
adamstark@46
|
613 }
|
adamstark@46
|
614 n++;
|
adamstark@46
|
615 }
|
adamstark@46
|
616
|
adamstark@46
|
617
|
adamstark@46
|
618 // shift cumulative score back one
|
adamstark@58
|
619 for (int i = 0;i < (onsetDFBufferSize-1);i++)
|
adamstark@46
|
620 {
|
adamstark@58
|
621 cumulativeScore[i] = cumulativeScore[i+1];
|
adamstark@46
|
622 }
|
adamstark@46
|
623
|
adamstark@46
|
624 // add new value to cumulative score
|
adamstark@59
|
625 cumulativeScore[onsetDFBufferSize-1] = ((1-alpha)*odfSample) + (alpha*max);
|
adamstark@46
|
626
|
adamstark@58
|
627 latestCumulativeScoreValue = cumulativeScore[onsetDFBufferSize-1];
|
adamstark@58
|
628
|
adamstark@46
|
629 }
|
adamstark@46
|
630
|
adamstark@51
|
631 //=======================================================================
|
adamstark@57
|
632 void BTrack::predictBeat()
|
adamstark@46
|
633 {
|
adamstark@58
|
634 int windowSize = (int) beatPeriod;
|
adamstark@58
|
635 double futureCumulativeScore[onsetDFBufferSize + windowSize];
|
adamstark@58
|
636 double w2[windowSize];
|
adamstark@46
|
637 // copy cumscore to first part of fcumscore
|
adamstark@58
|
638 for (int i = 0;i < onsetDFBufferSize;i++)
|
adamstark@46
|
639 {
|
adamstark@58
|
640 futureCumulativeScore[i] = cumulativeScore[i];
|
adamstark@46
|
641 }
|
adamstark@46
|
642
|
adamstark@46
|
643 // create future window
|
adamstark@54
|
644 double v = 1;
|
adamstark@58
|
645 for (int i = 0;i < windowSize;i++)
|
adamstark@46
|
646 {
|
adamstark@57
|
647 w2[i] = exp((-1*pow((v - (beatPeriod/2)),2)) / (2*pow((beatPeriod/2) ,2)));
|
adamstark@46
|
648 v++;
|
adamstark@46
|
649 }
|
adamstark@46
|
650
|
adamstark@46
|
651 // create past window
|
adamstark@57
|
652 v = -2*beatPeriod;
|
adamstark@58
|
653 int start = onsetDFBufferSize - round(2*beatPeriod);
|
adamstark@58
|
654 int end = onsetDFBufferSize - round(beatPeriod/2);
|
adamstark@46
|
655 int pastwinsize = end-start+1;
|
adamstark@54
|
656 double w1[pastwinsize];
|
adamstark@46
|
657
|
adamstark@46
|
658 for (int i = 0;i < pastwinsize;i++)
|
adamstark@46
|
659 {
|
adamstark@57
|
660 w1[i] = exp((-1*pow(tightness*log(-v/beatPeriod),2))/2);
|
adamstark@46
|
661 v = v+1;
|
adamstark@46
|
662 }
|
adamstark@46
|
663
|
adamstark@46
|
664
|
adamstark@46
|
665
|
adamstark@46
|
666 // calculate future cumulative score
|
adamstark@54
|
667 double max;
|
adamstark@46
|
668 int n;
|
adamstark@54
|
669 double wcumscore;
|
adamstark@58
|
670 for (int i = onsetDFBufferSize;i < (onsetDFBufferSize+windowSize);i++)
|
adamstark@46
|
671 {
|
adamstark@57
|
672 start = i - round(2*beatPeriod);
|
adamstark@57
|
673 end = i - round(beatPeriod/2);
|
adamstark@46
|
674
|
adamstark@46
|
675 max = 0;
|
adamstark@46
|
676 n = 0;
|
adamstark@46
|
677 for (int k=start;k <= end;k++)
|
adamstark@46
|
678 {
|
adamstark@58
|
679 wcumscore = futureCumulativeScore[k]*w1[n];
|
adamstark@46
|
680
|
adamstark@46
|
681 if (wcumscore > max)
|
adamstark@46
|
682 {
|
adamstark@46
|
683 max = wcumscore;
|
adamstark@46
|
684 }
|
adamstark@46
|
685 n++;
|
adamstark@46
|
686 }
|
adamstark@46
|
687
|
adamstark@58
|
688 futureCumulativeScore[i] = max;
|
adamstark@46
|
689 }
|
adamstark@46
|
690
|
adamstark@46
|
691
|
adamstark@46
|
692 // predict beat
|
adamstark@46
|
693 max = 0;
|
adamstark@46
|
694 n = 0;
|
adamstark@46
|
695
|
adamstark@58
|
696 for (int i = onsetDFBufferSize;i < (onsetDFBufferSize+windowSize);i++)
|
adamstark@46
|
697 {
|
adamstark@58
|
698 wcumscore = futureCumulativeScore[i]*w2[n];
|
adamstark@46
|
699
|
adamstark@46
|
700 if (wcumscore > max)
|
adamstark@46
|
701 {
|
adamstark@46
|
702 max = wcumscore;
|
adamstark@58
|
703 beatCounter = n;
|
adamstark@46
|
704 }
|
adamstark@46
|
705
|
adamstark@46
|
706 n++;
|
adamstark@46
|
707 }
|
adamstark@46
|
708
|
adamstark@46
|
709 // set next prediction time
|
adamstark@58
|
710 m0 = beatCounter+round(beatPeriod/2);
|
adamstark@46
|
711
|
adamstark@46
|
712
|
adamstark@46
|
713 } |