shithub: aubio

Download patch

ref: 77db4251fcafaa50893011c4325d269699dd9d11
parent: 18f14f96f086304b5047922062199f6a66562ed6
author: Paul Brossier <[email protected]>
date: Mon Nov 25 23:18:57 EST 2013

src/tempo/tempo.c: fix for different samplerates

--- a/src/tempo/beattracking.c
+++ b/src/tempo/beattracking.c
@@ -31,6 +31,8 @@
 
 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 @@
 };
 
 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 @@
 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.;
   }
--- a/src/tempo/beattracking.h
+++ b/src/tempo/beattracking.h
@@ -51,7 +51,8 @@
   \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 
 
--- a/src/tempo/tempo.c
+++ b/src/tempo/tempo.c
@@ -141,12 +141,12 @@
 {
   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_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);
--- a/tests/src/tempo/test-beattracking.c
+++ b/tests/src/tempo/test-beattracking.c
@@ -11,7 +11,7 @@
   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;
 
--- a/tests/src/tempo/test-tempo.c
+++ b/tests/src/tempo/test-tempo.c
@@ -8,15 +8,16 @@
     err = 2;
     PRINT_ERR("not enough arguments\n");
     PRINT_MSG("read a wave file as a mono vector\n");
-    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
+    PRINT_MSG("usage: %s <source_path> [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 @@
   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