import c files from aubio r920, using <aubio/aubio.h> include
[pd-aubio.git] / aubioquiet~.c
1 /**
2  *
3  * a puredata wrapper for aubioquiet
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/aubio.h>
12
13 char aubioquiet_version[] = "aubioquiet~ version 0.1";
14
15 static t_class *aubioquiet_tilde_class;
16
17 void aubioquiet_tilde_setup (void);
18
19 typedef struct _aubioquiet_tilde 
20 {
21         t_object x_obj;
22         t_float threshold;
23         t_int pos; /*frames%dspblocksize*/
24         t_int bufsize;
25         t_int hopsize;
26         t_int wassilence;
27         t_int issilence;
28         fvec_t *vec;
29         t_outlet *quietbang;
30         t_outlet *noisybang;
31 } t_aubioquiet_tilde;
32
33 static t_int *aubioquiet_tilde_perform(t_int *w) 
34 {
35         t_aubioquiet_tilde *x = (t_aubioquiet_tilde *)(w[1]);
36         t_sample *in          = (t_sample *)(w[2]);
37         int n                 = (int)(w[3]);
38         int j;
39         for (j=0;j<n;j++) {
40                 /* write input to datanew */
41                 fvec_write_sample(x->vec, in[j], 0, x->pos);
42                 /*time for fft*/
43                 if (x->pos == x->hopsize-1) {         
44                         /* block loop */
45                         if (aubio_silence_detection(x->vec, x->threshold)==1) {
46                                 if (x->wassilence==1) {
47                                         x->issilence = 1;
48                                 } else {
49                                         x->issilence = 2;
50                                         outlet_bang(x->quietbang);
51                                 }
52                                 x->wassilence=1;
53                         } else { 
54                                 if (x->wassilence<=0) {
55                                         x->issilence = 0;
56                                 } else {
57                                         x->issilence = -1;
58                                         outlet_bang(x->noisybang);
59                                 }
60                                 x->wassilence=0;
61                         }
62                         /* end of block loop */
63                         x->pos = -1; /* so it will be zero next j loop */
64                 }
65                 x->pos++;
66         }
67         return (w+4);
68 }
69
70 static void aubioquiet_tilde_dsp(t_aubioquiet_tilde *x, t_signal **sp)
71 {
72         dsp_add(aubioquiet_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
73 }
74
75 static void aubioquiet_tilde_debug(t_aubioquiet_tilde *x)
76 {
77         post("aubioquiet~ bufsize:\t%d", x->bufsize);
78         post("aubioquiet~ hopsize:\t%d", x->hopsize);
79         post("aubioquiet~ threshold:\t%f", x->threshold);
80         post("aubioquiet~ audio in:\t%f", x->vec->data[0][0]);
81 }
82
83 static void *aubioquiet_tilde_new (t_floatarg f)
84 {
85         t_aubioquiet_tilde *x = 
86                 (t_aubioquiet_tilde *)pd_new(aubioquiet_tilde_class);
87
88         x->threshold = (f < -1000.) ? -70 : (f >= 0.) ? -70. : f;
89         x->bufsize   = 1024;
90         x->hopsize   = x->bufsize / 2;
91
92         x->vec = (fvec_t *)new_fvec(x->hopsize,1);
93         x->wassilence = 1;
94
95         floatinlet_new (&x->x_obj, &x->threshold);
96         x->quietbang = outlet_new (&x->x_obj, &s_bang);
97         x->noisybang = outlet_new (&x->x_obj, &s_bang);
98         post(aubioquiet_version);
99         return (void *)x;
100 }
101
102 void aubioquiet_tilde_setup (void)
103 {
104         aubioquiet_tilde_class = class_new (gensym ("aubioquiet~"),
105                         (t_newmethod)aubioquiet_tilde_new,
106                         0, sizeof (t_aubioquiet_tilde),
107                         CLASS_DEFAULT, A_DEFFLOAT, 0);
108         class_addmethod(aubioquiet_tilde_class, 
109                         (t_method)aubioquiet_tilde_dsp, 
110                         gensym("dsp"), 0);
111         class_addmethod(aubioquiet_tilde_class, 
112                         (t_method)aubioquiet_tilde_debug,
113                         gensym("debug"), 0);
114         CLASS_MAINSIGNALIN(aubioquiet_tilde_class, 
115                         t_aubioquiet_tilde, threshold);
116 }
117