source: plugins/puredata/aubioonset~.c @ 01b8fcc

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

src/onset: rename aubio_onsetdetection to aubio_onsetdetection_do

  • Property mode set to 100644
File size: 3.1 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.1";
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_float threshold2;     
24        t_int pos; /*frames%dspblocksize*/
25        t_int bufsize;
26        t_int hopsize;
27        aubio_onsetdetection_t *o;
28        aubio_pvoc_t * pv;
29        aubio_peakpicker_t * parms;
30        fvec_t *vec;
31        fvec_t *onset;
32        cvec_t *fftgrain;
33        t_outlet *onsetbang;
34} t_aubioonset_tilde;
35
36static t_int *aubioonset_tilde_perform(t_int *w) 
37{
38        t_aubioonset_tilde *x = (t_aubioonset_tilde *)(w[1]);
39        t_sample *in          = (t_sample *)(w[2]);
40        int n                 = (int)(w[3]);
41        int j,isonset;
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                        aubio_pvoc_do (x->pv,x->vec, x->fftgrain);
49                        aubio_onsetdetection_do (x->o,x->fftgrain, x->onset);
50                        isonset = aubio_peakpick_pimrt(x->onset,x->parms);
51                        if (isonset) {
52                                /* test for silence */
53                                if (aubio_silence_detection(x->vec, x->threshold2)==1)
54                                        isonset=0;
55                                else
56                                        outlet_bang(x->onsetbang);
57                        }
58                        /* end of block loop */
59                        x->pos = -1; /* so it will be zero next j loop */
60                }
61                x->pos++;
62        }
63        return (w+4);
64}
65
66static void aubioonset_tilde_dsp(t_aubioonset_tilde *x, t_signal **sp)
67{
68        dsp_add(aubioonset_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
69}
70
71static void aubioonset_tilde_debug(t_aubioonset_tilde *x)
72{
73        post("aubioonset~ bufsize:\t%d", x->bufsize);
74        post("aubioonset~ hopsize:\t%d", x->hopsize);
75        post("aubioonset~ threshold:\t%f", x->threshold);
76        post("aubioonset~ audio in:\t%f", x->vec->data[0][0]);
77        post("aubioonset~ onset:\t%f", x->onset->data[0][0]);
78}
79
80static void *aubioonset_tilde_new (t_floatarg f)
81{
82        t_aubioonset_tilde *x = 
83                (t_aubioonset_tilde *)pd_new(aubioonset_tilde_class);
84
85        x->threshold = (f < 1e-5) ? 0.1 : (f > 10.) ? 10. : f;
86        x->threshold2 = -70.;
87        x->bufsize   = 1024;
88        x->hopsize   = x->bufsize / 2;
89
90        x->o = new_aubio_onsetdetection(aubio_onset_complex, x->bufsize, 1);
91        x->vec = (fvec_t *)new_fvec(x->hopsize,1);
92        x->pv = (aubio_pvoc_t *)new_aubio_pvoc(x->bufsize, x->hopsize, 1);
93        x->fftgrain  = (cvec_t *)new_cvec(x->bufsize,1);
94        x->onset = (fvec_t *)new_fvec(1,1);
95        x->parms = new_aubio_peakpicker(x->threshold);
96
97        floatinlet_new (&x->x_obj, &x->threshold);
98        x->onsetbang = outlet_new (&x->x_obj, &s_bang);
99        post(aubioonset_version);
100        return (void *)x;
101}
102
103void aubioonset_tilde_setup (void)
104{
105        aubioonset_tilde_class = class_new (gensym ("aubioonset~"),
106                        (t_newmethod)aubioonset_tilde_new,
107                        0, sizeof (t_aubioonset_tilde),
108                        CLASS_DEFAULT, A_DEFFLOAT, 0);
109        class_addmethod(aubioonset_tilde_class, 
110                        (t_method)aubioonset_tilde_dsp, 
111                        gensym("dsp"), 0);
112        class_addmethod(aubioonset_tilde_class, 
113                        (t_method)aubioonset_tilde_debug,
114                        gensym("debug"), 0);
115        CLASS_MAINSIGNALIN(aubioonset_tilde_class, 
116                        t_aubioonset_tilde, threshold);
117}
118
Note: See TracBrowser for help on using the repository browser.