source: examples/utils.c @ 15b97d9

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

utils.c: avoid signed/unsigned comparison

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