1 | /** |
---|
2 | * |
---|
3 | * a puredata wrapper for aubio onset detection functions |
---|
4 | * |
---|
5 | * Thanks to Johannes M Zmolnig for writing the excellent HOWTO: |
---|
6 | * http://iem.kug.ac.at/pd/externals-HOWTO/ |
---|
7 | * |
---|
8 | * */ |
---|
9 | |
---|
10 | #include <m_pd.h> |
---|
11 | #include <aubio.h> |
---|
12 | |
---|
13 | char aubioonset_version[] = "aubioonset~ version 0.3"; |
---|
14 | |
---|
15 | static t_class *aubioonset_tilde_class; |
---|
16 | |
---|
17 | void aubioonset_tilde_setup (void); |
---|
18 | |
---|
19 | typedef struct _aubioonset_tilde |
---|
20 | { |
---|
21 | t_object x_obj; |
---|
22 | t_float threshold; |
---|
23 | t_int pos; /*frames%dspblocksize */ |
---|
24 | t_int bufsize; |
---|
25 | t_int hopsize; |
---|
26 | aubio_onset_t *o; |
---|
27 | fvec_t *in; |
---|
28 | fvec_t *out; |
---|
29 | t_outlet *onsetbang; |
---|
30 | } t_aubioonset_tilde; |
---|
31 | |
---|
32 | static t_int * |
---|
33 | aubioonset_tilde_perform (t_int * w) |
---|
34 | { |
---|
35 | t_aubioonset_tilde *x = (t_aubioonset_tilde *) (w[1]); |
---|
36 | t_sample *in = (t_sample *) (w[2]); |
---|
37 | int n = (int) (w[3]); |
---|
38 | int j; |
---|
39 | for (j = 0; j < n; j++) { |
---|
40 | /* write input to datanew */ |
---|
41 | fvec_write_sample (x->in, in[j], 0, x->pos); |
---|
42 | /*time to do something */ |
---|
43 | if (x->pos == x->hopsize - 1) { |
---|
44 | /* block loop */ |
---|
45 | aubio_onset_do (x->o, x->in, x->out); |
---|
46 | if (fvec_read_sample (x->out, 0, 0) > 0.) { |
---|
47 | outlet_bang (x->onsetbang); |
---|
48 | } |
---|
49 | /* end of block loop */ |
---|
50 | x->pos = -1; /* so it will be zero next j loop */ |
---|
51 | } |
---|
52 | x->pos++; |
---|
53 | } |
---|
54 | return (w + 4); |
---|
55 | } |
---|
56 | |
---|
57 | static void |
---|
58 | aubioonset_tilde_dsp (t_aubioonset_tilde * x, t_signal ** sp) |
---|
59 | { |
---|
60 | dsp_add (aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); |
---|
61 | } |
---|
62 | |
---|
63 | static void |
---|
64 | aubioonset_tilde_debug (t_aubioonset_tilde * x) |
---|
65 | { |
---|
66 | post ("aubioonset~ bufsize:\t%d", x->bufsize); |
---|
67 | post ("aubioonset~ hopsize:\t%d", x->hopsize); |
---|
68 | post ("aubioonset~ threshold:\t%f", x->threshold); |
---|
69 | post ("aubioonset~ audio in:\t%f", x->in->data[0][0]); |
---|
70 | post ("aubioonset~ onset:\t%f", x->out->data[0][0]); |
---|
71 | } |
---|
72 | |
---|
73 | static void * |
---|
74 | aubioonset_tilde_new (t_floatarg f) |
---|
75 | { |
---|
76 | t_aubioonset_tilde *x = |
---|
77 | (t_aubioonset_tilde *) pd_new (aubioonset_tilde_class); |
---|
78 | |
---|
79 | x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f; |
---|
80 | x->bufsize = 1024; |
---|
81 | x->hopsize = x->bufsize / 2; |
---|
82 | |
---|
83 | x->o = new_aubio_onset ("complex", |
---|
84 | x->bufsize, x->hopsize, 1, (uint_t) sys_getsr ()); |
---|
85 | x->in = (fvec_t *) new_fvec (x->hopsize, 1); |
---|
86 | x->out = (fvec_t *) new_fvec (1, 1); |
---|
87 | |
---|
88 | floatinlet_new (&x->x_obj, &x->threshold); |
---|
89 | x->onsetbang = outlet_new (&x->x_obj, &s_bang); |
---|
90 | post (aubioonset_version); |
---|
91 | return (void *) x; |
---|
92 | } |
---|
93 | |
---|
94 | void |
---|
95 | aubioonset_tilde_setup (void) |
---|
96 | { |
---|
97 | aubioonset_tilde_class = class_new (gensym ("aubioonset~"), |
---|
98 | (t_newmethod) aubioonset_tilde_new, |
---|
99 | 0, sizeof (t_aubioonset_tilde), CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
100 | class_addmethod (aubioonset_tilde_class, |
---|
101 | (t_method) aubioonset_tilde_dsp, gensym ("dsp"), 0); |
---|
102 | class_addmethod (aubioonset_tilde_class, |
---|
103 | (t_method) aubioonset_tilde_debug, gensym ("debug"), 0); |
---|
104 | CLASS_MAINSIGNALIN (aubioonset_tilde_class, t_aubioonset_tilde, threshold); |
---|
105 | } |
---|