annotate src/BTrack.h @ 57:296af6af6c3d

Replaced switch statements in OnsetDetectionFunction with enums. Renamed lots of functions so that they have better names, in camel case. Added some unit tests for initialisation of BTrack.
author Adam Stark <adamstark@users.noreply.github.com>
date Thu, 23 Jan 2014 15:31:11 +0000
parents b6d440942ff6
children f84ccd07e17f
rev   line source
adamstark@46 1 //=======================================================================
adamstark@46 2 /** @file BTrack.h
adamstark@47 3 * @brief BTrack - a real-time beat tracker
adamstark@46 4 * @author Adam Stark
adamstark@46 5 * @copyright Copyright (C) 2008-2014 Queen Mary University of London
adamstark@46 6 *
adamstark@46 7 * This program is free software: you can redistribute it and/or modify
adamstark@46 8 * it under the terms of the GNU General Public License as published by
adamstark@46 9 * the Free Software Foundation, either version 3 of the License, or
adamstark@46 10 * (at your option) any later version.
adamstark@46 11 *
adamstark@46 12 * This program is distributed in the hope that it will be useful,
adamstark@46 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
adamstark@46 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
adamstark@46 15 * GNU General Public License for more details.
adamstark@46 16 *
adamstark@46 17 * You should have received a copy of the GNU General Public License
adamstark@46 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
adamstark@46 19 */
adamstark@46 20 //=======================================================================
adamstark@46 21
adamstark@46 22 #ifndef __BTRACK_H
adamstark@46 23 #define __BTRACK_H
adamstark@46 24
adamstark@55 25 #include "OnsetDetectionFunction.h"
adamstark@55 26
adamstark@46 27 class BTrack {
adamstark@46 28
adamstark@46 29 public:
adamstark@51 30
adamstark@55 31 /** constructor assuming hop size of 512 and frame size of 1024 */
adamstark@56 32 BTrack();
adamstark@51 33
adamstark@55 34 /** constructor assuming frame size will be double hopSize
adamstark@55 35 * @param hopSize the step size in audio samples by which we will receive audio frames
adamstark@55 36 */
adamstark@57 37 BTrack(int hopSize_);
adamstark@55 38
adamstark@55 39 /** constructor taking both hopSize and frameSize
adamstark@55 40 * @param hopSize the step size in audio samples by which we will receive audio frames
adamstark@55 41 * @param frameSize the audio frame size in audio samples
adamstark@55 42 */
adamstark@57 43 BTrack(int hopSize_,int frameSize_);
adamstark@55 44
adamstark@55 45 /** Process a single audio frame */
adamstark@56 46 void processAudioFrame(double *frame);
adamstark@55 47
adamstark@55 48 /** Add new onset detection function sample to buffer and apply beat tracking */
adamstark@56 49 void processOnsetDetectionFunctionSample(double sample);
adamstark@51 50
adamstark@57 51 /** @returns the current hop size being used by the beat tracker */
adamstark@57 52 int getHopSize();
adamstark@57 53
adamstark@51 54 /** Set the tempo of the beat tracker */
adamstark@57 55 void setTempo(double tempo);
adamstark@51 56
adamstark@51 57 /** fix tempo to roughly around some value */
adamstark@57 58 void fixTempo(double tempo);
adamstark@51 59
adamstark@51 60 /** do not fix the tempo anymore */
adamstark@57 61 void doNotFixTempo();
adamstark@55 62
adamstark@55 63 static double getBeatTimeInSeconds(long frameNumber,int hopSize,int fs);
adamstark@55 64
adamstark@55 65 static double getBeatTimeInSeconds(int frameNumber,int hopSize,int fs);
adamstark@55 66
adamstark@57 67 /** @returns true if a beat should occur in the current audio frame */
adamstark@57 68 bool beatDueInCurrentFrame();
adamstark@57 69
adamstark@56 70 double cscoreval;
adamstark@56 71 double est_tempo;
adamstark@46 72
adamstark@46 73 private:
adamstark@51 74
adamstark@57 75 void initialise(int hopSize_,int frameSize_);
adamstark@56 76
adamstark@56 77 /** Initialise with hop size and set all frame sizes accordingly */
adamstark@57 78 void setHopSize(int hopSize_);
adamstark@56 79
adamstark@51 80 /** Convert detection function from N samples to 512 */
adamstark@57 81 void resampleOnsetDetectionFunction();
adamstark@51 82
adamstark@51 83 /** update the cumulative score */
adamstark@57 84 void updateCumulativeScore(double df_sample);
adamstark@51 85
adamstark@51 86 /** predicts the next beat */
adamstark@57 87 void predictBeat();
adamstark@51 88
adamstark@51 89 /** Calculates the current tempo expressed as the beat period in detection function samples */
adamstark@57 90 void calculateTempo();
adamstark@51 91
adamstark@51 92 /** calculates an adaptive threshold which is used to remove low level energy from detection
adamstark@51 93 * function and emphasise peaks
adamstark@51 94 */
adamstark@57 95 void adaptiveThreshold(double *x,int N);
adamstark@51 96
adamstark@51 97 /** calculates the mean of values in an array from index locations [start,end] */
adamstark@57 98 double calculateMeanOfArray(double *array,int start,int end);
adamstark@51 99
adamstark@51 100 /** normalises a given array */
adamstark@57 101 void normaliseArray(double *array,int N);
adamstark@51 102
adamstark@51 103 /** calculates the balanced autocorrelation of the smoothed detection function */
adamstark@57 104 void calculateBalancedACF(double *df_thresh);
adamstark@51 105
adamstark@57 106 /** calculates the output of the comb filter bank */
adamstark@57 107 void calculateOutputOfCombFilterBank();
adamstark@46 108
adamstark@46 109 // buffers
adamstark@56 110 double *dfbuffer; /**< to hold detection function */
adamstark@56 111 double df512[512]; /**< to hold resampled detection function */
adamstark@56 112 double *cumscore; /**< to hold cumulative score */
adamstark@46 113
adamstark@57 114 double acf[512]; /**< to hold autocorrelation function */
adamstark@46 115
adamstark@56 116 double wv[128]; /**< to hold weighting vector */
adamstark@46 117
adamstark@57 118 double rcf[128]; /**< to hold comb filter output */
adamstark@56 119 double t_obs[41]; /**< to hold tempo version of comb filter output */
adamstark@46 120
adamstark@56 121 double delta[41]; /**< to hold final tempo candidate array */
adamstark@56 122 double prev_delta[41]; /**< previous delta */
adamstark@56 123 double prev_delta_fix[41]; /**< fixed tempo version of previous delta */
adamstark@46 124
adamstark@56 125 double t_tmat[41][41]; /**< transition matrix */
adamstark@46 126
adamstark@55 127 OnsetDetectionFunction odf;
adamstark@46 128
adamstark@56 129 // parameters
adamstark@56 130 double tightness;
adamstark@56 131 double alpha;
adamstark@57 132 double beatPeriod;
adamstark@56 133 double tempo;
adamstark@46 134
adamstark@46 135
adamstark@56 136 double p_fact;
adamstark@46 137
adamstark@46 138
adamstark@56 139 //
adamstark@56 140 int m0; // indicates when the next point to predict the next beat is
adamstark@56 141 int beat;
adamstark@46 142
adamstark@56 143 int dfbuffer_size;
adamstark@46 144
adamstark@46 145
adamstark@57 146 int hopSize;
adamstark@46 147
adamstark@46 148
adamstark@56 149 int tempofix;
adamstark@46 150
adamstark@57 151
adamstark@57 152 bool beatDueInFrame;
adamstark@46 153
adamstark@46 154 };
adamstark@46 155
adamstark@46 156 #endif