1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6
Centre for Digital Music, Queen Mary, University of London.
7
This file copyright 2005-2006 Christian Landone.and Matthew Davies.
9
This program is free software; you can redistribute it and/or
10
modify it under the terms of the GNU General Public License as
11
published by the Free Software Foundation; either version 2 of the
12
License, or (at your option) any later version. See the file
13
COPYING included with this distribution for more information.
16
#include "TempoTrack.h"
18
#include "maths/MathAliases.h"
19
#include "maths/MathUtilities.h"
25
//#define DEBUG_TEMPO_TRACK 1
30
//////////////////////////////////////////////////////////////////////
31
// Construction/Destruction
32
//////////////////////////////////////////////////////////////////////
34
TempoTrack::TempoTrack( TTParams Params )
36
m_tempoScratch = NULL;
38
m_smoothDFFrame = NULL;
53
TempoTrack::~TempoTrack()
58
void TempoTrack::initialise( TTParams Params )
60
m_winLength = Params.winLength;
61
m_lagLength = Params.lagLength;
64
m_sigma = sqrt(3.9017);
65
m_DFWVNnorm = exp( ( log( 2.0 ) / m_rayparam ) * ( m_winLength + 2 ) );
67
m_rawDFFrame = new double[ m_winLength ];
68
m_smoothDFFrame = new double[ m_winLength ];
69
m_frameACF = new double[ m_winLength ];
70
m_tempoScratch = new double[ m_lagLength ];
71
m_smoothRCF = new double[ m_lagLength ];
74
unsigned int winPre = Params.WinT.pre;
75
unsigned int winPost = Params.WinT.post;
77
m_DFFramer.configure( m_winLength, m_lagLength );
79
m_DFPParams.length = m_winLength;
80
m_DFPParams.AlphaNormParam = Params.alpha;
81
m_DFPParams.LPOrd = Params.LPOrd;
82
m_DFPParams.LPACoeffs = Params.LPACoeffs;
83
m_DFPParams.LPBCoeffs = Params.LPBCoeffs;
84
m_DFPParams.winPre = Params.WinT.pre;
85
m_DFPParams.winPost = Params.WinT.post;
86
m_DFPParams.isMedianPositive = true;
88
m_DFConditioning = new DFProcess( m_DFPParams );
91
// these are parameters for smoothing m_tempoScratch
92
m_RCFPParams.length = m_lagLength;
93
m_RCFPParams.AlphaNormParam = Params.alpha;
94
m_RCFPParams.LPOrd = Params.LPOrd;
95
m_RCFPParams.LPACoeffs = Params.LPACoeffs;
96
m_RCFPParams.LPBCoeffs = Params.LPBCoeffs;
97
m_RCFPParams.winPre = Params.WinT.pre;
98
m_RCFPParams.winPost = Params.WinT.post;
99
m_RCFPParams.isMedianPositive = true;
101
m_RCFConditioning = new DFProcess( m_RCFPParams );
105
void TempoTrack::deInitialise()
107
delete [] m_rawDFFrame;
109
delete [] m_smoothDFFrame;
111
delete [] m_smoothRCF;
113
delete [] m_frameACF;
115
delete [] m_tempoScratch;
117
delete m_DFConditioning;
119
delete m_RCFConditioning;
123
void TempoTrack::createCombFilter(double* Filter, unsigned int winLength, unsigned int TSig, double beatLag)
129
for( i = 0; i < winLength; i++ )
131
Filter[ i ] = ( ( i + 1 ) / pow( m_rayparam, 2.0) ) * exp( ( -pow(( i + 1 ),2.0 ) / ( 2.0 * pow( m_rayparam, 2.0))));
137
for( i = 0; i < winLength; i++ )
139
double dlag = (double)(i+1) - beatLag;
140
Filter[ i ] = exp(-0.5 * pow(( dlag / m_sigma), 2.0) ) / (sqrt( 2 * PI) * m_sigma);
145
double TempoTrack::tempoMM(double* ACF, double* weight, int tsig)
149
double maxValRCF = 0.0;
150
unsigned int maxIndexRCF = 0;
154
unsigned int maxIndexTemp;
158
unsigned int numelem,i,j;
161
for( i = 0; i < m_lagLength; i++ )
162
m_tempoScratch[ i ] = 0.0;
166
//if time sig is unknown, use metrically unbiased version of Filterbank
174
#ifdef DEBUG_TEMPO_TRACK
175
std::cerr << "tempoMM: m_winLength = " << m_winLength << ", m_lagLength = " << m_lagLength << ", numelem = " << numelem << std::endl;
178
for(i=1;i<m_lagLength-1;i++)
180
//first and last output values are left intentionally as zero
181
for (a=1;a<=numelem;a++)
187
m_tempoScratch[i] += ACF[a*(i+1)+b-1] * (1.0 / (2.0 * (double)a-1)) * weight[i];
191
m_tempoScratch[i] += ACF[a*(i+1)+b-1] * 1 * weight[i];
198
//////////////////////////////////////////////////
199
// MODIFIED BEAT PERIOD EXTRACTION //////////////
200
/////////////////////////////////////////////////
202
// find smoothed version of RCF ( as applied to Detection Function)
203
m_RCFConditioning->process( m_tempoScratch, m_smoothRCF);
205
if (tsig != 0) // i.e. in context dependent state
207
// NOW FIND MAX INDEX OF ACFOUT
208
for( i = 0; i < m_lagLength; i++)
210
if( m_tempoScratch[ i ] > maxValRCF)
212
maxValRCF = m_tempoScratch[ i ];
217
else // using rayleigh weighting
219
vector <vector<double> > rcfMat;
224
// now find the two values which minimise rcfMat
226
int p_i = 1; // periodicity for row i;
227
int p_j = 1; //periodicity for column j;
230
for ( i=0; i<m_lagLength; i++)
232
m_tempoScratch[i] =m_smoothRCF[i];
235
// normalise m_tempoScratch so that it sums to zero.
236
for ( i=0; i<m_lagLength; i++)
238
sumRcf += m_tempoScratch[i];
241
for( i=0; i<m_lagLength; i++)
243
m_tempoScratch[i] /= sumRcf;
246
// create a matrix to store m_tempoScratchValues modified by log2 ratio
247
for ( i=0; i<m_lagLength; i++)
249
rcfMat.push_back ( vector<double>() ); // adds a new row...
252
for (i=0; i<m_lagLength; i++)
254
for (j=0; j<m_lagLength; j++)
256
rcfMat[i].push_back (0.);
260
// the 'i' and 'j' indices deliberately start from '1' and not '0'
261
for ( i=1; i<m_lagLength; i++)
263
for (j=1; j<m_lagLength; j++)
265
double log2PeriodRatio = log( static_cast<double>(i)/static_cast<double>(j) ) / log(2.0);
266
rcfMat[i][j] = ( abs(1.0-abs(log2PeriodRatio)) );
267
rcfMat[i][j] += ( 0.01*( 1./(m_tempoScratch[i]+m_tempoScratch[j]) ) );
271
// set diagonal equal to maximum value in rcfMat
272
// we don't want to pick one strong middle peak - we need a combination of two peaks.
274
for ( i=1; i<m_lagLength; i++)
276
for (j=1; j<m_lagLength; j++)
278
if (rcfMat[i][j] > maxVal)
280
maxVal = rcfMat[i][j];
285
for ( i=1; i<m_lagLength; i++)
287
rcfMat[i][i] = maxVal;
290
// now find the row and column number which minimise rcfMat
293
for ( i=1; i<m_lagLength; i++)
295
for ( j=1; j<m_lagLength; j++)
297
if (rcfMat[i][j] < minVal)
299
minVal = rcfMat[i][j];
307
// initially choose p_j (arbitrary) - saves on an else statement
308
int beatPeriod = p_j;
309
if (m_tempoScratch[p_i] > m_tempoScratch[p_j])
314
// now write the output
315
maxIndexRCF = static_cast<int>(beatPeriod);
319
double locked = 5168.f / maxIndexRCF;
320
if (locked >= 30 && locked <= 180) {
321
m_lockedTempo = locked;
324
#ifdef DEBUG_TEMPO_TRACK
325
std::cerr << "tempoMM: locked tempo = " << m_lockedTempo << std::endl;
332
#ifdef DEBUG_TEMPO_TRACK
333
std::cerr << "tempoMM: maxIndexRCF = " << maxIndexRCF << std::endl;
338
#ifdef DEBUG_TEMPO_TRACK
339
std::cerr << "tsig == 4" << std::endl;
342
pdPeaks = new double[ 4 ];
343
for( i = 0; i < 4; i++ ){ pdPeaks[ i ] = 0.0;}
345
pdPeaks[ 0 ] = ( double )maxIndexRCF + 1;
351
for( i = (2 * maxIndexRCF + 1) - 1; i < (2 * maxIndexRCF + 1) + 2; i++ )
353
if( ACF[ i ] > maxValTemp )
355
maxValTemp = ACF[ i ];
356
maxIndexTemp = count;
360
pdPeaks[ 1 ] = (double)( maxIndexTemp + 1 + ( (2 * maxIndexRCF + 1 ) - 2 ) + 1 )/2;
366
for( i = (3 * maxIndexRCF + 2 ) - 2; i < (3 * maxIndexRCF + 2 ) + 3; i++ )
368
if( ACF[ i ] > maxValTemp )
370
maxValTemp = ACF[ i ];
371
maxIndexTemp = count;
375
pdPeaks[ 2 ] = (double)( maxIndexTemp + 1 + ( (3 * maxIndexRCF + 2) - 4 ) + 1 )/3;
381
for( i = ( 4 * maxIndexRCF + 3) - 3; i < ( 4 * maxIndexRCF + 3) + 4; i++ )
383
if( ACF[ i ] > maxValTemp )
385
maxValTemp = ACF[ i ];
386
maxIndexTemp = count;
390
pdPeaks[ 3 ] = (double)( maxIndexTemp + 1 + ( (4 * maxIndexRCF + 3) - 9 ) + 1 )/4 ;
393
period = MathUtilities::mean( pdPeaks, 4 );
397
#ifdef DEBUG_TEMPO_TRACK
398
std::cerr << "tsig != 4" << std::endl;
401
pdPeaks = new double[ 3 ];
402
for( i = 0; i < 3; i++ ){ pdPeaks[ i ] = 0.0;}
404
pdPeaks[ 0 ] = ( double )maxIndexRCF + 1;
410
for( i = (2 * maxIndexRCF + 1) - 1; i < (2 * maxIndexRCF + 1) + 2; i++ )
412
if( ACF[ i ] > maxValTemp )
414
maxValTemp = ACF[ i ];
415
maxIndexTemp = count;
419
pdPeaks[ 1 ] = (double)( maxIndexTemp + 1 + ( (2 * maxIndexRCF + 1 ) - 2 ) + 1 )/2;
425
for( i = (3 * maxIndexRCF + 2 ) - 2; i < (3 * maxIndexRCF + 2 ) + 3; i++ )
427
if( ACF[ i ] > maxValTemp )
429
maxValTemp = ACF[ i ];
430
maxIndexTemp = count;
434
pdPeaks[ 2 ] = (double)( maxIndexTemp + 1 + ( (3 * maxIndexRCF + 2) - 4 ) + 1 )/3;
437
period = MathUtilities::mean( pdPeaks, 3 );
445
void TempoTrack::stepDetect( double* periodP, double* periodG, int currentIdx, int* flag )
447
double stepthresh = 1 * 3.9017;
451
if(abs(periodG[ currentIdx ] - periodP[ currentIdx ]) > stepthresh)
458
if(fabs(periodG[ currentIdx ]-periodP[ currentIdx ]) > stepthresh)
465
void TempoTrack::constDetect( double* periodP, int currentIdx, int* flag )
467
double constthresh = 2 * 3.9017;
469
if( fabs( 2 * periodP[ currentIdx ] - periodP[ currentIdx - 1] - periodP[ currentIdx - 2] ) < constthresh)
479
int TempoTrack::findMeter(double *ACF, unsigned int len, double period)
482
int p = (int)MathUtilities::round( period );
485
double Energy_3 = 0.0;
486
double Energy_4 = 0.0;
493
double* dbf = new double[ len ]; int t = 0;
494
for( unsigned int u = 0; u < len; u++ ){ dbf[ u ] = 0.0; }
496
if( (double)len < 6 * p + 2 )
498
for( i = ( 3 * p - 2 ); i < ( 3 * p + 2 ) + 1; i++ )
501
dbf[ t++ ] = ACF[ i ];
504
for( i = ( 4 * p - 2 ); i < ( 4 * p + 2 ) + 1; i++ )
514
for( i = ( 3 * p - 2 ); i < ( 3 * p + 2 ) + 1; i++ )
519
for( i = ( 4 * p - 2 ); i < ( 4 * p + 2 ) + 1; i++ )
524
for( i = ( 6 * p - 2 ); i < ( 6 * p + 2 ) + 1; i++ )
529
for( i = ( 2 * p - 2 ); i < ( 2 * p + 2 ) + 1; i++ )
534
Energy_3 = temp3A + temp3B;
535
Energy_4 = temp4A + temp4B;
538
if (Energy_3 > Energy_4)
551
void TempoTrack::createPhaseExtractor(double *Filter, unsigned int winLength, double period, unsigned int fsp, unsigned int lastBeat)
553
int p = (int)MathUtilities::round( period );
554
int predictedOffset = 0;
556
#ifdef DEBUG_TEMPO_TRACK
557
std::cerr << "TempoTrack::createPhaseExtractor: period = " << period << ", p = " << p << std::endl;
561
std::cerr << "TempoTrack::createPhaseExtractor: WARNING! Highly implausible period value " << p << "!" << std::endl;
565
double* phaseScratch = new double[ p*2 + 2 ];
566
for (int i = 0; i < p*2 + 2; ++i) phaseScratch[i] = 0.0;
571
lastBeat = (int)MathUtilities::round((double)lastBeat );///(double)winLength);
573
predictedOffset = lastBeat + p - fsp;
575
if (predictedOffset < 0)
584
double sigma = (double)p/8;
585
double PhaseMin = 0.0;
586
double PhaseMax = 0.0;
587
unsigned int scratchLength = p*2;
590
for( int i = 0; i < scratchLength; i++ )
592
phaseScratch[ i ] = exp( -0.5 * pow( ( i - mu ) / sigma, 2 ) ) / ( sqrt( 2*PI ) *sigma );
595
MathUtilities::getFrameMinMax( phaseScratch, scratchLength, &PhaseMin, &PhaseMax );
597
for(int i = 0; i < scratchLength; i ++)
599
temp = phaseScratch[ i ];
600
phaseScratch[ i ] = (temp - PhaseMin)/PhaseMax;
603
#ifdef DEBUG_TEMPO_TRACK
604
std::cerr << "predictedOffset = " << predictedOffset << std::endl;
607
unsigned int index = 0;
608
for (int i = p - ( predictedOffset - 1); i < p + ( p - predictedOffset) + 1; i++)
610
#ifdef DEBUG_TEMPO_TRACK
611
std::cerr << "assigning to filter index " << index << " (size = " << p*2 << ")" << " value " << phaseScratch[i] << " from scratch index " << i << std::endl;
613
Filter[ index++ ] = phaseScratch[ i ];
618
for( int i = 0; i < p; i ++)
624
delete [] phaseScratch;
627
int TempoTrack::phaseMM(double *DF, double *weighting, unsigned int winLength, double period)
630
int p = (int)MathUtilities::round( period );
634
double* y = new double[ winLength ];
635
double* align = new double[ p ];
637
for( int i = 0; i < winLength; i++ )
639
y[ i ] = (double)( -i + winLength )/(double)winLength;
640
y[ i ] = pow(y [i ],2.0); // raise to power 2.
643
for( int o = 0; o < p; o++ )
646
for(int i = 1 + (o - 1); i< winLength; i += (p + 1))
648
temp = temp + DF[ i ] * y[ i ];
650
align[ o ] = temp * weighting[ o ];
654
double valTemp = 0.0;
655
for(int i = 0; i < p; i++)
657
if( align[ i ] > valTemp )
659
valTemp = align[ i ];
670
int TempoTrack::beatPredict(unsigned int FSP0, double alignment, double period, unsigned int step )
674
int p = (int)MathUtilities::round( period );
675
int align = (int)MathUtilities::round( alignment );
676
int FSP = (int)MathUtilities::round( FSP0 );
678
int FEP = FSP + ( step );
682
m_beats.push_back( beat );
684
while( beat + p < FEP )
688
m_beats.push_back( beat );
696
vector<int> TempoTrack::process( vector <double> DF,
697
vector <double> *tempoReturn )
699
m_dataLength = DF.size();
710
vector <double> causalDF;
714
//Prepare Causal Extension DFData
715
unsigned int DFCLength = m_dataLength + m_winLength;
717
for( unsigned int j = 0; j < m_winLength; j++ )
719
causalDF.push_back( 0 );
723
double* RW = new double[ m_lagLength ];
724
for( unsigned int clear = 0; clear < m_lagLength; clear++){ RW[ clear ] = 0.0;}
726
double* GW = new double[ m_lagLength ];
727
for(unsigned int clear = 0; clear < m_lagLength; clear++){ GW[ clear ] = 0.0;}
729
double* PW = new double[ m_lagLength ];
730
for(unsigned clear = 0; clear < m_lagLength; clear++){ PW[ clear ] = 0.0;}
732
m_DFFramer.setSource( &causalDF[0], m_dataLength );
734
unsigned int TTFrames = m_DFFramer.getMaxNoFrames();
736
#ifdef DEBUG_TEMPO_TRACK
737
std::cerr << "TTFrames = " << TTFrames << std::endl;
740
double* periodP = new double[ TTFrames ];
741
for(unsigned clear = 0; clear < TTFrames; clear++){ periodP[ clear ] = 0.0;}
743
double* periodG = new double[ TTFrames ];
744
for(unsigned clear = 0; clear < TTFrames; clear++){ periodG[ clear ] = 0.0;}
746
double* alignment = new double[ TTFrames ];
747
for(unsigned clear = 0; clear < TTFrames; clear++){ alignment[ clear ] = 0.0;}
751
createCombFilter( RW, m_lagLength, 0, 0 );
755
for( unsigned int i = 0; i < TTFrames; i++ )
757
m_DFFramer.getFrame( m_rawDFFrame );
759
m_DFConditioning->process( m_rawDFFrame, m_smoothDFFrame );
761
m_correlator.doAutoUnBiased( m_smoothDFFrame, m_frameACF, m_winLength );
763
periodP[ TTLoopIndex ] = tempoMM( m_frameACF, RW, 0 );
767
periodG[ TTLoopIndex ] = tempoMM( m_frameACF, GW, tsig );
771
periodG[ TTLoopIndex ] = 0.0;
774
stepDetect( periodP, periodG, TTLoopIndex, &stepFlag );
778
constDetect( periodP, TTLoopIndex, &constFlag );
793
tsig = findMeter( m_frameACF, m_winLength, periodP[ TTLoopIndex ] );
795
createCombFilter( GW, m_lagLength, tsig, periodP[ TTLoopIndex ] );
797
periodG[ TTLoopIndex ] = tempoMM( m_frameACF, GW, tsig );
799
period = periodG[ TTLoopIndex ];
801
#ifdef DEBUG_TEMPO_TRACK
802
std::cerr << "TempoTrack::process: constFlag == " << constFlag << ", TTLoopIndex = " << TTLoopIndex << ", period from periodG = " << period << std::endl;
805
createPhaseExtractor( PW, m_winLength, period, FSP, 0 );
814
period = periodG[ TTLoopIndex ];
816
#ifdef DEBUG_TEMPO_TRACK
817
std::cerr << "TempoTrack::process: GW[0] == " << GW[0] << ", TTLoopIndex = " << TTLoopIndex << ", period from periodG = " << period << std::endl;
820
if (period > 10000) {
821
std::cerr << "TempoTrack::process: WARNING! Highly implausible period value " << period << "!" << std::endl;
822
std::cerr << "periodG contains (of " << TTFrames << " frames): " << std::endl;
823
for (int i = 0; i < TTLoopIndex + 3 && i < TTFrames; ++i) {
824
std::cerr << i << " -> " << periodG[i] << std::endl;
826
std::cerr << "periodP contains (of " << TTFrames << " frames): " << std::endl;
827
for (int i = 0; i < TTLoopIndex + 3 && i < TTFrames; ++i) {
828
std::cerr << i << " -> " << periodP[i] << std::endl;
833
createPhaseExtractor( PW, m_winLength, period, FSP, lastBeat );
838
period = periodP[ TTLoopIndex ];
840
#ifdef DEBUG_TEMPO_TRACK
841
std::cerr << "TempoTrack::process: GW[0] == " << GW[0] << ", TTLoopIndex = " << TTLoopIndex << ", period from periodP = " << period << std::endl;
844
createPhaseExtractor( PW, m_winLength, period, FSP, 0 );
848
alignment[ TTLoopIndex ] = phaseMM( m_rawDFFrame, PW, m_winLength, period );
850
lastBeat = beatPredict(FSP, alignment[ TTLoopIndex ], period, m_lagLength );
852
FSP += (m_lagLength);
854
if (tempoReturn) tempoReturn->push_back(m_lockedTempo);