source: examples/utils.c @ 75a0f40

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 75a0f40 was 75a0f40, checked in by Paul Brossier <piem@piem.org>, 15 years ago

examples/utils.c: add --bufsize option and short message when option parsing went wrong (should never happen)

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