[cf83555] | 1 | /** |
---|
| 2 | * |
---|
| 3 | * a puredata wrapper for aubio tempo 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 | |
---|
[4cab19e] | 13 | char aubiotempo_version[] = "aubiotempo~ version 0.2"; |
---|
[cf83555] | 14 | |
---|
| 15 | static t_class *aubiotempo_tilde_class; |
---|
| 16 | |
---|
| 17 | void aubiotempo_tilde_setup (void); |
---|
| 18 | |
---|
| 19 | typedef struct _aubiotempo_tilde |
---|
| 20 | { |
---|
[4cab19e] | 21 | t_object x_obj; |
---|
| 22 | t_float threshold; |
---|
| 23 | t_float silence; |
---|
| 24 | t_int pos; /*frames%dspblocksize*/ |
---|
| 25 | t_int bufsize; |
---|
| 26 | t_int hopsize; |
---|
| 27 | aubio_tempo_t * t; |
---|
| 28 | fvec_t *vec; |
---|
| 29 | fvec_t *output; |
---|
| 30 | t_outlet *tempobang; |
---|
| 31 | t_outlet *onsetbang; |
---|
[cf83555] | 32 | } t_aubiotempo_tilde; |
---|
| 33 | |
---|
| 34 | static t_int *aubiotempo_tilde_perform(t_int *w) |
---|
| 35 | { |
---|
[4cab19e] | 36 | t_aubiotempo_tilde *x = (t_aubiotempo_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 */ |
---|
[5bf23f7] | 46 | aubio_tempo_do (x->t, x->vec, x->output); |
---|
[4cab19e] | 47 | if (x->output->data[0][0]) { |
---|
| 48 | outlet_bang(x->tempobang); |
---|
| 49 | } |
---|
| 50 | if (x->output->data[0][1]) { |
---|
| 51 | outlet_bang(x->onsetbang); |
---|
| 52 | } |
---|
| 53 | /* end of block loop */ |
---|
| 54 | x->pos = -1; /* so it will be zero next j loop */ |
---|
| 55 | } |
---|
| 56 | x->pos++; |
---|
| 57 | } |
---|
| 58 | return (w+4); |
---|
[cf83555] | 59 | } |
---|
| 60 | |
---|
| 61 | static void aubiotempo_tilde_dsp(t_aubiotempo_tilde *x, t_signal **sp) |
---|
| 62 | { |
---|
[4cab19e] | 63 | dsp_add(aubiotempo_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n); |
---|
[cf83555] | 64 | } |
---|
| 65 | |
---|
| 66 | static void aubiotempo_tilde_debug(t_aubiotempo_tilde *x) |
---|
| 67 | { |
---|
[4cab19e] | 68 | post("aubiotempo~ bufsize:\t%d", x->bufsize); |
---|
| 69 | post("aubiotempo~ hopsize:\t%d", x->hopsize); |
---|
| 70 | post("aubiotempo~ threshold:\t%f", x->threshold); |
---|
| 71 | post("aubiotempo~ audio in:\t%f", x->vec->data[0][0]); |
---|
[cf83555] | 72 | } |
---|
| 73 | |
---|
| 74 | static void *aubiotempo_tilde_new (t_floatarg f) |
---|
| 75 | { |
---|
[4cab19e] | 76 | t_aubiotempo_tilde *x = |
---|
| 77 | (t_aubiotempo_tilde *)pd_new(aubiotempo_tilde_class); |
---|
| 78 | |
---|
| 79 | x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f; |
---|
| 80 | x->silence = -70.; |
---|
| 81 | /* should get from block~ size */ |
---|
| 82 | x->bufsize = 1024; |
---|
| 83 | x->hopsize = x->bufsize / 2; |
---|
[cf83555] | 84 | |
---|
[4cab19e] | 85 | x->t = new_aubio_tempo (aubio_onset_complex, x->bufsize, x->hopsize, 1); |
---|
| 86 | aubio_tempo_set_silence(x->t,x->silence); |
---|
| 87 | aubio_tempo_set_threshold(x->t,x->threshold); |
---|
| 88 | x->output = (fvec_t *)new_fvec(2,1); |
---|
| 89 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
[cf83555] | 90 | |
---|
[4cab19e] | 91 | floatinlet_new (&x->x_obj, &x->threshold); |
---|
| 92 | x->tempobang = outlet_new (&x->x_obj, &s_bang); |
---|
| 93 | x->onsetbang = outlet_new (&x->x_obj, &s_bang); |
---|
| 94 | post(aubiotempo_version); |
---|
| 95 | return (void *)x; |
---|
| 96 | } |
---|
[cf83555] | 97 | |
---|
[4cab19e] | 98 | static void *aubiotempo_tilde_del(t_aubiotempo_tilde *x) |
---|
| 99 | { |
---|
| 100 | if(x->t) del_aubio_tempo(x->t); |
---|
| 101 | if(x->output) del_fvec(x->output); |
---|
| 102 | if(x->vec) del_fvec(x->vec); |
---|
| 103 | return 0; |
---|
[cf83555] | 104 | } |
---|
| 105 | |
---|
| 106 | void aubiotempo_tilde_setup (void) |
---|
| 107 | { |
---|
[4cab19e] | 108 | aubiotempo_tilde_class = class_new (gensym ("aubiotempo~"), |
---|
| 109 | (t_newmethod)aubiotempo_tilde_new, |
---|
| 110 | (t_method)aubiotempo_tilde_del, |
---|
| 111 | sizeof (t_aubiotempo_tilde), |
---|
| 112 | CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
| 113 | class_addmethod(aubiotempo_tilde_class, |
---|
| 114 | (t_method)aubiotempo_tilde_dsp, |
---|
| 115 | gensym("dsp"), 0); |
---|
| 116 | class_addmethod(aubiotempo_tilde_class, |
---|
| 117 | (t_method)aubiotempo_tilde_debug, |
---|
| 118 | gensym("debug"), 0); |
---|
| 119 | CLASS_MAINSIGNALIN(aubiotempo_tilde_class, |
---|
| 120 | t_aubiotempo_tilde, threshold); |
---|
[cf83555] | 121 | } |
---|
| 122 | |
---|