Mercurial > hg > btrack
changeset 91:a88d887bd281
Code style update to BTrack class
author | Adam Stark <adamstark.uk@gmail.com> |
---|---|
date | Wed, 11 May 2016 00:06:52 +0100 |
parents | b6fc77f471bb |
children | f6708e4c69f1 |
files | src/BTrack.cpp src/BTrack.h |
diffstat | 2 files changed, 95 insertions(+), 99 deletions(-) [+] |
line wrap: on
line diff
--- a/src/BTrack.cpp Tue May 10 23:54:33 2016 +0100 +++ b/src/BTrack.cpp Wed May 11 00:06:52 2016 +0100 @@ -26,35 +26,38 @@ #include <iostream> //======================================================================= -BTrack::BTrack() : odf(512,1024,ComplexSpectralDifferenceHWR,HanningWindow) +BTrack::BTrack() + : odf (512, 1024, ComplexSpectralDifferenceHWR, HanningWindow) { initialise(512, 1024); } //======================================================================= -BTrack::BTrack(int hopSize_) : odf(hopSize_,2*hopSize_,ComplexSpectralDifferenceHWR,HanningWindow) +BTrack::BTrack (int hopSize_) + : odf(hopSize_, 2*hopSize_, ComplexSpectralDifferenceHWR, HanningWindow) { initialise(hopSize_, 2*hopSize_); } //======================================================================= -BTrack::BTrack(int hopSize_,int frameSize_) : odf(hopSize_,frameSize_,ComplexSpectralDifferenceHWR,HanningWindow) +BTrack::BTrack (int hopSize_, int frameSize_) + : odf (hopSize_, frameSize_, ComplexSpectralDifferenceHWR, HanningWindow) { - initialise(hopSize_, frameSize_); + initialise (hopSize_, frameSize_); } //======================================================================= BTrack::~BTrack() { // destroy fft plan - fftw_destroy_plan(acfForwardFFT); - fftw_destroy_plan(acfBackwardFFT); - fftw_free(complexIn); - fftw_free(complexOut); + fftw_destroy_plan (acfForwardFFT); + fftw_destroy_plan (acfBackwardFFT); + fftw_free (complexIn); + fftw_free (complexOut); } //======================================================================= -double BTrack::getBeatTimeInSeconds(long frameNumber,int hopSize,int fs) +double BTrack::getBeatTimeInSeconds (long frameNumber, int hopSize, int fs) { double hop = (double) hopSize; double samplingFrequency = (double) fs; @@ -64,17 +67,17 @@ } //======================================================================= -double BTrack::getBeatTimeInSeconds(int frameNumber,int hopSize,int fs) +double BTrack::getBeatTimeInSeconds (int frameNumber, int hopSize, int fs) { long frameNum = (long) frameNumber; - return getBeatTimeInSeconds(frameNum, hopSize, fs); + return getBeatTimeInSeconds (frameNum, hopSize, fs); } //======================================================================= -void BTrack::initialise(int hopSize_, int frameSize_) +void BTrack::initialise (int hopSize_, int frameSize_) { double rayparam = 43; double pi = 3.14159265; @@ -94,13 +97,13 @@ // create rayleigh weighting vector - for (int n = 0;n < 128;n++) + for (int n = 0; n < 128; n++) { weightingVector[n] = ((double) n / pow(rayparam,2)) * exp((-1*pow((double)-n,2)) / (2*pow(rayparam,2))); } // initialise prev_delta - for (int i = 0;i < 41;i++) + for (int i = 0; i < 41; i++) { prevDelta[i] = 1; } @@ -134,15 +137,15 @@ // Set up FFT for calculating the auto-correlation function FFTLengthForACFCalculation = 1024; - complexIn = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * FFTLengthForACFCalculation); // complex array to hold fft data - complexOut = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * FFTLengthForACFCalculation); // complex array to hold fft data + complexIn = (fftw_complex*) fftw_malloc (sizeof(fftw_complex) * FFTLengthForACFCalculation); // complex array to hold fft data + complexOut = (fftw_complex*) fftw_malloc (sizeof(fftw_complex) * FFTLengthForACFCalculation); // complex array to hold fft data - acfForwardFFT = fftw_plan_dft_1d(FFTLengthForACFCalculation, complexIn, complexOut, FFTW_FORWARD, FFTW_ESTIMATE); // FFT plan initialisation - acfBackwardFFT = fftw_plan_dft_1d(FFTLengthForACFCalculation, complexOut, complexIn, FFTW_BACKWARD, FFTW_ESTIMATE); // FFT plan initialisation + acfForwardFFT = fftw_plan_dft_1d (FFTLengthForACFCalculation, complexIn, complexOut, FFTW_FORWARD, FFTW_ESTIMATE); // FFT plan initialisation + acfBackwardFFT = fftw_plan_dft_1d (FFTLengthForACFCalculation, complexOut, complexIn, FFTW_BACKWARD, FFTW_ESTIMATE); // FFT plan initialisation } //======================================================================= -void BTrack::setHopSize(int hopSize_) +void BTrack::setHopSize (int hopSize_) { hopSize = hopSize_; onsetDFBufferSize = (512*512)/hopSize; // calculate df buffer size @@ -150,18 +153,17 @@ beatPeriod = round(60/((((double) hopSize)/44100)*tempo)); // set size of onset detection function buffer - onsetDF.resize(onsetDFBufferSize); + onsetDF.resize (onsetDFBufferSize); // set size of cumulative score buffer - cumulativeScore.resize(onsetDFBufferSize); + cumulativeScore.resize (onsetDFBufferSize); // initialise df_buffer to zeros - for (int i = 0;i < onsetDFBufferSize;i++) + for (int i = 0; i < onsetDFBufferSize; i++) { onsetDF[i] = 0; cumulativeScore[i] = 0; - if ((i % ((int) round(beatPeriod))) == 0) { onsetDF[i] = 1; @@ -170,13 +172,13 @@ } //======================================================================= -void BTrack::updateHopAndFrameSize(int hopSize_,int frameSize_) +void BTrack::updateHopAndFrameSize (int hopSize_, int frameSize_) { // update the onset detection function object - odf.initialise(hopSize_, frameSize_); + odf.initialise (hopSize_, frameSize_); // update the hop size being used by the beat tracker - setHopSize(hopSize_); + setHopSize (hopSize_); } //======================================================================= @@ -204,23 +206,21 @@ } //======================================================================= -void BTrack::processAudioFrame(double *frame) +void BTrack::processAudioFrame (double* frame) { // calculate the onset detection function sample for the frame - double sample = odf.calculateOnsetDetectionFunctionSample(frame); - - + double sample = odf.calculateOnsetDetectionFunctionSample (frame); // process the new onset detection function sample in the beat tracking algorithm - processOnsetDetectionFunctionSample(sample); + processOnsetDetectionFunctionSample (sample); } //======================================================================= -void BTrack::processOnsetDetectionFunctionSample(double newSample) +void BTrack::processOnsetDetectionFunctionSample (double newSample) { // we need to ensure that the onset // detection function sample is positive - newSample = fabs(newSample); + newSample = fabs (newSample); // add a tiny constant to the sample to stop it from ever going // to zero. this is to avoid problems further down the line @@ -231,10 +231,10 @@ beatDueInFrame = false; // add new sample at the end - onsetDF.addSampleToEnd(newSample); + onsetDF.addSampleToEnd (newSample); // update cumulative score - updateCumulativeScore(newSample); + updateCumulativeScore (newSample); // if we are halfway between beats if (m0 == 0) @@ -254,7 +254,7 @@ } //======================================================================= -void BTrack::setTempo(double tempo) +void BTrack::setTempo (double tempo) { /////////// TEMPO INDICATION RESET ////////////////// @@ -321,7 +321,7 @@ } //======================================================================= -void BTrack::fixTempo(double tempo) +void BTrack::fixTempo (double tempo) { // firstly make sure tempo is between 80 and 160 bpm.. while (tempo > 160) @@ -362,7 +362,6 @@ { float output[512]; - float input[onsetDFBufferSize]; for (int i = 0;i < onsetDFBufferSize;i++) @@ -398,17 +397,16 @@ void BTrack::calculateTempo() { // adaptive threshold on input - adaptiveThreshold(resampledOnsetDF,512); + adaptiveThreshold (resampledOnsetDF,512); // calculate auto-correlation function of detection function - calculateBalancedACF(resampledOnsetDF); + calculateBalancedACF (resampledOnsetDF); // calculate output of comb filterbank calculateOutputOfCombFilterBank(); - // adaptive threshold on rcf - adaptiveThreshold(combFilterBankOutput,128); + adaptiveThreshold (combFilterBankOutput,128); int t_index; @@ -416,8 +414,8 @@ // calculate tempo observation vector from beat period observation vector for (int i = 0;i < 41;i++) { - t_index = (int) round(tempoToLagFactor / ((double) ((2*i)+80))); - t_index2 = (int) round(tempoToLagFactor / ((double) ((4*i)+160))); + t_index = (int) round (tempoToLagFactor / ((double) ((2*i)+80))); + t_index2 = (int) round (tempoToLagFactor / ((double) ((4*i)+160))); tempoObservationVector[i] = combFilterBankOutput[t_index-1] + combFilterBankOutput[t_index2-1]; @@ -442,7 +440,7 @@ maxval = -1; for (int i = 0;i < 41;i++) { - curval = prevDelta[i]*tempoTransitionMatrix[i][j]; + curval = prevDelta[i] * tempoTransitionMatrix[i][j]; if (curval > maxval) { @@ -450,7 +448,7 @@ } } - delta[j] = maxval*tempoObservationVector[j]; + delta[j] = maxval * tempoObservationVector[j]; } @@ -470,16 +468,16 @@ prevDelta[j] = delta[j]; } - beatPeriod = round((60.0*44100.0)/(((2*maxind)+80)*((double) hopSize))); + beatPeriod = round ((60.0*44100.0)/(((2*maxind)+80)*((double) hopSize))); if (beatPeriod > 0) { - estimatedTempo = 60.0/((((double) hopSize) / 44100.0)*beatPeriod); + estimatedTempo = 60.0/((((double) hopSize) / 44100.0) * beatPeriod); } } //======================================================================= -void BTrack::adaptiveThreshold(double *x,int N) +void BTrack::adaptiveThreshold (double*x, int N) { int i = 0; int k,t = 0; @@ -493,23 +491,23 @@ // find threshold for first 't' samples, where a full average cannot be computed yet for (i = 0;i <= t;i++) { - k = std::min((i+p_pre),N); - x_thresh[i] = calculateMeanOfArray(x,1,k); + k = std::min ((i+p_pre),N); + x_thresh[i] = calculateMeanOfArray (x,1,k); } // find threshold for bulk of samples across a moving average from [i-p_pre,i+p_post] for (i = t+1;i < N-p_post;i++) { - x_thresh[i] = calculateMeanOfArray(x,i-p_pre,i+p_post); + x_thresh[i] = calculateMeanOfArray (x,i-p_pre,i+p_post); } // for last few samples calculate threshold, again, not enough samples to do as above for (i = N-p_post;i < N;i++) { - k = std::max((i-p_post),1); - x_thresh[i] = calculateMeanOfArray(x,k,N); + k = std::max ((i-p_post),1); + x_thresh[i] = calculateMeanOfArray (x,k,N); } // subtract the threshold from the detection function and check that it is not less than 0 - for (i = 0;i < N;i++) + for (i = 0; i < N; i++) { x[i] = x[i] - x_thresh[i]; if (x[i] < 0) @@ -531,11 +529,11 @@ numelem = 4; - for (int i = 2;i <= 127;i++) // max beat period + for (int i = 2; i <= 127; i++) // max beat period { - for (int a = 1;a <= numelem;a++) // number of comb elements + for (int a = 1; a <= numelem; a++) // number of comb elements { - for (int b = 1-a;b <= a-1;b++) // general state using normalisation of comb elements + for (int b = 1-a; b <= a-1; b++) // general state using normalisation of comb elements { combFilterBankOutput[i-1] = combFilterBankOutput[i-1] + (acf[(a*i+b)-1]*weightingVector[i-1])/(2*a-1); // calculate value for comb filter row } @@ -544,7 +542,7 @@ } //======================================================================= -void BTrack::calculateBalancedACF(double *onsetDetectionFunction) +void BTrack::calculateBalancedACF (double* onsetDetectionFunction) { int onsetDetectionFunctionLength = 512; @@ -564,7 +562,7 @@ } // perform the fft - fftw_execute(acfForwardFFT); + fftw_execute (acfForwardFFT); // multiply by complex conjugate for (int i = 0;i < FFTLengthForACFCalculation;i++) @@ -574,15 +572,15 @@ } // perform the ifft - fftw_execute(acfBackwardFFT); + fftw_execute (acfBackwardFFT); double lag = 512; - for (int i = 0;i < 512;i++) + for (int i = 0; i < 512; i++) { // calculate absolute value of result - double absValue = sqrt(complexIn[i][0]*complexIn[i][0] + complexIn[i][1]*complexIn[i][1]); + double absValue = sqrt (complexIn[i][0]*complexIn[i][0] + complexIn[i][1]*complexIn[i][1]); // divide by inverse lad to deal with scale bias towards small lags acf[i] = absValue / lag; @@ -597,7 +595,7 @@ } //======================================================================= -double BTrack::calculateMeanOfArray(double *array,int startIndex,int endIndex) +double BTrack::calculateMeanOfArray (double* array, int startIndex, int endIndex) { int i; double sum = 0; @@ -605,7 +603,7 @@ int length = endIndex - startIndex; // find sum - for (i = startIndex;i < endIndex;i++) + for (i = startIndex; i < endIndex; i++) { sum = sum + array[i]; } @@ -621,11 +619,11 @@ } //======================================================================= -void BTrack::normaliseArray(double *array,int N) +void BTrack::normaliseArray(double* array, int N) { double sum = 0; - for (int i = 0;i < N;i++) + for (int i = 0; i < N; i++) { if (array[i] > 0) { @@ -635,7 +633,7 @@ if (sum > 0) { - for (int i = 0;i < N;i++) + for (int i = 0; i < N; i++) { array[i] = array[i] / sum; } @@ -643,13 +641,13 @@ } //======================================================================= -void BTrack::updateCumulativeScore(double odfSample) +void BTrack::updateCumulativeScore (double odfSample) { int start, end, winsize; double max; - start = onsetDFBufferSize - round(2*beatPeriod); - end = onsetDFBufferSize - round(beatPeriod/2); + start = onsetDFBufferSize - round (2 * beatPeriod); + end = onsetDFBufferSize - round (beatPeriod / 2); winsize = end-start+1; double w1[winsize]; @@ -658,16 +656,16 @@ // create window - for (int i = 0;i < winsize;i++) + for (int i = 0; i < winsize; i++) { - w1[i] = exp((-1*pow(tightness*log(-v/beatPeriod),2))/2); + w1[i] = exp((-1*pow (tightness * log (-v / beatPeriod), 2)) / 2); v = v+1; } // calculate new cumulative score value max = 0; int n = 0; - for (int i=start;i <= end;i++) + for (int i=start; i <= end; i++) { wcumscore = cumulativeScore[i]*w1[n]; @@ -681,7 +679,7 @@ latestCumulativeScoreValue = ((1-alpha)*odfSample) + (alpha*max); - cumulativeScore.addSampleToEnd(latestCumulativeScoreValue); + cumulativeScore.addSampleToEnd (latestCumulativeScoreValue); } //======================================================================= @@ -698,7 +696,7 @@ // create future window double v = 1; - for (int i = 0;i < windowSize;i++) + for (int i = 0; i < windowSize; i++) { w2[i] = exp((-1*pow((v - (beatPeriod/2)),2)) / (2*pow((beatPeriod/2) ,2))); v++; @@ -723,10 +721,10 @@ double max; int n; double wcumscore; - for (int i = onsetDFBufferSize;i < (onsetDFBufferSize+windowSize);i++) + for (int i = onsetDFBufferSize; i < (onsetDFBufferSize + windowSize); i++) { - start = i - round(2*beatPeriod); - end = i - round(beatPeriod/2); + start = i - round (2*beatPeriod); + end = i - round (beatPeriod/2); max = 0; n = 0; @@ -749,7 +747,7 @@ max = 0; n = 0; - for (int i = onsetDFBufferSize;i < (onsetDFBufferSize+windowSize);i++) + for (int i = onsetDFBufferSize; i < (onsetDFBufferSize + windowSize); i++) { wcumscore = futureCumulativeScore[i]*w2[n]; @@ -763,7 +761,5 @@ } // set next prediction time - m0 = beatCounter+round(beatPeriod/2); - - + m0 = beatCounter + round (beatPeriod / 2); } \ No newline at end of file
--- a/src/BTrack.h Tue May 10 23:54:33 2016 +0100 +++ b/src/BTrack.h Wed May 11 00:06:52 2016 +0100 @@ -43,13 +43,13 @@ /** Constructor assuming frame size will be double the hopSize * @param hopSize the hop size in audio samples */ - BTrack(int hopSize_); + BTrack (int hopSize_); /** Constructor taking both hopSize and frameSize * @param hopSize the hop size in audio samples * @param frameSize the frame size in audio samples */ - BTrack(int hopSize_,int frameSize_); + BTrack (int hopSize_, int frameSize_); /** Destructor */ ~BTrack(); @@ -59,19 +59,19 @@ * @param hopSize the hop size in audio samples * @param frameSize the frame size in audio samples */ - void updateHopAndFrameSize(int hopSize_,int frameSize_); + void updateHopAndFrameSize (int hopSize_, int frameSize_); //======================================================================= /** Process a single audio frame * @param frame a pointer to an array containing an audio frame. The number of samples should * match the frame size that the algorithm was initialised with. */ - void processAudioFrame(double *frame); + void processAudioFrame (double *frame); /** Add new onset detection function sample to buffer and apply beat tracking * @param sample an onset detection function sample */ - void processOnsetDetectionFunctionSample(double sample); + void processOnsetDetectionFunctionSample (double sample); //======================================================================= /** @returns the current hop size being used by the beat tracker */ @@ -90,13 +90,13 @@ /** Set the tempo of the beat tracker * @param tempo the tempo in beats per minute (bpm) */ - void setTempo(double tempo); + void setTempo (double tempo); /** Fix tempo to roughly around some value, so that the algorithm will only try to track * tempi around the given tempo * @param tempo the tempo in beats per minute (bpm) */ - void fixTempo(double tempo); + void fixTempo (double tempo); /** Tell the algorithm to not fix the tempo anymore */ void doNotFixTempo(); @@ -109,7 +109,7 @@ * @param fs the sampling frequency in Hz * @returns a beat time in seconds */ - static double getBeatTimeInSeconds(long frameNumber,int hopSize,int fs); + static double getBeatTimeInSeconds (long frameNumber, int hopSize, int fs); /** Calculates a beat time in seconds, given the frame number, hop size and sampling frequency. * This version uses an int to represent the frame number @@ -118,7 +118,7 @@ * @param fs the sampling frequency in Hz * @returns a beat time in seconds */ - static double getBeatTimeInSeconds(int frameNumber,int hopSize,int fs); + static double getBeatTimeInSeconds (int frameNumber, int hopSize, int fs); private: @@ -127,12 +127,12 @@ * @param hopSize_ the hop size in audio samples * @param frameSize_ the frame size in audio samples */ - void initialise(int hopSize_,int frameSize_); + void initialise (int hopSize_, int frameSize_); /** Initialise with hop size and set all array sizes accordingly * @param hopSize_ the hop size in audio samples */ - void setHopSize(int hopSize_); + void setHopSize (int hopSize_); /** Resamples the onset detection function from an arbitrary number of samples to 512 */ void resampleOnsetDetectionFunction(); @@ -140,7 +140,7 @@ /** Updates the cumulative score function with a new onset detection function sample * @param odfSample an onset detection function sample */ - void updateCumulativeScore(double odfSample); + void updateCumulativeScore (double odfSample); /** Predicts the next beat, based upon the internal program state */ void predictBeat(); @@ -153,7 +153,7 @@ * @param x a pointer to an array containing onset detection function samples * @param N the length of the array, x */ - void adaptiveThreshold(double *x,int N); + void adaptiveThreshold (double* x, int N); /** Calculates the mean of values in an array between index locations [startIndex,endIndex] * @param array a pointer to an array that contains the values we wish to find the mean from @@ -161,18 +161,18 @@ * @param endIndex the final index to which we would like to calculate the mean * @returns the mean of the sub-section of the array */ - double calculateMeanOfArray(double *array,int startIndex,int endIndex); + double calculateMeanOfArray (double*array, int startIndex, int endIndex); /** Normalises a given array * @param array a pointer to the array we wish to normalise * @param N the length of the array */ - void normaliseArray(double *array,int N); + void normaliseArray (double* array, int N); /** Calculates the balanced autocorrelation of the smoothed onset detection function * @param onsetDetectionFunction a pointer to an array containing the onset detection function */ - void calculateBalancedACF(double *onsetDetectionFunction); + void calculateBalancedACF (double* onsetDetectionFunction); /** Calculates the output of the comb filter bank */ void calculateOutputOfCombFilterBank();