[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 | |
---|
| 13 | char aubioonset_version[] = "aubioonset~ version 0.1"; |
---|
| 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; |
---|
| 23 | t_int bufsize; |
---|
| 24 | t_int hopsize; |
---|
| 25 | aubio_onsetdetection_t *o; |
---|
| 26 | aubio_pvoc_t * pv; |
---|
| 27 | fvec_t *vec; |
---|
| 28 | fvec_t *onset; |
---|
| 29 | cvec_t *fftgrain; |
---|
| 30 | t_outlet *onsetval; |
---|
| 31 | } t_aubioonset_tilde; |
---|
| 32 | |
---|
| 33 | static t_int *aubioonset_tilde_perform(t_int *w) |
---|
| 34 | { |
---|
| 35 | t_aubioonset_tilde *x = (t_aubioonset_tilde *)(w[1]); |
---|
| 36 | t_sample *in = (t_sample *)(w[2]); |
---|
| 37 | int n = (int)(w[3]); |
---|
| 38 | //t_sample f = (x->threshold < 0.) ? 0.2 : |
---|
| 39 | // (x->threshold > 10.) ? 10. : x->threshold; |
---|
| 40 | while (n--) //*(x->vec->data[0])++ = (*in++); |
---|
| 41 | x->vec->data[0][n] = in[n]; |
---|
| 42 | aubio_pvoc_do (x->pv, x->vec, x->fftgrain); |
---|
| 43 | aubio_onsetdetection(x->o, x->fftgrain, x->onset); |
---|
| 44 | outlet_float(x->onsetval, x->onset->data[0][0]); |
---|
| 45 | return (w+4); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | static void aubioonset_tilde_dsp(t_aubioonset_tilde *x, t_signal **sp) |
---|
| 49 | { |
---|
| 50 | dsp_add(aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | static void aubioonset_tilde_debug(t_aubioonset_tilde *x) |
---|
| 54 | { |
---|
| 55 | post("aubioonset~ bufsize:\t%d", x->bufsize); |
---|
| 56 | post("aubioonset~ hopsize:\t%d", x->hopsize); |
---|
| 57 | post("aubioonset~ threshold:\t%f", x->threshold); |
---|
| 58 | post("aubioonset~ audio in:\t%f", x->vec->data[0][0]); |
---|
| 59 | post("aubioonset~ onset:\t%f", x->onset->data[0][0]); |
---|
| 60 | outlet_float(x->onsetval, x->threshold); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | static void *aubioonset_tilde_new (t_floatarg f) |
---|
| 64 | { |
---|
| 65 | t_aubioonset_tilde *x = |
---|
| 66 | (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class); |
---|
| 67 | |
---|
| 68 | x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f; |
---|
| 69 | /* should get from block~ size */ |
---|
| 70 | x->bufsize = 1024; |
---|
| 71 | x->hopsize = x->bufsize / 2; |
---|
| 72 | |
---|
| 73 | x->o = new_aubio_onsetdetection(complexdomain, x->bufsize, 1); |
---|
| 74 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
| 75 | x->pv = new_aubio_pvoc(x->bufsize, x->hopsize, 1); |
---|
| 76 | x->fftgrain = new_cvec(x->bufsize,1); |
---|
| 77 | x->onset = (fvec_t *)new_fvec(1,1); |
---|
| 78 | |
---|
| 79 | floatinlet_new (&x->x_obj, &x->threshold); |
---|
| 80 | x->onsetval = outlet_new (&x->x_obj, &s_float); |
---|
| 81 | return (void *)x; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void aubioonset_tilde_setup (void) |
---|
| 85 | { |
---|
| 86 | aubioonset_tilde_class = class_new (gensym ("aubioonset~"), |
---|
| 87 | (t_newmethod)aubioonset_tilde_new, |
---|
| 88 | 0, sizeof (t_aubioonset_tilde), |
---|
| 89 | CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
| 90 | class_addmethod(aubioonset_tilde_class, |
---|
| 91 | (t_method)aubioonset_tilde_dsp, |
---|
| 92 | gensym("dsp"), 0); |
---|
| 93 | class_addmethod(aubioonset_tilde_class, |
---|
| 94 | (t_method)aubioonset_tilde_debug, |
---|
| 95 | gensym("debug"), 0); |
---|
| 96 | class_sethelpsymbol(aubioonset_tilde_class, |
---|
| 97 | gensym("help-aubioonset~.pd")); |
---|
| 98 | CLASS_MAINSIGNALIN(aubioonset_tilde_class, |
---|
| 99 | t_aubioonset_tilde, threshold); |
---|
| 100 | post("aubioonset~ v0.1 for puredata"); |
---|
| 101 | } |
---|
| 102 | |
---|