[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 | |
---|
[650e39b] | 16 | aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; |
---|
[660c1d82] | 17 | aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq; |
---|
| 18 | |
---|
| 19 | static t_class *aubiopitch_tilde_class; |
---|
| 20 | |
---|
| 21 | void aubiopitch_tilde_setup (void); |
---|
| 22 | |
---|
| 23 | typedef struct _aubiopitch_tilde |
---|
| 24 | { |
---|
| 25 | t_object x_obj; |
---|
| 26 | t_float threshold; |
---|
| 27 | t_float threshold2; |
---|
| 28 | t_int pos; /*frames%dspblocksize*/ |
---|
| 29 | t_int bufsize; |
---|
| 30 | t_int hopsize; |
---|
| 31 | aubio_pitchdetection_t *o; |
---|
| 32 | fvec_t *vec; |
---|
[61316a6] | 33 | fvec_t *pitchvec; |
---|
[660c1d82] | 34 | t_outlet *pitch; |
---|
| 35 | } t_aubiopitch_tilde; |
---|
| 36 | |
---|
| 37 | static t_int *aubiopitch_tilde_perform(t_int *w) |
---|
| 38 | { |
---|
| 39 | t_aubiopitch_tilde *x = (t_aubiopitch_tilde *)(w[1]); |
---|
| 40 | t_sample *in = (t_sample *)(w[2]); |
---|
| 41 | int n = (int)(w[3]); |
---|
| 42 | int j; |
---|
| 43 | for (j=0;j<n;j++) { |
---|
| 44 | /* write input to datanew */ |
---|
| 45 | fvec_write_sample(x->vec, in[j], 0, x->pos); |
---|
| 46 | /*time for fft*/ |
---|
| 47 | if (x->pos == x->hopsize-1) { |
---|
| 48 | /* block loop */ |
---|
[61316a6] | 49 | aubio_pitchdetection_do(x->o,x->vec, x->pitchvec); |
---|
| 50 | outlet_float(x->pitch, x->pitchvec->data[0][0]); |
---|
[660c1d82] | 51 | /* end of block loop */ |
---|
| 52 | x->pos = -1; /* so it will be zero next j loop */ |
---|
| 53 | } |
---|
| 54 | x->pos++; |
---|
| 55 | } |
---|
| 56 | return (w+4); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | static void aubiopitch_tilde_dsp(t_aubiopitch_tilde *x, t_signal **sp) |
---|
| 60 | { |
---|
| 61 | dsp_add(aubiopitch_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | static void aubiopitch_tilde_debug(t_aubiopitch_tilde *x) |
---|
| 65 | { |
---|
| 66 | post("aubiopitch~ bufsize:\t%d", x->bufsize); |
---|
| 67 | post("aubiopitch~ hopsize:\t%d", x->hopsize); |
---|
| 68 | post("aubiopitch~ threshold:\t%f", x->threshold); |
---|
| 69 | post("aubiopitch~ audio in:\t%f", x->vec->data[0][0]); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | //static void *aubiopitch_tilde_new (t_floatarg f) |
---|
[4d7ced9] | 73 | static void *aubiopitch_tilde_new (t_symbol * s) |
---|
[660c1d82] | 74 | { |
---|
| 75 | t_aubiopitch_tilde *x = |
---|
| 76 | (t_aubiopitch_tilde *)pd_new(aubiopitch_tilde_class); |
---|
| 77 | |
---|
| 78 | x->bufsize = 2048; |
---|
| 79 | x->hopsize = x->bufsize / 2; |
---|
| 80 | |
---|
[4d7ced9] | 81 | if (strcmp(s->s_name,"mcomb") == 0) |
---|
| 82 | type_pitch = aubio_pitch_mcomb; |
---|
| 83 | else if (strcmp(s->s_name,"yinfft") == 0) |
---|
| 84 | type_pitch = aubio_pitch_yin; |
---|
| 85 | else if (strcmp(s->s_name,"yin") == 0) |
---|
| 86 | type_pitch = aubio_pitch_yin; |
---|
| 87 | else if (strcmp(s->s_name,"schmitt") == 0) |
---|
| 88 | type_pitch = aubio_pitch_schmitt; |
---|
| 89 | else if (strcmp(s->s_name,"fcomb") == 0) |
---|
| 90 | type_pitch = aubio_pitch_fcomb; |
---|
| 91 | else { |
---|
| 92 | post("unknown pitch type, using default.\n"); |
---|
| 93 | } |
---|
| 94 | |
---|
[660c1d82] | 95 | //FIXME: get the real samplerate |
---|
[4d7ced9] | 96 | x->o = new_aubio_pitchdetection(x->bufsize, |
---|
[660c1d82] | 97 | x->hopsize, 1, 44100., type_pitch, mode_pitch); |
---|
[61316a6] | 98 | aubio_pitchdetection_set_tolerance (x->o, 0.7); |
---|
[660c1d82] | 99 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
[61316a6] | 100 | x->pitchvec = (fvec_t *)new_fvec(1,1); |
---|
[660c1d82] | 101 | |
---|
| 102 | //floatinlet_new (&x->x_obj, &x->threshold); |
---|
| 103 | x->pitch = outlet_new (&x->x_obj, &s_float); |
---|
| 104 | |
---|
| 105 | post(aubiopitch_version); |
---|
| 106 | return (void *)x; |
---|
| 107 | } |
---|
| 108 | |
---|
[845f2d4] | 109 | static void *aubiopitch_tilde_del(t_aubiopitch_tilde *x) |
---|
| 110 | { |
---|
| 111 | del_aubio_pitchdetection(x->o); |
---|
| 112 | del_fvec(x->vec); |
---|
[61316a6] | 113 | del_fvec(x->pitchvec); |
---|
[845f2d4] | 114 | return 0; |
---|
| 115 | } |
---|
| 116 | |
---|
[660c1d82] | 117 | void aubiopitch_tilde_setup (void) |
---|
| 118 | { |
---|
| 119 | aubiopitch_tilde_class = class_new (gensym ("aubiopitch~"), |
---|
| 120 | (t_newmethod)aubiopitch_tilde_new, |
---|
[845f2d4] | 121 | (t_method)aubiopitch_tilde_del, |
---|
| 122 | sizeof (t_aubiopitch_tilde), |
---|
[4d7ced9] | 123 | CLASS_DEFAULT, A_DEFSYMBOL, 0); |
---|
[660c1d82] | 124 | class_addmethod(aubiopitch_tilde_class, |
---|
| 125 | (t_method)aubiopitch_tilde_dsp, |
---|
| 126 | gensym("dsp"), 0); |
---|
| 127 | class_addmethod(aubiopitch_tilde_class, |
---|
| 128 | (t_method)aubiopitch_tilde_debug, |
---|
| 129 | gensym("debug"), 0); |
---|
| 130 | CLASS_MAINSIGNALIN(aubiopitch_tilde_class, |
---|
| 131 | t_aubiopitch_tilde, threshold); |
---|
| 132 | } |
---|
| 133 | |
---|