annotate src/BTrack.cpp @ 101:1fcc06afd9cb

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