Mercurial > hg > btrack
comparison src/BTrack.cpp @ 89:5ef334c782f3
Moved some parts of the algorithm from arrays needing lots of data moved around to lighted circular buffers
author | Adam Stark <adamstark.uk@gmail.com> |
---|---|
date | Fri, 04 Mar 2016 18:10:23 +0000 |
parents | 995ddf0eadd4 |
children | b6fc77f471bb |
comparison
equal
deleted
inserted
replaced
88:995ddf0eadd4 | 89:5ef334c782f3 |
---|---|
21 | 21 |
22 #include <cmath> | 22 #include <cmath> |
23 #include <algorithm> | 23 #include <algorithm> |
24 #include "BTrack.h" | 24 #include "BTrack.h" |
25 #include "samplerate.h" | 25 #include "samplerate.h" |
26 #include <iostream> | |
26 | 27 |
27 //======================================================================= | 28 //======================================================================= |
28 BTrack::BTrack() : odf(512,1024,ComplexSpectralDifferenceHWR,HanningWindow) | 29 BTrack::BTrack() : odf(512,1024,ComplexSpectralDifferenceHWR,HanningWindow) |
29 { | 30 { |
30 initialise(512, 1024); | 31 initialise(512, 1024); |
227 | 228 |
228 m0--; | 229 m0--; |
229 beatCounter--; | 230 beatCounter--; |
230 beatDueInFrame = false; | 231 beatDueInFrame = false; |
231 | 232 |
232 // move all samples back one step | 233 // // move all samples back one step |
233 for (int i=0;i < (onsetDFBufferSize-1);i++) | 234 // for (int i=0;i < (onsetDFBufferSize-1);i++) |
234 { | 235 // { |
235 onsetDF[i] = onsetDF[i+1]; | 236 // onsetDF[i] = onsetDF[i+1]; |
236 } | 237 // } |
237 | 238 |
238 // add new sample at the end | 239 // add new sample at the end |
239 onsetDF[onsetDFBufferSize-1] = newSample; | 240 //onsetDF[onsetDFBufferSize-1] = newSample; |
241 onsetDF.addSampleToEnd(newSample); | |
240 | 242 |
241 // update cumulative score | 243 // update cumulative score |
242 updateCumulativeScore(newSample); | 244 updateCumulativeScore(newSample); |
243 | 245 |
244 // if we are halfway between beats | 246 // if we are halfway between beats |
364 | 366 |
365 //======================================================================= | 367 //======================================================================= |
366 void BTrack::resampleOnsetDetectionFunction() | 368 void BTrack::resampleOnsetDetectionFunction() |
367 { | 369 { |
368 float output[512]; | 370 float output[512]; |
371 | |
372 | |
369 float input[onsetDFBufferSize]; | 373 float input[onsetDFBufferSize]; |
370 | 374 |
371 for (int i = 0;i < onsetDFBufferSize;i++) | 375 for (int i = 0;i < onsetDFBufferSize;i++) |
372 { | 376 { |
373 input[i] = (float) onsetDF[i]; | 377 input[i] = (float) onsetDF[i]; |
374 } | 378 } |
375 | 379 |
376 double src_ratio = 512.0/((double) onsetDFBufferSize); | 380 double src_ratio = 512.0/((double) onsetDFBufferSize); |
377 int BUFFER_LEN = onsetDFBufferSize; | 381 int BUFFER_LEN = onsetDFBufferSize; |
378 int output_len; | 382 int output_len; |
379 SRC_DATA src_data ; | 383 SRC_DATA src_data ; |
380 | 384 |
381 //output_len = (int) floor (((double) BUFFER_LEN) * src_ratio) ; | 385 //output_len = (int) floor (((double) BUFFER_LEN) * src_ratio) ; |
382 output_len = 512; | 386 output_len = 512; |
383 | 387 |
384 src_data.data_in = input; | 388 src_data.data_in = input; |
385 src_data.input_frames = BUFFER_LEN; | 389 src_data.input_frames = BUFFER_LEN; |
386 | 390 |
387 src_data.src_ratio = src_ratio; | 391 src_data.src_ratio = src_ratio; |
388 | 392 |
389 src_data.data_out = output; | 393 src_data.data_out = output; |
390 src_data.output_frames = output_len; | 394 src_data.output_frames = output_len; |
391 | 395 |
392 src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1); | 396 src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1); |
393 | 397 |
394 for (int i = 0;i < output_len;i++) | 398 for (int i = 0;i < output_len;i++) |
395 { | 399 { |
396 resampledOnsetDF[i] = (double) src_data.data_out[i]; | 400 resampledOnsetDF[i] = (double) src_data.data_out[i]; |
397 } | 401 } |
398 } | 402 } |
399 | 403 |
400 //======================================================================= | 404 //======================================================================= |
401 void BTrack::calculateTempo() | 405 void BTrack::calculateTempo() |
402 { | 406 { |
680 } | 684 } |
681 n++; | 685 n++; |
682 } | 686 } |
683 | 687 |
684 | 688 |
685 // shift cumulative score back one | 689 // // shift cumulative score back one |
686 for (int i = 0;i < (onsetDFBufferSize-1);i++) | 690 // for (int i = 0;i < (onsetDFBufferSize-1);i++) |
687 { | 691 // { |
688 cumulativeScore[i] = cumulativeScore[i+1]; | 692 // cumulativeScore[i] = cumulativeScore[i+1]; |
689 } | 693 // } |
690 | 694 |
695 latestCumulativeScoreValue = ((1-alpha)*odfSample) + (alpha*max); | |
696 | |
697 cumulativeScore.addSampleToEnd(latestCumulativeScoreValue); | |
698 | |
691 // add new value to cumulative score | 699 // add new value to cumulative score |
692 cumulativeScore[onsetDFBufferSize-1] = ((1-alpha)*odfSample) + (alpha*max); | 700 //cumulativeScore[onsetDFBufferSize-1] = ((1-alpha)*odfSample) + (alpha*max); |
693 | 701 |
694 latestCumulativeScoreValue = cumulativeScore[onsetDFBufferSize-1]; | 702 //latestCumulativeScoreValue = cumulativeScore[onsetDFBufferSize-1]; |
695 | 703 |
696 } | 704 } |
697 | 705 |
698 //======================================================================= | 706 //======================================================================= |
699 void BTrack::predictBeat() | 707 void BTrack::predictBeat() |