source: src/beattracking.c @ acf2ec07

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since acf2ec07 was 0ce9acc3, checked in by Paul Brossier <piem@altern.org>, 19 years ago

updated beattracking.c ad aubiotrack.c to support variable hopsize

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