# HG changeset patch # User Adam # Date 1390353225 0 # Node ID 2b94d3d2fb9d5b2ce12b915d00fbf4fbbfaafb0e # Parent 18fc3c2484368c6aa927c3f44198dfbb2826222e 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. diff -r 18fc3c248436 -r 2b94d3d2fb9d modules-and-plug-ins/python-module/btrack_python_module.cpp --- a/modules-and-plug-ins/python-module/btrack_python_module.cpp Tue Jan 21 10:24:33 2014 +0000 +++ b/modules-and-plug-ins/python-module/btrack_python_module.cpp Wed Jan 22 01:13:45 2014 +0000 @@ -28,11 +28,11 @@ double* data = (double*) PyArray_DATA(arr1); // get array size - int signal_length = PyArray_Size((PyObject*)arr1); + long signal_length = PyArray_Size((PyObject*)arr1); //int k = (int) theSize; // get data type - char type = PyArray_DESCR(arr1)->type; + //char type = PyArray_DESCR(arr1)->type; ////////// BEGIN PROCESS /////////////////// int hsize = 512; @@ -128,11 +128,11 @@ double* data = (double*) PyArray_DATA(arr1); // get array size - int signal_length = PyArray_Size((PyObject*)arr1); + long signal_length = PyArray_Size((PyObject*)arr1); //int k = (int) theSize; // get data type - char type = PyArray_DESCR(arr1)->type; + //char type = PyArray_DESCR(arr1)->type; ////////// BEGIN PROCESS /////////////////// int hsize = 512; @@ -252,11 +252,11 @@ double* data = (double*) PyArray_DATA(arr1); // get array size - int numframes = PyArray_Size((PyObject*)arr1); + long numframes = PyArray_Size((PyObject*)arr1); //int k = (int) theSize; // get data type - char type = PyArray_DESCR(arr1)->type; + //char type = PyArray_DESCR(arr1)->type; ////////// BEGIN PROCESS /////////////////// int hsize = 512; @@ -275,7 +275,7 @@ /////////////////////////////////////////// //////// Begin Processing Loop //////////// - for (int i=0;i < numframes;i++) + for (long i=0;i < numframes;i++) { df_val = (float) (data[i] + 0.0001); diff -r 18fc3c248436 -r 2b94d3d2fb9d src/BTrack.cpp --- a/src/BTrack.cpp Tue Jan 21 10:24:33 2014 +0000 +++ b/src/BTrack.cpp Wed Jan 22 01:13:45 2014 +0000 @@ -19,13 +19,10 @@ */ //======================================================================= -#include #include +#include #include "BTrack.h" #include "samplerate.h" -using namespace std; - - //======================================================================= @@ -381,12 +378,12 @@ int p_post = 7; int p_pre = 8; - t = min(N,p_post); // what is smaller, p_post of df size. This is to avoid accessing outside of arrays + t = std::min(N,p_post); // what is smaller, p_post of df size. This is to avoid accessing outside of arrays // find threshold for first 't' samples, where a full average cannot be computed yet for (i = 0;i <= t;i++) { - k = min((i+p_pre),N); + k = std::min((i+p_pre),N); x_thresh[i] = mean_array(x,1,k); } // find threshold for bulk of samples across a moving average from [i-p_pre,i+p_post] @@ -397,7 +394,7 @@ // for last few samples calculate threshold, again, not enough samples to do as above for (i = N-p_post;i < N;i++) { - k = max((i-p_post),1); + k = std::max((i-p_post),1); x_thresh[i] = mean_array(x,k,N); } diff -r 18fc3c248436 -r 2b94d3d2fb9d src/OnsetDetectionFunction.cpp --- a/src/OnsetDetectionFunction.cpp Tue Jan 21 10:24:33 2014 +0000 +++ b/src/OnsetDetectionFunction.cpp Wed Jan 22 01:13:45 2014 +0000 @@ -20,27 +20,9 @@ //======================================================================= #include -#include #include "OnsetDetectionFunction.h" -using namespace std; -//------------------------------------------------------------------------------- -// Constructor -OnsetDetectionFunction :: OnsetDetectionFunction() -{ - // indicate that we have not initialised yet - initialised = 0; - - // set pi - pi = 3.14159265358979; - - // initialise with hopsize = 512, framesize = 1024, complex spectral difference DF and hanning window - initialise(512,1024,6,1); -} - - -//------------------------------------------------------------------------------- -// Constructor (with arguments) +//======================================================================= OnsetDetectionFunction :: OnsetDetectionFunction(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type) { // indicate that we have not initialised yet @@ -54,8 +36,7 @@ } -//-------------------------------------------------------------------------------------- -// Destructor +//======================================================================= OnsetDetectionFunction :: ~OnsetDetectionFunction() { // destroy fft plan @@ -82,8 +63,7 @@ phase_old_2 = NULL; } -//------------------------------------------------------------------------------- -// Initialisation +//======================================================================= void OnsetDetectionFunction :: initialise(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type) { if (initialised == 1) // if we have already initialised some buffers and an FFT plan @@ -181,16 +161,13 @@ initialised = 1; } -//-------------------------------------------------------------------------------------- -// set the detection function type to that specified by the argument +//======================================================================= void OnsetDetectionFunction :: set_df_type(int arg_df_type) { df_type = arg_df_type; // set detection function type } - -//-------------------------------------------------------------------------------------- -// calculates a single detection function sample from a single audio frame. +//======================================================================= double OnsetDetectionFunction :: getDFsample(double inputbuffer[]) { double df_sample; @@ -248,8 +225,7 @@ } -//-------------------------------------------------------------------------------------- -// performs the fft, storing the complex result in 'out' +//======================================================================= void OnsetDetectionFunction :: perform_FFT() { int fsize2 = (framesize/2); @@ -271,8 +247,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////// Methods for Detection Functions ///////////////////////////////// -//-------------------------------------------------------------------------------------- -// calculates an energy envelope detection function sample +//======================================================================= double OnsetDetectionFunction :: energy_envelope() { double sum; @@ -288,8 +263,7 @@ return sum; // return sum } -//-------------------------------------------------------------------------------------- -// calculates a half-wave rectified energy difference detection function sample +//======================================================================= double OnsetDetectionFunction :: energy_difference() { double sum; @@ -317,8 +291,7 @@ } } -//-------------------------------------------------------------------------------------- -// calculates a spectral difference detection function sample +//======================================================================= double OnsetDetectionFunction :: spectral_difference() { double diff; @@ -361,8 +334,7 @@ return sum; } -//-------------------------------------------------------------------------------------- -// calculates a spectral difference detection function sample +//======================================================================= double OnsetDetectionFunction :: spectral_difference_hwr() { double diff; @@ -406,8 +378,7 @@ } -//-------------------------------------------------------------------------------------- -// calculates a phase deviation detection function sample +//======================================================================= double OnsetDetectionFunction :: phase_deviation() { double dev,pdev; @@ -452,8 +423,7 @@ return sum; } -//-------------------------------------------------------------------------------------- -// calculates a complex spectral difference detection function sample +//======================================================================= double OnsetDetectionFunction :: complex_spectral_difference() { double dev,pdev; @@ -508,8 +478,7 @@ return sum; } -//-------------------------------------------------------------------------------------- -// calculates a complex spectral difference detection function sample (half-wave rectified) +//======================================================================= double OnsetDetectionFunction :: complex_spectral_difference_hwr() { double dev,pdev; @@ -565,12 +534,10 @@ } -//-------------------------------------------------------------------------------------- -// calculates a high frequency content detection function sample +//======================================================================= double OnsetDetectionFunction :: high_frequency_content() { double sum; - double mag_diff; // perform the FFT perform_FFT(); @@ -593,8 +560,7 @@ return sum; } -//-------------------------------------------------------------------------------------- -// calculates a high frequency spectral difference detection function sample +//======================================================================= double OnsetDetectionFunction :: high_frequency_spectral_difference() { double sum; @@ -628,8 +594,7 @@ return sum; } -//-------------------------------------------------------------------------------------- -// calculates a high frequency spectral difference detection function sample (half-wave rectified) +//======================================================================= double OnsetDetectionFunction :: high_frequency_spectral_difference_hwr() { double sum; @@ -666,8 +631,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////// Methods to Calculate Windows //////////////////////////////////// -//-------------------------------------------------------------------------------------- -// HANNING: set the window in the buffer 'window' to a Hanning window +//======================================================================= void OnsetDetectionFunction :: set_win_hanning() { double N; // variable to store framesize minus 1 @@ -681,8 +645,7 @@ } } -//-------------------------------------------------------------------------------------- -// HAMMING: set the window in the buffer 'window' to a Hanning window +//======================================================================= void OnsetDetectionFunction :: set_win_hamming() { double N; // variable to store framesize minus 1 @@ -699,8 +662,7 @@ } } -//-------------------------------------------------------------------------------------- -// BLACKMAN: set the window in the buffer 'window' to a Blackman window +//======================================================================= void OnsetDetectionFunction :: set_win_blackman() { double N; // variable to store framesize minus 1 @@ -717,16 +679,13 @@ } } -//-------------------------------------------------------------------------------------- -// TUKEY: set the window in the buffer 'window' to a Tukey window +//======================================================================= void OnsetDetectionFunction :: set_win_tukey() { double N; // variable to store framesize minus 1 double n_val; // double version of index 'n' double alpha; // alpha [default value = 0.5]; - int lim1,lim2; - alpha = 0.5; N = (double) (framesize-1); // framesize minus 1 @@ -755,8 +714,7 @@ } -//-------------------------------------------------------------------------------------- -// RECTANGULAR: set the window in the buffer 'window' to a Rectangular window +//======================================================================= void OnsetDetectionFunction :: set_win_rectangular() { // Rectangular window calculation @@ -772,8 +730,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////// Other Handy Methods ////////////////////////////////////////// -//-------------------------------------------------------------------------------------- -// set phase values to the range [-pi,pi] +//======================================================================= double OnsetDetectionFunction :: princarg(double phaseval) { // if phase value is less than or equal to -pi then add 2*pi diff -r 18fc3c248436 -r 2b94d3d2fb9d src/OnsetDetectionFunction.h --- a/src/OnsetDetectionFunction.h Tue Jan 21 10:24:33 2014 +0000 +++ b/src/OnsetDetectionFunction.h Wed Jan 22 01:13:45 2014 +0000 @@ -27,64 +27,100 @@ class OnsetDetectionFunction { public: - OnsetDetectionFunction(); // Constructor - OnsetDetectionFunction(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type); // Constructor (with arguments) - ~OnsetDetectionFunction(); // Destructor - void initialise(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type); // Initialisation Function + + /** Constructor */ + OnsetDetectionFunction(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type); + + /** Destructor */ + ~OnsetDetectionFunction(); + + /** Initialisation Function */ + void initialise(int arg_hsize,int arg_fsize,int arg_df_type,int arg_win_type); - double getDFsample(double inputbuffer[]); // process input buffer and calculate detection function sample - void set_df_type(int arg_df_type); // set the detection function type + /** process input buffer and calculate detection function sample */ + double getDFsample(double inputbuffer[]); + + /** set the detection function type */ + void set_df_type(int arg_df_type); private: - void perform_FFT(); // perform the FFT on the data in 'frame' + /** perform the FFT on the data in 'frame' */ + void perform_FFT(); - double energy_envelope(); // calculate energy envelope detection function sample - double energy_difference(); // calculate energy difference detection function sample - double spectral_difference(); // calculate spectral difference detection function sample - double spectral_difference_hwr(); // calculate spectral difference (half wave rectified) detection function sample - double phase_deviation(); // calculate phase deviation detection function sample - double complex_spectral_difference(); // calculate complex spectral difference detection function sample - double complex_spectral_difference_hwr(); // calculate complex spectral difference detection function sample (half-wave rectified) - double high_frequency_content(); // calculate high frequency content detection function sample - double high_frequency_spectral_difference(); // calculate high frequency spectral difference detection function sample - double high_frequency_spectral_difference_hwr(); // calculate high frequency spectral difference detection function sample (half-wave rectified) + /** calculate energy envelope detection function sample */ + double energy_envelope(); + + /** calculate energy difference detection function sample */ + double energy_difference(); + + /** calculate spectral difference detection function sample */ + double spectral_difference(); + + /** calculate spectral difference (half wave rectified) detection function sample */ + double spectral_difference_hwr(); + + /** calculate phase deviation detection function sample */ + double phase_deviation(); + + /** calculate complex spectral difference detection function sample */ + double complex_spectral_difference(); + + /** calculate complex spectral difference detection function sample (half-wave rectified) */ + double complex_spectral_difference_hwr(); + + /** calculate high frequency content detection function sample */ + double high_frequency_content(); + + /** calculate high frequency spectral difference detection function sample */ + double high_frequency_spectral_difference(); + + /** calculate high frequency spectral difference detection function sample (half-wave rectified) */ + double high_frequency_spectral_difference_hwr(); - void set_win_rectangular(); // calculate a Rectangular window - void set_win_hanning(); // calculate a Hanning window - void set_win_hamming(); // calculate a Hamming window - void set_win_blackman(); // calculate a Blackman window - void set_win_tukey(); // calculate a Tukey window + /** calculate a Rectangular window */ + void set_win_rectangular(); + + /** calculate a Hanning window */ + void set_win_hanning(); + + /** calculate a Hamming window */ + void set_win_hamming(); + + /** calculate a Blackman window */ + void set_win_blackman(); + + /** calculate a Tukey window */ + void set_win_tukey(); + /** set phase values between [-pi, pi] */ + double princarg(double phaseval); - double princarg(double phaseval); // set phase values between [-pi, pi] + double pi; /**< pi, the constant */ - double pi; // pi, the constant + int framesize; /**< audio framesize */ + int hopsize; /**< audio hopsize */ + int df_type; /**< type of detection function */ - int framesize; // audio framesize - int hopsize; // audio hopsize - int df_type; // type of detection function + fftw_plan p; /**< create fft plan */ + fftw_complex *in; /**< to hold complex fft values for input */ + fftw_complex *out; /**< to hold complex fft values for output */ - fftw_plan p; // create fft plan - fftw_complex *in; // to hold complex fft values for input - fftw_complex *out; // to hold complex fft values for output + int initialised; /**< flag indicating whether buffers and FFT plans are initialised */ + + double *frame; /**< audio frame */ + double *window; /**< window */ + double *wframe; /**< windowed frame */ - int initialised; // flag indicating whether buffers and FFT plans have been initialised + double energy_sum_old; /**< to hold the previous energy sum value */ - - double *frame; // audio frame - double *window; // window - double *wframe; // windowed frame + double *mag; /**< magnitude spectrum */ + double *mag_old; /**< previous magnitude spectrum */ - double energy_sum_old; // to hold the previous energy sum value - - double *mag; // magnitude spectrum - double *mag_old; // previous magnitude spectrum - - double *phase; // FFT phase values - double *phase_old; // previous phase values - double *phase_old_2; // second order previous phase values + double *phase; /**< FFT phase values */ + double *phase_old; /**< previous phase values */ + double *phase_old_2; /**< second order previous phase values */ };