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 ("%s method: %s threshold: %.3f bufsize: %d hopsize: %d",
69 aubioonset_version, x->method, x->threshold, x->bufsize, x->hopsize);
73 aubioonset_tilde_new (t_symbol *s, int argc, t_atom *argv)
75 t_aubioonset_tilde *x = (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
79 x->method = "default";
82 if (argv[2].a_type == A_FLOAT) {
83 x->bufsize = (uint_t)(argv[2].a_w.w_float);
88 x->hopsize = x->bufsize / 2;
91 if (argv[3].a_type == A_FLOAT) {
92 x->hopsize = (uint_t)(argv[3].a_w.w_float);
97 // process 2 remaining arguments
98 // (can be 'method threshold' or 'threshold method')
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;
110 x->o = new_aubio_onset (x->method,
111 x->bufsize, x->hopsize, (uint_t) sys_getsr ());
113 if (x->o == NULL) return NULL;
115 if (x->threshold != -1) {
116 aubio_onset_set_threshold(x->o, x->threshold);
118 x->threshold = aubio_onset_get_threshold(x->o);
120 x->in = (fvec_t *) new_fvec (x->hopsize);
121 x->out = (fvec_t *) new_fvec (1);
123 x->inlet = floatinlet_new (&x->x_obj, &x->threshold);
124 x->onsetbang = outlet_new (&x->x_obj, &s_bang);
129 aubioonset_tilde_del (t_aubioonset_tilde *x)
131 inlet_free(x->inlet);
132 outlet_free(x->onsetbang);
133 del_aubio_onset(x->o);
139 aubioonset_tilde_setup (void)
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);