3 * a puredata wrapper for aubio tss detection functions
5 * Thanks to Johannes M Zmolnig for writing the excellent HOWTO:
6 * http://iem.kug.ac.at/pd/externals-HOWTO/
11 #define AUBIO_UNSTABLE 1
12 #include <aubio/aubio.h>
14 char aubiotss_version[] = "aubiotss~ version " PACKAGE_VERSION;
16 static t_class *aubiotss_tilde_class;
18 void aubiotss_tilde_setup (void);
20 typedef struct _aubiotss_tilde
24 t_int pos; /*frames%dspblocksize*/
39 static t_int *aubiotss_tilde_perform(t_int *w)
41 t_aubiotss_tilde *x = (t_aubiotss_tilde *)(w[1]);
42 t_sample *in = (t_sample *)(w[2]);
43 t_sample *outtrans = (t_sample *)(w[3]);
44 t_sample *outstead = (t_sample *)(w[4]);
48 /* write input to datanew */
49 fvec_set_sample(x->vec, in[j], x->pos);
51 if (x->pos == x->hopsize-1) {
53 /* test for silence */
54 //if (!aubio_silence_detection(x->vec, x->threshold2))
55 aubio_pvoc_do (x->pv, x->vec, x->fftgrain);
56 aubio_tss_set_threshold ( x->tss, x->thres);
57 aubio_tss_do (x->tss, x->fftgrain, x->ctrans, x->cstead);
58 aubio_pvoc_rdo (x->pvt, x->ctrans, x->trans);
59 aubio_pvoc_rdo (x->pvs, x->cstead, x->stead);
61 /* end of block loop */
62 x->pos = -1; /* so it will be zero next j loop */
65 *outtrans++ = x->trans->data[x->pos];
66 *outstead++ = x->stead->data[x->pos];
71 static void aubiotss_tilde_dsp(t_aubiotss_tilde *x, t_signal **sp)
73 dsp_add(aubiotss_tilde_perform, 5, x,
74 sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
77 static void aubiotss_tilde_debug(t_aubiotss_tilde *x)
79 post("aubiotss~ bufsize:\t%d", x->bufsize);
80 post("aubiotss~ hopsize:\t%d", x->hopsize);
81 post("aubiotss~ threshold:\t%f", x->thres);
82 post("aubiotss~ audio in:\t%f", x->vec->data[0]);
83 post("aubiotss~ audio out:\t%f", x->stead->data[0]);
86 static void *aubiotss_tilde_new (t_floatarg f)
87 //, t_floatarg bufsize)
90 (t_aubiotss_tilde *)pd_new(aubiotss_tilde_class);
92 x->thres = (f < 1e-5) ? 0.01 : (f > 1.) ? 1. : f;
93 x->bufsize = 1024; //(bufsize < 64) ? 1024: (bufsize > 16385) ? 16385: bufsize;
94 x->hopsize = x->bufsize / 4;
96 x->vec = (fvec_t *)new_fvec(x->hopsize);
98 x->fftgrain = (cvec_t *)new_cvec(x->bufsize);
99 x->ctrans = (cvec_t *)new_cvec(x->bufsize);
100 x->cstead = (cvec_t *)new_cvec(x->bufsize);
102 x->trans = (fvec_t *)new_fvec(x->hopsize);
103 x->stead = (fvec_t *)new_fvec(x->hopsize);
105 x->pv = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize);
106 x->pvt = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize);
107 x->pvs = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize);
109 x->tss = (aubio_tss_t *)new_aubio_tss(x->bufsize, x->hopsize);
111 floatinlet_new (&x->x_obj, &x->thres);
112 outlet_new(&x->x_obj, gensym("signal"));
113 outlet_new(&x->x_obj, gensym("signal"));
114 post(aubiotss_version);
118 void aubiotss_tilde_setup (void)
120 aubiotss_tilde_class = class_new (gensym ("aubiotss~"),
121 (t_newmethod)aubiotss_tilde_new,
122 0, sizeof (t_aubiotss_tilde),
123 CLASS_DEFAULT, A_DEFFLOAT, 0);
124 class_addmethod(aubiotss_tilde_class,
125 (t_method)aubiotss_tilde_dsp,
127 class_addmethod(aubiotss_tilde_class,
128 (t_method)aubiotss_tilde_debug,
130 CLASS_MAINSIGNALIN(aubiotss_tilde_class,
131 t_aubiotss_tilde, thres);