[660c1d82] | 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.h> |
---|
[4d7ced9] | 12 | #include <string.h> |
---|
[660c1d82] | 13 | |
---|
| 14 | char aubiopitch_version[] = "aubiopitch~ version 0.1"; |
---|
| 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_float threshold2; |
---|
| 25 | t_int pos; /*frames%dspblocksize*/ |
---|
| 26 | t_int bufsize; |
---|
| 27 | t_int hopsize; |
---|
[ca1abdd] | 28 | aubio_pitch_t *o; |
---|
[660c1d82] | 29 | fvec_t *vec; |
---|
[61316a6] | 30 | fvec_t *pitchvec; |
---|
[660c1d82] | 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_write_sample(x->vec, in[j], 0, x->pos); |
---|
| 43 | /*time for fft*/ |
---|
| 44 | if (x->pos == x->hopsize-1) { |
---|
| 45 | /* block loop */ |
---|
[ca1abdd] | 46 | aubio_pitch_do(x->o,x->vec, x->pitchvec); |
---|
[61316a6] | 47 | outlet_float(x->pitch, x->pitchvec->data[0][0]); |
---|
[660c1d82] | 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_debug(t_aubiopitch_tilde *x) |
---|
| 62 | { |
---|
| 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][0]); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | //static void *aubiopitch_tilde_new (t_floatarg f) |
---|
[4d7ced9] | 70 | static void *aubiopitch_tilde_new (t_symbol * s) |
---|
[660c1d82] | 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 | //FIXME: get the real samplerate |
---|
[ca1abdd] | 79 | x->o = new_aubio_pitch(s->s_name, x->bufsize, |
---|
[fe163ad] | 80 | x->hopsize, 1, 44100.); |
---|
[ca1abdd] | 81 | aubio_pitch_set_tolerance (x->o, 0.7); |
---|
[660c1d82] | 82 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
[61316a6] | 83 | x->pitchvec = (fvec_t *)new_fvec(1,1); |
---|
[660c1d82] | 84 | |
---|
| 85 | //floatinlet_new (&x->x_obj, &x->threshold); |
---|
| 86 | x->pitch = outlet_new (&x->x_obj, &s_float); |
---|
| 87 | |
---|
| 88 | post(aubiopitch_version); |
---|
| 89 | return (void *)x; |
---|
| 90 | } |
---|
| 91 | |
---|
[845f2d4] | 92 | static void *aubiopitch_tilde_del(t_aubiopitch_tilde *x) |
---|
| 93 | { |
---|
[ca1abdd] | 94 | del_aubio_pitch(x->o); |
---|
[845f2d4] | 95 | del_fvec(x->vec); |
---|
[61316a6] | 96 | del_fvec(x->pitchvec); |
---|
[845f2d4] | 97 | return 0; |
---|
| 98 | } |
---|
| 99 | |
---|
[660c1d82] | 100 | void aubiopitch_tilde_setup (void) |
---|
| 101 | { |
---|
| 102 | aubiopitch_tilde_class = class_new (gensym ("aubiopitch~"), |
---|
| 103 | (t_newmethod)aubiopitch_tilde_new, |
---|
[845f2d4] | 104 | (t_method)aubiopitch_tilde_del, |
---|
| 105 | sizeof (t_aubiopitch_tilde), |
---|
[4d7ced9] | 106 | CLASS_DEFAULT, A_DEFSYMBOL, 0); |
---|
[660c1d82] | 107 | class_addmethod(aubiopitch_tilde_class, |
---|
| 108 | (t_method)aubiopitch_tilde_dsp, |
---|
| 109 | gensym("dsp"), 0); |
---|
| 110 | class_addmethod(aubiopitch_tilde_class, |
---|
| 111 | (t_method)aubiopitch_tilde_debug, |
---|
| 112 | gensym("debug"), 0); |
---|
| 113 | CLASS_MAINSIGNALIN(aubiopitch_tilde_class, |
---|
| 114 | t_aubiopitch_tilde, threshold); |
---|
| 115 | } |
---|
| 116 | |
---|