src/aubioonset~.c: add aubioonset_tilde_del
[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   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_inlet *inlet;
30   t_outlet *onsetbang;
31 } t_aubioonset_tilde;
32
33 static t_int *
34 aubioonset_tilde_perform (t_int * w)
35 {
36   t_aubioonset_tilde *x = (t_aubioonset_tilde *) (w[1]);
37   t_sample *in = (t_sample *) (w[2]);
38   int n = (int) (w[3]);
39   int j;
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) {
45       /* block loop */
46       aubio_onset_do (x->o, x->in, x->out);
47       if (fvec_get_sample (x->out, 0) > 0.) {
48         outlet_bang (x->onsetbang);
49       }
50       /* end of block loop */
51       x->pos = -1;              /* so it will be zero next j loop */
52     }
53     x->pos++;
54   }
55   return (w + 4);
56 }
57
58 static void
59 aubioonset_tilde_dsp (t_aubioonset_tilde * x, t_signal ** sp)
60 {
61   dsp_add (aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
62 }
63
64 static void
65 aubioonset_tilde_debug (t_aubioonset_tilde * x)
66 {
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);
71 }
72
73 static void *
74 aubioonset_tilde_new (t_floatarg f)
75 {
76   t_aubioonset_tilde *x = (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
77
78   x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
79   x->bufsize = 1024;
80   x->hopsize = x->bufsize / 2;
81
82   x->o = new_aubio_onset ("default",
83       x->bufsize, x->hopsize, (uint_t) sys_getsr ());
84
85   if (x->o == NULL) return NULL;
86
87   x->in = (fvec_t *) new_fvec (x->hopsize);
88   x->out = (fvec_t *) new_fvec (1);
89
90   x->inlet = floatinlet_new (&x->x_obj, &x->threshold);
91   x->onsetbang = outlet_new (&x->x_obj, &s_bang);
92   return (void *) x;
93 }
94
95 static void
96 aubioonset_tilde_del (t_aubioonset_tilde *x)
97 {
98   inlet_free(x->inlet);
99   outlet_free(x->onsetbang);
100   del_aubio_onset(x->o);
101   del_fvec(x->in);
102   del_fvec(x->out);
103 }
104
105 void
106 aubioonset_tilde_setup (void)
107 {
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);
117 }