source: plugins/puredata/aubiopitch~.c @ 61316a6

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 61316a6 was 61316a6, checked in by Paul Brossier <piem@piem.org>, 12 years ago

examples plugins: update to latest pitch prototypes

  • Property mode set to 100644
File size: 3.6 KB
Line 
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#include <string.h>
13
14char aubiopitch_version[] = "aubiopitch~ version 0.1";
15
16aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft;
17aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
18
19static t_class *aubiopitch_tilde_class;
20
21void aubiopitch_tilde_setup (void);
22
23typedef struct _aubiopitch_tilde
24{
25        t_object x_obj;
26        t_float threshold;     
27        t_float threshold2;     
28        t_int pos; /*frames%dspblocksize*/
29        t_int bufsize;
30        t_int hopsize;
31        aubio_pitchdetection_t *o;
32        fvec_t *vec;
33        fvec_t *pitchvec;
34        t_outlet *pitch;
35} t_aubiopitch_tilde;
36
37static t_int *aubiopitch_tilde_perform(t_int *w) 
38{
39        t_aubiopitch_tilde *x = (t_aubiopitch_tilde *)(w[1]);
40        t_sample *in          = (t_sample *)(w[2]);
41        int n                 = (int)(w[3]);
42        int j;
43        for (j=0;j<n;j++) {
44                /* write input to datanew */
45                fvec_write_sample(x->vec, in[j], 0, x->pos);
46                /*time for fft*/
47                if (x->pos == x->hopsize-1) {         
48                        /* block loop */
49                        aubio_pitchdetection_do(x->o,x->vec, x->pitchvec);
50                        outlet_float(x->pitch, x->pitchvec->data[0][0]);
51                        /* end of block loop */
52                        x->pos = -1; /* so it will be zero next j loop */
53                }
54                x->pos++;
55        }
56        return (w+4);
57}
58
59static void aubiopitch_tilde_dsp(t_aubiopitch_tilde *x, t_signal **sp)
60{
61        dsp_add(aubiopitch_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
62}
63
64static void aubiopitch_tilde_debug(t_aubiopitch_tilde *x)
65{
66        post("aubiopitch~ bufsize:\t%d", x->bufsize);
67        post("aubiopitch~ hopsize:\t%d", x->hopsize);
68        post("aubiopitch~ threshold:\t%f", x->threshold);
69        post("aubiopitch~ audio in:\t%f", x->vec->data[0][0]);
70}
71
72//static void *aubiopitch_tilde_new (t_floatarg f)
73static void *aubiopitch_tilde_new (t_symbol * s)
74{
75        t_aubiopitch_tilde *x = 
76                (t_aubiopitch_tilde *)pd_new(aubiopitch_tilde_class);
77
78        x->bufsize   = 2048;
79        x->hopsize   = x->bufsize / 2;
80
81        if (strcmp(s->s_name,"mcomb") == 0) 
82                type_pitch = aubio_pitch_mcomb;
83        else if (strcmp(s->s_name,"yinfft") == 0) 
84                type_pitch = aubio_pitch_yin;
85        else if (strcmp(s->s_name,"yin") == 0) 
86                type_pitch = aubio_pitch_yin;
87        else if (strcmp(s->s_name,"schmitt") == 0) 
88                type_pitch = aubio_pitch_schmitt;
89        else if (strcmp(s->s_name,"fcomb") == 0) 
90                type_pitch = aubio_pitch_fcomb;
91        else {
92                post("unknown pitch type, using default.\n");
93        }
94
95        //FIXME: get the real samplerate
96        x->o = new_aubio_pitchdetection(x->bufsize, 
97                    x->hopsize, 1, 44100., type_pitch, mode_pitch);
98        aubio_pitchdetection_set_tolerance (x->o, 0.7);
99        x->vec = (fvec_t *)new_fvec(x->hopsize,1);
100        x->pitchvec = (fvec_t *)new_fvec(1,1);
101
102        //floatinlet_new (&x->x_obj, &x->threshold);
103        x->pitch = outlet_new (&x->x_obj, &s_float);
104
105        post(aubiopitch_version);
106        return (void *)x;
107}
108
109static void *aubiopitch_tilde_del(t_aubiopitch_tilde *x)
110{
111        del_aubio_pitchdetection(x->o);
112        del_fvec(x->vec);
113        del_fvec(x->pitchvec);
114        return 0;
115}
116
117void aubiopitch_tilde_setup (void)
118{
119        aubiopitch_tilde_class = class_new (gensym ("aubiopitch~"),
120                        (t_newmethod)aubiopitch_tilde_new,
121                        (t_method)aubiopitch_tilde_del,
122                        sizeof (t_aubiopitch_tilde),
123                        CLASS_DEFAULT, A_DEFSYMBOL, 0);
124        class_addmethod(aubiopitch_tilde_class, 
125                        (t_method)aubiopitch_tilde_dsp, 
126                        gensym("dsp"), 0);
127        class_addmethod(aubiopitch_tilde_class, 
128                        (t_method)aubiopitch_tilde_debug,
129                        gensym("debug"), 0);
130        CLASS_MAINSIGNALIN(aubiopitch_tilde_class, 
131                        t_aubiopitch_tilde, threshold);
132}
133
Note: See TracBrowser for help on using the repository browser.