annotate src/BTrack.h @ 18:450c53430540 develop

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