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~ method:\t%s", x->method);
70 post ("aubioonset~ threshold:\t%f", x->threshold);
71 post ("aubioonset~ bufsize:\t%d", x->bufsize);
72 post ("aubioonset~ hopsize:\t%d", x->hopsize);
76 aubioonset_tilde_new (t_symbol *s, int argc, t_atom *argv)
78 t_aubioonset_tilde *x = (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
82 x->method = "default";
85 if (argv[2].a_type == A_FLOAT) {
86 x->bufsize = (uint_t)(argv[2].a_w.w_float);
91 x->hopsize = x->bufsize / 2;
94 if (argv[3].a_type == A_FLOAT) {
95 x->hopsize = (uint_t)(argv[3].a_w.w_float);
100 // process 2 remaining arguments
101 // (can be 'method threshold' or 'threshold method')
103 if (argv->a_type == A_FLOAT) {
104 t_sample f = argv->a_w.w_float;
105 x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
106 } else if (argv->a_type == A_SYMBOL) {
107 x->method = argv->a_w.w_symbol->s_name;
113 x->o = new_aubio_onset (x->method,
114 x->bufsize, x->hopsize, (uint_t) sys_getsr ());
116 if (x->o == NULL) return NULL;
118 if (x->threshold != -1) {
119 aubio_onset_set_threshold(x->o, x->threshold);
122 x->in = (fvec_t *) new_fvec (x->hopsize);
123 x->out = (fvec_t *) new_fvec (1);
125 x->inlet = floatinlet_new (&x->x_obj, &x->threshold);
126 x->onsetbang = outlet_new (&x->x_obj, &s_bang);
131 aubioonset_tilde_del (t_aubioonset_tilde *x)
133 inlet_free(x->inlet);
134 outlet_free(x->onsetbang);
135 del_aubio_onset(x->o);
141 aubioonset_tilde_setup (void)
143 aubioonset_tilde_class = class_new (gensym ("aubioonset~"),
144 (t_newmethod) aubioonset_tilde_new,
145 (t_method) aubioonset_tilde_del,
146 sizeof (t_aubioonset_tilde), CLASS_DEFAULT, A_GIMME, 0);
147 class_addmethod (aubioonset_tilde_class,
148 (t_method) aubioonset_tilde_dsp, gensym ("dsp"), 0);
149 class_addmethod (aubioonset_tilde_class,
150 (t_method) aubioonset_tilde_debug, gensym ("debug"), 0);
151 CLASS_MAINSIGNALIN (aubioonset_tilde_class, t_aubioonset_tilde, threshold);