[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; |
---|
| 33 | t_outlet *pitch; |
---|
| 34 | } t_aubiopitch_tilde; |
---|
| 35 | |
---|
| 36 | static t_int *aubiopitch_tilde_perform(t_int *w) |
---|
| 37 | { |
---|
| 38 | t_aubiopitch_tilde *x = (t_aubiopitch_tilde *)(w[1]); |
---|
| 39 | t_sample *in = (t_sample *)(w[2]); |
---|
| 40 | int n = (int)(w[3]); |
---|
| 41 | int j; |
---|
| 42 | smpl_t pitch; |
---|
| 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 */ |
---|
| 49 | pitch = aubio_pitchdetection(x->o,x->vec); |
---|
| 50 | outlet_float(x->pitch, pitch); |
---|
| 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); |
---|
[650e39b] | 98 | aubio_pitchdetection_set_yinthresh(x->o, 0.7); |
---|
[660c1d82] | 99 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
| 100 | |
---|
| 101 | //floatinlet_new (&x->x_obj, &x->threshold); |
---|
| 102 | x->pitch = outlet_new (&x->x_obj, &s_float); |
---|
| 103 | |
---|
| 104 | post(aubiopitch_version); |
---|
| 105 | return (void *)x; |
---|
| 106 | } |
---|
| 107 | |
---|
[845f2d4] | 108 | static void *aubiopitch_tilde_del(t_aubiopitch_tilde *x) |
---|
| 109 | { |
---|
| 110 | del_aubio_pitchdetection(x->o); |
---|
| 111 | del_fvec(x->vec); |
---|
| 112 | return 0; |
---|
| 113 | } |
---|
| 114 | |
---|
[660c1d82] | 115 | void aubiopitch_tilde_setup (void) |
---|
| 116 | { |
---|
| 117 | aubiopitch_tilde_class = class_new (gensym ("aubiopitch~"), |
---|
| 118 | (t_newmethod)aubiopitch_tilde_new, |
---|
[845f2d4] | 119 | (t_method)aubiopitch_tilde_del, |
---|
| 120 | sizeof (t_aubiopitch_tilde), |
---|
[4d7ced9] | 121 | CLASS_DEFAULT, A_DEFSYMBOL, 0); |
---|
[660c1d82] | 122 | class_addmethod(aubiopitch_tilde_class, |
---|
| 123 | (t_method)aubiopitch_tilde_dsp, |
---|
| 124 | gensym("dsp"), 0); |
---|
| 125 | class_addmethod(aubiopitch_tilde_class, |
---|
| 126 | (t_method)aubiopitch_tilde_debug, |
---|
| 127 | gensym("debug"), 0); |
---|
| 128 | CLASS_MAINSIGNALIN(aubiopitch_tilde_class, |
---|
| 129 | t_aubiopitch_tilde, threshold); |
---|
| 130 | } |
---|
| 131 | |
---|