src/aubioonset~.c: always get threshold with aubio_onset_get_threshold
[pd-aubio.git] / src / aubioonset~.c
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/aubio.h>
12
13 char aubioonset_version[] = "aubioonset~ version " PACKAGE_VERSION;
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   char_t *method;
24   t_int pos;                    /*frames%dspblocksize */
25   t_int bufsize;
26   t_int hopsize;
27   aubio_onset_t *o;
28   fvec_t *in;
29   fvec_t *out;
30   t_inlet *inlet;
31   t_outlet *onsetbang;
32 } t_aubioonset_tilde;
33
34 static t_int *
35 aubioonset_tilde_perform (t_int * w)
36 {
37   t_aubioonset_tilde *x = (t_aubioonset_tilde *) (w[1]);
38   t_sample *in = (t_sample *) (w[2]);
39   int n = (int) (w[3]);
40   int j;
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) {
46       /* block loop */
47       aubio_onset_do (x->o, x->in, x->out);
48       if (fvec_get_sample (x->out, 0) > 0.) {
49         outlet_bang (x->onsetbang);
50       }
51       /* end of block loop */
52       x->pos = -1;              /* so it will be zero next j loop */
53     }
54     x->pos++;
55   }
56   return (w + 4);
57 }
58
59 static void
60 aubioonset_tilde_dsp (t_aubioonset_tilde * x, t_signal ** sp)
61 {
62   dsp_add (aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
63 }
64
65 static void
66 aubioonset_tilde_debug (t_aubioonset_tilde * x)
67 {
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);
73 }
74
75 static void *
76 aubioonset_tilde_new (t_symbol *s, int argc, t_atom *argv)
77 {
78   t_aubioonset_tilde *x = (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
79
80   x->threshold = -1;
81   x->bufsize = 1024;
82   x->method = "default";
83
84   if (argc >= 3) {
85     if (argv[2].a_type == A_FLOAT) {
86       x->bufsize = (uint_t)(argv[2].a_w.w_float);
87     }
88     argc--;
89   }
90
91   x->hopsize = x->bufsize / 2;
92
93   if (argc == 3) {
94     if (argv[3].a_type == A_FLOAT) {
95       x->hopsize = (uint_t)(argv[3].a_w.w_float);
96     }
97     argc--;
98   }
99
100   // process 2 remaining arguments
101   // (can be 'method threshold' or 'threshold method')
102   while (argc > 0) {
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;
108     }
109     argc--;
110     argv++;
111   }
112
113   x->o = new_aubio_onset (x->method,
114       x->bufsize, x->hopsize, (uint_t) sys_getsr ());
115
116   if (x->o == NULL) return NULL;
117
118   if (x->threshold != -1) {
119     aubio_onset_set_threshold(x->o, x->threshold);
120   }
121   x->threshold = aubio_onset_get_threshold(x->o);
122
123   x->in = (fvec_t *) new_fvec (x->hopsize);
124   x->out = (fvec_t *) new_fvec (1);
125
126   x->inlet = floatinlet_new (&x->x_obj, &x->threshold);
127   x->onsetbang = outlet_new (&x->x_obj, &s_bang);
128   return (void *) x;
129 }
130
131 void
132 aubioonset_tilde_del (t_aubioonset_tilde *x)
133 {
134   inlet_free(x->inlet);
135   outlet_free(x->onsetbang);
136   del_aubio_onset(x->o);
137   del_fvec(x->in);
138   del_fvec(x->out);
139 }
140
141 void
142 aubioonset_tilde_setup (void)
143 {
144   aubioonset_tilde_class = class_new (gensym ("aubioonset~"),
145       (t_newmethod) aubioonset_tilde_new,
146       (t_method) aubioonset_tilde_del,
147       sizeof (t_aubioonset_tilde), CLASS_DEFAULT, A_GIMME, 0);
148   class_addmethod (aubioonset_tilde_class,
149       (t_method) aubioonset_tilde_dsp, gensym ("dsp"), 0);
150   class_addmethod (aubioonset_tilde_class,
151       (t_method) aubioonset_tilde_debug, gensym ("debug"), 0);
152   CLASS_MAINSIGNALIN (aubioonset_tilde_class, t_aubioonset_tilde, threshold);
153 }