annotate dsp/tempotracking/TempoTrack.cpp @ 414:7e8d1f26b098

Fix compiler warnings with -Wall -Wextra
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 28 Sep 2015 12:33:17 +0100
parents c5e1b25d5177
children 7e52c034cf62
rev   line source
c@410 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@410 2
c@410 3 /*
c@410 4 QM DSP Library
c@410 5
c@410 6 Centre for Digital Music, Queen Mary, University of London.
c@410 7 This file copyright 2005-2006 Christian Landone.and Matthew Davies.
c@309 8
c@309 9 This program is free software; you can redistribute it and/or
c@309 10 modify it under the terms of the GNU General Public License as
c@309 11 published by the Free Software Foundation; either version 2 of the
c@309 12 License, or (at your option) any later version. See the file
c@410 13 COPYING included with this distribution for more information.
c@410 14 */
c@410 15
c@410 16 #include "TempoTrack.h"
c@410 17
c@410 18 #include "maths/MathAliases.h"
c@410 19 #include "maths/MathUtilities.h"
c@410 20
c@410 21 #include <iostream>
c@410 22
c@410 23 #include <cassert>
c@410 24
c@410 25 //#define DEBUG_TEMPO_TRACK 1
c@410 26
c@410 27
c@410 28 #define RAY43VAL
c@410 29
c@410 30 //////////////////////////////////////////////////////////////////////
c@410 31 // Construction/Destruction
c@410 32 //////////////////////////////////////////////////////////////////////
c@410 33
c@410 34 TempoTrack::TempoTrack( TTParams Params )
c@410 35 {
c@410 36 m_tempoScratch = NULL;
c@410 37 m_rawDFFrame = NULL;
c@410 38 m_smoothDFFrame = NULL;
c@410 39 m_frameACF = NULL;
c@410 40 m_smoothRCF = NULL;
c@410 41
c@410 42 m_dataLength = 0;
c@410 43 m_winLength = 0;
c@410 44 m_lagLength = 0;
c@410 45
c@410 46 m_rayparam = 0;
c@410 47 m_sigma = 0;
c@410 48 m_DFWVNnorm = 0;
c@410 49
c@410 50 initialise( Params );
c@410 51 }
c@410 52
c@410 53 TempoTrack::~TempoTrack()
c@410 54 {
c@410 55 deInitialise();
c@410 56 }
c@410 57
c@410 58 void TempoTrack::initialise( TTParams Params )
c@410 59 {
c@410 60 m_winLength = Params.winLength;
c@410 61 m_lagLength = Params.lagLength;
c@410 62
c@410 63 m_rayparam = 43.0;
c@410 64 m_sigma = sqrt(3.9017);
c@410 65 m_DFWVNnorm = exp( ( log( 2.0 ) / m_rayparam ) * ( m_winLength + 2 ) );
c@410 66
c@410 67 m_rawDFFrame = new double[ m_winLength ];
c@410 68 m_smoothDFFrame = new double[ m_winLength ];
c@410 69 m_frameACF = new double[ m_winLength ];
c@410 70 m_tempoScratch = new double[ m_lagLength ];
c@410 71 m_smoothRCF = new double[ m_lagLength ];
c@410 72
c@410 73 m_DFFramer.configure( m_winLength, m_lagLength );
c@410 74
c@410 75 m_DFPParams.length = m_winLength;
c@410 76 m_DFPParams.AlphaNormParam = Params.alpha;
c@410 77 m_DFPParams.LPOrd = Params.LPOrd;
c@410 78 m_DFPParams.LPACoeffs = Params.LPACoeffs;
c@410 79 m_DFPParams.LPBCoeffs = Params.LPBCoeffs;
c@410 80 m_DFPParams.winPre = Params.WinT.pre;
c@410 81 m_DFPParams.winPost = Params.WinT.post;
c@410 82 m_DFPParams.isMedianPositive = true;
c@410 83
c@410 84 m_DFConditioning = new DFProcess( m_DFPParams );
c@410 85
c@410 86
c@410 87 // these are parameters for smoothing m_tempoScratch
c@410 88 m_RCFPParams.length = m_lagLength;
c@410 89 m_RCFPParams.AlphaNormParam = Params.alpha;
c@410 90 m_RCFPParams.LPOrd = Params.LPOrd;
c@410 91 m_RCFPParams.LPACoeffs = Params.LPACoeffs;
c@410 92 m_RCFPParams.LPBCoeffs = Params.LPBCoeffs;
c@410 93 m_RCFPParams.winPre = Params.WinT.pre;
c@410 94 m_RCFPParams.winPost = Params.WinT.post;
c@410 95 m_RCFPParams.isMedianPositive = true;
c@410 96
c@410 97 m_RCFConditioning = new DFProcess( m_RCFPParams );
c@410 98
c@410 99 }
c@410 100
c@410 101 void TempoTrack::deInitialise()
c@410 102 {
c@410 103 delete [] m_rawDFFrame;
c@410 104
c@410 105 delete [] m_smoothDFFrame;
c@410 106
c@410 107 delete [] m_smoothRCF;
c@410 108
c@410 109 delete [] m_frameACF;
c@410 110
c@410 111 delete [] m_tempoScratch;
c@410 112
c@410 113 delete m_DFConditioning;
c@410 114
c@410 115 delete m_RCFConditioning;
c@410 116
c@410 117 }
c@410 118
c@414 119 void TempoTrack::createCombFilter(double* Filter, int winLength, int /* TSig */, double beatLag)
c@410 120 {
c@414 121 int i;
c@410 122
c@410 123 if( beatLag == 0 )
c@410 124 {
c@410 125 for( i = 0; i < winLength; i++ )
c@410 126 {
c@410 127 Filter[ i ] = ( ( i + 1 ) / pow( m_rayparam, 2.0) ) * exp( ( -pow(( i + 1 ),2.0 ) / ( 2.0 * pow( m_rayparam, 2.0))));
c@410 128 }
c@410 129 }
c@410 130 else
c@410 131 {
c@410 132 m_sigma = beatLag/4;
c@410 133 for( i = 0; i < winLength; i++ )
c@410 134 {
c@410 135 double dlag = (double)(i+1) - beatLag;
c@410 136 Filter[ i ] = exp(-0.5 * pow(( dlag / m_sigma), 2.0) ) / (sqrt( 2 * PI) * m_sigma);
c@410 137 }
c@410 138 }
c@410 139 }
c@410 140
c@410 141 double TempoTrack::tempoMM(double* ACF, double* weight, int tsig)
c@410 142 {
c@410 143
c@410 144 double period = 0;
c@410 145 double maxValRCF = 0.0;
c@414 146 int maxIndexRCF = 0;
c@410 147
c@410 148 double* pdPeaks;
c@410 149
c@414 150 int maxIndexTemp;
c@414 151 double maxValTemp;
c@414 152 int count;
c@410 153
c@414 154 int numelem,i,j;
c@410 155 int a, b;
c@410 156
c@410 157 for( i = 0; i < m_lagLength; i++ )
c@410 158 m_tempoScratch[ i ] = 0.0;
c@410 159
c@410 160 if( tsig == 0 )
c@410 161 {
c@410 162 //if time sig is unknown, use metrically unbiased version of Filterbank
c@410 163 numelem = 4;
c@410 164 }
c@410 165 else
c@410 166 {
c@410 167 numelem = tsig;
c@410 168 }
c@410 169
c@410 170 #ifdef DEBUG_TEMPO_TRACK
c@410 171 std::cerr << "tempoMM: m_winLength = " << m_winLength << ", m_lagLength = " << m_lagLength << ", numelem = " << numelem << std::endl;
c@410 172 #endif
c@410 173
c@410 174 for(i=1;i<m_lagLength-1;i++)
c@410 175 {
c@410 176 //first and last output values are left intentionally as zero
c@410 177 for (a=1;a<=numelem;a++)
c@410 178 {
c@410 179 for(b=(1-a);b<a;b++)
c@410 180 {
c@410 181 if( tsig == 0 )
c@410 182 {
c@410 183 m_tempoScratch[i] += ACF[a*(i+1)+b-1] * (1.0 / (2.0 * (double)a-1)) * weight[i];
c@410 184 }
c@410 185 else
c@410 186 {
c@410 187 m_tempoScratch[i] += ACF[a*(i+1)+b-1] * 1 * weight[i];
c@410 188 }
c@410 189 }
c@410 190 }
c@410 191 }
c@410 192
c@410 193
c@410 194 //////////////////////////////////////////////////
c@410 195 // MODIFIED BEAT PERIOD EXTRACTION //////////////
c@410 196 /////////////////////////////////////////////////
c@410 197
c@410 198 // find smoothed version of RCF ( as applied to Detection Function)
c@410 199 m_RCFConditioning->process( m_tempoScratch, m_smoothRCF);
c@410 200
c@410 201 if (tsig != 0) // i.e. in context dependent state
c@410 202 {
c@410 203 // NOW FIND MAX INDEX OF ACFOUT
c@410 204 for( i = 0; i < m_lagLength; i++)
c@410 205 {
c@410 206 if( m_tempoScratch[ i ] > maxValRCF)
c@410 207 {
c@410 208 maxValRCF = m_tempoScratch[ i ];
c@410 209 maxIndexRCF = i;
c@410 210 }
c@410 211 }
c@410 212 }
c@410 213 else // using rayleigh weighting
c@410 214 {
c@410 215 vector <vector<double> > rcfMat;
c@410 216
c@410 217 double sumRcf = 0.;
c@410 218
c@410 219 double maxVal = 0.;
c@410 220 // now find the two values which minimise rcfMat
c@410 221 double minVal = 0.;
c@410 222 int p_i = 1; // periodicity for row i;
c@410 223 int p_j = 1; //periodicity for column j;
c@410 224
c@410 225
c@410 226 for ( i=0; i<m_lagLength; i++)
c@410 227 {
c@410 228 m_tempoScratch[i] =m_smoothRCF[i];
c@410 229 }
c@410 230
c@410 231 // normalise m_tempoScratch so that it sums to zero.
c@410 232 for ( i=0; i<m_lagLength; i++)
c@410 233 {
c@410 234 sumRcf += m_tempoScratch[i];
c@410 235 }
c@410 236
c@410 237 for( i=0; i<m_lagLength; i++)
c@410 238 {
c@410 239 m_tempoScratch[i] /= sumRcf;
c@410 240 }
c@410 241
c@410 242 // create a matrix to store m_tempoScratchValues modified by log2 ratio
c@410 243 for ( i=0; i<m_lagLength; i++)
c@410 244 {
c@410 245 rcfMat.push_back ( vector<double>() ); // adds a new row...
c@410 246 }
c@410 247
c@410 248 for (i=0; i<m_lagLength; i++)
c@410 249 {
c@410 250 for (j=0; j<m_lagLength; j++)
c@410 251 {
c@410 252 rcfMat[i].push_back (0.);
c@410 253 }
c@410 254 }
c@410 255
c@410 256 // the 'i' and 'j' indices deliberately start from '1' and not '0'
c@410 257 for ( i=1; i<m_lagLength; i++)
c@410 258 {
c@410 259 for (j=1; j<m_lagLength; j++)
c@410 260 {
c@410 261 double log2PeriodRatio = log( static_cast<double>(i)/static_cast<double>(j) ) / log(2.0);
c@410 262 rcfMat[i][j] = ( abs(1.0-abs(log2PeriodRatio)) );
c@410 263 rcfMat[i][j] += ( 0.01*( 1./(m_tempoScratch[i]+m_tempoScratch[j]) ) );
c@410 264 }
c@410 265 }
c@410 266
c@410 267 // set diagonal equal to maximum value in rcfMat
c@410 268 // we don't want to pick one strong middle peak - we need a combination of two peaks.
c@410 269
c@410 270 for ( i=1; i<m_lagLength; i++)
c@410 271 {
c@410 272 for (j=1; j<m_lagLength; j++)
c@410 273 {
c@410 274 if (rcfMat[i][j] > maxVal)
c@410 275 {
c@410 276 maxVal = rcfMat[i][j];
c@410 277 }
c@410 278 }
c@410 279 }
c@410 280
c@410 281 for ( i=1; i<m_lagLength; i++)
c@410 282 {
c@410 283 rcfMat[i][i] = maxVal;
c@410 284 }
c@410 285
c@410 286 // now find the row and column number which minimise rcfMat
c@410 287 minVal = maxVal;
c@410 288
c@410 289 for ( i=1; i<m_lagLength; i++)
c@410 290 {
c@410 291 for ( j=1; j<m_lagLength; j++)
c@410 292 {
c@410 293 if (rcfMat[i][j] < minVal)
c@410 294 {
c@410 295 minVal = rcfMat[i][j];
c@410 296 p_i = i;
c@410 297 p_j = j;
c@410 298 }
c@410 299 }
c@410 300 }
c@410 301
c@410 302
c@410 303 // initially choose p_j (arbitrary) - saves on an else statement
c@410 304 int beatPeriod = p_j;
c@410 305 if (m_tempoScratch[p_i] > m_tempoScratch[p_j])
c@410 306 {
c@410 307 beatPeriod = p_i;
c@410 308 }
c@410 309
c@410 310 // now write the output
c@410 311 maxIndexRCF = static_cast<int>(beatPeriod);
c@410 312 }
c@410 313
c@410 314
c@410 315 double locked = 5168.f / maxIndexRCF;
c@410 316 if (locked >= 30 && locked <= 180) {
c@410 317 m_lockedTempo = locked;
c@410 318 }
c@410 319
c@410 320 #ifdef DEBUG_TEMPO_TRACK
c@410 321 std::cerr << "tempoMM: locked tempo = " << m_lockedTempo << std::endl;
c@410 322 #endif
c@410 323
c@410 324 if( tsig == 0 )
c@410 325 tsig = 4;
c@410 326
c@410 327
c@410 328 #ifdef DEBUG_TEMPO_TRACK
c@410 329 std::cerr << "tempoMM: maxIndexRCF = " << maxIndexRCF << std::endl;
c@410 330 #endif
c@410 331
c@410 332 if( tsig == 4 )
c@410 333 {
c@410 334 #ifdef DEBUG_TEMPO_TRACK
c@410 335 std::cerr << "tsig == 4" << std::endl;
c@410 336 #endif
c@410 337
c@410 338 pdPeaks = new double[ 4 ];
c@410 339 for( i = 0; i < 4; i++ ){ pdPeaks[ i ] = 0.0;}
c@410 340
c@410 341 pdPeaks[ 0 ] = ( double )maxIndexRCF + 1;
c@410 342
c@410 343 maxIndexTemp = 0;
c@410 344 maxValTemp = 0.0;
c@410 345 count = 0;
c@410 346
c@410 347 for( i = (2 * maxIndexRCF + 1) - 1; i < (2 * maxIndexRCF + 1) + 2; i++ )
c@410 348 {
c@410 349 if( ACF[ i ] > maxValTemp )
c@410 350 {
c@410 351 maxValTemp = ACF[ i ];
c@410 352 maxIndexTemp = count;
c@410 353 }
c@410 354 count++;
c@410 355 }
c@410 356 pdPeaks[ 1 ] = (double)( maxIndexTemp + 1 + ( (2 * maxIndexRCF + 1 ) - 2 ) + 1 )/2;
c@410 357
c@410 358 maxIndexTemp = 0;
c@410 359 maxValTemp = 0.0;
c@410 360 count = 0;
c@410 361
c@410 362 for( i = (3 * maxIndexRCF + 2 ) - 2; i < (3 * maxIndexRCF + 2 ) + 3; i++ )
c@410 363 {
c@410 364 if( ACF[ i ] > maxValTemp )
c@410 365 {
c@410 366 maxValTemp = ACF[ i ];
c@410 367 maxIndexTemp = count;
c@410 368 }
c@410 369 count++;
c@410 370 }
c@410 371 pdPeaks[ 2 ] = (double)( maxIndexTemp + 1 + ( (3 * maxIndexRCF + 2) - 4 ) + 1 )/3;
c@410 372
c@410 373 maxIndexTemp = 0;
c@410 374 maxValTemp = 0.0;
c@410 375 count = 0;
c@410 376
c@410 377 for( i = ( 4 * maxIndexRCF + 3) - 3; i < ( 4 * maxIndexRCF + 3) + 4; i++ )
c@410 378 {
c@410 379 if( ACF[ i ] > maxValTemp )
c@410 380 {
c@410 381 maxValTemp = ACF[ i ];
c@410 382 maxIndexTemp = count;
c@410 383 }
c@410 384 count++;
c@410 385 }
c@410 386 pdPeaks[ 3 ] = (double)( maxIndexTemp + 1 + ( (4 * maxIndexRCF + 3) - 9 ) + 1 )/4 ;
c@410 387
c@410 388
c@410 389 period = MathUtilities::mean( pdPeaks, 4 );
c@410 390 }
c@410 391 else
c@410 392 {
c@410 393 #ifdef DEBUG_TEMPO_TRACK
c@410 394 std::cerr << "tsig != 4" << std::endl;
c@410 395 #endif
c@410 396
c@410 397 pdPeaks = new double[ 3 ];
c@410 398 for( i = 0; i < 3; i++ ){ pdPeaks[ i ] = 0.0;}
c@410 399
c@410 400 pdPeaks[ 0 ] = ( double )maxIndexRCF + 1;
c@410 401
c@410 402 maxIndexTemp = 0;
c@410 403 maxValTemp = 0.0;
c@410 404 count = 0;
c@410 405
c@410 406 for( i = (2 * maxIndexRCF + 1) - 1; i < (2 * maxIndexRCF + 1) + 2; i++ )
c@410 407 {
c@410 408 if( ACF[ i ] > maxValTemp )
c@410 409 {
c@410 410 maxValTemp = ACF[ i ];
c@410 411 maxIndexTemp = count;
c@410 412 }
c@410 413 count++;
c@410 414 }
c@410 415 pdPeaks[ 1 ] = (double)( maxIndexTemp + 1 + ( (2 * maxIndexRCF + 1 ) - 2 ) + 1 )/2;
c@410 416
c@410 417 maxIndexTemp = 0;
c@410 418 maxValTemp = 0.0;
c@410 419 count = 0;
c@410 420
c@410 421 for( i = (3 * maxIndexRCF + 2 ) - 2; i < (3 * maxIndexRCF + 2 ) + 3; i++ )
c@410 422 {
c@410 423 if( ACF[ i ] > maxValTemp )
c@410 424 {
c@410 425 maxValTemp = ACF[ i ];
c@410 426 maxIndexTemp = count;
c@410 427 }
c@410 428 count++;
c@410 429 }
c@410 430 pdPeaks[ 2 ] = (double)( maxIndexTemp + 1 + ( (3 * maxIndexRCF + 2) - 4 ) + 1 )/3;
c@410 431
c@410 432
c@410 433 period = MathUtilities::mean( pdPeaks, 3 );
c@410 434 }
c@410 435
c@410 436 delete [] pdPeaks;
c@410 437
c@410 438 return period;
c@410 439 }
c@410 440
c@410 441 void TempoTrack::stepDetect( double* periodP, double* periodG, int currentIdx, int* flag )
c@410 442 {
c@410 443 double stepthresh = 1 * 3.9017;
c@410 444
c@410 445 if( *flag )
c@410 446 {
c@410 447 if(abs(periodG[ currentIdx ] - periodP[ currentIdx ]) > stepthresh)
c@410 448 {
c@410 449 // do nuffin'
c@410 450 }
c@410 451 }
c@410 452 else
c@410 453 {
c@410 454 if(fabs(periodG[ currentIdx ]-periodP[ currentIdx ]) > stepthresh)
c@410 455 {
c@410 456 *flag = 3;
c@410 457 }
c@410 458 }
c@410 459 }
c@410 460
c@410 461 void TempoTrack::constDetect( double* periodP, int currentIdx, int* flag )
c@410 462 {
c@410 463 double constthresh = 2 * 3.9017;
c@410 464
c@410 465 if( fabs( 2 * periodP[ currentIdx ] - periodP[ currentIdx - 1] - periodP[ currentIdx - 2] ) < constthresh)
c@410 466 {
c@410 467 *flag = 1;
c@410 468 }
c@410 469 else
c@410 470 {
c@410 471 *flag = 0;
c@410 472 }
c@410 473 }
c@410 474
c@414 475 int TempoTrack::findMeter(double *ACF, int len, double period)
c@410 476 {
c@410 477 int i;
c@410 478 int p = (int)MathUtilities::round( period );
c@410 479 int tsig;
c@410 480
c@410 481 double Energy_3 = 0.0;
c@410 482 double Energy_4 = 0.0;
c@410 483
c@410 484 double temp3A = 0.0;
c@410 485 double temp3B = 0.0;
c@410 486 double temp4A = 0.0;
c@410 487 double temp4B = 0.0;
c@410 488
c@410 489 double* dbf = new double[ len ]; int t = 0;
c@414 490 for( int u = 0; u < len; u++ ){ dbf[ u ] = 0.0; }
c@410 491
c@410 492 if( (double)len < 6 * p + 2 )
c@410 493 {
c@410 494 for( i = ( 3 * p - 2 ); i < ( 3 * p + 2 ) + 1; i++ )
c@410 495 {
c@410 496 temp3A += ACF[ i ];
c@410 497 dbf[ t++ ] = ACF[ i ];
c@410 498 }
c@410 499
c@410 500 for( i = ( 4 * p - 2 ); i < ( 4 * p + 2 ) + 1; i++ )
c@410 501 {
c@410 502 temp4A += ACF[ i ];
c@410 503 }
c@410 504
c@410 505 Energy_3 = temp3A;
c@410 506 Energy_4 = temp4A;
c@410 507 }
c@410 508 else
c@410 509 {
c@410 510 for( i = ( 3 * p - 2 ); i < ( 3 * p + 2 ) + 1; i++ )
c@410 511 {
c@410 512 temp3A += ACF[ i ];
c@410 513 }
c@410 514
c@410 515 for( i = ( 4 * p - 2 ); i < ( 4 * p + 2 ) + 1; i++ )
c@410 516 {
c@410 517 temp4A += ACF[ i ];
c@410 518 }
c@410 519
c@410 520 for( i = ( 6 * p - 2 ); i < ( 6 * p + 2 ) + 1; i++ )
c@410 521 {
c@410 522 temp3B += ACF[ i ];
c@410 523 }
c@410 524
c@410 525 for( i = ( 2 * p - 2 ); i < ( 2 * p + 2 ) + 1; i++ )
c@410 526 {
c@410 527 temp4B += ACF[ i ];
c@410 528 }
c@410 529
c@410 530 Energy_3 = temp3A + temp3B;
c@410 531 Energy_4 = temp4A + temp4B;
c@410 532 }
c@410 533
c@410 534 if (Energy_3 > Energy_4)
c@410 535 {
c@410 536 tsig = 3;
c@410 537 }
c@410 538 else
c@410 539 {
c@410 540 tsig = 4;
c@410 541 }
c@410 542
c@410 543
c@410 544 return tsig;
c@410 545 }
c@410 546
c@414 547 void TempoTrack::createPhaseExtractor(double *Filter, int /* winLength */, double period, int fsp, int lastBeat)
c@410 548 {
c@410 549 int p = (int)MathUtilities::round( period );
c@410 550 int predictedOffset = 0;
c@410 551
c@410 552 #ifdef DEBUG_TEMPO_TRACK
c@410 553 std::cerr << "TempoTrack::createPhaseExtractor: period = " << period << ", p = " << p << std::endl;
c@410 554 #endif
c@410 555
c@410 556 if (p > 10000) {
c@410 557 std::cerr << "TempoTrack::createPhaseExtractor: WARNING! Highly implausible period value " << p << "!" << std::endl;
c@410 558 period = 5168 / 120;
c@410 559 }
c@410 560
c@410 561 double* phaseScratch = new double[ p*2 + 2 ];
c@410 562 for (int i = 0; i < p*2 + 2; ++i) phaseScratch[i] = 0.0;
c@410 563
c@410 564
c@410 565 if( lastBeat != 0 )
c@410 566 {
c@410 567 lastBeat = (int)MathUtilities::round((double)lastBeat );///(double)winLength);
c@410 568
c@410 569 predictedOffset = lastBeat + p - fsp;
c@410 570
c@410 571 if (predictedOffset < 0)
c@410 572 {
c@410 573 lastBeat = 0;
c@410 574 }
c@410 575 }
c@410 576
c@410 577 if( lastBeat != 0 )
c@410 578 {
c@410 579 int mu = p;
c@410 580 double sigma = (double)p/8;
c@410 581 double PhaseMin = 0.0;
c@410 582 double PhaseMax = 0.0;
c@414 583 int scratchLength = p*2;
c@410 584 double temp = 0.0;
c@410 585
c@410 586 for( int i = 0; i < scratchLength; i++ )
c@410 587 {
c@410 588 phaseScratch[ i ] = exp( -0.5 * pow( ( i - mu ) / sigma, 2 ) ) / ( sqrt( 2*PI ) *sigma );
c@410 589 }
c@410 590
c@410 591 MathUtilities::getFrameMinMax( phaseScratch, scratchLength, &PhaseMin, &PhaseMax );
c@410 592
c@410 593 for(int i = 0; i < scratchLength; i ++)
c@410 594 {
c@410 595 temp = phaseScratch[ i ];
c@410 596 phaseScratch[ i ] = (temp - PhaseMin)/PhaseMax;
c@410 597 }
c@410 598
c@410 599 #ifdef DEBUG_TEMPO_TRACK
c@410 600 std::cerr << "predictedOffset = " << predictedOffset << std::endl;
c@410 601 #endif
c@410 602
c@414 603 int index = 0;
c@410 604 for (int i = p - ( predictedOffset - 1); i < p + ( p - predictedOffset) + 1; i++)
c@410 605 {
c@410 606 #ifdef DEBUG_TEMPO_TRACK
c@410 607 std::cerr << "assigning to filter index " << index << " (size = " << p*2 << ")" << " value " << phaseScratch[i] << " from scratch index " << i << std::endl;
c@410 608 #endif
c@410 609 Filter[ index++ ] = phaseScratch[ i ];
c@410 610 }
c@410 611 }
c@410 612 else
c@410 613 {
c@410 614 for( int i = 0; i < p; i ++)
c@410 615 {
c@410 616 Filter[ i ] = 1;
c@410 617 }
c@410 618 }
c@410 619
c@410 620 delete [] phaseScratch;
c@410 621 }
c@410 622
c@414 623 int TempoTrack::phaseMM(double *DF, double *weighting, int winLength, double period)
c@410 624 {
c@410 625 int alignment = 0;
c@410 626 int p = (int)MathUtilities::round( period );
c@410 627
c@410 628 double temp = 0.0;
c@410 629
c@410 630 double* y = new double[ winLength ];
c@410 631 double* align = new double[ p ];
c@410 632
c@410 633 for( int i = 0; i < winLength; i++ )
c@410 634 {
c@410 635 y[ i ] = (double)( -i + winLength )/(double)winLength;
c@410 636 y[ i ] = pow(y [i ],2.0); // raise to power 2.
c@410 637 }
c@410 638
c@410 639 for( int o = 0; o < p; o++ )
c@410 640 {
c@410 641 temp = 0.0;
c@410 642 for(int i = 1 + (o - 1); i< winLength; i += (p + 1))
c@410 643 {
c@410 644 temp = temp + DF[ i ] * y[ i ];
c@410 645 }
c@410 646 align[ o ] = temp * weighting[ o ];
c@410 647 }
c@410 648
c@410 649
c@410 650 double valTemp = 0.0;
c@410 651 for(int i = 0; i < p; i++)
c@410 652 {
c@410 653 if( align[ i ] > valTemp )
c@410 654 {
c@410 655 valTemp = align[ i ];
c@410 656 alignment = i;
c@410 657 }
c@410 658 }
c@410 659
c@410 660 delete [] y;
c@410 661 delete [] align;
c@410 662
c@410 663 return alignment;
c@410 664 }
c@410 665
c@414 666 int TempoTrack::beatPredict(int FSP0, double alignment, double period, int step )
c@410 667 {
c@410 668 int beat = 0;
c@410 669
c@410 670 int p = (int)MathUtilities::round( period );
c@410 671 int align = (int)MathUtilities::round( alignment );
c@410 672 int FSP = (int)MathUtilities::round( FSP0 );
c@410 673
c@410 674 int FEP = FSP + ( step );
c@410 675
c@410 676 beat = FSP + align;
c@410 677
c@410 678 m_beats.push_back( beat );
c@410 679
c@410 680 while( beat + p < FEP )
c@410 681 {
c@410 682 beat += p;
c@410 683
c@410 684 m_beats.push_back( beat );
c@410 685 }
c@410 686
c@410 687 return beat;
c@410 688 }
c@410 689
c@410 690
c@410 691
c@410 692 vector<int> TempoTrack::process( vector <double> DF,
c@410 693 vector <double> *tempoReturn )
c@410 694 {
c@410 695 m_dataLength = DF.size();
c@410 696
c@410 697 m_lockedTempo = 0.0;
c@410 698
c@410 699 double period = 0.0;
c@410 700 int stepFlag = 0;
c@410 701 int constFlag = 0;
c@410 702 int FSP = 0;
c@410 703 int tsig = 0;
c@410 704 int lastBeat = 0;
c@410 705
c@410 706 vector <double> causalDF;
c@410 707
c@410 708 causalDF = DF;
c@410 709
c@410 710 //Prepare Causal Extension DFData
c@414 711 // int DFCLength = m_dataLength + m_winLength;
c@410 712
c@414 713 for( int j = 0; j < m_winLength; j++ )
c@410 714 {
c@410 715 causalDF.push_back( 0 );
c@410 716 }
c@410 717
c@410 718
c@410 719 double* RW = new double[ m_lagLength ];
c@414 720 for (int clear = 0; clear < m_lagLength; clear++){ RW[ clear ] = 0.0;}
c@410 721
c@410 722 double* GW = new double[ m_lagLength ];
c@414 723 for (int clear = 0; clear < m_lagLength; clear++){ GW[ clear ] = 0.0;}
c@410 724
c@410 725 double* PW = new double[ m_lagLength ];
c@414 726 for(int clear = 0; clear < m_lagLength; clear++){ PW[ clear ] = 0.0;}
c@410 727
c@410 728 m_DFFramer.setSource( &causalDF[0], m_dataLength );
c@410 729
c@414 730 int TTFrames = m_DFFramer.getMaxNoFrames();
c@410 731
c@410 732 #ifdef DEBUG_TEMPO_TRACK
c@410 733 std::cerr << "TTFrames = " << TTFrames << std::endl;
c@410 734 #endif
c@410 735
c@410 736 double* periodP = new double[ TTFrames ];
c@414 737 for(int clear = 0; clear < TTFrames; clear++){ periodP[ clear ] = 0.0;}
c@410 738
c@410 739 double* periodG = new double[ TTFrames ];
c@414 740 for(int clear = 0; clear < TTFrames; clear++){ periodG[ clear ] = 0.0;}
c@410 741
c@410 742 double* alignment = new double[ TTFrames ];
c@414 743 for(int clear = 0; clear < TTFrames; clear++){ alignment[ clear ] = 0.0;}
c@410 744
c@410 745 m_beats.clear();
c@410 746
c@410 747 createCombFilter( RW, m_lagLength, 0, 0 );
c@410 748
c@410 749 int TTLoopIndex = 0;
c@410 750
c@414 751 for( int i = 0; i < TTFrames; i++ )
c@410 752 {
c@410 753 m_DFFramer.getFrame( m_rawDFFrame );
c@410 754
c@410 755 m_DFConditioning->process( m_rawDFFrame, m_smoothDFFrame );
c@410 756
c@410 757 m_correlator.doAutoUnBiased( m_smoothDFFrame, m_frameACF, m_winLength );
c@410 758
c@410 759 periodP[ TTLoopIndex ] = tempoMM( m_frameACF, RW, 0 );
c@410 760
c@410 761 if( GW[ 0 ] != 0 )
c@410 762 {
c@410 763 periodG[ TTLoopIndex ] = tempoMM( m_frameACF, GW, tsig );
c@410 764 }
c@410 765 else
c@410 766 {
c@410 767 periodG[ TTLoopIndex ] = 0.0;
c@410 768 }
c@410 769
c@410 770 stepDetect( periodP, periodG, TTLoopIndex, &stepFlag );
c@410 771
c@410 772 if( stepFlag == 1)
c@410 773 {
c@410 774 constDetect( periodP, TTLoopIndex, &constFlag );
c@410 775 stepFlag = 0;
c@410 776 }
c@410 777 else
c@410 778 {
c@410 779 stepFlag -= 1;
c@410 780 }
c@410 781
c@410 782 if( stepFlag < 0 )
c@410 783 {
c@410 784 stepFlag = 0;
c@410 785 }
c@410 786
c@410 787 if( constFlag != 0)
c@410 788 {
c@410 789 tsig = findMeter( m_frameACF, m_winLength, periodP[ TTLoopIndex ] );
c@410 790
c@410 791 createCombFilter( GW, m_lagLength, tsig, periodP[ TTLoopIndex ] );
c@410 792
c@410 793 periodG[ TTLoopIndex ] = tempoMM( m_frameACF, GW, tsig );
c@410 794
c@410 795 period = periodG[ TTLoopIndex ];
c@410 796
c@410 797 #ifdef DEBUG_TEMPO_TRACK
c@410 798 std::cerr << "TempoTrack::process: constFlag == " << constFlag << ", TTLoopIndex = " << TTLoopIndex << ", period from periodG = " << period << std::endl;
c@410 799 #endif
c@410 800
c@410 801 createPhaseExtractor( PW, m_winLength, period, FSP, 0 );
c@410 802
c@410 803 constFlag = 0;
c@410 804
c@410 805 }
c@410 806 else
c@410 807 {
c@410 808 if( GW[ 0 ] != 0 )
c@410 809 {
c@410 810 period = periodG[ TTLoopIndex ];
c@410 811
c@410 812 #ifdef DEBUG_TEMPO_TRACK
c@410 813 std::cerr << "TempoTrack::process: GW[0] == " << GW[0] << ", TTLoopIndex = " << TTLoopIndex << ", period from periodG = " << period << std::endl;
c@410 814 #endif
c@410 815
c@410 816 if (period > 10000) {
c@410 817 std::cerr << "TempoTrack::process: WARNING! Highly implausible period value " << period << "!" << std::endl;
c@410 818 std::cerr << "periodG contains (of " << TTFrames << " frames): " << std::endl;
c@410 819 for (int i = 0; i < TTLoopIndex + 3 && i < TTFrames; ++i) {
c@410 820 std::cerr << i << " -> " << periodG[i] << std::endl;
c@410 821 }
c@410 822 std::cerr << "periodP contains (of " << TTFrames << " frames): " << std::endl;
c@410 823 for (int i = 0; i < TTLoopIndex + 3 && i < TTFrames; ++i) {
c@410 824 std::cerr << i << " -> " << periodP[i] << std::endl;
c@410 825 }
c@410 826 period = 5168 / 120;
c@410 827 }
c@410 828
c@410 829 createPhaseExtractor( PW, m_winLength, period, FSP, lastBeat );
c@410 830
c@410 831 }
c@410 832 else
c@410 833 {
c@410 834 period = periodP[ TTLoopIndex ];
c@410 835
c@410 836 #ifdef DEBUG_TEMPO_TRACK
c@410 837 std::cerr << "TempoTrack::process: GW[0] == " << GW[0] << ", TTLoopIndex = " << TTLoopIndex << ", period from periodP = " << period << std::endl;
c@410 838 #endif
c@410 839
c@410 840 createPhaseExtractor( PW, m_winLength, period, FSP, 0 );
c@410 841 }
c@410 842 }
c@410 843
c@410 844 alignment[ TTLoopIndex ] = phaseMM( m_rawDFFrame, PW, m_winLength, period );
c@410 845
c@410 846 lastBeat = beatPredict(FSP, alignment[ TTLoopIndex ], period, m_lagLength );
c@410 847
c@410 848 FSP += (m_lagLength);
c@410 849
c@410 850 if (tempoReturn) tempoReturn->push_back(m_lockedTempo);
c@410 851
c@410 852 TTLoopIndex++;
c@410 853 }
c@410 854
c@410 855
c@410 856 delete [] periodP;
c@410 857 delete [] periodG;
c@410 858 delete [] alignment;
c@410 859
c@410 860 delete [] RW;
c@410 861 delete [] GW;
c@410 862 delete [] PW;
c@410 863
c@410 864 return m_beats;
c@410 865 }
c@410 866