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