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