source: plugins/puredata/aubioonset~.c @ 970bced

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 970bced was 970bced, checked in by Paul Brossier <piem@piem.org>, 15 years ago

plugins/puredata/aubioonset~.c: use onset driver

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/**
2 *
3 * a puredata wrapper for aubio onset 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
13char aubioonset_version[] = "aubioonset~ version 0.3";
14
15static t_class *aubioonset_tilde_class;
16
17void aubioonset_tilde_setup (void);
18
19typedef struct _aubioonset_tilde
20{
21        t_object x_obj;
22        t_float threshold;     
23        t_int pos; /*frames%dspblocksize*/
24        t_int bufsize;
25        t_int hopsize;
26        aubio_onset_t *o;
27        fvec_t *in;
28        fvec_t *out;
29        t_outlet *onsetbang;
30} t_aubioonset_tilde;
31
32static t_int *aubioonset_tilde_perform(t_int *w) 
33{
34        t_aubioonset_tilde *x = (t_aubioonset_tilde *)(w[1]);
35        t_sample *in          = (t_sample *)(w[2]);
36        int n                 = (int)(w[3]);
37        int j;
38        for (j=0;j<n;j++) {
39                /* write input to datanew */
40                fvec_write_sample(x->in, in[j], 0, x->pos);
41                /*time to do something */
42                if (x->pos == x->hopsize-1) {         
43                        /* block loop */
44                        aubio_onset_do (x->o, x->in, x->out);
45                        if (fvec_read_sample(x->out, 0, 0) > 0.) {
46                outlet_bang(x->onsetbang);
47                        }
48                        /* end of block loop */
49                        x->pos = -1; /* so it will be zero next j loop */
50                }
51                x->pos++;
52        }
53        return (w+4);
54}
55
56static void aubioonset_tilde_dsp(t_aubioonset_tilde *x, t_signal **sp)
57{
58        dsp_add(aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
59}
60
61static void aubioonset_tilde_debug(t_aubioonset_tilde *x)
62{
63        post("aubioonset~ bufsize:\t%d", x->bufsize);
64        post("aubioonset~ hopsize:\t%d", x->hopsize);
65        post("aubioonset~ threshold:\t%f", x->threshold);
66        post("aubioonset~ audio in:\t%f", x->in->data[0][0]);
67        post("aubioonset~ onset:\t%f", x->out->data[0][0]);
68}
69
70static void *aubioonset_tilde_new (t_floatarg f)
71{
72        t_aubioonset_tilde *x = 
73                (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
74
75        x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
76        x->bufsize   = 1024;
77        x->hopsize   = x->bufsize / 2;
78
79        x->o = new_aubio_onset ("complex",
80            x->bufsize, x->hopsize, 1, (uint_t)sys_getsr());
81        x->in = (fvec_t *)new_fvec(x->hopsize,1);
82        x->out = (fvec_t *)new_fvec(1,1);
83
84        floatinlet_new (&x->x_obj, &x->threshold);
85        x->onsetbang = outlet_new (&x->x_obj, &s_bang);
86        post(aubioonset_version);
87        return (void *)x;
88}
89
90void aubioonset_tilde_setup (void)
91{
92        aubioonset_tilde_class = class_new (gensym ("aubioonset~"),
93                        (t_newmethod)aubioonset_tilde_new,
94                        0, sizeof (t_aubioonset_tilde),
95                        CLASS_DEFAULT, A_DEFFLOAT, 0);
96        class_addmethod(aubioonset_tilde_class, 
97                        (t_method)aubioonset_tilde_dsp, 
98                        gensym("dsp"), 0);
99        class_addmethod(aubioonset_tilde_class, 
100                        (t_method)aubioonset_tilde_debug,
101                        gensym("debug"), 0);
102        CLASS_MAINSIGNALIN(aubioonset_tilde_class, 
103                        t_aubioonset_tilde, threshold);
104}
105
Note: See TracBrowser for help on using the repository browser.