src/aubiopitch~.c: fix aubiopitch_tilde_del, remove unused variable
[pd-aubio.git] / src / aubiopitch~.c
1 /**
2  *
3  * a puredata wrapper for aubio pitch 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 #include <string.h>
13
14 char aubiopitch_version[] = "aubiopitch~ version " PACKAGE_VERSION;
15
16 static t_class *aubiopitch_tilde_class;
17
18 void aubiopitch_tilde_setup (void);
19
20 typedef struct _aubiopitch_tilde
21 {
22   t_object x_obj;
23   t_float threshold;
24   t_int pos; /*frames%dspblocksize*/
25   t_int bufsize;
26   t_int hopsize;
27   aubio_pitch_t *o;
28   fvec_t *vec;
29   fvec_t *pitchvec;
30   t_outlet *pitch;
31 } t_aubiopitch_tilde;
32
33 static t_int *aubiopitch_tilde_perform(t_int *w)
34 {
35   t_aubiopitch_tilde *x = (t_aubiopitch_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_set_sample(x->vec, in[j], x->pos);
42     /*time for fft*/
43     if (x->pos == x->hopsize-1) {
44       /* block loop */
45       aubio_pitch_do(x->o, x->vec, x->pitchvec);
46       outlet_float(x->pitch, x->pitchvec->data[0]);
47       /* end of block loop */
48       x->pos = -1; /* so it will be zero next j loop */
49     }
50     x->pos++;
51   }
52   return (w+4);
53 }
54
55 static void aubiopitch_tilde_dsp(t_aubiopitch_tilde *x, t_signal **sp)
56 {
57   dsp_add(aubiopitch_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
58 }
59
60 static void aubiopitch_tilde_debug(t_aubiopitch_tilde *x)
61 {
62   post(aubiopitch_version);
63   post("aubiopitch~ bufsize:\t%d", x->bufsize);
64   post("aubiopitch~ hopsize:\t%d", x->hopsize);
65   post("aubiopitch~ threshold:\t%f", x->threshold);
66   post("aubiopitch~ audio in:\t%f", x->vec->data[0]);
67 }
68
69 //static void *aubiopitch_tilde_new (t_floatarg f)
70 static void *aubiopitch_tilde_new (t_symbol * s)
71 {
72   t_aubiopitch_tilde *x =
73     (t_aubiopitch_tilde *)pd_new(aubiopitch_tilde_class);
74
75   x->bufsize = 2048;
76   x->hopsize = x->bufsize / 2;
77
78   x->o = new_aubio_pitch(s->s_name, x->bufsize,
79       x->hopsize, (uint_t)sys_getsr() );
80   aubio_pitch_set_tolerance (x->o, 0.7);
81   x->vec = (fvec_t *)new_fvec(x->hopsize);
82   x->pitchvec = (fvec_t *)new_fvec(1);
83
84   //floatinlet_new (&x->x_obj, &x->threshold);
85   x->pitch = outlet_new (&x->x_obj, &s_float);
86
87   return (void *)x;
88 }
89
90 void aubiopitch_tilde_del(t_aubiopitch_tilde *x)
91 {
92   outlet_free(x->pitch);
93   del_aubio_pitch(x->o);
94   del_fvec(x->vec);
95   del_fvec(x->pitchvec);
96 }
97
98 void aubiopitch_tilde_setup (void)
99 {
100   aubiopitch_tilde_class = class_new (gensym ("aubiopitch~"),
101       (t_newmethod)aubiopitch_tilde_new,
102       (t_method)aubiopitch_tilde_del,
103       sizeof (t_aubiopitch_tilde),
104       CLASS_DEFAULT, A_DEFSYMBOL, 0);
105   class_addmethod(aubiopitch_tilde_class,
106       (t_method)aubiopitch_tilde_dsp,
107       gensym("dsp"), 0);
108   class_addmethod(aubiopitch_tilde_class,
109       (t_method)aubiopitch_tilde_debug,
110       gensym("debug"), 0);
111   CLASS_MAINSIGNALIN(aubiopitch_tilde_class,
112       t_aubiopitch_tilde, threshold);
113 }