c@277: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@277: c@277: /* c@277: QM DSP Library c@277: c@277: Centre for Digital Music, Queen Mary, University of London. c@277: This file copyright 2008-2009 Matthew Davies and QMUL. 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@309: COPYING included with this distribution for more information. c@277: */ c@277: c@277: #include "TempoTrackV2.h" c@277: c@277: #include c@277: #include c@278: #include c@277: c@279: #include "maths/MathUtilities.h" c@277: c@277: #define EPS 0.0000008 // just some arbitrary small number c@277: c@279: TempoTrackV2::TempoTrackV2(float rate, size_t increment) : c@279: m_rate(rate), m_increment(increment) { } c@277: TempoTrackV2::~TempoTrackV2() { } c@277: c@277: void c@277: TempoTrackV2::filter_df(d_vec_t &df) c@277: { c@278: d_vec_t a(3); c@278: d_vec_t b(3); c@278: d_vec_t lp_df(df.size()); c@277: c@278: //equivalent in matlab to [b,a] = butter(2,0.4); c@278: a[0] = 1.0000; c@278: a[1] = -0.3695; c@278: a[2] = 0.1958; c@278: b[0] = 0.2066; c@278: b[1] = 0.4131; c@278: b[2] = 0.2066; luis@327: c@278: double inp1 = 0.; c@278: double inp2 = 0.; c@278: double out1 = 0.; c@278: double out2 = 0.; c@277: c@277: c@278: // forwards filtering c@295: for (unsigned int i = 0;i < df.size();i++) c@278: { c@278: lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; c@278: inp2 = inp1; c@278: inp1 = df[i]; c@278: out2 = out1; c@278: out1 = lp_df[i]; c@278: } c@277: c@278: // copy forwards filtering to df... c@278: // but, time-reversed, ready for backwards filtering c@295: for (unsigned int i = 0;i < df.size();i++) c@278: { c@278: df[i] = lp_df[df.size()-i-1]; c@278: } c@277: c@295: for (unsigned int i = 0;i < df.size();i++) c@278: { luis@327: lp_df[i] = 0.; c@278: } c@277: c@278: inp1 = 0.; inp2 = 0.; c@278: out1 = 0.; out2 = 0.; c@277: c@277: // backwards filetering on time-reversed df c@295: for (unsigned int i = 0;i < df.size();i++) c@278: { c@278: lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; c@278: inp2 = inp1; c@278: inp1 = df[i]; c@278: out2 = out1; c@278: out1 = lp_df[i]; c@278: } c@277: c@277: // write the re-reversed (i.e. forward) version back to df c@295: for (unsigned int i = 0;i < df.size();i++) c@278: { c@278: df[i] = lp_df[df.size()-i-1]; c@278: } c@277: } c@277: c@277: luis@327: // MEPD 28/11/12 luis@327: // This function now allows for a user to specify an inputtempo (in BPM) luis@327: // and a flag "constraintempo" which replaces the general rayleigh weighting for periodicities luis@327: // with a gaussian which is centered around the input tempo luis@327: // Note, if inputtempo = 120 and constraintempo = false, then functionality is luis@327: // as it was before c@277: void c@304: TempoTrackV2::calculateBeatPeriod(const vector &df, c@304: vector &beat_period, luis@327: vector &tempi, luis@327: double inputtempo, bool constraintempo) c@277: { c@278: // to follow matlab.. split into 512 sample frames with a 128 hop size c@278: // calculate the acf, c@278: // then the rcf.. and then stick the rcfs as columns of a matrix c@278: // then call viterbi decoding with weight vector and transition matrix c@278: // and get best path c@277: c@295: unsigned int wv_len = 128; luis@327: luis@327: // MEPD 28/11/12 luis@327: // the default value of inputtempo in the beat tracking plugin is 120 luis@327: // so if the user specifies a different inputtempo, the rayparam will be updated luis@327: // accordingly. luis@327: // note: 60*44100/512 is a magic number luis@327: // this might (will?) break if a user specifies a different frame rate for the onset detection function luis@327: double rayparam = (60*44100/512)/inputtempo; luis@327: luis@327: // these debug statements can be removed. luis@327: std::cout << "inputtempo" << inputtempo << std::endl; luis@327: std::cout << "rayparam" << rayparam << std::endl; luis@327: std::cout << "constraintempo" << constraintempo << std::endl; c@277: c@278: // make rayleigh weighting curve c@278: d_vec_t wv(wv_len); luis@327: luis@327: // check whether or not to use rayleigh weighting (if constraintempo is false) luis@327: // or use gaussian weighting it (constraintempo is true) luis@327: if (constraintempo) c@277: { luis@327: for (unsigned int i=0; i (i)-rayparam),2.)) / (2.*pow(rayparam/4.,2.)) ); luis@327: } luis@327: } luis@327: else luis@327: { luis@327: for (unsigned int i=0; i (i) / pow(rayparam,2.)) * exp((-1.*pow(-static_cast (i),2.)) / (2.*pow(rayparam,2.))); luis@327: } c@277: } c@277: c@278: // beat tracking frame size (roughly 6 seconds) and hop (1.5 seconds) c@295: unsigned int winlen = 512; c@295: unsigned int step = 128; c@278: c@278: // matrix to store output of comb filter bank, increment column of matrix at each frame c@278: d_mat_t rcfmat; c@278: int col_counter = -1; c@278: c@278: // main loop for beat period calculation c@295: for (unsigned int i=0; i+winlen (sum/ (dfframe.size()-lag)); c@278: } c@277: c@278: // now apply comb filtering c@278: int numelem = 4; luis@327: c@295: for (unsigned int i = 2;i < rcf.size();i++) // max beat period c@278: { c@278: for (int a = 1;a <= numelem;a++) // number of comb elements c@278: { c@278: for (int b = 1-a;b <= a-1;b++) // general state using normalisation of comb elements c@278: { c@278: rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.); // calculate value for comb filter row c@278: } c@278: } c@278: } luis@327: c@278: // apply adaptive threshold to rcf c@279: MathUtilities::adaptiveThreshold(rcf); luis@327: c@278: double rcfsum =0.; c@295: for (unsigned int i=0; i(i); c@278: tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) ); c@278: } c@278: } c@277: c@278: // parameters for Viterbi decoding... this part is taken from c@278: // Murphy's matlab c@277: c@278: d_mat_t delta; c@278: i_mat_t psi; c@295: for (unsigned int i=0;i 0 ;t--) c@278: { c@278: bestpath[t] = psi[t+1][bestpath[t+1]]; c@278: } c@277: c@278: // weird but necessary hack -- couldn't get above loop to terminate at t >= 0 c@278: bestpath[0] = psi[1][bestpath[1]]; c@277: c@295: unsigned int lastind = 0; c@295: for (unsigned int i=0; i &df, c@304: const vector &beat_period, luis@327: vector &beats, double alpha, double tightness) c@277: { c@281: if (df.empty() || beat_period.empty()) return; c@281: c@278: d_vec_t cumscore(df.size()); // store cumulative score c@278: i_vec_t backlink(df.size()); // backlink (stores best beat locations at each time instant) c@278: d_vec_t localscore(df.size()); // localscore, for now this is the same as the detection function c@277: c@295: for (unsigned int i=0; i (beat_period[i]); c@278: txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2)); c@277: c@278: // IF IN THE ALLOWED RANGE, THEN LOOK AT CUMSCORE[I+PRANGE_MIN+J c@278: // ELSE LEAVE AT DEFAULT VALUE FROM INITIALISATION: D_VEC_T SCORECANDS (TXWT.SIZE()); c@277: c@278: int cscore_ind = i+prange_min+j; luis@327: if (cscore_ind >= 0) c@278: { c@278: scorecands[j] = txwt[j] * cumscore[cscore_ind]; c@278: } c@278: } c@277: c@278: // find max value and index of maximum value c@278: double vv = get_max_val(scorecands); c@278: int xx = get_max_ind(scorecands); c@277: c@278: cumscore[i] = alpha*vv + (1.-alpha)*localscore[i]; c@278: backlink[i] = i+prange_min+xx; c@280: c@282: // std::cerr << "backlink[" << i << "] <= " << backlink[i] << std::endl; c@278: } c@278: c@278: // STARTING POINT, I.E. LAST BEAT.. PICK A STRONG POINT IN cumscore VECTOR c@278: d_vec_t tmp_vec; c@295: for (unsigned int i=cumscore.size() - beat_period[beat_period.size()-1] ; i= backlink.size()) startpoint = backlink.size()-1; c@281: c@278: // USE BACKLINK TO GET EACH NEW BEAT (TOWARDS THE BEGINNING OF THE FILE) c@278: // BACKTRACKING FROM THE END TO THE BEGINNING.. MAKING SURE NOT TO GO BEFORE SAMPLE 0 c@278: i_vec_t ibeats; c@278: ibeats.push_back(startpoint); c@282: // std::cerr << "startpoint = " << startpoint << std::endl; c@278: while (backlink[ibeats.back()] > 0) c@278: { c@282: // std::cerr << "backlink[" << ibeats.back() << "] = " << backlink[ibeats.back()] << std::endl; c@281: int b = ibeats.back(); c@281: if (backlink[b] == b) break; // shouldn't happen... haha c@281: ibeats.push_back(backlink[b]); c@278: } luis@327: c@278: // REVERSE SEQUENCE OF IBEATS AND STORE AS BEATS c@295: for (unsigned int i=0; i(ibeats[ibeats.size()-i-1]) ); c@278: } c@277: } c@277: c@277: