c@410: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@410: c@410: /* c@410: QM DSP Library c@410: c@410: Centre for Digital Music, Queen Mary, University of London. c@410: This file copyright 2005-2006 Christian Landone.and Matthew Davies. c@309: c@309: This program is free software; you can redistribute it and/or c@309: modify it under the terms of the GNU General Public License as c@309: published by the Free Software Foundation; either version 2 of the c@309: License, or (at your option) any later version. See the file c@410: COPYING included with this distribution for more information. c@410: */ c@410: c@410: #include "TempoTrack.h" c@410: c@410: #include "maths/MathAliases.h" c@410: #include "maths/MathUtilities.h" c@410: c@410: #include c@410: c@410: #include cannam@490: #include cannam@492: #include c@410: cannam@493: using std::vector; cannam@493: c@410: //#define DEBUG_TEMPO_TRACK 1 c@410: c@410: ////////////////////////////////////////////////////////////////////// c@410: // Construction/Destruction c@410: ////////////////////////////////////////////////////////////////////// c@410: c@410: TempoTrack::TempoTrack( TTParams Params ) c@410: { c@410: m_tempoScratch = NULL; c@410: m_rawDFFrame = NULL; c@410: m_smoothDFFrame = NULL; c@410: m_frameACF = NULL; cannam@479: m_smoothRCF = NULL; c@410: c@410: m_dataLength = 0; c@410: m_winLength = 0; c@410: m_lagLength = 0; c@410: c@410: m_rayparam = 0; c@410: m_sigma = 0; c@410: m_DFWVNnorm = 0; c@410: c@410: initialise( Params ); c@410: } c@410: c@410: TempoTrack::~TempoTrack() c@410: { c@410: deInitialise(); c@410: } c@410: c@410: void TempoTrack::initialise( TTParams Params ) cannam@479: { c@410: m_winLength = Params.winLength; c@410: m_lagLength = Params.lagLength; c@410: cannam@479: m_rayparam = 43.0; c@410: m_sigma = sqrt(3.9017); c@410: m_DFWVNnorm = exp( ( log( 2.0 ) / m_rayparam ) * ( m_winLength + 2 ) ); c@410: c@410: m_rawDFFrame = new double[ m_winLength ]; c@410: m_smoothDFFrame = new double[ m_winLength ]; c@410: m_frameACF = new double[ m_winLength ]; c@410: m_tempoScratch = new double[ m_lagLength ]; cannam@479: m_smoothRCF = new double[ m_lagLength ]; c@410: c@410: m_DFFramer.configure( m_winLength, m_lagLength ); cannam@479: c@410: m_DFPParams.length = m_winLength; c@410: m_DFPParams.AlphaNormParam = Params.alpha; c@410: m_DFPParams.LPOrd = Params.LPOrd; c@410: m_DFPParams.LPACoeffs = Params.LPACoeffs; c@410: m_DFPParams.LPBCoeffs = Params.LPBCoeffs; c@410: m_DFPParams.winPre = Params.WinT.pre; c@410: m_DFPParams.winPost = Params.WinT.post; c@410: m_DFPParams.isMedianPositive = true; cannam@479: c@410: m_DFConditioning = new DFProcess( m_DFPParams ); c@410: cannam@479: // these are parameters for smoothing m_tempoScratch c@410: m_RCFPParams.length = m_lagLength; c@410: m_RCFPParams.AlphaNormParam = Params.alpha; c@410: m_RCFPParams.LPOrd = Params.LPOrd; c@410: m_RCFPParams.LPACoeffs = Params.LPACoeffs; c@410: m_RCFPParams.LPBCoeffs = Params.LPBCoeffs; c@410: m_RCFPParams.winPre = Params.WinT.pre; c@410: m_RCFPParams.winPost = Params.WinT.post; c@410: m_RCFPParams.isMedianPositive = true; c@410: c@410: m_RCFConditioning = new DFProcess( m_RCFPParams ); c@410: } c@410: c@410: void TempoTrack::deInitialise() cannam@479: { c@410: delete [] m_rawDFFrame; c@410: delete [] m_smoothDFFrame; cannam@479: delete [] m_smoothRCF; c@410: delete [] m_frameACF; c@410: delete [] m_tempoScratch; c@410: delete m_DFConditioning; cannam@479: delete m_RCFConditioning; c@410: } c@410: c@414: void TempoTrack::createCombFilter(double* Filter, int winLength, int /* TSig */, double beatLag) c@410: { c@414: int i; c@410: cannam@479: if( beatLag == 0 ) { cannam@479: for( i = 0; i < winLength; i++ ) { cannam@479: Filter[ i ] = cannam@479: ( ( i + 1 ) / pow( m_rayparam, 2.0) ) * cannam@479: exp( ( -pow(( i + 1 ),2.0 ) / cannam@479: ( 2.0 * pow( m_rayparam, 2.0)))); cannam@479: } cannam@479: } else { cannam@479: m_sigma = beatLag/4; cannam@479: for( i = 0; i < winLength; i++ ) { cannam@479: double dlag = (double)(i+1) - beatLag; cannam@479: Filter[ i ] = exp(-0.5 * pow(( dlag / m_sigma), 2.0) ) / cannam@487: (sqrt(TWO_PI) * m_sigma); cannam@479: } c@410: } c@410: } c@410: c@410: double TempoTrack::tempoMM(double* ACF, double* weight, int tsig) c@410: { c@410: double period = 0; c@410: double maxValRCF = 0.0; c@414: int maxIndexRCF = 0; c@410: c@410: double* pdPeaks; c@410: c@414: int maxIndexTemp; c@414: double maxValTemp; c@414: int count; cannam@479: c@414: int numelem,i,j; c@410: int a, b; c@410: cannam@479: for( i = 0; i < m_lagLength; i++ ) { cannam@479: m_tempoScratch[ i ] = 0.0; cannam@479: } c@410: cannam@479: if( tsig == 0 ) { cannam@479: //if time sig is unknown, use metrically unbiased version of Filterbank cannam@479: numelem = 4; cannam@479: } else { cannam@479: numelem = tsig; c@410: } c@410: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "tempoMM: m_winLength = " << m_winLength << ", m_lagLength = " << m_lagLength << ", numelem = " << numelem << std::endl; c@410: #endif c@410: cannam@479: for(i=1;iprocess( m_tempoScratch, m_smoothRCF); c@410: cannam@479: if (tsig != 0) { // i.e. in context dependent state cannam@479: c@410: // NOW FIND MAX INDEX OF ACFOUT cannam@479: for( i = 0; i < m_lagLength; i++) { cannam@479: if( m_tempoScratch[ i ] > maxValRCF) { cannam@479: maxValRCF = m_tempoScratch[ i ]; cannam@479: maxIndexRCF = i; cannam@479: } cannam@479: } cannam@479: cannam@479: } else { // using rayleigh weighting cannam@479: cannam@479: vector > rcfMat; cannam@479: cannam@479: double sumRcf = 0.; cannam@479: cannam@479: double maxVal = 0.; cannam@479: // now find the two values which minimise rcfMat cannam@479: double minVal = 0.; cannam@479: int p_i = 1; // periodicity for row i; cannam@479: int p_j = 1; //periodicity for column j; cannam@479: cannam@479: for ( i=0; i() ); // adds a new row... cannam@479: } cannam@479: cannam@479: for (i=0; i(i)/ cannam@479: static_cast(j) ) / cannam@479: log(2.0); cannam@479: rcfMat[i][j] = ( abs(1.0-abs(log2PeriodRatio)) ); cannam@479: rcfMat[i][j] += ( 0.01*( 1./(m_tempoScratch[i]+m_tempoScratch[j]) ) ); cannam@479: } cannam@479: } cannam@479: cannam@479: // set diagonal equal to maximum value in rcfMat cannam@479: // we don't want to pick one strong middle peak - we need a combination of two peaks. cannam@479: cannam@479: for ( i=1; i maxVal) { cannam@479: maxVal = rcfMat[i][j]; c@410: } c@410: } cannam@479: } cannam@479: cannam@479: for ( i=1; i m_tempoScratch[p_j]) { cannam@479: beatPeriod = p_i; cannam@479: } cannam@479: cannam@479: // now write the output cannam@479: maxIndexRCF = static_cast(beatPeriod); cannam@479: } c@410: c@410: c@410: double locked = 5168.f / maxIndexRCF; c@410: if (locked >= 30 && locked <= 180) { c@410: m_lockedTempo = locked; c@410: } c@410: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "tempoMM: locked tempo = " << m_lockedTempo << std::endl; c@410: #endif c@410: cannam@479: if( tsig == 0 ) { cannam@479: tsig = 4; cannam@479: } c@410: c@410: #ifdef DEBUG_TEMPO_TRACK cannam@479: std::cerr << "tempoMM: maxIndexRCF = " << maxIndexRCF << std::endl; c@410: #endif cannam@479: cannam@479: if( tsig == 4 ) { cannam@479: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "tsig == 4" << std::endl; c@410: #endif c@410: cannam@479: pdPeaks = new double[ 4 ]; cannam@479: for( i = 0; i < 4; i++ ){ pdPeaks[ i ] = 0.0;} c@410: cannam@479: pdPeaks[ 0 ] = ( double )maxIndexRCF + 1; c@410: cannam@479: maxIndexTemp = 0; cannam@479: maxValTemp = 0.0; cannam@479: count = 0; c@410: cannam@479: for( i = (2 * maxIndexRCF + 1) - 1; i < (2 * maxIndexRCF + 1) + 2; i++ ) { cannam@479: if( ACF[ i ] > maxValTemp ) { cannam@479: maxValTemp = ACF[ i ]; cannam@479: maxIndexTemp = count; cannam@479: } cannam@479: count++; cannam@479: } cannam@479: pdPeaks[ 1 ] = (double)( maxIndexTemp + 1 + ( (2 * maxIndexRCF + 1 ) - 2 ) + 1 )/2; c@410: cannam@479: maxIndexTemp = 0; cannam@479: maxValTemp = 0.0; cannam@479: count = 0; c@410: cannam@479: for( i = (3 * maxIndexRCF + 2 ) - 2; i < (3 * maxIndexRCF + 2 ) + 3; i++ ) { cannam@479: if( ACF[ i ] > maxValTemp ) { cannam@479: maxValTemp = ACF[ i ]; cannam@479: maxIndexTemp = count; cannam@479: } cannam@479: count++; cannam@479: } cannam@479: pdPeaks[ 2 ] = (double)( maxIndexTemp + 1 + ( (3 * maxIndexRCF + 2) - 4 ) + 1 )/3; c@410: cannam@479: maxIndexTemp = 0; cannam@479: maxValTemp = 0.0; cannam@479: count = 0; c@410: cannam@479: for( i = ( 4 * maxIndexRCF + 3) - 3; i < ( 4 * maxIndexRCF + 3) + 4; i++ ) { cannam@479: if( ACF[ i ] > maxValTemp ) { cannam@479: maxValTemp = ACF[ i ]; cannam@479: maxIndexTemp = count; cannam@479: } cannam@479: count++; cannam@479: } c@410: cannam@479: pdPeaks[ 3 ] = (double)( maxIndexTemp + 1 + ( (4 * maxIndexRCF + 3) - 9 ) + 1 )/4 ; c@410: cannam@479: cannam@479: period = MathUtilities::mean( pdPeaks, 4 ); cannam@479: cannam@479: } else { cannam@479: c@410: #ifdef DEBUG_TEMPO_TRACK cannam@479: std::cerr << "tsig != 4" << std::endl; c@410: #endif c@410: cannam@479: pdPeaks = new double[ 3 ]; cannam@479: for( i = 0; i < 3; i++ ) { cannam@479: pdPeaks[ i ] = 0.0; cannam@479: } c@410: cannam@479: pdPeaks[ 0 ] = ( double )maxIndexRCF + 1; c@410: cannam@479: maxIndexTemp = 0; cannam@479: maxValTemp = 0.0; cannam@479: count = 0; c@410: cannam@479: for( i = (2 * maxIndexRCF + 1) - 1; i < (2 * maxIndexRCF + 1) + 2; i++ ) { cannam@479: if( ACF[ i ] > maxValTemp ) { cannam@479: maxValTemp = ACF[ i ]; cannam@479: maxIndexTemp = count; cannam@479: } cannam@479: count++; cannam@479: } cannam@479: pdPeaks[ 1 ] = (double)( maxIndexTemp + 1 + ( (2 * maxIndexRCF + 1 ) - 2 ) + 1 )/2; c@410: cannam@479: maxIndexTemp = 0; cannam@479: maxValTemp = 0.0; cannam@479: count = 0; c@410: cannam@479: for( i = (3 * maxIndexRCF + 2 ) - 2; i < (3 * maxIndexRCF + 2 ) + 3; i++ ) { cannam@479: if( ACF[ i ] > maxValTemp ) { cannam@479: maxValTemp = ACF[ i ]; cannam@479: maxIndexTemp = count; cannam@479: } cannam@479: count++; cannam@479: } cannam@479: pdPeaks[ 2 ] = (double)( maxIndexTemp + 1 + ( (3 * maxIndexRCF + 2) - 4 ) + 1 )/3; c@410: c@410: cannam@479: period = MathUtilities::mean( pdPeaks, 3 ); c@410: } c@410: c@410: delete [] pdPeaks; c@410: c@410: return period; c@410: } c@410: c@410: void TempoTrack::stepDetect( double* periodP, double* periodG, int currentIdx, int* flag ) c@410: { c@410: double stepthresh = 1 * 3.9017; c@410: cannam@479: if( *flag ) { cannam@479: if(abs(periodG[ currentIdx ] - periodP[ currentIdx ]) > stepthresh) { cannam@479: // do nuffin' cannam@479: } cannam@479: } else { cannam@479: if(fabs(periodG[ currentIdx ]-periodP[ currentIdx ]) > stepthresh) { cannam@479: *flag = 3; cannam@479: } c@410: } c@410: } c@410: c@410: void TempoTrack::constDetect( double* periodP, int currentIdx, int* flag ) c@410: { c@410: double constthresh = 2 * 3.9017; c@410: cannam@479: if( fabs( 2 * periodP[ currentIdx ] - periodP[ currentIdx - 1] - periodP[ currentIdx - 2] ) < constthresh) { cannam@479: *flag = 1; cannam@479: } else { cannam@479: *flag = 0; c@410: } c@410: } c@410: c@414: int TempoTrack::findMeter(double *ACF, int len, double period) c@410: { c@410: int i; c@410: int p = (int)MathUtilities::round( period ); c@410: int tsig; c@410: c@410: double Energy_3 = 0.0; c@410: double Energy_4 = 0.0; c@410: c@410: double temp3A = 0.0; c@410: double temp3B = 0.0; c@410: double temp4A = 0.0; c@410: double temp4B = 0.0; c@410: c@410: double* dbf = new double[ len ]; int t = 0; c@414: for( int u = 0; u < len; u++ ){ dbf[ u ] = 0.0; } c@410: cannam@479: if( (double)len < 6 * p + 2 ) { cannam@479: cannam@479: for( i = ( 3 * p - 2 ); i < ( 3 * p + 2 ) + 1; i++ ) { cannam@479: temp3A += ACF[ i ]; cannam@479: dbf[ t++ ] = ACF[ i ]; cannam@479: } cannam@479: cannam@479: for( i = ( 4 * p - 2 ); i < ( 4 * p + 2 ) + 1; i++ ) { cannam@479: temp4A += ACF[ i ]; cannam@479: } c@410: cannam@479: Energy_3 = temp3A; cannam@479: Energy_4 = temp4A; c@410: cannam@479: } else { cannam@479: cannam@479: for( i = ( 3 * p - 2 ); i < ( 3 * p + 2 ) + 1; i++ ) { cannam@479: temp3A += ACF[ i ]; cannam@479: } cannam@479: cannam@479: for( i = ( 4 * p - 2 ); i < ( 4 * p + 2 ) + 1; i++ ) { cannam@479: temp4A += ACF[ i ]; cannam@479: } c@410: cannam@479: for( i = ( 6 * p - 2 ); i < ( 6 * p + 2 ) + 1; i++ ) { cannam@479: temp3B += ACF[ i ]; cannam@479: } cannam@479: cannam@479: for( i = ( 2 * p - 2 ); i < ( 2 * p + 2 ) + 1; i++ ) { cannam@479: temp4B += ACF[ i ]; cannam@479: } cannam@479: cannam@479: Energy_3 = temp3A + temp3B; cannam@479: Energy_4 = temp4A + temp4B; c@410: } c@410: cannam@479: if (Energy_3 > Energy_4) { cannam@479: tsig = 3; cannam@479: } else { cannam@479: tsig = 4; c@410: } c@410: c@410: return tsig; c@410: } c@410: c@414: void TempoTrack::createPhaseExtractor(double *Filter, int /* winLength */, double period, int fsp, int lastBeat) cannam@479: { c@410: int p = (int)MathUtilities::round( period ); c@410: int predictedOffset = 0; c@410: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "TempoTrack::createPhaseExtractor: period = " << period << ", p = " << p << std::endl; c@410: #endif c@410: c@410: if (p > 10000) { c@410: std::cerr << "TempoTrack::createPhaseExtractor: WARNING! Highly implausible period value " << p << "!" << std::endl; c@410: period = 5168 / 120; c@410: } c@410: c@410: double* phaseScratch = new double[ p*2 + 2 ]; c@410: for (int i = 0; i < p*2 + 2; ++i) phaseScratch[i] = 0.0; c@410: cannam@479: cannam@479: if ( lastBeat != 0 ) { cannam@479: cannam@479: lastBeat = (int)MathUtilities::round((double)lastBeat );///(double)winLength); c@410: c@410: predictedOffset = lastBeat + p - fsp; c@410: cannam@479: if (predictedOffset < 0) { c@410: lastBeat = 0; c@410: } c@410: } c@410: cannam@479: if ( lastBeat != 0 ) { cannam@479: cannam@479: int mu = p; cannam@479: double sigma = (double)p/8; cannam@479: double PhaseMin = 0.0; cannam@479: double PhaseMax = 0.0; cannam@479: int scratchLength = p*2; cannam@479: double temp = 0.0; c@410: cannam@479: for( int i = 0; i < scratchLength; i++ ) { cannam@487: phaseScratch[ i ] = exp( -0.5 * pow( ( i - mu ) / sigma, 2 ) ) / ( sqrt(TWO_PI) *sigma ); cannam@479: } c@410: cannam@479: MathUtilities::getFrameMinMax( phaseScratch, scratchLength, &PhaseMin, &PhaseMax ); cannam@479: cannam@479: for(int i = 0; i < scratchLength; i ++) { cannam@479: temp = phaseScratch[ i ]; cannam@479: phaseScratch[ i ] = (temp - PhaseMin)/PhaseMax; cannam@479: } c@410: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "predictedOffset = " << predictedOffset << std::endl; c@410: #endif c@410: cannam@479: int index = 0; cannam@479: for (int i = p - ( predictedOffset - 1); i < p + ( p - predictedOffset) + 1; i++) { c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "assigning to filter index " << index << " (size = " << p*2 << ")" << " value " << phaseScratch[i] << " from scratch index " << i << std::endl; c@410: #endif cannam@479: Filter[ index++ ] = phaseScratch[ i ]; cannam@479: } cannam@479: } else { cannam@479: for( int i = 0; i < p; i ++) { cannam@479: Filter[ i ] = 1; cannam@479: } c@410: } cannam@479: c@410: delete [] phaseScratch; c@410: } c@410: c@414: int TempoTrack::phaseMM(double *DF, double *weighting, int winLength, double period) c@410: { c@410: int alignment = 0; c@410: int p = (int)MathUtilities::round( period ); c@410: c@410: double temp = 0.0; c@410: c@410: double* y = new double[ winLength ]; c@410: double* align = new double[ p ]; c@410: cannam@479: for( int i = 0; i < winLength; i++ ) { cannam@479: y[ i ] = (double)( -i + winLength )/(double)winLength; cannam@479: y[ i ] = pow(y [i ],2.0); // raise to power 2. c@410: } c@410: cannam@479: for( int o = 0; o < p; o++ ) { cannam@479: temp = 0.0; cannam@479: for (int i = 1 + (o - 1); i < winLength; i += (p + 1)) { cannam@479: temp = temp + DF[ i ] * y[ i ]; cannam@479: } cannam@479: align[ o ] = temp * weighting[ o ]; c@410: } c@410: c@410: c@410: double valTemp = 0.0; cannam@479: for(int i = 0; i < p; i++) { cannam@479: if( align[ i ] > valTemp ) { cannam@479: valTemp = align[ i ]; cannam@479: alignment = i; cannam@479: } c@410: } c@410: c@410: delete [] y; c@410: delete [] align; c@410: c@410: return alignment; c@410: } c@410: c@414: int TempoTrack::beatPredict(int FSP0, double alignment, double period, int step ) c@410: { c@410: int beat = 0; c@410: c@410: int p = (int)MathUtilities::round( period ); c@410: int align = (int)MathUtilities::round( alignment ); c@410: int FSP = (int)MathUtilities::round( FSP0 ); c@410: c@410: int FEP = FSP + ( step ); c@410: c@410: beat = FSP + align; c@410: c@410: m_beats.push_back( beat ); c@410: cannam@479: while( beat + p < FEP ) { cannam@479: beat += p; cannam@479: m_beats.push_back( beat ); c@410: } c@410: c@410: return beat; c@410: } c@410: c@410: c@410: c@410: vector TempoTrack::process( vector DF, c@410: vector *tempoReturn ) c@410: { c@410: m_dataLength = DF.size(); cannam@479: c@410: m_lockedTempo = 0.0; c@410: cannam@479: double period = 0.0; c@410: int stepFlag = 0; c@410: int constFlag = 0; c@410: int FSP = 0; c@410: int tsig = 0; c@410: int lastBeat = 0; c@410: c@410: vector causalDF; c@410: c@410: causalDF = DF; c@410: c@410: //Prepare Causal Extension DFData c@414: // int DFCLength = m_dataLength + m_winLength; cannam@479: cannam@479: for( int j = 0; j < m_winLength; j++ ) { cannam@479: causalDF.push_back( 0 ); c@410: } cannam@479: cannam@479: c@410: double* RW = new double[ m_lagLength ]; c@414: for (int clear = 0; clear < m_lagLength; clear++){ RW[ clear ] = 0.0;} c@410: c@410: double* GW = new double[ m_lagLength ]; c@414: for (int clear = 0; clear < m_lagLength; clear++){ GW[ clear ] = 0.0;} c@410: c@410: double* PW = new double[ m_lagLength ]; c@414: for(int clear = 0; clear < m_lagLength; clear++){ PW[ clear ] = 0.0;} c@410: c@410: m_DFFramer.setSource( &causalDF[0], m_dataLength ); c@410: c@414: int TTFrames = m_DFFramer.getMaxNoFrames(); c@410: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "TTFrames = " << TTFrames << std::endl; c@410: #endif cannam@479: c@410: double* periodP = new double[ TTFrames ]; c@414: for(int clear = 0; clear < TTFrames; clear++){ periodP[ clear ] = 0.0;} cannam@479: c@410: double* periodG = new double[ TTFrames ]; c@414: for(int clear = 0; clear < TTFrames; clear++){ periodG[ clear ] = 0.0;} cannam@479: c@410: double* alignment = new double[ TTFrames ]; c@414: for(int clear = 0; clear < TTFrames; clear++){ alignment[ clear ] = 0.0;} c@410: c@410: m_beats.clear(); c@410: c@410: createCombFilter( RW, m_lagLength, 0, 0 ); c@410: c@410: int TTLoopIndex = 0; c@410: cannam@479: for( int i = 0; i < TTFrames; i++ ) { cannam@479: cannam@479: m_DFFramer.getFrame( m_rawDFFrame ); c@410: cannam@479: m_DFConditioning->process( m_rawDFFrame, m_smoothDFFrame ); c@410: cannam@479: m_correlator.doAutoUnBiased( m_smoothDFFrame, m_frameACF, m_winLength ); cannam@479: cannam@479: periodP[ TTLoopIndex ] = tempoMM( m_frameACF, RW, 0 ); c@410: cannam@479: if( GW[ 0 ] != 0 ) { cannam@479: periodG[ TTLoopIndex ] = tempoMM( m_frameACF, GW, tsig ); cannam@479: } else { cannam@479: periodG[ TTLoopIndex ] = 0.0; cannam@479: } c@410: cannam@479: stepDetect( periodP, periodG, TTLoopIndex, &stepFlag ); c@410: cannam@479: if( stepFlag == 1) { cannam@479: constDetect( periodP, TTLoopIndex, &constFlag ); cannam@479: stepFlag = 0; cannam@479: } else { cannam@479: stepFlag -= 1; cannam@479: } c@410: cannam@479: if( stepFlag < 0 ) { cannam@479: stepFlag = 0; cannam@479: } c@410: cannam@479: if( constFlag != 0) { cannam@479: cannam@479: tsig = findMeter( m_frameACF, m_winLength, periodP[ TTLoopIndex ] ); cannam@479: cannam@479: createCombFilter( GW, m_lagLength, tsig, periodP[ TTLoopIndex ] ); cannam@479: cannam@479: periodG[ TTLoopIndex ] = tempoMM( m_frameACF, GW, tsig ); c@410: cannam@479: period = periodG[ TTLoopIndex ]; c@410: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "TempoTrack::process: constFlag == " << constFlag << ", TTLoopIndex = " << TTLoopIndex << ", period from periodG = " << period << std::endl; c@410: #endif c@410: cannam@479: createPhaseExtractor( PW, m_winLength, period, FSP, 0 ); c@410: cannam@479: constFlag = 0; c@410: cannam@479: } else { cannam@479: cannam@479: if( GW[ 0 ] != 0 ) { cannam@479: period = periodG[ TTLoopIndex ]; c@410: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "TempoTrack::process: GW[0] == " << GW[0] << ", TTLoopIndex = " << TTLoopIndex << ", period from periodG = " << period << std::endl; c@410: #endif c@410: c@410: if (period > 10000) { c@410: std::cerr << "TempoTrack::process: WARNING! Highly implausible period value " << period << "!" << std::endl; c@410: std::cerr << "periodG contains (of " << TTFrames << " frames): " << std::endl; c@410: for (int i = 0; i < TTLoopIndex + 3 && i < TTFrames; ++i) { c@410: std::cerr << i << " -> " << periodG[i] << std::endl; c@410: } c@410: std::cerr << "periodP contains (of " << TTFrames << " frames): " << std::endl; c@410: for (int i = 0; i < TTLoopIndex + 3 && i < TTFrames; ++i) { c@410: std::cerr << i << " -> " << periodP[i] << std::endl; c@410: } c@410: period = 5168 / 120; c@410: } c@410: cannam@479: createPhaseExtractor( PW, m_winLength, period, FSP, lastBeat ); c@410: cannam@479: } cannam@479: else cannam@479: { cannam@479: period = periodP[ TTLoopIndex ]; c@410: c@410: #ifdef DEBUG_TEMPO_TRACK c@410: std::cerr << "TempoTrack::process: GW[0] == " << GW[0] << ", TTLoopIndex = " << TTLoopIndex << ", period from periodP = " << period << std::endl; c@410: #endif c@410: cannam@479: createPhaseExtractor( PW, m_winLength, period, FSP, 0 ); cannam@479: } cannam@479: } c@410: cannam@479: alignment[ TTLoopIndex ] = phaseMM( m_rawDFFrame, PW, m_winLength, period ); c@410: cannam@479: lastBeat = beatPredict(FSP, alignment[ TTLoopIndex ], period, m_lagLength ); c@410: cannam@479: FSP += (m_lagLength); c@410: c@410: if (tempoReturn) tempoReturn->push_back(m_lockedTempo); c@410: cannam@479: TTLoopIndex++; c@410: } c@410: c@410: c@410: delete [] periodP; c@410: delete [] periodG; c@410: delete [] alignment; c@410: c@410: delete [] RW; c@410: delete [] GW; c@410: delete [] PW; c@410: c@410: return m_beats; c@410: } c@410: