src/aubioonset~.c: add method, bufsize, and hopsize arguments
[pd-aubio.git] / src / aubiotempo~.c
1 /**
2  *
3  * a puredata wrapper for aubio tempo 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 aubiotempo_version[] = "aubiotempo~ version " PACKAGE_VERSION;
14
15 static t_class *aubiotempo_tilde_class;
16
17 void aubiotempo_tilde_setup (void);
18
19 typedef struct _aubiotempo_tilde
20 {
21   t_object x_obj;
22   t_float threshold;
23   t_float silence;
24   t_int pos; /*frames%dspblocksize*/
25   t_int bufsize;
26   t_int hopsize;
27   aubio_tempo_t * t;
28   fvec_t *vec;
29   fvec_t *output;
30   t_outlet *tempobang;
31   t_outlet *onsetbang;
32 } t_aubiotempo_tilde;
33
34 static t_int *aubiotempo_tilde_perform(t_int *w)
35 {
36   t_aubiotempo_tilde *x = (t_aubiotempo_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->vec, in[j], x->pos);
43     /*time for fft*/
44     if (x->pos == x->hopsize-1) {
45       /* block loop */
46       aubio_tempo_do (x->t, x->vec, x->output);
47       if (x->output->data[0]) {
48         outlet_bang(x->tempobang);
49       }
50       if (x->output->data[1]) {
51         outlet_bang(x->onsetbang);
52       }
53       /* end of block loop */
54       x->pos = -1; /* so it will be zero next j loop */
55     }
56     x->pos++;
57   }
58   return (w+4);
59 }
60
61 static void aubiotempo_tilde_dsp(t_aubiotempo_tilde *x, t_signal **sp)
62 {
63   dsp_add(aubiotempo_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
64 }
65
66 static void aubiotempo_tilde_debug(t_aubiotempo_tilde *x)
67 {
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]);
73 }
74
75 static void *aubiotempo_tilde_new (t_floatarg f)
76 {
77   t_aubiotempo_tilde *x =
78     (t_aubiotempo_tilde *)pd_new(aubiotempo_tilde_class);
79
80   x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
81   x->silence = -70.;
82   /* should get from block~ size */
83   x->bufsize   = 1024;
84   x->hopsize   = x->bufsize / 2;
85
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);
92
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);
96   return (void *)x;
97 }
98
99 static void *aubiotempo_tilde_del(t_aubiotempo_tilde *x)
100 {
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);
104   return 0;
105 }
106
107 void aubiotempo_tilde_setup (void)
108 {
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,
116       gensym("dsp"), 0);
117   class_addmethod(aubiotempo_tilde_class,
118       (t_method)aubiotempo_tilde_debug,
119       gensym("debug"), 0);
120   CLASS_MAINSIGNALIN(aubiotempo_tilde_class,
121       t_aubiotempo_tilde, threshold);
122 }