[cf83555] | 1 | /** |
---|
| 2 | * |
---|
| 3 | * a puredata wrapper for aubio tempo detection functions |
---|
| 4 | * |
---|
| 5 | * Thanks to Johannes M Zmolnig for writing the excellent HOWTO: |
---|
| 6 | * http://iem.kug.ac.at/pd/externals-HOWTO/ |
---|
| 7 | * |
---|
| 8 | * */ |
---|
| 9 | |
---|
| 10 | #include <m_pd.h> |
---|
| 11 | #include <aubio.h> |
---|
| 12 | |
---|
| 13 | char aubiotempo_version[] = "aubiotempo~ version 0.1"; |
---|
| 14 | |
---|
| 15 | static t_class *aubiotempo_tilde_class; |
---|
| 16 | |
---|
| 17 | void aubiotempo_tilde_setup (void); |
---|
| 18 | |
---|
| 19 | typedef struct _aubiotempo_tilde |
---|
| 20 | { |
---|
| 21 | t_object x_obj; |
---|
| 22 | t_float threshold; |
---|
| 23 | t_float threshold2; |
---|
| 24 | t_int pos; /*frames%dspblocksize*/ |
---|
| 25 | t_int bufsize; |
---|
| 26 | t_int hopsize; |
---|
| 27 | t_int pos2; |
---|
| 28 | t_int winlen; |
---|
| 29 | t_int step; |
---|
| 30 | aubio_onsetdetection_t *o; |
---|
| 31 | aubio_pvoc_t * pv; |
---|
| 32 | aubio_pickpeak_t * parms; |
---|
| 33 | aubio_beattracking_t * bt; |
---|
| 34 | fvec_t *out; |
---|
| 35 | fvec_t *dfframe; |
---|
| 36 | fvec_t *vec; |
---|
| 37 | fvec_t *onset; |
---|
| 38 | cvec_t *fftgrain; |
---|
| 39 | t_outlet *tempobang; |
---|
| 40 | t_outlet *onsetbang; |
---|
| 41 | } t_aubiotempo_tilde; |
---|
| 42 | |
---|
| 43 | static t_int *aubiotempo_tilde_perform(t_int *w) |
---|
| 44 | { |
---|
| 45 | t_aubiotempo_tilde *x = (t_aubiotempo_tilde *)(w[1]); |
---|
| 46 | t_sample *in = (t_sample *)(w[2]); |
---|
| 47 | int n = (int)(w[3]); |
---|
| 48 | int winlen = x->winlen; |
---|
| 49 | int step = x->step; |
---|
| 50 | int j,i,isonset,istactus; |
---|
| 51 | smpl_t * btoutput = x->out->data[0]; |
---|
| 52 | for (j=0;j<n;j++) { |
---|
| 53 | /* write input to datanew */ |
---|
| 54 | fvec_write_sample(x->vec, in[j], 0, x->pos); |
---|
| 55 | /*time for fft*/ |
---|
| 56 | if (x->pos == x->hopsize-1) { |
---|
| 57 | /* block loop */ |
---|
| 58 | aubio_pvoc_do (x->pv,x->vec, x->fftgrain); |
---|
| 59 | aubio_onsetdetection(x->o,x->fftgrain, x->onset); |
---|
| 60 | if (x->pos2 == step -1 ) { |
---|
| 61 | aubio_beattracking_do(x->bt,x->dfframe,x->out); |
---|
| 62 | /* rotate dfframe */ |
---|
| 63 | for (i = 0 ; i < winlen - step; i++ ) |
---|
| 64 | x->dfframe->data[0][i] = x->dfframe->data[0][i+step]; |
---|
| 65 | for (i = winlen - step ; i < winlen; i++ ) |
---|
| 66 | x->dfframe->data[0][i] = 0.; |
---|
| 67 | x->pos2 = -1; |
---|
| 68 | } |
---|
| 69 | x->pos2++; |
---|
| 70 | isonset = aubio_peakpick_pimrt_wt(x->onset,x->parms,&(x->dfframe->data[0][winlen - step + x->pos2])); |
---|
| 71 | /* end of second level loop */ |
---|
| 72 | istactus = 0; |
---|
| 73 | i=0; |
---|
| 74 | for (i = 1; i < btoutput[0]; i++ ) { |
---|
| 75 | /* test for silence */ |
---|
| 76 | if (aubio_silence_detection(x->vec, x->threshold2)==1) { |
---|
| 77 | isonset = 0; istactus = 0; |
---|
| 78 | } else { |
---|
| 79 | if (x->pos2 == btoutput[i]) { |
---|
| 80 | outlet_bang(x->tempobang); |
---|
| 81 | } |
---|
| 82 | if (isonset) { |
---|
| 83 | outlet_bang(x->onsetbang); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /* end of block loop */ |
---|
| 89 | x->pos = -1; /* so it will be zero next j loop */ |
---|
| 90 | } |
---|
| 91 | x->pos++; |
---|
| 92 | } |
---|
| 93 | return (w+4); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | static void aubiotempo_tilde_dsp(t_aubiotempo_tilde *x, t_signal **sp) |
---|
| 97 | { |
---|
| 98 | dsp_add(aubiotempo_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | static void aubiotempo_tilde_debug(t_aubiotempo_tilde *x) |
---|
| 102 | { |
---|
| 103 | post("aubiotempo~ bufsize:\t%d", x->bufsize); |
---|
| 104 | post("aubiotempo~ hopsize:\t%d", x->hopsize); |
---|
| 105 | post("aubiotempo~ threshold:\t%f", x->threshold); |
---|
| 106 | post("aubiotempo~ audio in:\t%f", x->vec->data[0][0]); |
---|
| 107 | post("aubiotempo~ onset:\t%f", x->onset->data[0][0]); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | static void *aubiotempo_tilde_new (t_floatarg f) |
---|
| 111 | { |
---|
| 112 | t_aubiotempo_tilde *x = |
---|
| 113 | (t_aubiotempo_tilde *)pd_new(aubiotempo_tilde_class); |
---|
| 114 | |
---|
| 115 | x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f; |
---|
| 116 | x->threshold2 = -70.; |
---|
| 117 | /* should get from block~ size */ |
---|
| 118 | x->bufsize = 1024; |
---|
| 119 | x->hopsize = x->bufsize / 2; |
---|
| 120 | x->winlen = 512*512/x->hopsize; |
---|
| 121 | x->step = x->winlen/4; |
---|
| 122 | |
---|
| 123 | x->o = new_aubio_onsetdetection(aubio_onset_complex, x->bufsize, 1); |
---|
| 124 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
| 125 | x->pv = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize, 1); |
---|
| 126 | x->fftgrain = (cvec_t *)new_cvec(x->bufsize,1); |
---|
| 127 | x->onset = (fvec_t *)new_fvec(1,1); |
---|
| 128 | x->parms = new_aubio_peakpicker(x->threshold); |
---|
| 129 | x->bt = (aubio_beattracking_t *)new_aubio_beattracking(x->winlen,1); |
---|
| 130 | x->dfframe = (fvec_t *)new_fvec(x->winlen,1); |
---|
| 131 | x->out = (fvec_t *)new_fvec(x->step,1); |
---|
| 132 | |
---|
| 133 | floatinlet_new (&x->x_obj, &x->threshold); |
---|
| 134 | x->tempobang = outlet_new (&x->x_obj, &s_bang); |
---|
| 135 | x->onsetbang = outlet_new (&x->x_obj, &s_bang); |
---|
| 136 | post(aubiotempo_version); |
---|
| 137 | return (void *)x; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void aubiotempo_tilde_setup (void) |
---|
| 141 | { |
---|
| 142 | aubiotempo_tilde_class = class_new (gensym ("aubiotempo~"), |
---|
| 143 | (t_newmethod)aubiotempo_tilde_new, |
---|
| 144 | 0, sizeof (t_aubiotempo_tilde), |
---|
| 145 | CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
| 146 | class_addmethod(aubiotempo_tilde_class, |
---|
| 147 | (t_method)aubiotempo_tilde_dsp, |
---|
| 148 | gensym("dsp"), 0); |
---|
| 149 | class_addmethod(aubiotempo_tilde_class, |
---|
| 150 | (t_method)aubiotempo_tilde_debug, |
---|
| 151 | gensym("debug"), 0); |
---|
| 152 | CLASS_MAINSIGNALIN(aubiotempo_tilde_class, |
---|
| 153 | t_aubiotempo_tilde, threshold); |
---|
| 154 | } |
---|
| 155 | |
---|