[30cbd419] | 1 | /** |
---|
| 2 | * |
---|
| 3 | * a puredata wrapper for aubio onset 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 | |
---|
[316092e] | 13 | char aubioonset_version[] = "aubioonset~ version 0.2"; |
---|
[30cbd419] | 14 | |
---|
| 15 | static t_class *aubioonset_tilde_class; |
---|
| 16 | |
---|
| 17 | void aubioonset_tilde_setup (void); |
---|
| 18 | |
---|
| 19 | typedef struct _aubioonset_tilde |
---|
| 20 | { |
---|
| 21 | t_object x_obj; |
---|
| 22 | t_float threshold; |
---|
[cf83555] | 23 | t_float threshold2; |
---|
| 24 | t_int pos; /*frames%dspblocksize*/ |
---|
[30cbd419] | 25 | t_int bufsize; |
---|
| 26 | t_int hopsize; |
---|
| 27 | aubio_onsetdetection_t *o; |
---|
| 28 | aubio_pvoc_t * pv; |
---|
[a808d05] | 29 | aubio_peakpicker_t * parms; |
---|
[30cbd419] | 30 | fvec_t *vec; |
---|
| 31 | fvec_t *onset; |
---|
| 32 | cvec_t *fftgrain; |
---|
[cf83555] | 33 | t_outlet *onsetbang; |
---|
[30cbd419] | 34 | } t_aubioonset_tilde; |
---|
| 35 | |
---|
| 36 | static t_int *aubioonset_tilde_perform(t_int *w) |
---|
| 37 | { |
---|
| 38 | t_aubioonset_tilde *x = (t_aubioonset_tilde *)(w[1]); |
---|
| 39 | t_sample *in = (t_sample *)(w[2]); |
---|
| 40 | int n = (int)(w[3]); |
---|
[cf83555] | 41 | int j,isonset; |
---|
| 42 | for (j=0;j<n;j++) { |
---|
| 43 | /* write input to datanew */ |
---|
| 44 | fvec_write_sample(x->vec, in[j], 0, x->pos); |
---|
| 45 | /*time for fft*/ |
---|
| 46 | if (x->pos == x->hopsize-1) { |
---|
| 47 | /* block loop */ |
---|
| 48 | aubio_pvoc_do (x->pv,x->vec, x->fftgrain); |
---|
[01b8fcc] | 49 | aubio_onsetdetection_do (x->o,x->fftgrain, x->onset); |
---|
[47f17fc] | 50 | isonset = aubio_peakpicker_do (x->parms, x->onset); |
---|
[cf83555] | 51 | if (isonset) { |
---|
| 52 | /* test for silence */ |
---|
| 53 | if (aubio_silence_detection(x->vec, x->threshold2)==1) |
---|
| 54 | isonset=0; |
---|
| 55 | else |
---|
| 56 | outlet_bang(x->onsetbang); |
---|
| 57 | } |
---|
| 58 | /* end of block loop */ |
---|
| 59 | x->pos = -1; /* so it will be zero next j loop */ |
---|
| 60 | } |
---|
| 61 | x->pos++; |
---|
| 62 | } |
---|
[30cbd419] | 63 | return (w+4); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | static void aubioonset_tilde_dsp(t_aubioonset_tilde *x, t_signal **sp) |
---|
| 67 | { |
---|
| 68 | dsp_add(aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static void aubioonset_tilde_debug(t_aubioonset_tilde *x) |
---|
| 72 | { |
---|
| 73 | post("aubioonset~ bufsize:\t%d", x->bufsize); |
---|
| 74 | post("aubioonset~ hopsize:\t%d", x->hopsize); |
---|
| 75 | post("aubioonset~ threshold:\t%f", x->threshold); |
---|
| 76 | post("aubioonset~ audio in:\t%f", x->vec->data[0][0]); |
---|
| 77 | post("aubioonset~ onset:\t%f", x->onset->data[0][0]); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | static void *aubioonset_tilde_new (t_floatarg f) |
---|
| 81 | { |
---|
| 82 | t_aubioonset_tilde *x = |
---|
| 83 | (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class); |
---|
| 84 | |
---|
| 85 | x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f; |
---|
[cf83555] | 86 | x->threshold2 = -70.; |
---|
[30cbd419] | 87 | x->bufsize = 1024; |
---|
| 88 | x->hopsize = x->bufsize / 2; |
---|
| 89 | |
---|
[5cf415f] | 90 | x->o = new_aubio_onsetdetection(aubio_onset_complex, x->bufsize, 1); |
---|
[30cbd419] | 91 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
[cf83555] | 92 | x->pv = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize, 1); |
---|
| 93 | x->fftgrain = (cvec_t *)new_cvec(x->bufsize,1); |
---|
[30cbd419] | 94 | x->onset = (fvec_t *)new_fvec(1,1); |
---|
[cf83555] | 95 | x->parms = new_aubio_peakpicker(x->threshold); |
---|
[30cbd419] | 96 | |
---|
| 97 | floatinlet_new (&x->x_obj, &x->threshold); |
---|
[cf83555] | 98 | x->onsetbang = outlet_new (&x->x_obj, &s_bang); |
---|
| 99 | post(aubioonset_version); |
---|
[30cbd419] | 100 | return (void *)x; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void aubioonset_tilde_setup (void) |
---|
| 104 | { |
---|
| 105 | aubioonset_tilde_class = class_new (gensym ("aubioonset~"), |
---|
| 106 | (t_newmethod)aubioonset_tilde_new, |
---|
| 107 | 0, sizeof (t_aubioonset_tilde), |
---|
| 108 | CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
| 109 | class_addmethod(aubioonset_tilde_class, |
---|
| 110 | (t_method)aubioonset_tilde_dsp, |
---|
| 111 | gensym("dsp"), 0); |
---|
| 112 | class_addmethod(aubioonset_tilde_class, |
---|
| 113 | (t_method)aubioonset_tilde_debug, |
---|
| 114 | gensym("debug"), 0); |
---|
| 115 | CLASS_MAINSIGNALIN(aubioonset_tilde_class, |
---|
| 116 | t_aubioonset_tilde, threshold); |
---|
| 117 | } |
---|
| 118 | |
---|