[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> |
---|
| 12 | |
---|
| 13 | char aubiopitch_version[] = "aubiopitch~ version 0.1"; |
---|
| 14 | |
---|
[650e39b] | 15 | aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; |
---|
[660c1d82] | 16 | aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq; |
---|
| 17 | |
---|
| 18 | static t_class *aubiopitch_tilde_class; |
---|
| 19 | |
---|
| 20 | void aubiopitch_tilde_setup (void); |
---|
| 21 | |
---|
| 22 | typedef struct _aubiopitch_tilde |
---|
| 23 | { |
---|
| 24 | t_object x_obj; |
---|
| 25 | t_float threshold; |
---|
| 26 | t_float threshold2; |
---|
| 27 | t_int pos; /*frames%dspblocksize*/ |
---|
| 28 | t_int bufsize; |
---|
| 29 | t_int hopsize; |
---|
| 30 | aubio_pitchdetection_t *o; |
---|
| 31 | fvec_t *vec; |
---|
| 32 | t_outlet *pitch; |
---|
| 33 | } t_aubiopitch_tilde; |
---|
| 34 | |
---|
| 35 | static t_int *aubiopitch_tilde_perform(t_int *w) |
---|
| 36 | { |
---|
| 37 | t_aubiopitch_tilde *x = (t_aubiopitch_tilde *)(w[1]); |
---|
| 38 | t_sample *in = (t_sample *)(w[2]); |
---|
| 39 | int n = (int)(w[3]); |
---|
| 40 | int j; |
---|
| 41 | smpl_t pitch; |
---|
| 42 | for (j=0;j<n;j++) { |
---|
| 43 | /* write input to datanew */ |
---|
| 44 | fvec_write_sample(x->vec, in[j], 0, x->pos); |
---|
| 45 | /*time for fft*/ |
---|
| 46 | if (x->pos == x->hopsize-1) { |
---|
| 47 | /* block loop */ |
---|
| 48 | pitch = aubio_pitchdetection(x->o,x->vec); |
---|
| 49 | outlet_float(x->pitch, pitch); |
---|
| 50 | /* end of block loop */ |
---|
| 51 | x->pos = -1; /* so it will be zero next j loop */ |
---|
| 52 | } |
---|
| 53 | x->pos++; |
---|
| 54 | } |
---|
| 55 | return (w+4); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | static void aubiopitch_tilde_dsp(t_aubiopitch_tilde *x, t_signal **sp) |
---|
| 59 | { |
---|
| 60 | dsp_add(aubiopitch_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | static void aubiopitch_tilde_debug(t_aubiopitch_tilde *x) |
---|
| 64 | { |
---|
| 65 | post("aubiopitch~ bufsize:\t%d", x->bufsize); |
---|
| 66 | post("aubiopitch~ hopsize:\t%d", x->hopsize); |
---|
| 67 | post("aubiopitch~ threshold:\t%f", x->threshold); |
---|
| 68 | post("aubiopitch~ audio in:\t%f", x->vec->data[0][0]); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | //static void *aubiopitch_tilde_new (t_floatarg f) |
---|
| 72 | static void *aubiopitch_tilde_new (void) |
---|
| 73 | { |
---|
| 74 | t_aubiopitch_tilde *x = |
---|
| 75 | (t_aubiopitch_tilde *)pd_new(aubiopitch_tilde_class); |
---|
| 76 | |
---|
| 77 | x->bufsize = 2048; |
---|
| 78 | x->hopsize = x->bufsize / 2; |
---|
| 79 | |
---|
| 80 | //FIXME: get the real samplerate |
---|
| 81 | x->o = new_aubio_pitchdetection(x->bufsize*4, |
---|
| 82 | x->hopsize, 1, 44100., type_pitch, mode_pitch); |
---|
[650e39b] | 83 | aubio_pitchdetection_set_yinthresh(x->o, 0.7); |
---|
[660c1d82] | 84 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
| 85 | |
---|
| 86 | //floatinlet_new (&x->x_obj, &x->threshold); |
---|
| 87 | x->pitch = outlet_new (&x->x_obj, &s_float); |
---|
| 88 | |
---|
| 89 | post(aubiopitch_version); |
---|
| 90 | return (void *)x; |
---|
| 91 | } |
---|
| 92 | |
---|
[845f2d4] | 93 | static void *aubiopitch_tilde_del(t_aubiopitch_tilde *x) |
---|
| 94 | { |
---|
| 95 | del_aubio_pitchdetection(x->o); |
---|
| 96 | del_fvec(x->vec); |
---|
| 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), |
---|
[660c1d82] | 106 | CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
| 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 | |
---|