[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 | |
---|
| 15 | aubio_pitchdetection_type type_pitch = aubio_pitch_schmitt; // aubio_pitch_mcomb |
---|
| 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); |
---|
| 83 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
| 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 | |
---|
| 92 | void aubiopitch_tilde_setup (void) |
---|
| 93 | { |
---|
| 94 | aubiopitch_tilde_class = class_new (gensym ("aubiopitch~"), |
---|
| 95 | (t_newmethod)aubiopitch_tilde_new, |
---|
| 96 | 0, sizeof (t_aubiopitch_tilde), |
---|
| 97 | CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
| 98 | class_addmethod(aubiopitch_tilde_class, |
---|
| 99 | (t_method)aubiopitch_tilde_dsp, |
---|
| 100 | gensym("dsp"), 0); |
---|
| 101 | class_addmethod(aubiopitch_tilde_class, |
---|
| 102 | (t_method)aubiopitch_tilde_debug, |
---|
| 103 | gensym("debug"), 0); |
---|
| 104 | class_sethelpsymbol(aubiopitch_tilde_class, |
---|
| 105 | gensym("help-aubiopitch~.pd")); |
---|
| 106 | CLASS_MAINSIGNALIN(aubiopitch_tilde_class, |
---|
| 107 | t_aubiopitch_tilde, threshold); |
---|
| 108 | } |
---|
| 109 | |
---|