Mercurial > hg > btrack
comparison src/OnsetDetectionFunction.cpp @ 52:45231107c9d6
Reformatted comments, removed the OnsetDetectionFunction constructor with no arguments, removed a number of unused variables and made changes to the python module to fix some casting problems and removed some unused variables there also. Still getting the same results, so no overall changes to the algorithm.
author | Adam Stark <adamstark@users.noreply.github.com> |
---|---|
date | Wed, 22 Jan 2014 01:13:45 +0000 |
parents | b7e3ed593fb0 |
children | 73c64ca0ed23 |
comparison
equal
deleted
inserted
replaced
51:68d01fea1e8d | 52:45231107c9d6 |
---|---|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 */ | 19 */ |
20 //======================================================================= | 20 //======================================================================= |
21 | 21 |
22 #include <math.h> | 22 #include <math.h> |
23 #include <iostream> | |
24 #include "OnsetDetectionFunction.h" | 23 #include "OnsetDetectionFunction.h" |
25 using namespace std; | 24 |
26 | 25 //======================================================================= |
27 //------------------------------------------------------------------------------- | |
28 // Constructor | |
29 OnsetDetectionFunction :: OnsetDetectionFunction() | |
30 { | |
31 // indicate that we have not initialised yet | |
32 initialised = 0; | |
33 | |
34 // set pi | |
35 pi = 3.14159265358979; | |
36 | |
37 // initialise with hopsize = 512, framesize = 1024, complex spectral difference DF and hanning window | |
38 initialise(512,1024,6,1); | |
39 } | |
40 | |
41 | |
42 //------------------------------------------------------------------------------- | |
43 // Constructor (with arguments) | |
44 OnsetDetectionFunction :: OnsetDetectionFunction(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type) | 26 OnsetDetectionFunction :: OnsetDetectionFunction(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type) |
45 { | 27 { |
46 // indicate that we have not initialised yet | 28 // indicate that we have not initialised yet |
47 initialised = 0; | 29 initialised = 0; |
48 | 30 |
52 // initialise with arguments to constructor | 34 // initialise with arguments to constructor |
53 initialise(arg_hsize,arg_fsize,arg_df_type,arg_win_type); | 35 initialise(arg_hsize,arg_fsize,arg_df_type,arg_win_type); |
54 } | 36 } |
55 | 37 |
56 | 38 |
57 //-------------------------------------------------------------------------------------- | 39 //======================================================================= |
58 // Destructor | |
59 OnsetDetectionFunction :: ~OnsetDetectionFunction() | 40 OnsetDetectionFunction :: ~OnsetDetectionFunction() |
60 { | 41 { |
61 // destroy fft plan | 42 // destroy fft plan |
62 fftw_destroy_plan(p); | 43 fftw_destroy_plan(p); |
63 fftw_free(in); | 44 fftw_free(in); |
80 phase_old = NULL; | 61 phase_old = NULL; |
81 delete [] phase_old_2; | 62 delete [] phase_old_2; |
82 phase_old_2 = NULL; | 63 phase_old_2 = NULL; |
83 } | 64 } |
84 | 65 |
85 //------------------------------------------------------------------------------- | 66 //======================================================================= |
86 // Initialisation | |
87 void OnsetDetectionFunction :: initialise(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type) | 67 void OnsetDetectionFunction :: initialise(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type) |
88 { | 68 { |
89 if (initialised == 1) // if we have already initialised some buffers and an FFT plan | 69 if (initialised == 1) // if we have already initialised some buffers and an FFT plan |
90 { | 70 { |
91 ////////////////////////////////// | 71 ////////////////////////////////// |
179 p = fftw_plan_dft_1d(framesize, in, out, FFTW_FORWARD, FFTW_ESTIMATE); // FFT plan initialisation | 159 p = fftw_plan_dft_1d(framesize, in, out, FFTW_FORWARD, FFTW_ESTIMATE); // FFT plan initialisation |
180 | 160 |
181 initialised = 1; | 161 initialised = 1; |
182 } | 162 } |
183 | 163 |
184 //-------------------------------------------------------------------------------------- | 164 //======================================================================= |
185 // set the detection function type to that specified by the argument | |
186 void OnsetDetectionFunction :: set_df_type(int arg_df_type) | 165 void OnsetDetectionFunction :: set_df_type(int arg_df_type) |
187 { | 166 { |
188 df_type = arg_df_type; // set detection function type | 167 df_type = arg_df_type; // set detection function type |
189 } | 168 } |
190 | 169 |
191 | 170 //======================================================================= |
192 //-------------------------------------------------------------------------------------- | |
193 // calculates a single detection function sample from a single audio frame. | |
194 double OnsetDetectionFunction :: getDFsample(double inputbuffer[]) | 171 double OnsetDetectionFunction :: getDFsample(double inputbuffer[]) |
195 { | 172 { |
196 double df_sample; | 173 double df_sample; |
197 | 174 |
198 // shift audio samples back in frame by hop size | 175 // shift audio samples back in frame by hop size |
246 | 223 |
247 return df_sample; | 224 return df_sample; |
248 } | 225 } |
249 | 226 |
250 | 227 |
251 //-------------------------------------------------------------------------------------- | 228 //======================================================================= |
252 // performs the fft, storing the complex result in 'out' | |
253 void OnsetDetectionFunction :: perform_FFT() | 229 void OnsetDetectionFunction :: perform_FFT() |
254 { | 230 { |
255 int fsize2 = (framesize/2); | 231 int fsize2 = (framesize/2); |
256 | 232 |
257 // window frame and copy to complex array, swapping the first and second half of the signal | 233 // window frame and copy to complex array, swapping the first and second half of the signal |
269 | 245 |
270 //////////////////////////////////////////////////////////////////////////////////////////////// | 246 //////////////////////////////////////////////////////////////////////////////////////////////// |
271 //////////////////////////////////////////////////////////////////////////////////////////////// | 247 //////////////////////////////////////////////////////////////////////////////////////////////// |
272 ////////////////////////////// Methods for Detection Functions ///////////////////////////////// | 248 ////////////////////////////// Methods for Detection Functions ///////////////////////////////// |
273 | 249 |
274 //-------------------------------------------------------------------------------------- | 250 //======================================================================= |
275 // calculates an energy envelope detection function sample | |
276 double OnsetDetectionFunction :: energy_envelope() | 251 double OnsetDetectionFunction :: energy_envelope() |
277 { | 252 { |
278 double sum; | 253 double sum; |
279 | 254 |
280 sum = 0; // initialise sum | 255 sum = 0; // initialise sum |
286 } | 261 } |
287 | 262 |
288 return sum; // return sum | 263 return sum; // return sum |
289 } | 264 } |
290 | 265 |
291 //-------------------------------------------------------------------------------------- | 266 //======================================================================= |
292 // calculates a half-wave rectified energy difference detection function sample | |
293 double OnsetDetectionFunction :: energy_difference() | 267 double OnsetDetectionFunction :: energy_difference() |
294 { | 268 { |
295 double sum; | 269 double sum; |
296 double sample; | 270 double sample; |
297 | 271 |
315 { | 289 { |
316 return 0; | 290 return 0; |
317 } | 291 } |
318 } | 292 } |
319 | 293 |
320 //-------------------------------------------------------------------------------------- | 294 //======================================================================= |
321 // calculates a spectral difference detection function sample | |
322 double OnsetDetectionFunction :: spectral_difference() | 295 double OnsetDetectionFunction :: spectral_difference() |
323 { | 296 { |
324 double diff; | 297 double diff; |
325 double sum; | 298 double sum; |
326 | 299 |
359 } | 332 } |
360 | 333 |
361 return sum; | 334 return sum; |
362 } | 335 } |
363 | 336 |
364 //-------------------------------------------------------------------------------------- | 337 //======================================================================= |
365 // calculates a spectral difference detection function sample | |
366 double OnsetDetectionFunction :: spectral_difference_hwr() | 338 double OnsetDetectionFunction :: spectral_difference_hwr() |
367 { | 339 { |
368 double diff; | 340 double diff; |
369 double sum; | 341 double sum; |
370 | 342 |
404 | 376 |
405 return sum; | 377 return sum; |
406 } | 378 } |
407 | 379 |
408 | 380 |
409 //-------------------------------------------------------------------------------------- | 381 //======================================================================= |
410 // calculates a phase deviation detection function sample | |
411 double OnsetDetectionFunction :: phase_deviation() | 382 double OnsetDetectionFunction :: phase_deviation() |
412 { | 383 { |
413 double dev,pdev; | 384 double dev,pdev; |
414 double sum; | 385 double sum; |
415 | 386 |
450 } | 421 } |
451 | 422 |
452 return sum; | 423 return sum; |
453 } | 424 } |
454 | 425 |
455 //-------------------------------------------------------------------------------------- | 426 //======================================================================= |
456 // calculates a complex spectral difference detection function sample | |
457 double OnsetDetectionFunction :: complex_spectral_difference() | 427 double OnsetDetectionFunction :: complex_spectral_difference() |
458 { | 428 { |
459 double dev,pdev; | 429 double dev,pdev; |
460 double sum; | 430 double sum; |
461 double mag_diff,phase_diff; | 431 double mag_diff,phase_diff; |
506 } | 476 } |
507 | 477 |
508 return sum; | 478 return sum; |
509 } | 479 } |
510 | 480 |
511 //-------------------------------------------------------------------------------------- | 481 //======================================================================= |
512 // calculates a complex spectral difference detection function sample (half-wave rectified) | |
513 double OnsetDetectionFunction :: complex_spectral_difference_hwr() | 482 double OnsetDetectionFunction :: complex_spectral_difference_hwr() |
514 { | 483 { |
515 double dev,pdev; | 484 double dev,pdev; |
516 double sum; | 485 double sum; |
517 double mag_diff,phase_diff; | 486 double mag_diff,phase_diff; |
563 | 532 |
564 return sum; | 533 return sum; |
565 } | 534 } |
566 | 535 |
567 | 536 |
568 //-------------------------------------------------------------------------------------- | 537 //======================================================================= |
569 // calculates a high frequency content detection function sample | |
570 double OnsetDetectionFunction :: high_frequency_content() | 538 double OnsetDetectionFunction :: high_frequency_content() |
571 { | 539 { |
572 double sum; | 540 double sum; |
573 double mag_diff; | |
574 | 541 |
575 // perform the FFT | 542 // perform the FFT |
576 perform_FFT(); | 543 perform_FFT(); |
577 | 544 |
578 sum = 0; // initialise sum to zero | 545 sum = 0; // initialise sum to zero |
591 } | 558 } |
592 | 559 |
593 return sum; | 560 return sum; |
594 } | 561 } |
595 | 562 |
596 //-------------------------------------------------------------------------------------- | 563 //======================================================================= |
597 // calculates a high frequency spectral difference detection function sample | |
598 double OnsetDetectionFunction :: high_frequency_spectral_difference() | 564 double OnsetDetectionFunction :: high_frequency_spectral_difference() |
599 { | 565 { |
600 double sum; | 566 double sum; |
601 double mag_diff; | 567 double mag_diff; |
602 | 568 |
626 } | 592 } |
627 | 593 |
628 return sum; | 594 return sum; |
629 } | 595 } |
630 | 596 |
631 //-------------------------------------------------------------------------------------- | 597 //======================================================================= |
632 // calculates a high frequency spectral difference detection function sample (half-wave rectified) | |
633 double OnsetDetectionFunction :: high_frequency_spectral_difference_hwr() | 598 double OnsetDetectionFunction :: high_frequency_spectral_difference_hwr() |
634 { | 599 { |
635 double sum; | 600 double sum; |
636 double mag_diff; | 601 double mag_diff; |
637 | 602 |
664 | 629 |
665 //////////////////////////////////////////////////////////////////////////////////////////////// | 630 //////////////////////////////////////////////////////////////////////////////////////////////// |
666 //////////////////////////////////////////////////////////////////////////////////////////////// | 631 //////////////////////////////////////////////////////////////////////////////////////////////// |
667 ////////////////////////////// Methods to Calculate Windows //////////////////////////////////// | 632 ////////////////////////////// Methods to Calculate Windows //////////////////////////////////// |
668 | 633 |
669 //-------------------------------------------------------------------------------------- | 634 //======================================================================= |
670 // HANNING: set the window in the buffer 'window' to a Hanning window | |
671 void OnsetDetectionFunction :: set_win_hanning() | 635 void OnsetDetectionFunction :: set_win_hanning() |
672 { | 636 { |
673 double N; // variable to store framesize minus 1 | 637 double N; // variable to store framesize minus 1 |
674 | 638 |
675 N = (double) (framesize-1); // framesize minus 1 | 639 N = (double) (framesize-1); // framesize minus 1 |
679 { | 643 { |
680 window[n] = 0.5*(1-cos(2*pi*(n/N))); | 644 window[n] = 0.5*(1-cos(2*pi*(n/N))); |
681 } | 645 } |
682 } | 646 } |
683 | 647 |
684 //-------------------------------------------------------------------------------------- | 648 //======================================================================= |
685 // HAMMING: set the window in the buffer 'window' to a Hanning window | |
686 void OnsetDetectionFunction :: set_win_hamming() | 649 void OnsetDetectionFunction :: set_win_hamming() |
687 { | 650 { |
688 double N; // variable to store framesize minus 1 | 651 double N; // variable to store framesize minus 1 |
689 double n_val; // double version of index 'n' | 652 double n_val; // double version of index 'n' |
690 | 653 |
697 window[n] = 0.54 - (0.46*cos(2*pi*(n_val/N))); | 660 window[n] = 0.54 - (0.46*cos(2*pi*(n_val/N))); |
698 n_val = n_val+1; | 661 n_val = n_val+1; |
699 } | 662 } |
700 } | 663 } |
701 | 664 |
702 //-------------------------------------------------------------------------------------- | 665 //======================================================================= |
703 // BLACKMAN: set the window in the buffer 'window' to a Blackman window | |
704 void OnsetDetectionFunction :: set_win_blackman() | 666 void OnsetDetectionFunction :: set_win_blackman() |
705 { | 667 { |
706 double N; // variable to store framesize minus 1 | 668 double N; // variable to store framesize minus 1 |
707 double n_val; // double version of index 'n' | 669 double n_val; // double version of index 'n' |
708 | 670 |
715 window[n] = 0.42 - (0.5*cos(2*pi*(n_val/N))) + (0.08*cos(4*pi*(n_val/N))); | 677 window[n] = 0.42 - (0.5*cos(2*pi*(n_val/N))) + (0.08*cos(4*pi*(n_val/N))); |
716 n_val = n_val+1; | 678 n_val = n_val+1; |
717 } | 679 } |
718 } | 680 } |
719 | 681 |
720 //-------------------------------------------------------------------------------------- | 682 //======================================================================= |
721 // TUKEY: set the window in the buffer 'window' to a Tukey window | |
722 void OnsetDetectionFunction :: set_win_tukey() | 683 void OnsetDetectionFunction :: set_win_tukey() |
723 { | 684 { |
724 double N; // variable to store framesize minus 1 | 685 double N; // variable to store framesize minus 1 |
725 double n_val; // double version of index 'n' | 686 double n_val; // double version of index 'n' |
726 double alpha; // alpha [default value = 0.5]; | 687 double alpha; // alpha [default value = 0.5]; |
727 | 688 |
728 int lim1,lim2; | |
729 | |
730 alpha = 0.5; | 689 alpha = 0.5; |
731 | 690 |
732 N = (double) (framesize-1); // framesize minus 1 | 691 N = (double) (framesize-1); // framesize minus 1 |
733 | 692 |
734 // Tukey window calculation | 693 // Tukey window calculation |
753 n_val = n_val+1; | 712 n_val = n_val+1; |
754 } | 713 } |
755 | 714 |
756 } | 715 } |
757 | 716 |
758 //-------------------------------------------------------------------------------------- | 717 //======================================================================= |
759 // RECTANGULAR: set the window in the buffer 'window' to a Rectangular window | |
760 void OnsetDetectionFunction :: set_win_rectangular() | 718 void OnsetDetectionFunction :: set_win_rectangular() |
761 { | 719 { |
762 // Rectangular window calculation | 720 // Rectangular window calculation |
763 for (int n = 0;n < framesize;n++) | 721 for (int n = 0;n < framesize;n++) |
764 { | 722 { |
770 | 728 |
771 //////////////////////////////////////////////////////////////////////////////////////////////// | 729 //////////////////////////////////////////////////////////////////////////////////////////////// |
772 //////////////////////////////////////////////////////////////////////////////////////////////// | 730 //////////////////////////////////////////////////////////////////////////////////////////////// |
773 ///////////////////////////////// Other Handy Methods ////////////////////////////////////////// | 731 ///////////////////////////////// Other Handy Methods ////////////////////////////////////////// |
774 | 732 |
775 //-------------------------------------------------------------------------------------- | 733 //======================================================================= |
776 // set phase values to the range [-pi,pi] | |
777 double OnsetDetectionFunction :: princarg(double phaseval) | 734 double OnsetDetectionFunction :: princarg(double phaseval) |
778 { | 735 { |
779 // if phase value is less than or equal to -pi then add 2*pi | 736 // if phase value is less than or equal to -pi then add 2*pi |
780 while (phaseval <= (-pi)) | 737 while (phaseval <= (-pi)) |
781 { | 738 { |