source: src/tempo/beattracking.c @ 4dd7816

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 4dd7816 was 2823389, checked in by Paul Brossier <piem@piem.org>, 11 years ago

src/tempo/beattracking.c: do nothing if period is zero

  • Property mode set to 100644
File size: 12.1 KB
Line 
1/*
2  Copyright (C) 2005-2009 Matthew Davies and Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "aubio_priv.h"
22#include "fvec.h"
23#include "mathutils.h"
24#include "tempo/beattracking.h"
25
26/** define to 1 to print out tracking difficulties */
27#define AUBIO_BEAT_WARNINGS 0
28
29uint_t fvec_gettimesig (fvec_t * acf, uint_t acflen, uint_t gp);
30void aubio_beattracking_checkstate (aubio_beattracking_t * bt);
31
32struct _aubio_beattracking_t
33{
34  fvec_t *rwv;           /** rayleigh weighting for beat period in general model */
35  fvec_t *dfwv;          /** exponential weighting for beat alignment in general model */
36  fvec_t *gwv;           /** gaussian weighting for beat period in context dependant model */
37  fvec_t *phwv;          /** gaussian weighting for beat alignment in context dependant model */
38  fvec_t *dfrev;         /** reversed onset detection function */
39  fvec_t *acf;           /** vector for autocorrelation function (of current detection function frame) */
40  fvec_t *acfout;        /** store result of passing acf through s.i.c.f.b. */
41  fvec_t *phout;
42  uint_t timesig;        /** time signature of input, set to zero until context dependent model activated */
43  uint_t step;
44  uint_t rayparam;       /** Rayleigh parameter */
45  smpl_t lastbeat;
46  sint_t counter;
47  uint_t flagstep;
48  smpl_t g_var;
49  smpl_t gp;
50  smpl_t bp;
51  smpl_t rp;
52  smpl_t rp1;
53  smpl_t rp2;
54};
55
56aubio_beattracking_t *
57new_aubio_beattracking (uint_t winlen)
58{
59
60  aubio_beattracking_t *p = AUBIO_NEW (aubio_beattracking_t);
61  uint_t i = 0;
62  /* parameter for rayleigh weight vector - sets preferred tempo to
63   * 120bpm [43] */
64  smpl_t rayparam = 48. / 512. * winlen;
65  smpl_t dfwvnorm = EXP ((LOG (2.0) / rayparam) * (winlen + 2));
66  /* length over which beat period is found [128] */
67  uint_t laglen = winlen / 4;
68  /* step increment - both in detection function samples -i.e. 11.6ms or
69   * 1 onset frame [128] */
70  uint_t step = winlen / 4;     /* 1.5 seconds */
71
72  p->lastbeat = 0;
73  p->counter = 0;
74  p->flagstep = 0;
75  p->g_var = 3.901;             // constthresh empirically derived!
76  p->rp = 1;
77  p->gp = 0;
78
79  p->rayparam = rayparam;
80  p->step = step;
81  p->rwv = new_fvec (laglen);
82  p->gwv = new_fvec (laglen);
83  p->dfwv = new_fvec (winlen);
84  p->dfrev = new_fvec (winlen);
85  p->acf = new_fvec (winlen);
86  p->acfout = new_fvec (laglen);
87  p->phwv = new_fvec (2 * laglen);
88  p->phout = new_fvec (winlen);
89
90  p->timesig = 0;
91
92  /* exponential weighting, dfwv = 0.5 when i =  43 */
93  for (i = 0; i < winlen; i++) {
94    p->dfwv->data[i] = (EXP ((LOG (2.0) / rayparam) * (i + 1)))
95        / dfwvnorm;
96  }
97
98  for (i = 0; i < (laglen); i++) {
99    p->rwv->data[i] = ((smpl_t) (i + 1.) / SQR ((smpl_t) rayparam)) *
100        EXP ((-SQR ((smpl_t) (i + 1.)) / (2. * SQR ((smpl_t) rayparam))));
101  }
102
103  return p;
104
105}
106
107void
108del_aubio_beattracking (aubio_beattracking_t * p)
109{
110  del_fvec (p->rwv);
111  del_fvec (p->gwv);
112  del_fvec (p->dfwv);
113  del_fvec (p->dfrev);
114  del_fvec (p->acf);
115  del_fvec (p->acfout);
116  del_fvec (p->phwv);
117  del_fvec (p->phout);
118  AUBIO_FREE (p);
119}
120
121
122void
123aubio_beattracking_do (aubio_beattracking_t * bt, fvec_t * dfframe,
124    fvec_t * output)
125{
126
127  uint_t i, k;
128  uint_t step = bt->step;
129  uint_t laglen = bt->rwv->length;
130  uint_t winlen = bt->dfwv->length;
131  uint_t maxindex = 0;
132  //number of harmonics in shift invariant comb filterbank
133  uint_t numelem = 4;
134
135  smpl_t phase;                 // beat alignment (step - lastbeat)
136  smpl_t beat;                  // beat position
137  smpl_t bp;                    // beat period
138  uint_t a, b;                  // used to build shift invariant comb filterbank
139  uint_t kmax;                  // number of elements used to find beat phase
140
141  /* copy dfframe, apply detection function weighting, and revert */
142  fvec_copy (dfframe, bt->dfrev);
143  fvec_weight (bt->dfrev, bt->dfwv);
144  fvec_rev (bt->dfrev);
145
146  /* compute autocorrelation function */
147  aubio_autocorr (dfframe, bt->acf);
148
149  /* if timesig is unknown, use metrically unbiased version of filterbank */
150  if (!bt->timesig) {
151    numelem = 4;
152  } else {
153    numelem = bt->timesig;
154  }
155
156  /* first and last output values are left intentionally as zero */
157  fvec_zeros (bt->acfout);
158
159  /* compute shift invariant comb filterbank */
160  for (i = 1; i < laglen - 1; i++) {
161    for (a = 1; a <= numelem; a++) {
162      for (b = 1; b < 2 * a; b++) {
163        bt->acfout->data[i] += bt->acf->data[i * a + b - 1]
164            * 1. / (2. * a - 1.);
165      }
166    }
167  }
168  /* apply Rayleigh weight */
169  fvec_weight (bt->acfout, bt->rwv);
170
171  /* find non-zero Rayleigh period */
172  maxindex = fvec_max_elem (bt->acfout);
173  bt->rp = maxindex ? fvec_quadratic_peak_pos (bt->acfout, maxindex) : 1;
174  //rp = (maxindex==127) ? 43 : maxindex; //rayparam
175  bt->rp = (maxindex == bt->acfout->length - 1) ? bt->rayparam : maxindex;      //rayparam
176
177  /* activate biased filterbank */
178  aubio_beattracking_checkstate (bt);
179#if 0                           // debug metronome mode
180  bt->bp = 36.9142;
181#endif
182  bp = bt->bp;
183  /* end of biased filterbank */
184
185  if (bp == 0) {
186    output->data[0] = 0;
187    return;
188  }
189
190  /* deliberate integer operation, could be set to 3 max eventually */
191  kmax = FLOOR (winlen / bp);
192
193  /* initialize output */
194  fvec_zeros (bt->phout);
195  for (i = 0; i < bp; i++) {
196    for (k = 0; k < kmax; k++) {
197      bt->phout->data[i] += bt->dfrev->data[i + (uint_t) ROUND (bp * k)];
198    }
199  }
200  fvec_weight (bt->phout, bt->phwv);
201
202  /* find Rayleigh period */
203  maxindex = fvec_max_elem (bt->phout);
204  if (maxindex >= winlen - 1) {
205#if AUBIO_BEAT_WARNINGS
206    AUBIO_WRN ("no idea what this groove's phase is\n");
207#endif /* AUBIO_BEAT_WARNINGS */
208    phase = step - bt->lastbeat;
209  } else {
210    phase = fvec_quadratic_peak_pos (bt->phout, maxindex);
211  }
212  /* take back one frame delay */
213  phase += 1.;
214#if 0                           // debug metronome mode
215  phase = step - bt->lastbeat;
216#endif
217
218  /* reset output */
219  fvec_zeros (output);
220
221  i = 1;
222  beat = bp - phase;
223
224  // AUBIO_DBG ("bp: %f, phase: %f, lastbeat: %f, step: %d, winlen: %d\n",
225  //    bp, phase, bt->lastbeat, step, winlen);
226
227  /* the next beat will be earlier than 60% of the tempo period
228    skip this one */
229  if ( ( step - bt->lastbeat - phase ) < -0.40 * bp ) {
230#if AUBIO_BEAT_WARNINGS
231    AUBIO_WRN ("back off-beat error, skipping this beat\n");
232#endif /* AUBIO_BEAT_WARNINGS */
233    beat += bp;
234  }
235
236  /* start counting the beats */
237  while (beat + bp < 0) {
238    beat += bp;
239  }
240
241  if (beat >= 0) {
242    //AUBIO_DBG ("beat: %d, %f, %f\n", i, bp, beat);
243    output->data[i] = beat;
244    i++;
245  }
246
247  while (beat + bp <= step) {
248    beat += bp;
249    //AUBIO_DBG ("beat: %d, %f, %f\n", i, bp, beat);
250    output->data[i] = beat;
251    i++;
252  }
253
254  bt->lastbeat = beat;
255  /* store the number of beats in this frame as the first element */
256  output->data[0] = i;
257}
258
259uint_t
260fvec_gettimesig (fvec_t * acf, uint_t acflen, uint_t gp)
261{
262  sint_t k = 0;
263  smpl_t three_energy = 0., four_energy = 0.;
264  if (acflen > 6 * gp + 2) {
265    for (k = -2; k < 2; k++) {
266      three_energy += acf->data[3 * gp + k];
267      four_energy += acf->data[4 * gp + k];
268    }
269  } else {
270    /*Expanded to be more accurate in time sig estimation */
271    for (k = -2; k < 2; k++) {
272      three_energy += acf->data[3 * gp + k] + acf->data[6 * gp + k];
273      four_energy += acf->data[4 * gp + k] + acf->data[2 * gp + k];
274    }
275  }
276  return (three_energy > four_energy) ? 3 : 4;
277}
278
279void
280aubio_beattracking_checkstate (aubio_beattracking_t * bt)
281{
282  uint_t i, j, a, b;
283  uint_t flagconst = 0;
284  sint_t counter = bt->counter;
285  uint_t flagstep = bt->flagstep;
286  smpl_t gp = bt->gp;
287  smpl_t bp = bt->bp;
288  smpl_t rp = bt->rp;
289  smpl_t rp1 = bt->rp1;
290  smpl_t rp2 = bt->rp2;
291  uint_t laglen = bt->rwv->length;
292  uint_t acflen = bt->acf->length;
293  uint_t step = bt->step;
294  fvec_t *acf = bt->acf;
295  fvec_t *acfout = bt->acfout;
296
297  if (gp) {
298    // doshiftfbank again only if context dependent model is in operation
299    //acfout = doshiftfbank(acf,gwv,timesig,laglen,acfout);
300    //don't need acfout now, so can reuse vector
301    // gwv is, in first loop, definitely all zeros, but will have
302    // proper values when context dependent model is activated
303    fvec_zeros (acfout);
304    for (i = 1; i < laglen - 1; i++) {
305      for (a = 1; a <= bt->timesig; a++) {
306        for (b = 1; b < 2 * a; b++) {
307          acfout->data[i] += acf->data[i * a + b - 1];
308        }
309      }
310    }
311    fvec_weight (acfout, bt->gwv);
312    gp = fvec_quadratic_peak_pos (acfout, fvec_max_elem (acfout));
313    /*
314       while(gp<32) gp =gp*2;
315       while(gp>64) gp = gp/2;
316     */
317  } else {
318    //still only using general model
319    gp = 0;
320  }
321
322  //now look for step change - i.e. a difference between gp and rp that
323  // is greater than 2*constthresh - always true in first case, since gp = 0
324  if (counter == 0) {
325    if (ABS (gp - rp) > 2. * bt->g_var) {
326      flagstep = 1;             // have observed  step change.
327      counter = 3;              // setup 3 frame counter
328    } else {
329      flagstep = 0;
330    }
331  }
332  //i.e. 3rd frame after flagstep initially set
333  if (counter == 1 && flagstep == 1) {
334    //check for consistency between previous beatperiod values
335    if (ABS (2. * rp - rp1 - rp2) < bt->g_var) {
336      //if true, can activate context dependent model
337      flagconst = 1;
338      counter = 0;              // reset counter and flagstep
339    } else {
340      //if not consistent, then don't flag consistency!
341      flagconst = 0;
342      counter = 2;              // let it look next time
343    }
344  } else if (counter > 0) {
345    //if counter doesn't = 1,
346    counter = counter - 1;
347  }
348
349  rp2 = rp1;
350  rp1 = rp;
351
352  if (flagconst) {
353    /* first run of new hypothesis */
354    gp = rp;
355    bt->timesig = fvec_gettimesig (acf, acflen, gp);
356    for (j = 0; j < laglen; j++)
357      bt->gwv->data[j] =
358          EXP (-.5 * SQR ((smpl_t) (j + 1. - gp)) / SQR (bt->g_var));
359    flagconst = 0;
360    bp = gp;
361    /* flat phase weighting */
362    fvec_ones (bt->phwv);
363  } else if (bt->timesig) {
364    /* context dependant model */
365    bp = gp;
366    /* gaussian phase weighting */
367    if (step > bt->lastbeat) {
368      for (j = 0; j < 2 * laglen; j++) {
369        bt->phwv->data[j] =
370            EXP (-.5 * SQR ((smpl_t) (1. + j - step +
371                    bt->lastbeat)) / (bp / 8.));
372      }
373    } else {
374      //AUBIO_DBG("NOT using phase weighting as step is %d and lastbeat %d \n",
375      //                step,bt->lastbeat);
376      fvec_ones (bt->phwv);
377    }
378  } else {
379    /* initial state */
380    bp = rp;
381    /* flat phase weighting */
382    fvec_ones (bt->phwv);
383  }
384
385  /* do some further checks on the final bp value */
386
387  /* if tempo is > 206 bpm, half it */
388  while (0 < bp && bp < 25) {
389#if AUBIO_BEAT_WARNINGS
390    AUBIO_WRN ("doubling from %f (%f bpm) to %f (%f bpm)\n",
391        bp, 60.*44100./512./bp, bp/2., 60.*44100./512./bp/2. );
392    //AUBIO_DBG("warning, halving the tempo from %f\n", 60.*samplerate/hopsize/bp);
393#endif /* AUBIO_BEAT_WARNINGS */
394    bp = bp * 2;
395  }
396
397  //AUBIO_DBG("tempo:\t%3.5f bpm | ", 5168./bp);
398
399  /* smoothing */
400  //bp = (uint_t) (0.8 * (smpl_t)bp + 0.2 * (smpl_t)bp2);
401  //AUBIO_DBG("tempo:\t%3.5f bpm smoothed | bp2 %d | bp %d | ", 5168./bp, bp2, bp);
402  //bp2 = bp;
403  //AUBIO_DBG("time signature: %d \n", bt->timesig);
404  bt->counter = counter;
405  bt->flagstep = flagstep;
406  bt->gp = gp;
407  bt->bp = bp;
408  bt->rp1 = rp1;
409  bt->rp2 = rp2;
410}
411
412smpl_t
413aubio_beattracking_get_bpm (aubio_beattracking_t * bt)
414{
415  if (bt->bp != 0 && bt->timesig != 0 && bt->counter == 0 && bt->flagstep == 0) {
416    return 5168. / fvec_quadratic_peak_pos (bt->acfout, bt->bp);
417  } else {
418    return 0.;
419  }
420}
421
422smpl_t
423aubio_beattracking_get_confidence (aubio_beattracking_t * bt)
424{
425  if (bt->gp) {
426    return fvec_max (bt->acfout);
427  } else {
428    return 0.;
429  }
430}
Note: See TracBrowser for help on using the repository browser.