annotate src/BTrack.cpp @ 103:6b522d568ba4

Made log-gaussian transition window calculated in a function, rather than with duplicated code
author Adam Stark <adamstark.uk@gmail.com>
date Mon, 11 Sep 2017 23:53:06 +0100
parents 0a99d93955bb
children 677c44fe42e0
rev   line source
adamstark@46 1 //=======================================================================
adamstark@46 2 /** @file BTrack.cpp
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 #include <cmath>
adamstark@52 23 #include <algorithm>
adamstark@97 24 #include <numeric>
adamstark@46 25 #include "BTrack.h"
adamstark@46 26 #include "samplerate.h"
adamstark@89 27 #include <iostream>
adamstark@46 28
adamstark@55 29 //=======================================================================
adamstark@91 30 BTrack::BTrack()
adamstark@91 31 : odf (512, 1024, ComplexSpectralDifferenceHWR, HanningWindow)
adamstark@55 32 {
adamstark@93 33 initialise (512, 1024);
adamstark@55 34 }
adamstark@46 35
adamstark@51 36 //=======================================================================
adamstark@91 37 BTrack::BTrack (int hopSize_)
adamstark@97 38 : odf (hopSize_, 2 * hopSize_, ComplexSpectralDifferenceHWR, HanningWindow)
adamstark@46 39 {
adamstark@97 40 initialise (hopSize_, 2 * hopSize_);
adamstark@55 41 }
adamstark@55 42
adamstark@55 43 //=======================================================================
adamstark@91 44 BTrack::BTrack (int hopSize_, int frameSize_)
adamstark@91 45 : odf (hopSize_, frameSize_, ComplexSpectralDifferenceHWR, HanningWindow)
adamstark@55 46 {
adamstark@91 47 initialise (hopSize_, frameSize_);
adamstark@55 48 }
adamstark@55 49
adamstark@55 50 //=======================================================================
adamstark@88 51 BTrack::~BTrack()
adamstark@88 52 {
adamstark@93 53 #ifdef USE_FFTW
adamstark@88 54 // destroy fft plan
adamstark@91 55 fftw_destroy_plan (acfForwardFFT);
adamstark@91 56 fftw_destroy_plan (acfBackwardFFT);
adamstark@91 57 fftw_free (complexIn);
adamstark@91 58 fftw_free (complexOut);
adamstark@93 59 #endif
adamstark@93 60
adamstark@93 61 #ifdef USE_KISS_FFT
adamstark@93 62 free (cfgForwards);
adamstark@93 63 free (cfgBackwards);
adamstark@93 64 delete [] fftIn;
adamstark@93 65 delete [] fftOut;
adamstark@93 66 #endif
adamstark@88 67 }
adamstark@88 68
adamstark@88 69 //=======================================================================
adamstark@91 70 double BTrack::getBeatTimeInSeconds (long frameNumber, int hopSize, int fs)
adamstark@55 71 {
adamstark@55 72 double hop = (double) hopSize;
adamstark@55 73 double samplingFrequency = (double) fs;
adamstark@55 74 double frameNum = (double) frameNumber;
adamstark@55 75
adamstark@55 76 return ((hop / samplingFrequency) * frameNum);
adamstark@55 77 }
adamstark@55 78
adamstark@55 79 //=======================================================================
adamstark@91 80 void BTrack::initialise (int hopSize_, int frameSize_)
adamstark@55 81 {
adamstark@97 82 // set vector sizes
adamstark@97 83 resampledOnsetDF.resize (512);
adamstark@97 84 acf.resize (512);
adamstark@97 85 weightingVector.resize (128);
adamstark@97 86 combFilterBankOutput.resize (128);
adamstark@97 87 tempoObservationVector.resize (41);
adamstark@97 88 delta.resize (41);
adamstark@97 89 prevDelta.resize (41);
adamstark@97 90 prevDeltaFixed.resize (41);
adamstark@97 91
adamstark@98 92 double rayleighParameter = 43;
adamstark@54 93 double pi = 3.14159265;
adamstark@46 94
adamstark@46 95
adamstark@46 96 // initialise parameters
adamstark@46 97 tightness = 5;
adamstark@46 98 alpha = 0.9;
adamstark@58 99 estimatedTempo = 120.0;
adamstark@100 100 tempoToLagFactor = 60. * 44100. / 512.;
adamstark@46 101
adamstark@46 102 m0 = 10;
adamstark@58 103 beatCounter = -1;
adamstark@46 104
adamstark@57 105 beatDueInFrame = false;
adamstark@46 106
adamstark@58 107
adamstark@46 108 // create rayleigh weighting vector
adamstark@91 109 for (int n = 0; n < 128; n++)
adamstark@98 110 weightingVector[n] = ((double) n / pow (rayleighParameter, 2)) * exp((-1 * pow((double) - n, 2)) / (2 * pow (rayleighParameter, 2)));
adamstark@46 111
adamstark@100 112 // initialise prevDelta
adamstark@97 113 std::fill (prevDelta.begin(), prevDelta.end(), 1);
adamstark@97 114
adamstark@54 115 double t_mu = 41/2;
adamstark@54 116 double m_sig;
adamstark@54 117 double x;
adamstark@46 118 // create tempo transition matrix
adamstark@46 119 m_sig = 41/8;
adamstark@46 120 for (int i = 0;i < 41;i++)
adamstark@46 121 {
adamstark@46 122 for (int j = 0;j < 41;j++)
adamstark@46 123 {
adamstark@46 124 x = j+1;
adamstark@46 125 t_mu = i+1;
adamstark@58 126 tempoTransitionMatrix[i][j] = (1 / (m_sig * sqrt(2*pi))) * exp( (-1*pow((x-t_mu),2)) / (2*pow(m_sig,2)) );
adamstark@46 127 }
adamstark@55 128 }
adamstark@46 129
adamstark@46 130 // tempo is not fixed
adamstark@58 131 tempoFixed = false;
adamstark@58 132
adamstark@58 133 // initialise latest cumulative score value
adamstark@58 134 // in case it is requested before any processing takes place
adamstark@58 135 latestCumulativeScoreValue = 0;
adamstark@55 136
adamstark@55 137 // initialise algorithm given the hopsize
adamstark@100 138 setHopSize (hopSize_);
adamstark@88 139
adamstark@88 140 // Set up FFT for calculating the auto-correlation function
adamstark@88 141 FFTLengthForACFCalculation = 1024;
adamstark@88 142
adamstark@93 143 #ifdef USE_FFTW
adamstark@91 144 complexIn = (fftw_complex*) fftw_malloc (sizeof(fftw_complex) * FFTLengthForACFCalculation); // complex array to hold fft data
adamstark@91 145 complexOut = (fftw_complex*) fftw_malloc (sizeof(fftw_complex) * FFTLengthForACFCalculation); // complex array to hold fft data
adamstark@88 146
adamstark@91 147 acfForwardFFT = fftw_plan_dft_1d (FFTLengthForACFCalculation, complexIn, complexOut, FFTW_FORWARD, FFTW_ESTIMATE); // FFT plan initialisation
adamstark@91 148 acfBackwardFFT = fftw_plan_dft_1d (FFTLengthForACFCalculation, complexOut, complexIn, FFTW_BACKWARD, FFTW_ESTIMATE); // FFT plan initialisation
adamstark@93 149 #endif
adamstark@93 150
adamstark@93 151 #ifdef USE_KISS_FFT
adamstark@93 152 fftIn = new kiss_fft_cpx[FFTLengthForACFCalculation];
adamstark@93 153 fftOut = new kiss_fft_cpx[FFTLengthForACFCalculation];
adamstark@93 154 cfgForwards = kiss_fft_alloc (FFTLengthForACFCalculation, 0, 0, 0);
adamstark@93 155 cfgBackwards = kiss_fft_alloc (FFTLengthForACFCalculation, 1, 0, 0);
adamstark@93 156 #endif
adamstark@46 157 }
adamstark@46 158
adamstark@51 159 //=======================================================================
adamstark@91 160 void BTrack::setHopSize (int hopSize_)
adamstark@46 161 {
adamstark@57 162 hopSize = hopSize_;
adamstark@97 163 onsetDFBufferSize = (512 * 512) / hopSize; // calculate df buffer size
adamstark@101 164 beatPeriod = round(60/((((double) hopSize)/44100) * 120.));
adamstark@63 165
adamstark@63 166 // set size of onset detection function buffer
adamstark@91 167 onsetDF.resize (onsetDFBufferSize);
adamstark@63 168
adamstark@63 169 // set size of cumulative score buffer
adamstark@91 170 cumulativeScore.resize (onsetDFBufferSize);
adamstark@46 171
adamstark@46 172 // initialise df_buffer to zeros
adamstark@91 173 for (int i = 0; i < onsetDFBufferSize; i++)
adamstark@46 174 {
adamstark@58 175 onsetDF[i] = 0;
adamstark@58 176 cumulativeScore[i] = 0;
adamstark@46 177
adamstark@57 178 if ((i % ((int) round(beatPeriod))) == 0)
adamstark@46 179 {
adamstark@58 180 onsetDF[i] = 1;
adamstark@46 181 }
adamstark@46 182 }
adamstark@46 183 }
adamstark@46 184
adamstark@51 185 //=======================================================================
adamstark@91 186 void BTrack::updateHopAndFrameSize (int hopSize_, int frameSize_)
adamstark@65 187 {
adamstark@65 188 // update the onset detection function object
adamstark@91 189 odf.initialise (hopSize_, frameSize_);
adamstark@65 190
adamstark@65 191 // update the hop size being used by the beat tracker
adamstark@91 192 setHopSize (hopSize_);
adamstark@65 193 }
adamstark@65 194
adamstark@65 195 //=======================================================================
adamstark@57 196 bool BTrack::beatDueInCurrentFrame()
adamstark@57 197 {
adamstark@57 198 return beatDueInFrame;
adamstark@57 199 }
adamstark@57 200
adamstark@57 201 //=======================================================================
adamstark@78 202 double BTrack::getCurrentTempoEstimate()
adamstark@78 203 {
adamstark@78 204 return estimatedTempo;
adamstark@78 205 }
adamstark@78 206
adamstark@78 207 //=======================================================================
adamstark@57 208 int BTrack::getHopSize()
adamstark@57 209 {
adamstark@57 210 return hopSize;
adamstark@57 211 }
adamstark@57 212
adamstark@57 213 //=======================================================================
adamstark@58 214 double BTrack::getLatestCumulativeScoreValue()
adamstark@58 215 {
adamstark@58 216 return latestCumulativeScoreValue;
adamstark@58 217 }
adamstark@58 218
adamstark@58 219 //=======================================================================
adamstark@91 220 void BTrack::processAudioFrame (double* frame)
adamstark@55 221 {
adamstark@55 222 // calculate the onset detection function sample for the frame
adamstark@91 223 double sample = odf.calculateOnsetDetectionFunctionSample (frame);
adamstark@55 224
adamstark@55 225 // process the new onset detection function sample in the beat tracking algorithm
adamstark@91 226 processOnsetDetectionFunctionSample (sample);
adamstark@55 227 }
adamstark@55 228
adamstark@55 229 //=======================================================================
adamstark@91 230 void BTrack::processOnsetDetectionFunctionSample (double newSample)
adamstark@56 231 {
adamstark@56 232 // we need to ensure that the onset
adamstark@56 233 // detection function sample is positive
adamstark@91 234 newSample = fabs (newSample);
adamstark@56 235
adamstark@56 236 // add a tiny constant to the sample to stop it from ever going
adamstark@56 237 // to zero. this is to avoid problems further down the line
adamstark@56 238 newSample = newSample + 0.0001;
adamstark@56 239
adamstark@46 240 m0--;
adamstark@58 241 beatCounter--;
adamstark@57 242 beatDueInFrame = false;
adamstark@90 243
adamstark@46 244 // add new sample at the end
adamstark@91 245 onsetDF.addSampleToEnd (newSample);
adamstark@46 246
adamstark@46 247 // update cumulative score
adamstark@91 248 updateCumulativeScore (newSample);
adamstark@46 249
adamstark@97 250 // if we are halfway between beats, predict a beat
adamstark@46 251 if (m0 == 0)
adamstark@97 252 predictBeat();
adamstark@46 253
adamstark@46 254 // if we are at a beat
adamstark@58 255 if (beatCounter == 0)
adamstark@46 256 {
adamstark@57 257 beatDueInFrame = true; // indicate a beat should be output
adamstark@46 258
adamstark@46 259 // recalculate the tempo
adamstark@57 260 resampleOnsetDetectionFunction();
adamstark@57 261 calculateTempo();
adamstark@46 262 }
adamstark@46 263 }
adamstark@46 264
adamstark@51 265 //=======================================================================
adamstark@91 266 void BTrack::setTempo (double tempo)
adamstark@97 267 {
adamstark@46 268 /////////// TEMPO INDICATION RESET //////////////////
adamstark@46 269
adamstark@46 270 // firstly make sure tempo is between 80 and 160 bpm..
adamstark@46 271 while (tempo > 160)
adamstark@97 272 tempo = tempo / 2;
adamstark@46 273
adamstark@46 274 while (tempo < 80)
adamstark@97 275 tempo = tempo * 2;
adamstark@46 276
adamstark@46 277 // convert tempo from bpm value to integer index of tempo probability
adamstark@46 278 int tempo_index = (int) round((tempo - 80)/2);
adamstark@46 279
adamstark@97 280 // now set previous tempo observations to zero and set desired tempo index to 1
adamstark@97 281 std::fill (prevDelta.begin(), prevDelta.end(), 0);
adamstark@58 282 prevDelta[tempo_index] = 1;
adamstark@46 283
adamstark@46 284 /////////// CUMULATIVE SCORE ARTIFICAL TEMPO UPDATE //////////////////
adamstark@46 285
adamstark@46 286 // calculate new beat period
adamstark@97 287 int newBeatPeriod = (int) round (60 / ((((double) hopSize) / 44100) * tempo));
adamstark@46 288
adamstark@97 289 int k = 1;
adamstark@97 290
adamstark@97 291 // initialise onset detection function with delta functions spaced
adamstark@97 292 // at the new beat period
adamstark@97 293 for (int i = onsetDFBufferSize - 1; i >= 0; i--)
adamstark@46 294 {
adamstark@97 295 if (k == 1)
adamstark@46 296 {
adamstark@58 297 cumulativeScore[i] = 150;
adamstark@58 298 onsetDF[i] = 150;
adamstark@46 299 }
adamstark@46 300 else
adamstark@46 301 {
adamstark@58 302 cumulativeScore[i] = 10;
adamstark@58 303 onsetDF[i] = 10;
adamstark@46 304 }
adamstark@46 305
adamstark@97 306 k++;
adamstark@46 307
adamstark@97 308 if (k > newBeatPeriod)
adamstark@46 309 {
adamstark@97 310 k = 1;
adamstark@46 311 }
adamstark@46 312 }
adamstark@46 313
adamstark@46 314 /////////// INDICATE THAT THIS IS A BEAT //////////////////
adamstark@46 315
adamstark@46 316 // beat is now
adamstark@58 317 beatCounter = 0;
adamstark@46 318
adamstark@46 319 // offbeat is half of new beat period away
adamstark@97 320 m0 = (int) round (((double) newBeatPeriod) / 2);
adamstark@46 321 }
adamstark@46 322
adamstark@51 323 //=======================================================================
adamstark@91 324 void BTrack::fixTempo (double tempo)
adamstark@46 325 {
adamstark@46 326 // firstly make sure tempo is between 80 and 160 bpm..
adamstark@46 327 while (tempo > 160)
adamstark@100 328 tempo = tempo / 2;
adamstark@46 329
adamstark@46 330 while (tempo < 80)
adamstark@100 331 tempo = tempo * 2;
adamstark@46 332
adamstark@46 333 // convert tempo from bpm value to integer index of tempo probability
adamstark@100 334 int tempoIndex = (int) round((tempo - 80) / 2);
adamstark@46 335
adamstark@46 336 // now set previous fixed previous tempo observation values to zero
adamstark@46 337 for (int i=0;i < 41;i++)
adamstark@46 338 {
adamstark@58 339 prevDeltaFixed[i] = 0;
adamstark@46 340 }
adamstark@46 341
adamstark@46 342 // set desired tempo index to 1
adamstark@100 343 prevDeltaFixed[tempoIndex] = 1;
adamstark@46 344
adamstark@46 345 // set the tempo fix flag
adamstark@58 346 tempoFixed = true;
adamstark@46 347 }
adamstark@46 348
adamstark@51 349 //=======================================================================
adamstark@57 350 void BTrack::doNotFixTempo()
adamstark@46 351 {
adamstark@46 352 // set the tempo fix flag
adamstark@58 353 tempoFixed = false;
adamstark@46 354 }
adamstark@46 355
adamstark@51 356 //=======================================================================
adamstark@57 357 void BTrack::resampleOnsetDetectionFunction()
adamstark@46 358 {
adamstark@46 359 float output[512];
adamstark@58 360 float input[onsetDFBufferSize];
adamstark@54 361
adamstark@58 362 for (int i = 0;i < onsetDFBufferSize;i++)
adamstark@58 363 input[i] = (float) onsetDF[i];
adamstark@89 364
adamstark@97 365 double ratio = 512.0 / ((double) onsetDFBufferSize);
adamstark@97 366 int bufferLength = onsetDFBufferSize;
adamstark@97 367 int outputLength = 512;
adamstark@89 368
adamstark@97 369 SRC_DATA src_data;
adamstark@89 370 src_data.data_in = input;
adamstark@97 371 src_data.input_frames = bufferLength;
adamstark@97 372 src_data.src_ratio = ratio;
adamstark@89 373 src_data.data_out = output;
adamstark@97 374 src_data.output_frames = outputLength;
adamstark@89 375
adamstark@89 376 src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1);
adamstark@89 377
adamstark@97 378 for (int i = 0; i < outputLength; i++)
adamstark@89 379 resampledOnsetDF[i] = (double) src_data.data_out[i];
adamstark@46 380 }
adamstark@46 381
adamstark@51 382 //=======================================================================
adamstark@57 383 void BTrack::calculateTempo()
adamstark@46 384 {
adamstark@46 385 // adaptive threshold on input
adamstark@100 386 adaptiveThreshold (resampledOnsetDF);
adamstark@46 387
adamstark@46 388 // calculate auto-correlation function of detection function
adamstark@100 389 calculateBalancedACF (resampledOnsetDF);
adamstark@46 390
adamstark@46 391 // calculate output of comb filterbank
adamstark@57 392 calculateOutputOfCombFilterBank();
adamstark@46 393
adamstark@46 394 // adaptive threshold on rcf
adamstark@100 395 adaptiveThreshold (combFilterBankOutput);
adamstark@46 396
adamstark@59 397 // calculate tempo observation vector from beat period observation vector
adamstark@100 398 for (int i = 0; i < 41; i++)
adamstark@46 399 {
adamstark@100 400 int tempoIndex1 = (int) round (tempoToLagFactor / ((double) ((2*i)+80)));
adamstark@100 401 int tempoIndex2 = (int) round (tempoToLagFactor / ((double) ((4*i)+160)));
adamstark@100 402 tempoObservationVector[i] = combFilterBankOutput[tempoIndex1 - 1] + combFilterBankOutput[tempoIndex2 - 1];
adamstark@46 403 }
adamstark@46 404
adamstark@46 405 // if tempo is fixed then always use a fixed set of tempi as the previous observation probability function
adamstark@58 406 if (tempoFixed)
adamstark@46 407 {
adamstark@100 408 for (int k = 0; k < 41; k++)
adamstark@100 409 prevDelta[k] = prevDeltaFixed[k];
adamstark@46 410 }
adamstark@46 411
adamstark@100 412 for (int j = 0; j < 41; j++)
adamstark@46 413 {
adamstark@100 414 double maxValue = -1;
adamstark@100 415
adamstark@100 416 for (int i = 0; i < 41; i++)
adamstark@46 417 {
adamstark@100 418 double currentValue = prevDelta[i] * tempoTransitionMatrix[i][j];
adamstark@46 419
adamstark@100 420 if (currentValue > maxValue)
adamstark@100 421 maxValue = currentValue;
adamstark@46 422 }
adamstark@46 423
adamstark@100 424 delta[j] = maxValue * tempoObservationVector[j];
adamstark@46 425 }
adamstark@46 426
adamstark@100 427 normaliseVector (delta);
adamstark@46 428
adamstark@100 429 double maxIndex = -1;
adamstark@100 430 double maxValue = -1;
adamstark@46 431
adamstark@100 432 for (int j = 0; j < 41; j++)
adamstark@46 433 {
adamstark@100 434 if (delta[j] > maxValue)
adamstark@46 435 {
adamstark@100 436 maxValue = delta[j];
adamstark@100 437 maxIndex = j;
adamstark@46 438 }
adamstark@46 439
adamstark@58 440 prevDelta[j] = delta[j];
adamstark@46 441 }
adamstark@46 442
adamstark@100 443 beatPeriod = round ((60.0 * 44100.0) / (((2 * maxIndex) + 80) * ((double) hopSize)));
adamstark@46 444
adamstark@57 445 if (beatPeriod > 0)
adamstark@100 446 estimatedTempo = 60.0/((((double) hopSize) / 44100.0) * beatPeriod);
adamstark@46 447 }
adamstark@46 448
adamstark@51 449 //=======================================================================
adamstark@100 450 void BTrack::adaptiveThreshold (std::vector<double>& x)
adamstark@46 451 {
adamstark@100 452 int N = static_cast<int> (x.size());
adamstark@100 453 double threshold[N];
adamstark@46 454
adamstark@46 455 int p_post = 7;
adamstark@46 456 int p_pre = 8;
adamstark@46 457
adamstark@100 458 int t = std::min (N, p_post); // what is smaller, p_post or df size. This is to avoid accessing outside of arrays
adamstark@46 459
adamstark@46 460 // find threshold for first 't' samples, where a full average cannot be computed yet
adamstark@100 461 for (int i = 0; i <= t; i++)
adamstark@46 462 {
adamstark@100 463 int k = std::min ((i + p_pre), N);
adamstark@100 464 threshold[i] = calculateMeanOfVector (x, 1, k);
adamstark@46 465 }
adamstark@100 466
adamstark@46 467 // find threshold for bulk of samples across a moving average from [i-p_pre,i+p_post]
adamstark@100 468 for (int i = t + 1; i < N - p_post; i++)
adamstark@46 469 {
adamstark@100 470 threshold[i] = calculateMeanOfVector (x, i - p_pre, i + p_post);
adamstark@46 471 }
adamstark@100 472
adamstark@46 473 // for last few samples calculate threshold, again, not enough samples to do as above
adamstark@100 474 for (int i = N - p_post; i < N; i++)
adamstark@46 475 {
adamstark@100 476 int k = std::max ((i - p_post), 1);
adamstark@100 477 threshold[i] = calculateMeanOfVector (x, k, N);
adamstark@46 478 }
adamstark@46 479
adamstark@46 480 // subtract the threshold from the detection function and check that it is not less than 0
adamstark@100 481 for (int i = 0; i < N; i++)
adamstark@46 482 {
adamstark@100 483 x[i] = x[i] - threshold[i];
adamstark@100 484
adamstark@46 485 if (x[i] < 0)
adamstark@100 486 x[i] = 0;
adamstark@46 487 }
adamstark@46 488 }
adamstark@46 489
adamstark@51 490 //=======================================================================
adamstark@57 491 void BTrack::calculateOutputOfCombFilterBank()
adamstark@46 492 {
adamstark@100 493 std::fill (combFilterBankOutput.begin(), combFilterBankOutput.end(), 0.0);
adamstark@100 494 int numCombElements = 4;
adamstark@46 495
adamstark@91 496 for (int i = 2; i <= 127; i++) // max beat period
adamstark@46 497 {
adamstark@100 498 for (int a = 1; a <= numCombElements; a++) // number of comb elements
adamstark@46 499 {
adamstark@100 500 for (int b = 1 - a; b <= a - 1; b++) // general state using normalisation of comb elements
adamstark@46 501 {
adamstark@102 502 combFilterBankOutput[i-1] += (acf[(a * i + b) - 1] * weightingVector[i - 1]) / (2 * a - 1); // calculate value for comb filter row
adamstark@46 503 }
adamstark@46 504 }
adamstark@46 505 }
adamstark@46 506 }
adamstark@46 507
adamstark@51 508 //=======================================================================
adamstark@100 509 void BTrack::calculateBalancedACF (std::vector<double>& onsetDetectionFunction)
adamstark@46 510 {
adamstark@88 511 int onsetDetectionFunctionLength = 512;
adamstark@88 512
adamstark@93 513 #ifdef USE_FFTW
adamstark@88 514 // copy into complex array and zero pad
adamstark@88 515 for (int i = 0;i < FFTLengthForACFCalculation;i++)
adamstark@88 516 {
adamstark@88 517 if (i < onsetDetectionFunctionLength)
adamstark@88 518 {
adamstark@88 519 complexIn[i][0] = onsetDetectionFunction[i];
adamstark@88 520 complexIn[i][1] = 0.0;
adamstark@88 521 }
adamstark@88 522 else
adamstark@88 523 {
adamstark@88 524 complexIn[i][0] = 0.0;
adamstark@88 525 complexIn[i][1] = 0.0;
adamstark@88 526 }
adamstark@88 527 }
adamstark@88 528
adamstark@88 529 // perform the fft
adamstark@91 530 fftw_execute (acfForwardFFT);
adamstark@88 531
adamstark@88 532 // multiply by complex conjugate
adamstark@88 533 for (int i = 0;i < FFTLengthForACFCalculation;i++)
adamstark@88 534 {
adamstark@88 535 complexOut[i][0] = complexOut[i][0]*complexOut[i][0] + complexOut[i][1]*complexOut[i][1];
adamstark@88 536 complexOut[i][1] = 0.0;
adamstark@88 537 }
adamstark@88 538
adamstark@88 539 // perform the ifft
adamstark@91 540 fftw_execute (acfBackwardFFT);
adamstark@88 541
adamstark@93 542 #endif
adamstark@93 543
adamstark@93 544 #ifdef USE_KISS_FFT
adamstark@93 545 // copy into complex array and zero pad
adamstark@93 546 for (int i = 0;i < FFTLengthForACFCalculation;i++)
adamstark@93 547 {
adamstark@93 548 if (i < onsetDetectionFunctionLength)
adamstark@93 549 {
adamstark@93 550 fftIn[i].r = onsetDetectionFunction[i];
adamstark@93 551 fftIn[i].i = 0.0;
adamstark@93 552 }
adamstark@93 553 else
adamstark@93 554 {
adamstark@93 555 fftIn[i].r = 0.0;
adamstark@93 556 fftIn[i].i = 0.0;
adamstark@93 557 }
adamstark@93 558 }
adamstark@93 559
adamstark@93 560 // execute kiss fft
adamstark@93 561 kiss_fft (cfgForwards, fftIn, fftOut);
adamstark@93 562
adamstark@93 563 // multiply by complex conjugate
adamstark@93 564 for (int i = 0;i < FFTLengthForACFCalculation;i++)
adamstark@93 565 {
adamstark@93 566 fftOut[i].r = fftOut[i].r * fftOut[i].r + fftOut[i].i * fftOut[i].i;
adamstark@93 567 fftOut[i].i = 0.0;
adamstark@93 568 }
adamstark@93 569
adamstark@93 570 // perform the ifft
adamstark@93 571 kiss_fft (cfgBackwards, fftOut, fftIn);
adamstark@93 572
adamstark@93 573 #endif
adamstark@88 574
adamstark@88 575 double lag = 512;
adamstark@88 576
adamstark@91 577 for (int i = 0; i < 512; i++)
adamstark@88 578 {
adamstark@93 579 #ifdef USE_FFTW
adamstark@88 580 // calculate absolute value of result
adamstark@91 581 double absValue = sqrt (complexIn[i][0]*complexIn[i][0] + complexIn[i][1]*complexIn[i][1]);
adamstark@93 582 #endif
adamstark@88 583
adamstark@93 584 #ifdef USE_KISS_FFT
adamstark@93 585 // calculate absolute value of result
adamstark@93 586 double absValue = sqrt (fftIn[i].r * fftIn[i].r + fftIn[i].i * fftIn[i].i);
adamstark@93 587 #endif
adamstark@88 588 // divide by inverse lad to deal with scale bias towards small lags
adamstark@88 589 acf[i] = absValue / lag;
adamstark@88 590
adamstark@88 591 // this division by 1024 is technically unnecessary but it ensures the algorithm produces
adamstark@88 592 // exactly the same ACF output as the old time domain implementation. The time difference is
adamstark@88 593 // minimal so I decided to keep it
adamstark@88 594 acf[i] = acf[i] / 1024.;
adamstark@88 595
adamstark@88 596 lag = lag - 1.;
adamstark@88 597 }
adamstark@46 598 }
adamstark@46 599
adamstark@51 600 //=======================================================================
adamstark@100 601 double BTrack::calculateMeanOfVector (std::vector<double>& vector, int startIndex, int endIndex)
adamstark@46 602 {
adamstark@97 603 int length = endIndex - startIndex;
adamstark@100 604 double sum = std::accumulate (vector.begin() + startIndex, vector.begin() + endIndex, 0.0);
adamstark@47 605
adamstark@47 606 if (length > 0)
adamstark@97 607 return sum / static_cast<double> (length); // average and return
adamstark@47 608 else
adamstark@47 609 return 0;
adamstark@46 610 }
adamstark@46 611
adamstark@51 612 //=======================================================================
adamstark@100 613 void BTrack::normaliseVector (std::vector<double>& vector)
adamstark@46 614 {
adamstark@100 615 double sum = std::accumulate (vector.begin(), vector.end(), 0.0);
adamstark@46 616
adamstark@46 617 if (sum > 0)
adamstark@97 618 {
adamstark@100 619 for (int i = 0; i < vector.size(); i++)
adamstark@100 620 vector[i] = vector[i] / sum;
adamstark@97 621 }
adamstark@46 622 }
adamstark@46 623
adamstark@51 624 //=======================================================================
adamstark@100 625 void BTrack::updateCumulativeScore (double onsetDetectionFunctionSample)
adamstark@98 626 {
adamstark@100 627 int windowStart = onsetDFBufferSize - round (2. * beatPeriod);
adamstark@100 628 int windowEnd = onsetDFBufferSize - round (beatPeriod / 2.);
adamstark@100 629 int windowSize = windowEnd - windowStart + 1;
adamstark@46 630
adamstark@103 631 double logGaussianTransitionWeighting[windowSize];
adamstark@103 632 createLogGaussianTransitionWeighting (logGaussianTransitionWeighting, windowSize, beatPeriod);
adamstark@46 633
adamstark@46 634 // calculate new cumulative score value
adamstark@98 635 double maxValue = 0;
adamstark@46 636 int n = 0;
adamstark@100 637 for (int i = windowStart; i <= windowEnd; i++)
adamstark@46 638 {
adamstark@103 639 double weightedCumulativeScore = cumulativeScore[i] * logGaussianTransitionWeighting[n];
adamstark@46 640
adamstark@98 641 if (weightedCumulativeScore > maxValue)
adamstark@98 642 maxValue = weightedCumulativeScore;
adamstark@98 643
adamstark@46 644 n++;
adamstark@46 645 }
adamstark@46 646
adamstark@100 647 latestCumulativeScoreValue = ((1 - alpha) * onsetDetectionFunctionSample) + (alpha * maxValue);
adamstark@91 648 cumulativeScore.addSampleToEnd (latestCumulativeScoreValue);
adamstark@46 649 }
adamstark@46 650
adamstark@51 651 //=======================================================================
adamstark@57 652 void BTrack::predictBeat()
adamstark@46 653 {
adamstark@102 654 int beatExpectationWindowSize = (int) beatPeriod;
adamstark@102 655 double futureCumulativeScore[onsetDFBufferSize + beatExpectationWindowSize];
adamstark@102 656 double beatExpectationWindow[beatExpectationWindowSize];
adamstark@93 657
adamstark@102 658 // copy cumulativeScore to first part of futureCumulativeScore
adamstark@58 659 for (int i = 0;i < onsetDFBufferSize;i++)
adamstark@102 660 futureCumulativeScore[i] = cumulativeScore[i];
adamstark@102 661
adamstark@102 662 // Create a beat expectation window for predicting future beats from the "future" of the cumulative score.
adamstark@102 663 // We are making this beat prediction at the midpoint between beats, and so we make a Gaussian
adamstark@102 664 // weighting centred on the most likely beat position (half a beat period into the future)
adamstark@102 665 // This is W2 in Adam Stark's PhD thesis, equation 3.6, page 62
adamstark@102 666
adamstark@102 667 double v = 1;
adamstark@102 668 for (int i = 0; i < beatExpectationWindowSize; i++)
adamstark@46 669 {
adamstark@102 670 beatExpectationWindow[i] = exp((-1 * pow ((v - (beatPeriod / 2)), 2)) / (2 * pow (beatPeriod / 2, 2)));
adamstark@46 671 v++;
adamstark@46 672 }
adamstark@46 673
adamstark@102 674 // Create window for "synthesizing" the cumulative score into the future
adamstark@102 675 // It is a log-Gaussian transition weighting running from from 2 beat periods
adamstark@102 676 // in the past to half a beat period in the past. It favours the time exactly
adamstark@102 677 // one beat period in the past
adamstark@102 678 // This is W1 in Adam Stark's PhD thesis, equation 3.2, page 60
adamstark@102 679
adamstark@103 680
adamstark@102 681 int startIndex = onsetDFBufferSize - round (2 * beatPeriod);
adamstark@102 682 int endIndex = onsetDFBufferSize - round (beatPeriod / 2);
adamstark@102 683 int pastWindowSize = endIndex - startIndex + 1;
adamstark@103 684
adamstark@102 685 double logGaussianTransitionWeighting[pastWindowSize];
adamstark@103 686 createLogGaussianTransitionWeighting (logGaussianTransitionWeighting, pastWindowSize, beatPeriod);
adamstark@46 687
adamstark@102 688 // Calculate the future cumulative score, using the log Gaussian transition weighting
adamstark@102 689
adamstark@102 690 for (int i = onsetDFBufferSize; i < (onsetDFBufferSize + beatExpectationWindowSize); i++)
adamstark@46 691 {
adamstark@102 692 startIndex = i - round (2 * beatPeriod);
adamstark@102 693 endIndex = i - round (beatPeriod / 2);
adamstark@46 694
adamstark@102 695 double maxValue = 0;
adamstark@102 696 int n = 0;
adamstark@102 697 for (int k = startIndex; k <= endIndex; k++)
adamstark@46 698 {
adamstark@102 699 double weightedCumulativeScore = futureCumulativeScore[k] * logGaussianTransitionWeighting[n];
adamstark@46 700
adamstark@102 701 if (weightedCumulativeScore > maxValue)
adamstark@102 702 maxValue = weightedCumulativeScore;
adamstark@102 703
adamstark@46 704 n++;
adamstark@46 705 }
adamstark@46 706
adamstark@102 707 futureCumulativeScore[i] = maxValue;
adamstark@46 708 }
adamstark@46 709
adamstark@102 710 // Predict the next beat, finding the maximum point of the future cumulative score
adamstark@102 711 // over the next beat, after being weighted by the beat expectation window
adamstark@102 712
adamstark@102 713 double maxValue = 0;
adamstark@102 714 int n = 0;
adamstark@46 715
adamstark@102 716 for (int i = onsetDFBufferSize; i < (onsetDFBufferSize + beatExpectationWindowSize); i++)
adamstark@46 717 {
adamstark@102 718 double weightedCumulativeScore = futureCumulativeScore[i] * beatExpectationWindow[n];
adamstark@46 719
adamstark@102 720 if (weightedCumulativeScore > maxValue)
adamstark@46 721 {
adamstark@102 722 maxValue = weightedCumulativeScore;
adamstark@58 723 beatCounter = n;
adamstark@46 724 }
adamstark@46 725
adamstark@46 726 n++;
adamstark@46 727 }
adamstark@46 728
adamstark@46 729 // set next prediction time
adamstark@91 730 m0 = beatCounter + round (beatPeriod / 2);
adamstark@97 731 }
adamstark@103 732
adamstark@103 733 //=======================================================================
adamstark@103 734 void BTrack::createLogGaussianTransitionWeighting (double* weightingArray, int numSamples, double beatPeriod)
adamstark@103 735 {
adamstark@103 736 double v = -2. * beatPeriod;
adamstark@103 737
adamstark@103 738 for (int i = 0; i < numSamples; i++)
adamstark@103 739 {
adamstark@103 740 double a = tightness * log (-v / beatPeriod);
adamstark@103 741 weightingArray[i] = exp ((-1. * a * a) / 2.);
adamstark@103 742 v++;
adamstark@103 743 }
adamstark@103 744 }