3 * a puredata wrapper for aubio tempo 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 aubiotempo_version[] = "aubiotempo~ version " PACKAGE_VERSION;
15 static t_class *aubiotempo_tilde_class;
17 void aubiotempo_tilde_setup (void);
19 typedef struct _aubiotempo_tilde
24 t_int pos; /*frames%dspblocksize*/
34 static t_int *aubiotempo_tilde_perform(t_int *w)
36 t_aubiotempo_tilde *x = (t_aubiotempo_tilde *)(w[1]);
37 t_sample *in = (t_sample *)(w[2]);
41 /* write input to datanew */
42 fvec_set_sample(x->vec, in[j], x->pos);
44 if (x->pos == x->hopsize-1) {
46 aubio_tempo_do (x->t, x->vec, x->output);
47 if (x->output->data[0]) {
48 outlet_bang(x->tempobang);
50 if (x->output->data[1]) {
51 outlet_bang(x->onsetbang);
53 /* end of block loop */
54 x->pos = -1; /* so it will be zero next j loop */
61 static void aubiotempo_tilde_dsp(t_aubiotempo_tilde *x, t_signal **sp)
63 dsp_add(aubiotempo_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
66 static void aubiotempo_tilde_debug(t_aubiotempo_tilde *x)
68 post(aubiotempo_version);
69 post("aubiotempo~ bufsize:\t%d", x->bufsize);
70 post("aubiotempo~ hopsize:\t%d", x->hopsize);
71 post("aubiotempo~ threshold:\t%f", x->threshold);
72 post("aubiotempo~ audio in:\t%f", x->vec->data[0]);
75 static void *aubiotempo_tilde_new (t_floatarg f)
77 t_aubiotempo_tilde *x =
78 (t_aubiotempo_tilde *)pd_new(aubiotempo_tilde_class);
80 x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
82 /* should get from block~ size */
84 x->hopsize = x->bufsize / 2;
86 x->t = new_aubio_tempo ("specdiff", x->bufsize, x->hopsize,
87 (uint_t) sys_getsr ());
88 aubio_tempo_set_silence(x->t,x->silence);
89 aubio_tempo_set_threshold(x->t,x->threshold);
90 x->output = (fvec_t *)new_fvec(2);
91 x->vec = (fvec_t *)new_fvec(x->hopsize);
93 floatinlet_new (&x->x_obj, &x->threshold);
94 x->tempobang = outlet_new (&x->x_obj, &s_bang);
95 x->onsetbang = outlet_new (&x->x_obj, &s_bang);
99 static void *aubiotempo_tilde_del(t_aubiotempo_tilde *x)
101 if(x->t) del_aubio_tempo(x->t);
102 if(x->output) del_fvec(x->output);
103 if(x->vec) del_fvec(x->vec);
107 void aubiotempo_tilde_setup (void)
109 aubiotempo_tilde_class = class_new (gensym ("aubiotempo~"),
110 (t_newmethod)aubiotempo_tilde_new,
111 (t_method)aubiotempo_tilde_del,
112 sizeof (t_aubiotempo_tilde),
113 CLASS_DEFAULT, A_DEFFLOAT, 0);
114 class_addmethod(aubiotempo_tilde_class,
115 (t_method)aubiotempo_tilde_dsp,
117 class_addmethod(aubiotempo_tilde_class,
118 (t_method)aubiotempo_tilde_debug,
120 CLASS_MAINSIGNALIN(aubiotempo_tilde_class,
121 t_aubiotempo_tilde, threshold);