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