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
23 t_int pos; /*frames%dspblocksize */
34 aubioonset_tilde_perform (t_int * w)
36 t_aubioonset_tilde *x = (t_aubioonset_tilde *) (w[1]);
37 t_sample *in = (t_sample *) (w[2]);
40 for (j = 0; j < n; j++) {
41 /* write input to datanew */
42 fvec_set_sample (x->in, in[j], x->pos);
43 /*time to do something */
44 if (x->pos == x->hopsize - 1) {
46 aubio_onset_do (x->o, x->in, x->out);
47 if (fvec_get_sample (x->out, 0) > 0.) {
48 outlet_bang (x->onsetbang);
50 /* end of block loop */
51 x->pos = -1; /* so it will be zero next j loop */
59 aubioonset_tilde_dsp (t_aubioonset_tilde * x, t_signal ** sp)
61 dsp_add (aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
65 aubioonset_tilde_debug (t_aubioonset_tilde * x)
67 post (aubioonset_version);
68 post ("aubioonset~ bufsize:\t%d", x->bufsize);
69 post ("aubioonset~ hopsize:\t%d", x->hopsize);
70 post ("aubioonset~ threshold:\t%f", x->threshold);
74 aubioonset_tilde_new (t_floatarg f)
76 t_aubioonset_tilde *x = (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
78 x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
80 x->hopsize = x->bufsize / 2;
82 x->o = new_aubio_onset ("default",
83 x->bufsize, x->hopsize, (uint_t) sys_getsr ());
85 if (x->o == NULL) return NULL;
87 x->in = (fvec_t *) new_fvec (x->hopsize);
88 x->out = (fvec_t *) new_fvec (1);
90 x->inlet = floatinlet_new (&x->x_obj, &x->threshold);
91 x->onsetbang = outlet_new (&x->x_obj, &s_bang);
96 aubioonset_tilde_del (t_aubioonset_tilde *x)
99 outlet_free(x->onsetbang);
100 del_aubio_onset(x->o);
106 aubioonset_tilde_setup (void)
108 aubioonset_tilde_class = class_new (gensym ("aubioonset~"),
109 (t_newmethod) aubioonset_tilde_new,
110 (t_method) aubioonset_tilde_del,
111 sizeof (t_aubioonset_tilde), CLASS_DEFAULT, A_DEFFLOAT, 0);
112 class_addmethod (aubioonset_tilde_class,
113 (t_method) aubioonset_tilde_dsp, gensym ("dsp"), 0);
114 class_addmethod (aubioonset_tilde_class,
115 (t_method) aubioonset_tilde_debug, gensym ("debug"), 0);
116 CLASS_MAINSIGNALIN (aubioonset_tilde_class, t_aubioonset_tilde, threshold);