874a266ca74338e0a00a9fc5001969fdf35526af
[pd-aubio.git] / src / aubiozcr~.c
1
2 /**
3  *
4  * a puredata wrapper for aubio zero crossing rate function
5  *
6  * Thanks to Johannes M Zmolnig for writing the excellent HOWTO:
7  *       http://iem.kug.ac.at/pd/externals-HOWTO/
8  *
9  * */
10
11 #include <m_pd.h>
12 #include <aubio/aubio.h>
13
14 char aubiozcr_version[] = "aubiozcr~ version " PACKAGE_VERSION;
15
16 static t_class *aubiozcr_tilde_class;
17
18 void aubiozcr_tilde_setup (void);
19
20 typedef struct _aubiozcr_tilde
21 {
22   t_object x_obj;
23   t_int pos; /*frames%dspblocksize*/
24   t_int bufsize;
25   t_float f;
26   fvec_t *vec;
27   t_outlet *zcr;
28 } t_aubiozcr_tilde;
29
30 static t_int *aubiozcr_tilde_perform(t_int *w)
31 {
32   t_aubiozcr_tilde *x = (t_aubiozcr_tilde *)(w[1]);
33   t_sample *in        = (t_sample *)(w[2]);
34   int n               = (int)(w[3]);
35   int j;
36   for (j=0;j<n;j++) {
37     /* write input to datanew */
38     fvec_set_sample(x->vec, in[j], x->pos);
39     /*time for fft*/
40     if (x->pos == x->bufsize-1) {
41       /* block loop */
42       outlet_float(x->zcr, aubio_zero_crossing_rate(x->vec));
43       /* end of block loop */
44       x->pos = -1; /* so it will be zero next j loop */
45     }
46     x->pos++;
47   }
48   return (w+4);
49 }
50
51 static void aubiozcr_tilde_dsp(t_aubiozcr_tilde *x, t_signal **sp)
52 {
53   dsp_add(aubiozcr_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
54 }
55
56 static void aubiozcr_tilde_debug(t_aubiozcr_tilde *x)
57 {
58   post("aubiozcr~ bufsize:\t%d", x->bufsize);
59   post("aubiozcr~ audio in:\t%f", x->vec->data[0]);
60 }
61
62 static void *aubiozcr_tilde_new (void)
63 {
64   t_aubiozcr_tilde *x =
65     (t_aubiozcr_tilde *)pd_new(aubiozcr_tilde_class);
66
67   x->bufsize   = 1024;
68
69   x->vec = (fvec_t *)new_fvec(x->bufsize);
70
71   x->zcr = outlet_new (&x->x_obj, &s_float);
72   post(aubiozcr_version);
73   return (void *)x;
74 }
75
76 void aubiozcr_tilde_setup (void)
77 {
78   aubiozcr_tilde_class = class_new (gensym ("aubiozcr~"),
79       (t_newmethod)aubiozcr_tilde_new,
80       0, sizeof (t_aubiozcr_tilde),
81       CLASS_DEFAULT, 0);
82   class_addmethod(aubiozcr_tilde_class,
83       (t_method)aubiozcr_tilde_dsp,
84       gensym("dsp"), 0);
85   class_addmethod(aubiozcr_tilde_class,
86       (t_method)aubiozcr_tilde_debug,
87       gensym("debug"), 0);
88   CLASS_MAINSIGNALIN(aubiozcr_tilde_class,
89       t_aubiozcr_tilde, f);
90 }