[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 | |
---|
[d25bd12] | 85 | x->t = new_aubio_tempo ("complex", x->bufsize, x->hopsize, 1, |
---|
| 86 | (uint_t) sys_getsr ()); |
---|
[4cab19e] | 87 | aubio_tempo_set_silence(x->t,x->silence); |
---|
| 88 | aubio_tempo_set_threshold(x->t,x->threshold); |
---|
| 89 | x->output = (fvec_t *)new_fvec(2,1); |
---|
| 90 | x->vec = (fvec_t *)new_fvec(x->hopsize,1); |
---|
[cf83555] | 91 | |
---|
[4cab19e] | 92 | floatinlet_new (&x->x_obj, &x->threshold); |
---|
| 93 | x->tempobang = outlet_new (&x->x_obj, &s_bang); |
---|
| 94 | x->onsetbang = outlet_new (&x->x_obj, &s_bang); |
---|
| 95 | post(aubiotempo_version); |
---|
| 96 | return (void *)x; |
---|
| 97 | } |
---|
[cf83555] | 98 | |
---|
[4cab19e] | 99 | static void *aubiotempo_tilde_del(t_aubiotempo_tilde *x) |
---|
| 100 | { |
---|
| 101 | if(x->t) del_aubio_tempo(x->t); |
---|
| 102 | if(x->output) del_fvec(x->output); |
---|
| 103 | if(x->vec) del_fvec(x->vec); |
---|
| 104 | return 0; |
---|
[cf83555] | 105 | } |
---|
| 106 | |
---|
| 107 | void aubiotempo_tilde_setup (void) |
---|
| 108 | { |
---|
[4cab19e] | 109 | aubiotempo_tilde_class = class_new (gensym ("aubiotempo~"), |
---|
| 110 | (t_newmethod)aubiotempo_tilde_new, |
---|
| 111 | (t_method)aubiotempo_tilde_del, |
---|
| 112 | sizeof (t_aubiotempo_tilde), |
---|
| 113 | CLASS_DEFAULT, A_DEFFLOAT, 0); |
---|
| 114 | class_addmethod(aubiotempo_tilde_class, |
---|
| 115 | (t_method)aubiotempo_tilde_dsp, |
---|
| 116 | gensym("dsp"), 0); |
---|
| 117 | class_addmethod(aubiotempo_tilde_class, |
---|
| 118 | (t_method)aubiotempo_tilde_debug, |
---|
| 119 | gensym("debug"), 0); |
---|
| 120 | CLASS_MAINSIGNALIN(aubiotempo_tilde_class, |
---|
| 121 | t_aubiotempo_tilde, threshold); |
---|
[cf83555] | 122 | } |
---|
| 123 | |
---|