Mercurial > hg > btrack
comparison src/BTrack.cpp @ 51:68d01fea1e8d
Added a unit testing project, and did some commenting. Also moved python-module into a modules-and-plug-ins folder
author | Adam Stark <adamstark@users.noreply.github.com> |
---|---|
date | Tue, 21 Jan 2014 10:24:33 +0000 |
parents | bb3803edaa17 |
children | 45231107c9d6 |
comparison
equal
deleted
inserted
replaced
50:bb3803edaa17 | 51:68d01fea1e8d |
---|---|
26 using namespace std; | 26 using namespace std; |
27 | 27 |
28 | 28 |
29 | 29 |
30 | 30 |
31 //------------------------------------------------------------------------------- | 31 //======================================================================= |
32 // Constructor | |
33 BTrack :: BTrack() | 32 BTrack :: BTrack() |
34 { | 33 { |
35 float rayparam = 43; | 34 float rayparam = 43; |
36 float pi = 3.14159265; | 35 float pi = 3.14159265; |
37 | 36 |
80 | 79 |
81 // tempo is not fixed | 80 // tempo is not fixed |
82 tempofix = 0; | 81 tempofix = 0; |
83 } | 82 } |
84 | 83 |
85 //------------------------------------------------------------------------------- | 84 //======================================================================= |
86 // Destructor | |
87 BTrack :: ~BTrack() | 85 BTrack :: ~BTrack() |
88 { | 86 { |
89 | 87 |
90 } | 88 } |
91 | 89 |
92 //------------------------------------------------------------------------------- | 90 |
93 // Initialise with frame size and set all frame sizes accordingly | 91 |
92 //======================================================================= | |
94 void BTrack :: initialise(int fsize) | 93 void BTrack :: initialise(int fsize) |
95 { | 94 { |
96 framesize = fsize; | 95 framesize = fsize; |
97 dfbuffer_size = (512*512)/fsize; // calculate df buffer size | 96 dfbuffer_size = (512*512)/fsize; // calculate df buffer size |
98 | 97 |
114 dfbuffer[i] = 1; | 113 dfbuffer[i] = 1; |
115 } | 114 } |
116 } | 115 } |
117 } | 116 } |
118 | 117 |
119 //------------------------------------------------------------------------------- | 118 //======================================================================= |
120 // Add new sample to buffer and apply beat tracking | |
121 void BTrack :: process(float df_sample) | 119 void BTrack :: process(float df_sample) |
122 { | 120 { |
123 m0--; | 121 m0--; |
124 beat--; | 122 beat--; |
125 playbeat = 0; | 123 playbeat = 0; |
151 dfconvert(); | 149 dfconvert(); |
152 calcTempo(); | 150 calcTempo(); |
153 } | 151 } |
154 } | 152 } |
155 | 153 |
156 //------------------------------------------------------------------------------- | 154 //======================================================================= |
157 // Set the tempo of the beat tracker | |
158 void BTrack :: settempo(float tempo) | 155 void BTrack :: settempo(float tempo) |
159 { | 156 { |
160 | 157 |
161 /////////// TEMPO INDICATION RESET ////////////////// | 158 /////////// TEMPO INDICATION RESET ////////////////// |
162 | 159 |
219 | 216 |
220 // offbeat is half of new beat period away | 217 // offbeat is half of new beat period away |
221 m0 = (int) round(((float) new_bperiod)/2); | 218 m0 = (int) round(((float) new_bperiod)/2); |
222 } | 219 } |
223 | 220 |
224 | 221 //======================================================================= |
225 //------------------------------------------------------------------------------- | |
226 // fix tempo to roughly around some value | |
227 void BTrack :: fixtempo(float tempo) | 222 void BTrack :: fixtempo(float tempo) |
228 { | 223 { |
229 // firstly make sure tempo is between 80 and 160 bpm.. | 224 // firstly make sure tempo is between 80 and 160 bpm.. |
230 while (tempo > 160) | 225 while (tempo > 160) |
231 { | 226 { |
251 | 246 |
252 // set the tempo fix flag | 247 // set the tempo fix flag |
253 tempofix = 1; | 248 tempofix = 1; |
254 } | 249 } |
255 | 250 |
256 //------------------------------------------------------------------------------- | 251 //======================================================================= |
257 // do not fix the tempo anymore | |
258 void BTrack :: unfixtempo() | 252 void BTrack :: unfixtempo() |
259 { | 253 { |
260 // set the tempo fix flag | 254 // set the tempo fix flag |
261 tempofix = 0; | 255 tempofix = 0; |
262 } | 256 } |
263 | 257 |
264 //------------------------------------------------------------------------------- | 258 //======================================================================= |
265 // Convert detection function from N samples to 512 | |
266 void BTrack :: dfconvert() | 259 void BTrack :: dfconvert() |
267 { | 260 { |
268 float output[512]; | 261 float output[512]; |
269 | 262 |
270 double src_ratio = 512.0/((double) dfbuffer_size); | 263 double src_ratio = 512.0/((double) dfbuffer_size); |
289 { | 282 { |
290 df512[i] = src_data.data_out[i]; | 283 df512[i] = src_data.data_out[i]; |
291 } | 284 } |
292 } | 285 } |
293 | 286 |
294 //------------------------------------------------------------------------------- | 287 //======================================================================= |
295 // To calculate the current tempo expressed as the beat period in detection function samples | |
296 void BTrack :: calcTempo() | 288 void BTrack :: calcTempo() |
297 { | 289 { |
298 // adaptive threshold on input | 290 // adaptive threshold on input |
299 adapt_thresh(df512,512); | 291 adapt_thresh(df512,512); |
300 | 292 |
376 } | 368 } |
377 | 369 |
378 //cout << bperiod << endl; | 370 //cout << bperiod << endl; |
379 } | 371 } |
380 | 372 |
381 //------------------------------------------------------------------------------- | 373 //======================================================================= |
382 // calculates an adaptive threshold which is used to remove low level energy from detection function and emphasise peaks | |
383 void BTrack :: adapt_thresh(float x[],int N) | 374 void BTrack :: adapt_thresh(float x[],int N) |
384 { | 375 { |
385 //int N = 512; // length of df | 376 //int N = 512; // length of df |
386 int i = 0; | 377 int i = 0; |
387 int k,t = 0; | 378 int k,t = 0; |
419 x[i] = 0; | 410 x[i] = 0; |
420 } | 411 } |
421 } | 412 } |
422 } | 413 } |
423 | 414 |
424 //------------------------------------------------------------------------------- | 415 //======================================================================= |
425 // returns the output of the comb filter | |
426 void BTrack :: getrcfoutput() | 416 void BTrack :: getrcfoutput() |
427 { | 417 { |
428 int numelem; | 418 int numelem; |
429 | 419 |
430 for (int i = 0;i < 128;i++) | 420 for (int i = 0;i < 128;i++) |
444 } | 434 } |
445 } | 435 } |
446 } | 436 } |
447 } | 437 } |
448 | 438 |
449 //------------------------------------------------------------------------------- | 439 //======================================================================= |
450 // calculates the balanced autocorrelation of the smoothed detection function | |
451 void BTrack :: acf_bal(float df_thresh[]) | 440 void BTrack :: acf_bal(float df_thresh[]) |
452 { | 441 { |
453 int l, n = 0; | 442 int l, n = 0; |
454 float sum, tmp; | 443 float sum, tmp; |
455 | 444 |
467 | 456 |
468 acf[l] = sum / (512-l); // weight by number of mults and add to acf buffer | 457 acf[l] = sum / (512-l); // weight by number of mults and add to acf buffer |
469 } | 458 } |
470 } | 459 } |
471 | 460 |
472 | 461 //======================================================================= |
473 //------------------------------------------------------------------------------- | |
474 // calculates the mean of values in an array from index locations [start,end] | |
475 float BTrack :: mean_array(float array[],int start,int end) | 462 float BTrack :: mean_array(float array[],int start,int end) |
476 { | 463 { |
477 int i; | 464 int i; |
478 double sum = 0; | 465 double sum = 0; |
479 | 466 |
493 { | 480 { |
494 return 0; | 481 return 0; |
495 } | 482 } |
496 } | 483 } |
497 | 484 |
498 //------------------------------------------------------------------------------- | 485 //======================================================================= |
499 // normalise the array | |
500 void BTrack :: normalise(float array[],int N) | 486 void BTrack :: normalise(float array[],int N) |
501 { | 487 { |
502 double sum = 0; | 488 double sum = 0; |
503 | 489 |
504 for (int i = 0;i < N;i++) | 490 for (int i = 0;i < N;i++) |
516 array[i] = array[i] / sum; | 502 array[i] = array[i] / sum; |
517 } | 503 } |
518 } | 504 } |
519 } | 505 } |
520 | 506 |
521 //------------------------------------------------------------------------------- | 507 //======================================================================= |
522 // plot contents of detection function buffer | |
523 void BTrack :: plotdfbuffer() | |
524 { | |
525 for (int i=0;i < dfbuffer_size;i++) | |
526 { | |
527 cout << dfbuffer[i] << endl; | |
528 } | |
529 | |
530 cout << "--------------------------------" << endl; | |
531 } | |
532 | |
533 //------------------------------------------------------------------------------- | |
534 // update the cumulative score | |
535 void BTrack :: updatecumscore(float df_sample) | 508 void BTrack :: updatecumscore(float df_sample) |
536 { | 509 { |
537 int start, end, winsize; | 510 int start, end, winsize; |
538 float max; | 511 float max; |
539 | 512 |
581 | 554 |
582 //cout << cumscore[dfbuffer_size-1] << endl; | 555 //cout << cumscore[dfbuffer_size-1] << endl; |
583 | 556 |
584 } | 557 } |
585 | 558 |
586 //------------------------------------------------------------------------------- | 559 //======================================================================= |
587 // plot contents of detection function buffer | |
588 void BTrack :: predictbeat() | 560 void BTrack :: predictbeat() |
589 { | 561 { |
590 int winsize = (int) bperiod; | 562 int winsize = (int) bperiod; |
591 float fcumscore[dfbuffer_size + winsize]; | 563 float fcumscore[dfbuffer_size + winsize]; |
592 float w2[winsize]; | 564 float w2[winsize]; |
662 n++; | 634 n++; |
663 } | 635 } |
664 | 636 |
665 | 637 |
666 // set beat | 638 // set beat |
667 beat = beat; | 639 //beat = beat; |
668 | 640 |
669 // set next prediction time | 641 // set next prediction time |
670 m0 = beat+round(bperiod/2); | 642 m0 = beat+round(bperiod/2); |
671 | 643 |
672 | 644 |