README.md: improve instructions
[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 tolerance;
24   t_int pos; /*frames%dspblocksize*/
25   t_int bufsize;
26   t_int hopsize;
27   char_t *method;
28   aubio_pitch_t *o;
29   fvec_t *vec;
30   fvec_t *pitchvec;
31   t_outlet *pitch;
32 } t_aubiopitch_tilde;
33
34 static t_int *aubiopitch_tilde_perform(t_int *w)
35 {
36   t_aubiopitch_tilde *x = (t_aubiopitch_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_pitch_do(x->o, x->vec, x->pitchvec);
47       outlet_float(x->pitch, x->pitchvec->data[0]);
48       /* end of block loop */
49       x->pos = -1; /* so it will be zero next j loop */
50     }
51     x->pos++;
52   }
53   return (w+4);
54 }
55
56 static void aubiopitch_tilde_dsp(t_aubiopitch_tilde *x, t_signal **sp)
57 {
58   dsp_add(aubiopitch_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
59 }
60
61 static void aubiopitch_tilde_tolerance(t_aubiopitch_tilde *x, t_floatarg a)
62 {
63   if (a > 0) {
64     x->tolerance = a;
65     aubio_pitch_set_tolerance(x->o, x->tolerance);
66   } else {
67     post("aubiopitch~ tolerance set to %.2f",
68         aubio_pitch_get_tolerance(x->o));
69   }
70 }
71
72 static void aubiopitch_tilde_debug(t_aubiopitch_tilde *x)
73 {
74   smpl_t tolerance = aubio_pitch_get_tolerance(x->o);
75   post("%s method: %s tolerance: %.3f bufsize: %d hopsize: %d",
76       aubiopitch_version, x->method, tolerance, x->bufsize, x->hopsize);
77 }
78
79 static void *aubiopitch_tilde_new (t_symbol * s, int argc, t_atom *argv)
80 {
81
82   t_aubiopitch_tilde *x = (t_aubiopitch_tilde *)pd_new(aubiopitch_tilde_class);
83
84   x->method = "default";
85   x->bufsize = 2048;
86   x->hopsize = x->bufsize / 2;
87
88   if (argc >= 2) {
89     if (argv[1].a_type == A_FLOAT) {
90       x->bufsize = (uint_t)(argv[1].a_w.w_float);
91     }
92     argc--;
93   }
94
95   x->hopsize = x->bufsize / 2;
96
97   if (argc == 2) {
98     if (argv[2].a_type == A_FLOAT) {
99       x->hopsize = (uint_t)(argv[2].a_w.w_float);
100     }
101     argc--;
102   }
103
104   if (argc == 1) {
105     if (argv[0].a_type == A_SYMBOL) {
106       x->method = argv[0].a_w.w_symbol->s_name;
107     } else {
108       post("aubiopitch~: first argument should be a symbol");
109       return NULL;
110     }
111   }
112
113   x->o = new_aubio_pitch(x->method, x->bufsize, x->hopsize,
114       (uint_t)sys_getsr());
115
116   if (x->o == NULL) return NULL;
117
118   x->vec = (fvec_t *)new_fvec(x->hopsize);
119   x->pitchvec = (fvec_t *)new_fvec(1);
120
121   x->pitch = outlet_new (&x->x_obj, &s_float);
122
123   return (void *)x;
124 }
125
126 void aubiopitch_tilde_del(t_aubiopitch_tilde *x)
127 {
128   outlet_free(x->pitch);
129   del_aubio_pitch(x->o);
130   del_fvec(x->vec);
131   del_fvec(x->pitchvec);
132 }
133
134 void aubiopitch_tilde_setup (void)
135 {
136   aubiopitch_tilde_class = class_new (gensym ("aubiopitch~"),
137       (t_newmethod)aubiopitch_tilde_new,
138       (t_method)aubiopitch_tilde_del,
139       sizeof (t_aubiopitch_tilde),
140       CLASS_DEFAULT, A_GIMME, 0);
141   class_addmethod(aubiopitch_tilde_class,
142       (t_method)aubiopitch_tilde_dsp,
143       gensym("dsp"), 0);
144   class_addmethod(aubiopitch_tilde_class,
145       (t_method)aubiopitch_tilde_tolerance,
146       gensym("tolerance"), A_DEFFLOAT, 0);
147   class_addmethod(aubiopitch_tilde_class,
148       (t_method)aubiopitch_tilde_tolerance,
149       gensym("tol"), A_DEFFLOAT, 0);
150   class_addmethod(aubiopitch_tilde_class,
151       (t_method)aubiopitch_tilde_debug,
152       gensym("debug"), 0);
153   CLASS_MAINSIGNALIN(aubiopitch_tilde_class,
154       t_aubiopitch_tilde, tolerance);
155 }