[cf83555] | 1 | /** |
---|
| 2 | * |
---|
| 3 | * a puredata wrapper for aubio tss 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 aubiotss_version[] = "aubiotss~ version 0.1"; |
---|
| 14 | |
---|
| 15 | static t_class *aubiotss_tilde_class; |
---|
| 16 | |
---|
| 17 | void aubiotss_tilde_setup (void); |
---|
| 18 | |
---|
| 19 | typedef struct _aubiotss_tilde |
---|
| 20 | { |
---|
| 21 | t_object x_obj; |
---|
| 22 | t_float thres; |
---|
| 23 | t_float alpha; |
---|
| 24 | t_float beta; |
---|
| 25 | t_int pos; /*frames%dspblocksize*/ |
---|
| 26 | t_int bufsize; |
---|
| 27 | t_int hopsize; |
---|
| 28 | aubio_pvoc_t * pv; |
---|
| 29 | aubio_pvoc_t * pvt; |
---|
| 30 | aubio_pvoc_t * pvs; |
---|
| 31 | aubio_tss_t * tss; |
---|
| 32 | fvec_t *vec; |
---|
| 33 | cvec_t *fftgrain; |
---|
| 34 | cvec_t *cstead; |
---|
| 35 | cvec_t *ctrans; |
---|
| 36 | fvec_t *trans; |
---|
| 37 | fvec_t *stead; |
---|
| 38 | } t_aubiotss_tilde; |
---|
| 39 | |
---|
| 40 | static t_int *aubiotss_tilde_perform(t_int *w) |
---|
| 41 | { |
---|
| 42 | t_aubiotss_tilde *x = (t_aubiotss_tilde *)(w[1]); |
---|
| 43 | t_sample *in = (t_sample *)(w[2]); |
---|
| 44 | t_sample *outtrans = (t_sample *)(w[3]); |
---|
| 45 | t_sample *outstead = (t_sample *)(w[4]); |
---|
| 46 | int n = (int)(w[5]); |
---|
| 47 | int j; |
---|
| 48 | for (j=0;j<n;j++) { |
---|
| 49 | /* write input to datanew */ |
---|
| 50 | fvec_write_sample(x->vec, in[j], 0, x->pos); |
---|
| 51 | /*time for fft*/ |
---|
| 52 | if (x->pos == x->hopsize-1) { |
---|
| 53 | /* block loop */ |
---|
| 54 | /* test for silence */ |
---|
| 55 | //if (!aubio_silence_detection(x->vec, x->threshold2)) |
---|
| 56 | aubio_pvoc_do (x->pv, x->vec, x->fftgrain); |
---|
| 57 | aubio_tss_set_thres( x->tss, x->thres); |
---|
| 58 | aubio_tss_do (x->tss, x->fftgrain, x->ctrans, x->cstead); |
---|
| 59 | aubio_pvoc_rdo (x->pvt, x->ctrans, x->trans); |
---|
| 60 | aubio_pvoc_rdo (x->pvs, x->cstead, x->stead); |
---|
| 61 | //} |
---|
| 62 | /* end of block loop */ |
---|
| 63 | x->pos = -1; /* so it will be zero next j loop */ |
---|
| 64 | } |
---|
| 65 | x->pos++; |
---|
| 66 | *outtrans++ = x->trans->data[0][x->pos]; |
---|
| 67 | *outstead++ = x->stead->data[0][x->pos]; |
---|
| 68 | } |
---|
| 69 | return (w+6); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | static void aubiotss_tilde_dsp(t_aubiotss_tilde *x, t_signal **sp) |
---|
| 73 | { |
---|
| 74 | dsp_add(aubiotss_tilde_perform, 5, x, |
---|
| 75 | sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | static void aubiotss_tilde_debug(t_aubiotss_tilde *x) |
---|
| 79 | { |
---|
| 80 | post("aubiotss~ bufsize:\t%d", x->bufsize); |
---|
| 81 | post("aubiotss~ hopsize:\t%d", x->hopsize); |
---|
| 82 | post("aubiotss~ threshold:\t%f", x->thres); |
---|
| 83 | post("aubiotss~ audio in:\t%f", x->vec->data[0][0]); |
---|
| 84 | post("aubiotss~ audio out:\t%f", x->stead->data[0][0]); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | static void *aubiotss_tilde_new (t_floatarg f) |
---|
| 88 | //, t_floatarg bufsize) |
---|
| 89 | { |
---|
| 90 | t_aubiotss_tilde *x = |
---|
| 91 | (t_aubiotss_tilde *)pd_new(aubiotss_tilde_class); |
---|
| 92 | |
---|
| 93 | x->thres = (f < 1e-5) ? 0.01 : (f > 1.) ? 1. : f; |
---|
| 94 | x->bufsize = 1024; //(bufsize < 64) ? 1024: (bufsize > 16385) ? 16385: bufsize; |
---|
| 95 | x->hopsize = x->bufsize / 4; |
---|
| 96 | x->alpha = 3.; |
---|
| 97 | x->beta = 4.; |
---|
| 98 | |
---|
| 99 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
| 100 | |
---|
| 101 | x->fftgrain = (cvec_t *)new_cvec(x->bufsize,1); |
---|
| 102 | x->ctrans = (cvec_t *)new_cvec(x->bufsize,1); |
---|
| 103 | x->cstead = (cvec_t *)new_cvec(x->bufsize,1); |
---|
| 104 | |
---|
| 105 | x->trans = (fvec_t *)new_fvec(x->hopsize,1); |
---|
| 106 | x->stead = (fvec_t *)new_fvec(x->hopsize,1); |
---|
| 107 | |
---|
| 108 | x->pv = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize, 1); |
---|
| 109 | x->pvt = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize, 1); |
---|
| 110 | x->pvs = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize, 1); |
---|
| 111 | |
---|
| 112 | x->tss = (aubio_tss_t *)new_aubio_tss(x->thres, x->alpha, x->beta, |
---|
| 113 | x->bufsize, x->hopsize, 1); |
---|
| 114 | |
---|
| 115 | floatinlet_new (&x->x_obj, &x->thres); |
---|
| 116 | outlet_new(&x->x_obj, gensym("signal")); |
---|
| 117 | outlet_new(&x->x_obj, gensym("signal")); |
---|
| 118 | post(aubiotss_version); |
---|
| 119 | return (void *)x; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | void aubiotss_tilde_setup (void) |
---|
| 123 | { |
---|
| 124 | aubiotss_tilde_class = class_new (gensym ("aubiotss~"), |
---|
| 125 | (t_newmethod)aubiotss_tilde_new, |
---|
| 126 | 0, sizeof (t_aubiotss_tilde), |
---|
| 127 | CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
| 128 | class_addmethod(aubiotss_tilde_class, |
---|
| 129 | (t_method)aubiotss_tilde_dsp, |
---|
| 130 | gensym("dsp"), 0); |
---|
| 131 | class_addmethod(aubiotss_tilde_class, |
---|
| 132 | (t_method)aubiotss_tilde_debug, |
---|
| 133 | gensym("debug"), 0); |
---|
| 134 | CLASS_MAINSIGNALIN(aubiotss_tilde_class, |
---|
| 135 | t_aubiotss_tilde, thres); |
---|
| 136 | } |
---|