source: plugins/puredata/aubiopitch~.c @ 4d7ced9

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

plugins/puredata/aubiopitch~.c: add optional creation argument to select pitch method, fix buffer size

  • Property mode set to 100644
File size: 3.5 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        t_outlet *pitch;
34} t_aubiopitch_tilde;
35
36static t_int *aubiopitch_tilde_perform(t_int *w) 
37{
38        t_aubiopitch_tilde *x = (t_aubiopitch_tilde *)(w[1]);
39        t_sample *in          = (t_sample *)(w[2]);
40        int n                 = (int)(w[3]);
41        int j;
42        smpl_t pitch;
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                        pitch = aubio_pitchdetection(x->o,x->vec);
50                        outlet_float(x->pitch, pitch);
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_yinthresh(x->o, 0.7);
99        x->vec = (fvec_t *)new_fvec(x->hopsize,1);
100
101        //floatinlet_new (&x->x_obj, &x->threshold);
102        x->pitch = outlet_new (&x->x_obj, &s_float);
103
104        post(aubiopitch_version);
105        return (void *)x;
106}
107
108static void *aubiopitch_tilde_del(t_aubiopitch_tilde *x)
109{
110        del_aubio_pitchdetection(x->o);
111        del_fvec(x->vec);
112        return 0;
113}
114
115void aubiopitch_tilde_setup (void)
116{
117        aubiopitch_tilde_class = class_new (gensym ("aubiopitch~"),
118                        (t_newmethod)aubiopitch_tilde_new,
119                        (t_method)aubiopitch_tilde_del,
120                        sizeof (t_aubiopitch_tilde),
121                        CLASS_DEFAULT, A_DEFSYMBOL, 0);
122        class_addmethod(aubiopitch_tilde_class, 
123                        (t_method)aubiopitch_tilde_dsp, 
124                        gensym("dsp"), 0);
125        class_addmethod(aubiopitch_tilde_class, 
126                        (t_method)aubiopitch_tilde_debug,
127                        gensym("debug"), 0);
128        CLASS_MAINSIGNALIN(aubiopitch_tilde_class, 
129                        t_aubiopitch_tilde, threshold);
130}
131
Note: See TracBrowser for help on using the repository browser.