examples/: use wavetable to play pitch and to replace woodblock
[aubio.git] / examples / aubiopitch.c
1 /*
2   Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "utils.h"
22
23 unsigned int pos = 0; /*frames%dspblocksize*/
24
25 aubio_pitch_t *o;
26 aubio_wavetable_t *wavetable;
27 fvec_t *pitch;
28
29 static int aubio_process(smpl_t **input, smpl_t **output, int nframes) {
30   unsigned int j;       /*frames*/
31   for (j=0;j<(unsigned)nframes;j++) {
32     if(usejack) {
33       /* write input to datanew */
34       fvec_write_sample(ibuf, input[0][j], pos);
35       /* put synthnew in output */
36       output[0][j] = fvec_read_sample(obuf, pos);
37     }
38     /*time for fft*/
39     if (pos == overlap_size-1) {         
40       /* block loop */
41       aubio_pitch_do (o, ibuf, pitch);
42       smpl_t freq = fvec_read_sample(pitch, 0);
43       smpl_t amp = powf(10., aubio_db_spl(ibuf)*.05 );
44       aubio_wavetable_set_amp ( wavetable, amp );
45       if (freq != 0.0) {
46         aubio_wavetable_set_freq ( wavetable, freq );
47       } else {
48         aubio_wavetable_set_freq ( wavetable, 0.0 );
49       }
50       aubio_wavetable_do (wavetable, obuf, obuf);
51       /* end of block loop */
52       pos = -1; /* so it will be zero next j loop */
53     }
54     pos++;
55   }
56   return 1;
57 }
58
59 static void process_print (void) {
60       if (!verbose && usejack) return;
61       smpl_t pitch_found = fvec_read_sample(pitch, 0);
62       outmsg("%f %f\n",(frames) 
63               *overlap_size/(float)samplerate, pitch_found);
64 }
65
66 int main(int argc, char **argv) {
67   examples_common_init(argc,argv);
68
69   o = new_aubio_pitch (pitch_mode, buffer_size, overlap_size, samplerate);
70   pitch = new_fvec (1);
71
72   wavetable = new_aubio_wavetable (samplerate, overlap_size);
73   aubio_wavetable_play ( wavetable );
74
75   examples_common_process(aubio_process,process_print);
76
77   del_aubio_pitch (o);
78   del_aubio_wavetable (wavetable);
79   del_fvec (pitch);
80
81   examples_common_del();
82   debug("End of program.\n");
83   fflush(stderr);
84   return 0;
85 }
86