26 #define EPS 0.0000008 // just some arbitrary small number 29 m_rate(rate), m_increment(increment) {
37 int df_len = int(df.size());
58 for (
int i = 0; i < df_len; i++) {
59 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2;
68 for (
int i = 0; i < df_len; i++) {
69 df[i] = lp_df[df_len - i - 1];
72 for (
int i = 0; i < df_len; i++) {
80 for (
int i = 0; i < df_len; i++) {
81 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2;
89 for (
int i = 0; i < df_len; i++) {
90 df[i] = lp_df[df_len - i - 1];
103 vector<double> &beat_period,
104 vector<double> &tempi,
105 double inputtempo,
bool constraintempo)
121 double rayparam = (60*44100/512)/inputtempo;
128 if (constraintempo) {
129 for (
int i = 0; i < wv_len; i++) {
132 wv[i] = exp( (-1.*pow((
double(i)-rayparam),2.)) / (2.*pow(rayparam/4.,2.)) );
135 for (
int i = 0; i < wv_len; i++) {
138 wv[i] = (double(i) / pow(rayparam,2.)) * exp((-1.*pow(-
double(i),2.)) / (2.*pow(rayparam,2.)));
148 int col_counter = -1;
149 int df_len = int(df.size());
152 for (
int i = 0; i+winlen < df_len; i+=step) {
156 for (
int k=0; k < winlen; k++) {
157 dfframe[k] = df[i+k];
165 for (
int j = 0; j < wv_len; j++) {
166 rcfmat[col_counter].push_back( rcf[j] );
188 int dfframe_len = int(dfframe.size());
189 int rcf_len = int(rcf.size());
193 for (
int lag = 0; lag < dfframe_len; lag++) {
197 for (
int n = 0; n < (dfframe_len - lag); n++) {
198 tmp = dfframe[n] * dfframe[n + lag];
201 acf[lag] = double(sum/ (dfframe_len - lag));
207 for (
int i = 2; i < rcf_len; i++) {
208 for (
int a = 1; a <= numelem; a++) {
209 for (
int b = 1-a; b <= a-1; b++) {
210 rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.);
219 for (
int i = 0; i < rcf_len; i++) {
225 for (
int i = 0; i < rcf_len; i++) {
226 rcf[i] /= (rcfsum +
EPS);
236 int wv_len = int(wv.size());
240 for (
int i = 0; i < wv_len; i++) {
242 for (
int j = 0; j < wv_len; j++) {
243 tmat[i].push_back(0.);
251 for (
int i = 20; i < wv_len - 20; i++) {
252 for (
int j = 20; j < wv_len - 20; j++) {
253 double mu = double(i);
254 tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) );
263 for (
int i = 0; i < int(rcfmat.size()); i++) {
266 for (
int j = 0; j < int(rcfmat[i].size()); j++) {
267 delta[i].push_back(0.);
272 int T = int(delta.size());
276 int Q = int(delta[0].size());
279 for (
int j = 0; j < Q; j++) {
280 delta[0][j] = wv[j] * rcfmat[0][j];
284 double deltasum = 0.;
285 for (
int i = 0; i < Q; i++) {
286 deltasum += delta[0][i];
288 for (
int i = 0; i < Q; i++) {
289 delta[0][i] /= (deltasum +
EPS);
292 for (
int t=1; t < T; t++)
296 for (
int j = 0; j < Q; j++) {
297 for (
int i = 0; i < Q; i++) {
298 tmp_vec[i] = delta[t-1][i] * tmat[j][i];
305 delta[t][j] *= rcfmat[t][j];
309 double deltasum = 0.;
310 for (
int i = 0; i < Q; i++) {
311 deltasum += delta[t][i];
313 for (
int i = 0; i < Q; i++) {
314 delta[t][i] /= (deltasum +
EPS);
320 for (
int i = 0; i < Q; i++) {
321 tmp_vec[i] = delta[T-1][i];
328 for (
int t=T-2; t>0 ;t--) {
329 bestpath[t] = psi[t+1][bestpath[t+1]];
333 bestpath[0] = psi[1][bestpath[1]];
336 for (
int i = 0; i < T; i++) {
338 for (
int j = 0; j < step; j++) {
340 beat_period[lastind] = bestpath[i];
346 for (
int i = lastind; i < int(beat_period.size()); i++) {
347 beat_period[i] = beat_period[lastind];
350 for (
int i = 0; i < int(beat_period.size()); i++) {
359 int df_len = int(df.size());
361 for (
int i = 0; i < df_len; i++) {
362 if (maxval < df[i]) {
375 int df_len = int(df.size());
377 for (
int i = 0; i < df_len; i++) {
378 if (maxval < df[i]) {
391 int df_len = int(df.size());
393 for (
int i = 0; i < df_len; i++) {
397 for (
int i = 0; i < df_len; i++) {
408 const vector<double> &beat_period,
409 vector<double> &beats,
double alpha,
double tightness)
411 if (df.empty() || beat_period.empty())
return;
413 int df_len = int(df.size());
419 for (
int i = 0; i < df_len; i++) {
420 localscore[i] = df[i];
432 for (
int i = 0; i < df_len; i++) {
434 int prange_min = -2*beat_period[i];
435 int prange_max = round(-0.5*beat_period[i]);
438 int txwt_len = prange_max - prange_min + 1;
442 for (
int j = 0; j < txwt_len; j++) {
444 double mu = double(beat_period[i]);
445 txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2));
450 int cscore_ind = i + prange_min + j;
451 if (cscore_ind >= 0) {
452 scorecands[j] = txwt[j] * cumscore[cscore_ind];
460 cumscore[i] = alpha*vv + (1.-alpha)*localscore[i];
461 backlink[i] = i+prange_min+xx;
468 for (
int i = df_len - beat_period[beat_period.size()-1] ; i < df_len; i++) {
469 tmp_vec.push_back(cumscore[i]);
473 df_len - beat_period[beat_period.size()-1] ;
476 if (startpoint >=
int(backlink.size())) {
477 startpoint = int(backlink.size()) - 1;
483 ibeats.push_back(startpoint);
485 while (backlink[ibeats.back()] > 0) {
487 int b = ibeats.back();
488 if (backlink[b] == b)
break;
489 ibeats.push_back(backlink[b]);
493 for (
int i = 0; i < int(ibeats.size()); i++) {
494 beats.push_back(
double(ibeats[ibeats.size() - i - 1]));
static void adaptiveThreshold(std::vector< double > &data)
Threshold the input/output vector data against a moving-mean average filter.
void normalise_vec(d_vec_t &df)
void viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &bp, d_vec_t &tempi)
double get_max_val(const d_vec_t &df)
std::vector< std::vector< int > > i_mat_t
int get_max_ind(const d_vec_t &df)
std::vector< std::vector< double > > d_mat_t
void calculateBeatPeriod(const std::vector< double > &df, std::vector< double > &beatPeriod, std::vector< double > &tempi)
void get_rcf(const d_vec_t &dfframe, const d_vec_t &wv, d_vec_t &rcf)
std::vector< double > d_vec_t
TempoTrackV2(float sampleRate, int dfIncrement)
Construct a tempo tracker that will operate on beat detection function data calculated from audio at ...
void filter_df(d_vec_t &df)
void calculateBeats(const std::vector< double > &df, const std::vector< double > &beatPeriod, std::vector< double > &beats)
std::vector< int > i_vec_t