3 * a puredata wrapper for aubio onset detection functions
5 * Thanks to Johannes M Zmolnig for writing the excellent HOWTO:
6 * http://iem.kug.ac.at/pd/externals-HOWTO/
11 #include <aubio/aubio.h>
13 char aubioonset_version[] = "aubioonset~ version " PACKAGE_VERSION;
15 static t_class *aubioonset_tilde_class;
17 void aubioonset_tilde_setup (void);
19 typedef struct _aubioonset_tilde
24 t_int pos; /*frames%dspblocksize */
35 aubioonset_tilde_perform (t_int * w)
37 t_aubioonset_tilde *x = (t_aubioonset_tilde *) (w[1]);
38 t_sample *in = (t_sample *) (w[2]);
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) {
47 aubio_onset_do (x->o, x->in, x->out);
48 if (fvec_get_sample (x->out, 0) > 0.) {
49 outlet_bang (x->onsetbang);
51 /* end of block loop */
52 x->pos = -1; /* so it will be zero next j loop */
60 aubioonset_tilde_dsp (t_aubioonset_tilde * x, t_signal ** sp)
62 dsp_add (aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
66 aubioonset_tilde_debug (t_aubioonset_tilde * x)
68 post (aubioonset_version);
69 post ("aubioonset~ bufsize:\t%d", x->bufsize);
70 post ("aubioonset~ hopsize:\t%d", x->hopsize);
71 post ("aubioonset~ threshold:\t%f", x->threshold);
75 aubioonset_tilde_new (t_symbol *s, int argc, t_atom *argv)
77 t_aubioonset_tilde *x = (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
81 x->method = "default";
84 if (argv[2].a_type == A_FLOAT) {
85 x->bufsize = (uint_t)(argv[2].a_w.w_float);
90 x->hopsize = x->bufsize / 2;
93 if (argv[3].a_type == A_FLOAT) {
94 x->hopsize = (uint_t)(argv[3].a_w.w_float);
99 // process 2 remaining arguments
100 // (can be 'method threshold' or 'threshold method')
102 if (argv->a_type == A_FLOAT) {
103 t_sample f = argv->a_w.w_float;
104 x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
105 } else if (argv->a_type == A_SYMBOL) {
106 x->method = argv->a_w.w_symbol->s_name;
112 x->o = new_aubio_onset (x->method,
113 x->bufsize, x->hopsize, (uint_t) sys_getsr ());
115 if (x->o == NULL) return NULL;
117 if (x->threshold != -1) {
118 aubio_onset_set_threshold(x->o, x->threshold);
121 x->in = (fvec_t *) new_fvec (x->hopsize);
122 x->out = (fvec_t *) new_fvec (1);
124 x->inlet = floatinlet_new (&x->x_obj, &x->threshold);
125 x->onsetbang = outlet_new (&x->x_obj, &s_bang);
130 aubioonset_tilde_del (t_aubioonset_tilde *x)
132 inlet_free(x->inlet);
133 outlet_free(x->onsetbang);
134 del_aubio_onset(x->o);
140 aubioonset_tilde_setup (void)
142 aubioonset_tilde_class = class_new (gensym ("aubioonset~"),
143 (t_newmethod) aubioonset_tilde_new,
144 (t_method) aubioonset_tilde_del,
145 sizeof (t_aubioonset_tilde), CLASS_DEFAULT, A_GIMME, 0);
146 class_addmethod (aubioonset_tilde_class,
147 (t_method) aubioonset_tilde_dsp, gensym ("dsp"), 0);
148 class_addmethod (aubioonset_tilde_class,
149 (t_method) aubioonset_tilde_debug, gensym ("debug"), 0);
150 CLASS_MAINSIGNALIN (aubioonset_tilde_class, t_aubioonset_tilde, threshold);