From 77db4251fcafaa50893011c4325d269699dd9d11 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 26 Nov 2013 04:18:57 +0100 Subject: [PATCH] src/tempo/tempo.c: fix for different samplerates --- src/tempo/beattracking.c | 15 +++++++++------ src/tempo/beattracking.h | 3 ++- src/tempo/tempo.c | 6 +++--- tests/src/tempo/test-beattracking.c | 2 +- tests/src/tempo/test-tempo.c | 13 +++++++------ 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/tempo/beattracking.c b/src/tempo/beattracking.c index 1c35641f..ac422166 100644 --- a/src/tempo/beattracking.c +++ b/src/tempo/beattracking.c @@ -31,6 +31,8 @@ void aubio_beattracking_checkstate (aubio_beattracking_t * bt); struct _aubio_beattracking_t { + uint_t hop_size; /** length of one tempo detection function sample, in audio samples */ + uint_t samplerate; /** samplerate of the original signal */ fvec_t *rwv; /** rayleigh weighting for beat period in general model */ fvec_t *dfwv; /** exponential weighting for beat alignment in general model */ fvec_t *gwv; /** gaussian weighting for beat period in context dependant model */ @@ -54,14 +56,15 @@ struct _aubio_beattracking_t }; aubio_beattracking_t * -new_aubio_beattracking (uint_t winlen) +new_aubio_beattracking (uint_t winlen, uint_t hop_size, uint_t samplerate) { aubio_beattracking_t *p = AUBIO_NEW (aubio_beattracking_t); uint_t i = 0; - /* parameter for rayleigh weight vector - sets preferred tempo to - * 120bpm [43] */ - smpl_t rayparam = 48. / 512. * winlen; + p->hop_size = hop_size; + p->samplerate = samplerate; + /* default value for rayleigh weighting - sets preferred tempo to 120bpm */ + smpl_t rayparam = 60. * samplerate / 120. / hop_size; smpl_t dfwvnorm = EXP ((LOG (2.0) / rayparam) * (winlen + 2)); /* length over which beat period is found [128] */ uint_t laglen = winlen / 4; @@ -414,8 +417,8 @@ aubio_beattracking_checkstate (aubio_beattracking_t * bt) smpl_t aubio_beattracking_get_bpm (aubio_beattracking_t * bt) { - if (bt->bp != 0 && bt->timesig != 0 && bt->counter == 0 && bt->flagstep == 0) { - return 5168. / fvec_quadratic_peak_pos (bt->acfout, bt->bp); + if (bt->bp != 0) { + return 60. * bt->samplerate/ bt->bp / bt->hop_size; } else { return 0.; } diff --git a/src/tempo/beattracking.h b/src/tempo/beattracking.h index dcfa3cbd..a8d7e439 100644 --- a/src/tempo/beattracking.h +++ b/src/tempo/beattracking.h @@ -51,7 +51,8 @@ typedef struct _aubio_beattracking_t aubio_beattracking_t; \param hop_size number of onset detection samples [512] */ -aubio_beattracking_t * new_aubio_beattracking(uint_t hop_size); +aubio_beattracking_t * new_aubio_beattracking(uint_t winlen, uint_t hop_size, + uint_t samplerate); /** track the beat diff --git a/src/tempo/tempo.c b/src/tempo/tempo.c index ad8693b6..bb6e84b6 100644 --- a/src/tempo/tempo.c +++ b/src/tempo/tempo.c @@ -141,12 +141,12 @@ aubio_tempo_t * new_aubio_tempo (char_t * onset_mode, { aubio_tempo_t * o = AUBIO_NEW(aubio_tempo_t); o->samplerate = samplerate; - o->winlen = SQR(512)/hop_size; + /* length of observations, worth about 6 seconds */ + o->winlen = aubio_next_power_of_two(5.8 * samplerate / hop_size); o->step = o->winlen/4; o->blockpos = 0; o->threshold = 0.3; o->silence = -90.; - o->blockpos = 0; o->total_frames = 0; o->last_beat = 0; o->delay = 0; @@ -159,7 +159,7 @@ aubio_tempo_t * new_aubio_tempo (char_t * onset_mode, aubio_peakpicker_set_threshold (o->pp, o->threshold); o->od = new_aubio_specdesc(onset_mode,buf_size); o->of = new_fvec(1); - o->bt = new_aubio_beattracking(o->winlen); + o->bt = new_aubio_beattracking(o->winlen, o->hop_size, o->samplerate); o->onset = new_fvec(1); /*if (usedoubled) { o2 = new_aubio_specdesc(type_onset2,buffer_size); diff --git a/tests/src/tempo/test-beattracking.c b/tests/src/tempo/test-beattracking.c index 92550907..84d41b6b 100644 --- a/tests/src/tempo/test-beattracking.c +++ b/tests/src/tempo/test-beattracking.c @@ -11,7 +11,7 @@ int main () fvec_t * out = new_fvec (win_s / 4); // output beat position // create beattracking object - aubio_beattracking_t * tempo = new_aubio_beattracking(win_s); + aubio_beattracking_t * tempo = new_aubio_beattracking(win_s, 256, 44100); smpl_t bpm, confidence; diff --git a/tests/src/tempo/test-tempo.c b/tests/src/tempo/test-tempo.c index 7e32f711..fc066e10 100644 --- a/tests/src/tempo/test-tempo.c +++ b/tests/src/tempo/test-tempo.c @@ -8,15 +8,16 @@ int main (int argc, char **argv) err = 2; PRINT_ERR("not enough arguments\n"); PRINT_MSG("read a wave file as a mono vector\n"); - PRINT_MSG("usage: %s [samplerate] [hop_size]\n", argv[0]); + PRINT_MSG("usage: %s [samplerate] [win_size] [hop_size]\n", argv[0]); return err; } uint_t samplerate = 0; - uint_t win_s = 1024; // window size - uint_t hop_size = win_s / 4; + if ( argc >= 3 ) samplerate = atoi(argv[2]); + uint_t win_size = 1024; // window size + if ( argc >= 4 ) win_size = atoi(argv[3]); + uint_t hop_size = win_size / 4; + if ( argc >= 5 ) hop_size = atoi(argv[4]); uint_t n_frames = 0, read = 0; - if ( argc == 3 ) samplerate = atoi(argv[2]); - if ( argc == 4 ) hop_size = atoi(argv[3]); char_t *source_path = argv[1]; aubio_source_t * source = new_aubio_source(source_path, samplerate, hop_size); @@ -29,7 +30,7 @@ int main (int argc, char **argv) fvec_t * out = new_fvec (2); // output position // create tempo object - aubio_tempo_t * o = new_aubio_tempo("default", win_s, hop_size, samplerate); + aubio_tempo_t * o = new_aubio_tempo("default", win_size, hop_size, samplerate); do { // put some fresh data in input vector -- 2.11.0