cleaned up verbose output
[aubio.git] / examples / testforclam.c
1 /*
2    Copyright (C) 2003 Paul Brossier
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <getopt.h>
24 #include <unistd.h>
25
26 #include "aubio.h"      
27
28 #include "utils.h"
29
30
31 const char * output_filename = NULL;
32 const char * input_filename = NULL;
33
34 const char * onset_filename = "/usr/share/sounds/aubio/woodblock.aiff";
35
36 int verbose = 0;
37 int usejack = 0;
38
39 int aubio_process(float **input, float **output, int nframes);
40
41 /* settings */
42
43 /* energy,specdiff,hfc,complexdomain,phase */
44 aubio_onsetdetection_type type_onset;
45 smpl_t threshold = 0.1;
46
47 smpl_t threshold2 = -80.;
48
49 int buffer_size = 1024;
50 int overlap_size = 512;
51 int channels = 1;
52 int samplerate = 44100;
53
54 /* global objects */
55 aubio_pvoc_t * pv;
56
57 fvec_t * ibuf;
58 fvec_t * obuf;
59 cvec_t * fftgrain;
60
61 fvec_t * woodblock;
62
63 aubio_onsetdetection_t *o;
64 fvec_t *onset;
65 //fvec_t *onset_keep;
66 //fvec_t *onset_proc;
67 int post = 5;
68 int pre  = 1;
69
70 int isonset = 0;
71
72 aubio_pickpeak_t * parms;
73
74 int aubio_process(float **input, float **output, int nframes) {
75         unsigned int i;         /*channels*/
76         unsigned int j;         /*frames*/   
77         unsigned int pos = 0;   /*frames%dspblocksize*/
78         //unsigned int ons = 0; /*onset counter*/
79         for (j=0;j<nframes;j++) {
80
81                 for (i=0;i<channels;i++) { 
82                         /* write input to datanew */
83                         fvec_write_sample(ibuf, input[i][j], i, pos);
84                         /* put synthnew in output */
85                         output[i][j] = fvec_read_sample(obuf, i, pos);
86                 }
87                 /*time for fft*/
88                 if (pos == overlap_size-1) {                    
89                         aubio_pvoc_do (pv,ibuf, fftgrain);
90                         aubio_onsetdetection(o,fftgrain, onset);
91                         fprintf(stderr,"onsetfunc: %f\n",onset->data[0][0]);
92                         isonset = aubio_peakpick_pimrt(onset,parms);
93                         if (isonset) {
94                                 /* test for silence */
95                                 if (aubio_silence_detection(ibuf, threshold2)==1)
96                                         isonset=0; 
97                                 else
98                                         for (pos = 0; pos < overlap_size; pos++)
99                                                 obuf->data[0][pos] = 
100                                                         woodblock->data[0][pos];
101                         } else {
102                                 for (pos = 0; pos < overlap_size; pos++)
103                                         obuf->data[0][pos] = 0.;
104                         }
105                         //aubio_pvoc_rdo(pv,fftgrain, obuf);
106                         pos = -1; /* so it will be zero next j loop */
107                 }
108                 pos++;
109         }
110         return 1;
111 }
112
113 int main (int argc, char **argv) {  
114         int frames;
115         //int pos;
116
117         aubio_file_t * file = NULL;
118         aubio_file_t * fileout = NULL;
119
120         aubio_file_t * onsetfile = new_file_ro(onset_filename);
121         parse_args(argc, argv);
122
123         if(!usejack) 
124         {
125                 debug("Opening  files ...\n");
126                 file = new_file_ro (input_filename);
127                 file_info(file);
128                 channels = aubio_file_channels(file);
129                 if (output_filename != NULL)
130                         fileout = new_file_wo(file, output_filename);
131         }
132
133         ibuf = new_fvec(overlap_size, channels);
134         obuf = new_fvec(overlap_size, channels);
135         woodblock = new_fvec(buffer_size,1);
136         fftgrain = new_cvec(buffer_size, channels);
137
138         file_read(onsetfile, overlap_size, woodblock);
139
140         /* phase vocoder */
141         debug("Phase voc init ... \n");
142         pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
143
144         /* onset init */
145         debug("type_onset: %d\n", (int)type_onset);
146         debug("threshold : %f\n", threshold);
147         parms = new_aubio_peakpicker(threshold);
148
149         o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
150         /*onset = new_fvec(overlap_size, channels);
151         for (frames=0;frames<overlap_size;frames++){
152                 onset->data[0][frames] = 0.; 
153         }*/
154         onset = new_fvec(0, channels);
155         
156         //onset_keep = new_fvec(post+pre+1, channels);
157         //onset_proc = new_fvec(post+pre+1, channels);
158
159         // command line argument parsing
160         if(usejack) {
161 #ifdef JACK_SUPPORT
162                 aubio_jack_t * jack_setup;
163                 debug("Jack init ...\n");
164                 jack_setup = new_aubio_jack(channels, channels, 
165                                 (aubio_process_func_t)aubio_process);
166
167                 /* initialise fftgrain */
168                 aubio_pvoc_do (pv,ibuf, fftgrain);
169
170                 debug("Jack activation ...\n");
171                 aubio_jack_activate(jack_setup);
172
173                 debug("Processing (Ctrl+C to quit) ...\n");
174                 pause();
175
176                 aubio_jack_close(jack_setup);
177 #endif
178
179         } else {
180                 fvec_t * inputbuf ;
181                 fvec_t * outputbuf;
182                 /* file objects */
183
184                 inputbuf        = new_fvec(overlap_size, channels);
185                 outputbuf       = new_fvec(overlap_size, channels);     
186                 /* phasevoc init */
187                 debug("Processing ...\n");
188
189                 /** \bug phasevoc not initialized yet */
190                 /* initialise fftgrain and input bug */
191                 for (frames=0;frames<2;frames++) {
192                         aubio_pvoc_do (pv,inputbuf, fftgrain);
193                 }
194
195                 /** start filling the phasevoc */
196                 //for (frames=0;frames<1;frames++) {
197                         //file_read(file, overlap_size, inputbuf);
198                         //aubio_pvoc_do (pv,inputbuf, fftgrain);
199                         /** \bug onset is still badly initialized */
200                         //aubio_onsetdetection(o,fftgrain, onset);
201                 //}
202
203                 //debug("coucou\n");
204                 frames = 0;
205                 while ((overlap_size == file_read(file, overlap_size, inputbuf)) ) 
206                 {
207                         isonset=0;
208                         aubio_process(inputbuf->data, outputbuf->data, overlap_size);
209                         if (isonset) {
210                                 /*output times in seconds*/
211                                 //errmsg("%f\n",(frames-1)*overlap_size/(float)samplerate);
212                                 errmsg("%f\n",(frames-3)*overlap_size/(float)samplerate);
213                         }
214                         if (output_filename != NULL)
215                                 file_write(fileout,overlap_size,outputbuf);
216                         frames++;
217                 }
218                 debug("Processed %d frames of %d samples.\n", frames, buffer_size);
219                 del_file(file);
220                 if (output_filename != NULL)
221                         del_file(fileout);
222         }
223
224         del_aubio_pvoc(pv);
225
226         del_fvec(obuf);
227         del_fvec(ibuf);
228         del_cvec(fftgrain);
229         //del_fvec(onset_keep);
230         //del_fvec(onset_proc);
231         del_fvec(onset);
232
233         debug("End of program.\n");
234         fflush(stderr);
235         return 0;
236 }
237
238