src/aubioonset~.c: simplify debug output
[pd-aubio.git] / src / aubioonset~.c
1 /**
2  *
3  * a puredata wrapper for aubio onset 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/aubio.h>
12
13 char aubioonset_version[] = "aubioonset~ version " PACKAGE_VERSION;
14
15 static t_class *aubioonset_tilde_class;
16
17 void aubioonset_tilde_setup (void);
18
19 typedef struct _aubioonset_tilde
20 {
21   t_object x_obj;
22   t_float threshold;
23   char_t *method;
24   t_int pos;                    /*frames%dspblocksize */
25   t_int bufsize;
26   t_int hopsize;
27   aubio_onset_t *o;
28   fvec_t *in;
29   fvec_t *out;
30   t_inlet *inlet;
31   t_outlet *onsetbang;
32 } t_aubioonset_tilde;
33
34 static t_int *
35 aubioonset_tilde_perform (t_int * w)
36 {
37   t_aubioonset_tilde *x = (t_aubioonset_tilde *) (w[1]);
38   t_sample *in = (t_sample *) (w[2]);
39   int n = (int) (w[3]);
40   int j;
41   for (j = 0; j < n; j++) {
42     /* write input to datanew */
43     fvec_set_sample (x->in, in[j], x->pos);
44     /*time to do something */
45     if (x->pos == x->hopsize - 1) {
46       /* block loop */
47       aubio_onset_do (x->o, x->in, x->out);
48       if (fvec_get_sample (x->out, 0) > 0.) {
49         outlet_bang (x->onsetbang);
50       }
51       /* end of block loop */
52       x->pos = -1;              /* so it will be zero next j loop */
53     }
54     x->pos++;
55   }
56   return (w + 4);
57 }
58
59 static void
60 aubioonset_tilde_dsp (t_aubioonset_tilde * x, t_signal ** sp)
61 {
62   dsp_add (aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
63 }
64
65 static void
66 aubioonset_tilde_debug (t_aubioonset_tilde * x)
67 {
68   post ("%s method: %s threshold: %.3f bufsize: %d hopsize: %d",
69       aubioonset_version, x->method, x->threshold, x->bufsize, x->hopsize);
70 }
71
72 static void *
73 aubioonset_tilde_new (t_symbol *s, int argc, t_atom *argv)
74 {
75   t_aubioonset_tilde *x = (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
76
77   x->threshold = -1;
78   x->bufsize = 1024;
79   x->method = "default";
80
81   if (argc >= 3) {
82     if (argv[2].a_type == A_FLOAT) {
83       x->bufsize = (uint_t)(argv[2].a_w.w_float);
84     }
85     argc--;
86   }
87
88   x->hopsize = x->bufsize / 2;
89
90   if (argc == 3) {
91     if (argv[3].a_type == A_FLOAT) {
92       x->hopsize = (uint_t)(argv[3].a_w.w_float);
93     }
94     argc--;
95   }
96
97   // process 2 remaining arguments
98   // (can be 'method threshold' or 'threshold method')
99   while (argc > 0) {
100     if (argv->a_type == A_FLOAT) {
101       t_sample f = argv->a_w.w_float;
102       x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
103     } else if (argv->a_type == A_SYMBOL) {
104       x->method = argv->a_w.w_symbol->s_name;
105     }
106     argc--;
107     argv++;
108   }
109
110   x->o = new_aubio_onset (x->method,
111       x->bufsize, x->hopsize, (uint_t) sys_getsr ());
112
113   if (x->o == NULL) return NULL;
114
115   if (x->threshold != -1) {
116     aubio_onset_set_threshold(x->o, x->threshold);
117   }
118   x->threshold = aubio_onset_get_threshold(x->o);
119
120   x->in = (fvec_t *) new_fvec (x->hopsize);
121   x->out = (fvec_t *) new_fvec (1);
122
123   x->inlet = floatinlet_new (&x->x_obj, &x->threshold);
124   x->onsetbang = outlet_new (&x->x_obj, &s_bang);
125   return (void *) x;
126 }
127
128 void
129 aubioonset_tilde_del (t_aubioonset_tilde *x)
130 {
131   inlet_free(x->inlet);
132   outlet_free(x->onsetbang);
133   del_aubio_onset(x->o);
134   del_fvec(x->in);
135   del_fvec(x->out);
136 }
137
138 void
139 aubioonset_tilde_setup (void)
140 {
141   aubioonset_tilde_class = class_new (gensym ("aubioonset~"),
142       (t_newmethod) aubioonset_tilde_new,
143       (t_method) aubioonset_tilde_del,
144       sizeof (t_aubioonset_tilde), CLASS_DEFAULT, A_GIMME, 0);
145   class_addmethod (aubioonset_tilde_class,
146       (t_method) aubioonset_tilde_dsp, gensym ("dsp"), 0);
147   class_addmethod (aubioonset_tilde_class,
148       (t_method) aubioonset_tilde_debug, gensym ("debug"), 0);
149   CLASS_MAINSIGNALIN (aubioonset_tilde_class, t_aubioonset_tilde, threshold);
150 }