rename file_ to aubio_sndfile, protect aubio_pitdetection_{mode,type} enumerators
[aubio.git] / examples / utils.c
1
2 #include "aubio.h"
3
4 #ifndef JACK_SUPPORT
5 #define JACK_SUPPORT 0
6 #endif
7
8 #include <getopt.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <math.h> /* for isfinite */
13 #include "utils.h"
14
15 /* not supported yet */
16 #ifdef LADCCA_SUPPORT
17 #include <ladcca/ladcca.h>
18 cca_client_t * aubio_cca_client;
19 #endif /* LADCCA_SUPPORT */
20
21 /* settings */
22 const char * output_filename = NULL;
23 const char * input_filename  = NULL;
24 const char * onset_filename  = "/usr/share/sounds/aubio/woodblock.aiff";
25 int frames = 0;
26 int verbose = 0;
27 int usejack = 0;
28 int usedoubled = 1;
29
30
31 /* energy,specdiff,hfc,complexdomain,phase */
32 aubio_onsetdetection_type type_onset  = kl;
33 aubio_onsetdetection_type type_onset2 = complexdomain;
34 smpl_t threshold                      = 0.3;
35 smpl_t threshold2                     = -90.;
36 uint_t buffer_size                    = 512; //1024;
37 uint_t overlap_size                   = 256; //512;
38 uint_t channels                       = 1;
39 uint_t samplerate                     = 44100;
40
41
42 aubio_sndfile_t * file = NULL;
43 aubio_sndfile_t * fileout = NULL;
44
45 aubio_pvoc_t * pv;
46 fvec_t * ibuf;
47 fvec_t * obuf;
48 cvec_t * fftgrain;
49 fvec_t * woodblock;
50 aubio_onsetdetection_t *o;
51 aubio_onsetdetection_t *o2;
52 fvec_t *onset;
53 fvec_t *onset2;
54 int isonset = 0;
55 aubio_pickpeak_t * parms;
56
57
58 /* pitch objects */
59 smpl_t pitch               = 0.;
60 aubio_pitchdetection_t * pitchdet;
61 aubio_pitchdetection_type mode = aubio_pitch_yin; // aubio_pitch_mcomb
62 uint_t median         = 6;
63
64 fvec_t * note_buffer  = NULL;
65 fvec_t * note_buffer2 = NULL;
66 smpl_t curlevel       = 0.;
67 smpl_t maxonset       = 0.;
68
69 /* midi objects */
70 aubio_midi_player_t * mplay; 
71 aubio_midi_driver_t * mdriver; 
72 aubio_midi_event_t  * event;
73
74 smpl_t curnote = 0.;
75 smpl_t newnote = 0.;
76 uint_t isready = 0;
77
78
79
80 /* badly redeclare some things */
81 aubio_onsetdetection_type type_onset;
82 smpl_t threshold;
83 smpl_t averaging;
84 const char * prog_name;
85
86 void usage (FILE * stream, int exit_code)
87 {
88         fprintf(stream, "usage: %s [ options ] \n", prog_name);
89         fprintf(stream, 
90                         "       -j      --jack          Use Jack.\n"
91                         "       -o      --output        Output type.\n"
92                         "       -i      --input         Input type.\n"
93                         "       -h      --help          Display this message.\n"
94                         "       -v      --verbose       Print verbose message.\n"
95                );
96         exit(exit_code);
97 }
98
99 int parse_args (int argc, char **argv) {
100         const char *options = "hvjo:i:O:t:s:H:a";
101         int next_option;
102         struct option long_options[] =
103         {
104                 {"help"     , 0, NULL, 'h'},
105                 {"verbose"  , 0, NULL, 'v'},
106                 {"jack"     , 0, NULL, 'j'},
107                 {"output"   , 0, NULL, 'o'},
108                 {"input"    , 0, NULL, 'i'},
109                 {"onset"    , 0, NULL, 'O'},
110                 {"threshold", 0, NULL, 't'},
111                 {"silence"  , 0, NULL, 's'},
112                 {"averaging", 0, NULL, 'a'},
113                 {"hopsize",   0, NULL, 'H'},
114                 {NULL       , 0, NULL, 0}
115         };
116         prog_name = argv[0];    
117         if( argc < 1 ) {
118                 usage (stderr, 1);
119                 return -1;
120         }
121         do {
122                 next_option = getopt_long (argc, argv, options, 
123                                 long_options, NULL);
124                 switch (next_option) {
125                         case 'o':
126                                 output_filename = optarg;
127                                 break;
128                         case 'i':
129                                 input_filename = optarg;
130                                 break;
131                         case 'h':       /* help */
132                                 usage (stdout, 0);
133                                 return -1;
134                         case 'v':               /* verbose */
135                                 verbose = 1;
136                                 break;
137                         case 'j':               /* verbose */
138                                 usejack = 1;
139                                 break;
140                         case 'O':   /*onset type*/
141                                 if (strcmp(optarg,"energy") == 0) 
142                                         type_onset = energy;
143                                 else if (strcmp(optarg,"specdiff") == 0) 
144                                         type_onset = specdiff;
145                                 else if (strcmp(optarg,"hfc") == 0) 
146                                         type_onset = hfc;
147                                 else if (strcmp(optarg,"complexdomain") == 0) 
148                                         type_onset = complexdomain;
149                                 else if (strcmp(optarg,"phase") == 0) 
150                                         type_onset = phase;
151                                 else {
152                                         debug("could not get onset type.\n");
153                                         abort();
154                                 }
155                                 usedoubled = 0;
156                                 break;
157                         case 's':   /* threshold value for onset */
158                                 threshold2 = (smpl_t)atof(optarg);
159                                 break;
160                         case 't':   /* threshold value for onset */
161                                 threshold = (smpl_t)atof(optarg);
162                                 /*
163                                    if (!isfinite(threshold)) {
164                                    debug("could not get threshold.\n");
165                                    abort();
166                                    }
167                                    */
168                                 break;
169                         case 'a':
170                                 averaging = 1;
171                                 break; 
172                         case 'H':
173                                 overlap_size = atoi(optarg);
174                                 break;
175                         case '?':       /* unknown options */
176                                 usage(stderr, 1);
177                                 break;
178                         case -1:                /* done with options */
179                                 break;
180                         default:                /*something else unexpected */
181                                 abort ();
182                 }
183         }
184         while (next_option != -1);
185
186         if (input_filename != NULL) {
187                 debug ("Input file : %s\n", input_filename );
188         } else if (input_filename != NULL && output_filename != NULL) {
189                 debug ("Input file : %s\n", input_filename );
190                 debug ("Output file : %s\n", output_filename );
191         } else {
192                 if (JACK_SUPPORT)
193                 {
194                         debug ("Jack input output\n");
195                         usejack = 1;
196                 } else {
197                         debug ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
198                         exit(1);
199                 }
200         }       
201         return 0;
202 }
203
204 void examples_common_init(int argc,char ** argv) {
205
206
207   aubio_sndfile_t * onsetfile;
208   /* parse command line arguments */
209   parse_args(argc, argv);
210
211   woodblock = new_fvec(buffer_size,1);
212   if (output_filename || usejack) {
213           (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
214                   (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
215                   (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
216           /* read the output sound once */
217           aubio_sndfile_read(onsetfile, overlap_size, woodblock);
218   }
219
220   if(!usejack)
221   {
222     debug("Opening files ...\n");
223     file = new_aubio_sndfile_ro (input_filename);
224     if (verbose) aubio_sndfile_info(file);
225     channels = aubio_sndfile_channels(file);
226     samplerate = aubio_sndfile_samplerate(file);
227     if (output_filename != NULL)
228       fileout = new_aubio_sndfile_wo(file, output_filename);
229   }
230
231   ibuf      = new_fvec(overlap_size, channels);
232   obuf      = new_fvec(overlap_size, channels);
233   fftgrain  = new_cvec(buffer_size, channels);
234
235   if (usepitch) {
236     pitchdet = new_aubio_pitchdetection(buffer_size*4, 
237                     overlap_size, channels, samplerate, mode, aubio_pitchm_freq);
238   
239   if (median) {
240           note_buffer = new_fvec(median, 1);
241           note_buffer2= new_fvec(median, 1);
242   }
243   }
244   /* phase vocoder */
245   pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
246   /* onsets */
247   parms = new_aubio_peakpicker(threshold);
248   o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
249   onset = new_fvec(1, channels);
250   if (usedoubled)    {
251     o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
252     onset2 = new_fvec(1 , channels);
253   }
254
255 }
256
257
258 void examples_common_del(void){
259   if (usepitch) {
260           send_noteon(curnote,0);
261           del_aubio_pitchdetection(pitchdet);
262           if (median) {
263                   del_fvec(note_buffer);
264                   del_fvec(note_buffer2);
265           }
266   }
267   del_aubio_pvoc(pv);
268   del_fvec(obuf);
269   del_fvec(ibuf);
270   del_cvec(fftgrain);
271   del_fvec(onset);
272 }
273
274 void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
275   if(usejack) {
276 #if JACK_SUPPORT
277     aubio_jack_t * jack_setup;
278     debug("Jack init ...\n");
279     jack_setup = new_aubio_jack(channels, channels,
280           (aubio_process_func_t)process_func);
281     if (usepitch) {
282             debug("Midi init ...\n");
283             mplay = new_aubio_midi_player();
284             mdriver = new_aubio_midi_driver("alsa_seq",
285                             (handle_midi_event_func_t)aubio_midi_send_event, mplay);
286             event = new_aubio_midi_event();
287     }
288     debug("Jack activation ...\n");
289     aubio_jack_activate(jack_setup);
290     debug("Processing (Ctrl+C to quit) ...\n");
291     pause();
292     aubio_jack_close(jack_setup);
293     if (usepitch) {
294             send_noteon(curnote,0);
295             del_aubio_midi_driver(mdriver);
296     }
297 #else
298     usage(stderr, 1);
299     outmsg("Compiled without jack output, exiting.\n");
300 #endif
301
302   } else {
303     /* phasevoc */
304     debug("Processing ...\n");
305
306     frames = 0;
307
308     while (overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
309     {
310       isonset=0;
311       process_func(ibuf->data, obuf->data, overlap_size);
312       print(); 
313       if (output_filename != NULL) {
314         aubio_sndfile_write(fileout,overlap_size,obuf);
315       }
316       frames++;
317     }
318
319     debug("Processed %d frames of %d samples.\n", frames, buffer_size);
320     del_aubio_sndfile(file);
321
322     if (output_filename != NULL)
323       del_aubio_sndfile(fileout);
324
325   }
326 }
327
328
329
330 void send_noteon(int pitch, int velo)
331 {
332     smpl_t mpitch = (FLOOR)(aubio_freqtomidi(pitch)+.5);
333     /* we should check if we use midi here, not jack */
334 #if ALSA_SUPPORT
335     if (usejack) {
336         if (velo==0) {
337             aubio_midi_event_set_type(event,NOTE_OFF);
338         } else {
339             aubio_midi_event_set_type(event,NOTE_ON);
340         }
341         aubio_midi_event_set_channel(event,0);
342         aubio_midi_event_set_pitch(event,mpitch);
343         aubio_midi_event_set_velocity(event,velo);
344         aubio_midi_direct_output(mdriver,event);
345     } else 
346 #endif
347     if (!verbose)
348     {
349         if (velo==0) {
350             outmsg("%f\n",frames*overlap_size/(float)samplerate);
351         } else {
352             outmsg("%f\t%f\t", mpitch,
353                         frames*overlap_size/(float)samplerate);
354         }
355     }
356 }
357
358
359 void note_append(fvec_t * note_buffer, smpl_t curnote) {
360   uint_t i = 0;
361   for (i = 0; i < note_buffer->length - 1; i++) { 
362       note_buffer->data[0][i] = note_buffer->data[0][i+1];
363   }
364   note_buffer->data[0][note_buffer->length - 1] = curnote;
365   return;
366 }
367
368 uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
369   uint_t i = 0;
370   for (i = 0; i < note_buffer->length; i++) { 
371       note_buffer2->data[0][i] = note_buffer->data[0][i];
372   }
373   return vec_median(note_buffer2);
374 }
375