aubiomfcc.c: added a dump filterbank funcion
[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 #ifdef LASH_SUPPORT
16 #include <lash/lash.h>
17 #include <pthread.h>
18 lash_client_t * aubio_lash_client;
19 lash_args_t * lash_args;
20 void * lash_thread_main (void * data);
21 int lash_main (void);
22 void save_data (void);
23 void restore_data(lash_config_t * lash_config);
24 pthread_t lash_thread;
25 #endif /* LASH_SUPPORT */
26
27 /* settings */
28 const char * output_filename = NULL;
29 const char * input_filename  = NULL;
30 const char * onset_filename  = AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff";
31 int frames = 0;
32 int verbose = 0;
33 int usejack = 0;
34 int usedoubled = 1;
35
36
37 /* energy,specdiff,hfc,complexdomain,phase */
38 aubio_onsetdetection_type type_onset  = aubio_onset_kl;
39 aubio_onsetdetection_type type_onset2 = aubio_onset_complex;
40 smpl_t threshold                      = 0.3;
41 smpl_t silence                        = -90.;
42 // uint_t buffer_size                    = 512;
43 // uint_t overlap_size                   = 256;
44 uint_t buffer_size                    = 1024;
45 uint_t overlap_size                   = 512;
46 uint_t channels                       = 1;
47 uint_t samplerate                     = 44100;
48
49
50 aubio_sndfile_t * file = NULL;
51 aubio_sndfile_t * fileout = NULL;
52
53 aubio_pvoc_t * pv;
54 fvec_t * ibuf;
55 fvec_t * obuf;
56 cvec_t * fftgrain;
57 fvec_t * woodblock;
58 aubio_onsetdetection_t *o;
59 aubio_onsetdetection_t *o2;
60 fvec_t *onset;
61 fvec_t *onset2;
62 int isonset = 0;
63 aubio_pickpeak_t * parms;
64
65 /* pitch objects */
66 smpl_t pitch               = 0.;
67 aubio_pitchdetection_t * pitchdet;
68 aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; // aubio_pitch_mcomb
69 aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
70 uint_t median         = 6;
71
72 fvec_t * note_buffer  = NULL;
73 fvec_t * note_buffer2 = NULL;
74 smpl_t curlevel       = 0.;
75 smpl_t maxonset       = 0.;
76
77 /* midi objects */
78 aubio_midi_player_t * mplay; 
79 aubio_midi_driver_t * mdriver; 
80 aubio_midi_event_t  * event;
81
82 smpl_t curnote = 0.;
83 smpl_t newnote = 0.;
84 uint_t isready = 0;
85
86
87
88 /* badly redeclare some things */
89 aubio_onsetdetection_type type_onset;
90 smpl_t threshold;
91 smpl_t averaging;
92 const char * prog_name;
93
94 void usage (FILE * stream, int exit_code)
95 {
96         fprintf(stream, "usage: %s [ options ] \n", prog_name);
97         fprintf(stream, 
98                         "       -h      --help          Display this message.\n"
99                         "       -v      --verbose       Be verbose.\n"
100                         "       -j      --jack          Use Jack.\n"
101                         "       -o      --output        Output type.\n"
102                         "       -i      --input         Input type.\n"
103                         "       -O      --onset         Select onset detection algorithm.\n"
104                         "       -t      --threshold     Set onset detection threshold.\n"
105                         "       -s      --silence       Select silence threshold.\n"
106                         "       -p      --pitch         Select pitch detection algorithm.\n"
107                         "       -H      --hopsize       Set hopsize.\n"
108                         "       -a      --averaging     Use averaging.\n"
109                );
110         exit(exit_code);
111 }
112
113 int parse_args (int argc, char **argv) {
114         const char *options = "hvjo:i:O:t:s:p:H:a";
115         int next_option;
116         struct option long_options[] =
117         {
118                 {"help"     , 0, NULL, 'h'},
119                 {"verbose"  , 0, NULL, 'v'},
120                 {"jack"     , 0, NULL, 'j'},
121                 {"output"   , 1, NULL, 'o'},
122                 {"input"    , 1, NULL, 'i'},
123                 {"onset"    , 1, NULL, 'O'},
124                 {"threshold", 1, NULL, 't'},
125                 {"silence"  , 1, NULL, 's'},
126                 {"pitch"    , 1, NULL, 'p'},
127                 {"averaging", 0, NULL, 'a'},
128                 {"hopsize",   1, NULL, 'H'},
129                 {NULL       , 0, NULL, 0}
130         };
131 #ifdef LASH_SUPPORT
132         lash_args = lash_extract_args(&argc, &argv);
133 #endif /* LASH_SUPPORT */
134         prog_name = argv[0];
135         if( argc < 1 ) {
136                 usage (stderr, 1);
137                 return -1;
138         }
139         do {
140                 next_option = getopt_long (argc, argv, options, 
141                                 long_options, NULL);
142                 switch (next_option) {
143                         case 'o':
144                                 output_filename = optarg;
145                                 break;
146                         case 'i':
147                                 input_filename = optarg;
148                                 break;
149                         case 'h': /* help */
150                                 usage (stdout, 0);
151                                 return -1;
152                         case 'v': /* verbose */
153                                 verbose = 1;
154                                 break;
155                         case 'j':
156                                 usejack = 1;
157                                 break;
158                         case 'O':   /*onset type*/
159                                 if (strcmp(optarg,"energy") == 0) 
160                                         type_onset = aubio_onset_energy;
161                                 else if (strcmp(optarg,"specdiff") == 0) 
162                                         type_onset = aubio_onset_specdiff;
163                                 else if (strcmp(optarg,"hfc") == 0) 
164                                         type_onset = aubio_onset_hfc;
165                                 else if (strcmp(optarg,"complexdomain") == 0) 
166                                         type_onset = aubio_onset_complex;
167                                 else if (strcmp(optarg,"complex") == 0) 
168                                         type_onset = aubio_onset_complex;
169                                 else if (strcmp(optarg,"phase") == 0) 
170                                         type_onset = aubio_onset_phase;
171                                 else if (strcmp(optarg,"mkl") == 0) 
172                                         type_onset = aubio_onset_mkl;
173                                 else if (strcmp(optarg,"kl") == 0) 
174                                         type_onset = aubio_onset_kl;
175                                 else {
176                                         errmsg("unknown onset type.\n");
177                                         abort();
178                                 }
179                                 usedoubled = 0;
180                                 break;
181                         case 's':   /* threshold value for onset */
182                                 silence = (smpl_t)atof(optarg);
183                                 break;
184                         case 't':   /* threshold value for onset */
185                                 threshold = (smpl_t)atof(optarg);
186                                 /*
187                                    if (!isfinite(threshold)) {
188                                    debug("could not get threshold.\n");
189                                    abort();
190                                    }
191                                    */
192                                 break;
193                         case 'p':
194                                 if (strcmp(optarg,"mcomb") == 0) 
195                                         type_pitch = aubio_pitch_mcomb;
196                                 else if (strcmp(optarg,"yinfft") == 0) 
197                                         type_pitch = aubio_pitch_yin;
198                                 else if (strcmp(optarg,"yin") == 0) 
199                                         type_pitch = aubio_pitch_yin;
200                                 else if (strcmp(optarg,"schmitt") == 0) 
201                                         type_pitch = aubio_pitch_schmitt;
202                                 else if (strcmp(optarg,"fcomb") == 0) 
203                                         type_pitch = aubio_pitch_fcomb;
204                                 else {
205                                         errmsg("unknown pitch type.\n");
206                                         abort();
207                                 }
208                                 break;
209                         case 'a':
210                                 averaging = 1;
211                                 break; 
212                         case 'H':
213                                 overlap_size = atoi(optarg);
214                                 break;
215                         case '?': /* unknown options */
216                                 usage(stderr, 1);
217                                 break;
218                         case -1: /* done with options */
219                                 break;
220                         default: /*something else unexpected */
221                                 abort ();
222                 }
223         }
224         while (next_option != -1);
225
226         if (input_filename != NULL) {
227                 debug ("Input file : %s\n", input_filename );
228         } else if (input_filename != NULL && output_filename != NULL) {
229                 debug ("Input file : %s\n", input_filename );
230                 debug ("Output file : %s\n", output_filename );
231         } else {
232                 if (JACK_SUPPORT)
233                 {
234                         debug ("Jack input output\n");
235                         usejack = 1;
236                 } else {
237                         debug ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
238                         exit(1);
239                 }
240         }
241
242         return 0;
243 }
244
245 void examples_common_init(int argc,char ** argv) {
246
247
248   aubio_sndfile_t * onsetfile = NULL;
249   /* parse command line arguments */
250   parse_args(argc, argv);
251
252   woodblock = new_fvec(buffer_size,1);
253   if (output_filename || usejack) {
254           /* dummy assignement to keep egcs happy */
255           isonset = (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
256                   (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
257                   (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
258           if (onsetfile == NULL) {
259             outmsg("Could not find woodblock.aiff\n");
260             exit(1);
261           }
262   }
263   if (onsetfile) {
264           /* read the output sound once */
265           aubio_sndfile_read(onsetfile, overlap_size, woodblock);
266   }
267
268   if(!usejack)
269   {
270     debug("Opening files ...\n");
271     file = new_aubio_sndfile_ro (input_filename);
272     if (file == NULL) {
273       outmsg("Could not open input file %s.\n", input_filename);
274       exit(1);
275     }
276     if (verbose) aubio_sndfile_info(file);
277     channels = aubio_sndfile_channels(file);
278     samplerate = aubio_sndfile_samplerate(file);
279     if (output_filename != NULL)
280       fileout = new_aubio_sndfile_wo(file, output_filename);
281   }
282 #ifdef LASH_SUPPORT
283   else {
284     aubio_lash_client = lash_init(lash_args, argv[0],
285         LASH_Config_Data_Set | LASH_Terminal,
286         LASH_PROTOCOL(2, 0));
287     if (!aubio_lash_client) {
288       fprintf(stderr, "%s: could not initialise lash\n", __FUNCTION__);
289     }
290     /* tell the lash server our client id */
291     if (lash_enabled(aubio_lash_client)) {
292       lash_event_t * event = (lash_event_t *)lash_event_new_with_type(LASH_Client_Name);
293       lash_event_set_string(event, "aubio");
294       lash_send_event(aubio_lash_client, event);
295       pthread_create(&lash_thread, NULL, lash_thread_main, NULL);
296     }
297   }
298 #endif /* LASH_SUPPORT */
299
300   ibuf      = new_fvec(overlap_size, channels);
301   obuf      = new_fvec(overlap_size, channels);
302   fftgrain  = new_cvec(buffer_size, channels);
303
304   
305   if (usepitch) {
306     pitchdet = new_aubio_pitchdetection(buffer_size*4, 
307         overlap_size, channels, samplerate, type_pitch, mode_pitch);
308     aubio_pitchdetection_set_yinthresh(pitchdet, 0.7);
309
310     if (median) {
311       note_buffer = new_fvec(median, 1);
312       note_buffer2= new_fvec(median, 1);
313     }
314   }
315   /* phase vocoder */
316   pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
317   
318   /* onsets */
319   parms = new_aubio_peakpicker(threshold);
320   o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
321   onset = new_fvec(1, channels);
322   if (usedoubled)    {
323     o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
324     onset2 = new_fvec(1 , channels);
325   }
326
327 }
328
329
330 void examples_common_del(void){
331   if (usepitch) {
332           send_noteon(curnote,0);
333           del_aubio_pitchdetection(pitchdet);
334           if (median) {
335                   del_fvec(note_buffer);
336                   del_fvec(note_buffer2);
337           }
338   }
339   if (usedoubled)    {
340     del_aubio_onsetdetection(o2);
341     del_fvec(onset2);
342   }
343   del_aubio_onsetdetection(o);
344   del_aubio_peakpicker(parms);
345   del_aubio_pvoc(pv);
346   del_fvec(obuf);
347   del_fvec(ibuf);
348   del_cvec(fftgrain);
349   del_fvec(onset);
350   del_fvec(woodblock);
351   
352   aubio_cleanup();
353 }
354
355 void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
356   if(usejack) {
357 #if JACK_SUPPORT
358     aubio_jack_t * jack_setup;
359     debug("Jack init ...\n");
360     jack_setup = new_aubio_jack(channels, channels,
361           (aubio_process_func_t)process_func);
362     if (usepitch) {
363             debug("Midi init ...\n");
364             mplay = new_aubio_midi_player();
365             mdriver = new_aubio_midi_driver("alsa_seq",
366                             (handle_midi_event_func_t)aubio_midi_send_event, mplay);
367             event = new_aubio_midi_event();
368     }
369     debug("Jack activation ...\n");
370     aubio_jack_activate(jack_setup);
371     debug("Processing (Ctrl+C to quit) ...\n");
372     pause();
373     aubio_jack_close(jack_setup);
374     if (usepitch) {
375             send_noteon(curnote,0);
376             del_aubio_midi_driver(mdriver);
377     }
378 #else
379     usage(stderr, 1);
380     outmsg("Compiled without jack output, exiting.\n");
381 #endif
382
383   } else {
384     /* phasevoc */
385     debug("Processing ...\n");
386
387     frames = 0;
388
389     while ((signed)overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
390     {
391       isonset=0;
392       process_func(ibuf->data, obuf->data, overlap_size);
393       print(); 
394       if (output_filename != NULL) {
395         aubio_sndfile_write(fileout,overlap_size,obuf);
396       }
397       frames++;
398     }
399
400     debug("Processed %d frames of %d samples.\n", frames, buffer_size);
401     del_aubio_sndfile(file);
402
403     if (output_filename != NULL)
404       del_aubio_sndfile(fileout);
405
406   }
407 }
408
409
410
411 void send_noteon(int pitch, int velo)
412 {
413     smpl_t mpitch = floor(aubio_freqtomidi(pitch)+.5);
414     /* we should check if we use midi here, not jack */
415 #if ALSA_SUPPORT
416     if (usejack) {
417         if (velo==0) {
418             aubio_midi_event_set_type(event,NOTE_OFF);
419         } else {
420             aubio_midi_event_set_type(event,NOTE_ON);
421         }
422         aubio_midi_event_set_channel(event,0);
423         aubio_midi_event_set_pitch(event,mpitch);
424         aubio_midi_event_set_velocity(event,velo);
425         aubio_midi_direct_output(mdriver,event);
426     } else 
427 #endif
428     if (!verbose)
429     {
430         if (velo==0) {
431             outmsg("%f\n",frames*overlap_size/(float)samplerate);
432         } else {
433             outmsg("%f\t%f\t", mpitch,
434                         frames*overlap_size/(float)samplerate);
435         }
436     }
437 }
438
439
440 void note_append(fvec_t * note_buffer, smpl_t curnote) {
441   uint_t i = 0;
442   for (i = 0; i < note_buffer->length - 1; i++) { 
443       note_buffer->data[0][i] = note_buffer->data[0][i+1];
444   }
445   note_buffer->data[0][note_buffer->length - 1] = curnote;
446   return;
447 }
448
449 uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
450   uint_t i = 0;
451   for (i = 0; i < note_buffer->length; i++) { 
452       note_buffer2->data[0][i] = note_buffer->data[0][i];
453   }
454   return vec_median(note_buffer2);
455 }
456
457 #if LASH_SUPPORT
458
459 void * lash_thread_main(void *data __attribute__((unused)))
460 {
461         printf("LASH thread running\n");
462
463         while (!lash_main())
464                 usleep(1000);
465
466         printf("LASH thread finished\n");
467         return NULL;
468 }
469
470 int lash_main(void) {
471         lash_event_t *lash_event;
472         lash_config_t *lash_config;
473
474         while ((lash_event = lash_get_event(aubio_lash_client))) {
475                 switch (lash_event_get_type(lash_event)) {
476                 case LASH_Quit:
477                         lash_event_destroy(lash_event);
478       exit(1);
479       return 1;
480                 case LASH_Restore_Data_Set:
481                         lash_send_event(aubio_lash_client, lash_event);
482                         break;
483                 case LASH_Save_Data_Set:
484                         save_data();
485                         lash_send_event(aubio_lash_client, lash_event);
486                         break;
487                 case LASH_Server_Lost:
488                         return 1;
489                 default:
490                         printf("%s: received unknown LASH event of type %d",
491                                    __FUNCTION__, lash_event_get_type(lash_event));
492                         lash_event_destroy(lash_event);
493       break;
494                 }
495         }
496
497         while ((lash_config = lash_get_config(aubio_lash_client))) {
498                 restore_data(lash_config);
499                 lash_config_destroy(lash_config);
500         }
501
502         return 0;
503 }
504
505 void save_data() {
506         lash_config_t *lash_config;
507
508         lash_config = lash_config_new_with_key("threshold");
509         lash_config_set_value_double(lash_config, threshold);
510         lash_send_config(aubio_lash_client, lash_config);
511
512 }
513
514 void restore_data(lash_config_t * lash_config) {
515         const char *lash_key;
516
517         lash_key = lash_config_get_key(lash_config);
518
519         if (strcmp(lash_key, "threshold") == 0) {
520                 threshold = lash_config_get_value_double(lash_config);
521                 return;
522         }
523
524 }
525
526 #endif /* LASH_SUPPORT */
527