source: examples/utils.c @ 862d78f

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

utils.c, utils.py: add specflux onset function

  • Property mode set to 100644
File size: 17.2 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 if (strcmp(optarg,"specflux") == 0) 
177                                        type_onset = aubio_onset_specflux;
178                                else {
179                                        errmsg("unknown onset type.\n");
180                                        abort();
181                                }
182                                usedoubled = 0;
183                                break;
184                        case 's':   /* threshold value for onset */
185                                silence = (smpl_t)atof(optarg);
186                                break;
187                        case 't':   /* threshold value for onset */
188                                threshold = (smpl_t)atof(optarg);
189                                /*
190                                   if (!isfinite(threshold)) {
191                                   debug("could not get threshold.\n");
192                                   abort();
193                                   }
194                                   */
195                                break;
196                        case 'p':
197                                if (strcmp(optarg,"mcomb") == 0) 
198                                        type_pitch = aubio_pitch_mcomb;
199                                else if (strcmp(optarg,"yinfft") == 0) 
200                                        type_pitch = aubio_pitch_yin;
201                                else if (strcmp(optarg,"yin") == 0) 
202                                        type_pitch = aubio_pitch_yin;
203                                else if (strcmp(optarg,"schmitt") == 0) 
204                                        type_pitch = aubio_pitch_schmitt;
205                                else if (strcmp(optarg,"fcomb") == 0) 
206                                        type_pitch = aubio_pitch_fcomb;
207                                else {
208                                        errmsg("unknown pitch type.\n");
209                                        abort();
210                                }
211                                break;
212                        case 'a':
213                                averaging = 1;
214                                break; 
215                        case 'H':
216                                overlap_size = atoi(optarg);
217                                break;
218                        case '?': /* unknown options */
219                                usage(stderr, 1);
220                                break;
221                        case -1: /* done with options */
222                                break;
223                        default: /*something else unexpected */
224                                abort ();
225                }
226        }
227        while (next_option != -1);
228
229        if (input_filename != NULL) {
230                debug ("Input file : %s\n", input_filename );
231        } else if (input_filename != NULL && output_filename != NULL) {
232                debug ("Input file : %s\n", input_filename );
233                debug ("Output file : %s\n", output_filename );
234        } else {
235                if (JACK_SUPPORT)
236                {
237                        debug ("Jack input output\n");
238                        usejack = 1;
239                } else {
240                        debug ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
241                        exit(1);
242                }
243        }
244
245        return 0;
246}
247
248void examples_common_init(int argc,char ** argv) {
249
250
251  aubio_sndfile_t * onsetfile = NULL;
252  /* parse command line arguments */
253  parse_args(argc, argv);
254
255  woodblock = new_fvec(buffer_size,1);
256  if (output_filename || usejack) {
257          /* dummy assignement to keep egcs happy */
258          isonset = (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
259                  (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
260                  (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
261          if (onsetfile == NULL) {
262            outmsg("Could not find woodblock.aiff\n");
263            exit(1);
264          }
265  }
266  if (onsetfile) {
267          /* read the output sound once */
268          aubio_sndfile_read(onsetfile, overlap_size, woodblock);
269  }
270
271  if(!usejack)
272  {
273    debug("Opening files ...\n");
274    file = new_aubio_sndfile_ro (input_filename);
275    if (file == NULL) {
276      outmsg("Could not open input file %s.\n", input_filename);
277      exit(1);
278    }
279    if (verbose) aubio_sndfile_info(file);
280    channels = aubio_sndfile_channels(file);
281    samplerate = aubio_sndfile_samplerate(file);
282    if (output_filename != NULL)
283      fileout = new_aubio_sndfile_wo(file, output_filename);
284  }
285#ifdef LASH_SUPPORT
286  else {
287    aubio_lash_client = lash_init(lash_args, argv[0],
288        LASH_Config_Data_Set | LASH_Terminal,
289        LASH_PROTOCOL(2, 0));
290    if (!aubio_lash_client) {
291      fprintf(stderr, "%s: could not initialise lash\n", __FUNCTION__);
292    }
293    /* tell the lash server our client id */
294    if (lash_enabled(aubio_lash_client)) {
295      lash_event_t * event = (lash_event_t *)lash_event_new_with_type(LASH_Client_Name);
296      lash_event_set_string(event, "aubio");
297      lash_send_event(aubio_lash_client, event);
298      pthread_create(&lash_thread, NULL, lash_thread_main, NULL);
299    }
300  }
301#endif /* LASH_SUPPORT */
302
303  ibuf      = new_fvec(overlap_size, channels);
304  obuf      = new_fvec(overlap_size, channels);
305  fftgrain  = new_cvec(buffer_size, channels);
306
307  if (usepitch) {
308    pitchdet = new_aubio_pitchdetection(buffer_size*4, 
309        overlap_size, channels, samplerate, type_pitch, mode_pitch);
310    aubio_pitchdetection_set_yinthresh(pitchdet, 0.7);
311
312    if (median) {
313      note_buffer = new_fvec(median, 1);
314      note_buffer2= new_fvec(median, 1);
315    }
316  }
317  /* phase vocoder */
318  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
319  /* onsets */
320  parms = new_aubio_peakpicker(threshold);
321  o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
322  onset = new_fvec(1, channels);
323  if (usedoubled)    {
324    o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
325    onset2 = new_fvec(1 , channels);
326  }
327
328}
329
330
331void examples_common_del(void){
332  if (usepitch) {
333          send_noteon(curnote,0);
334          del_aubio_pitchdetection(pitchdet);
335          if (median) {
336                  del_fvec(note_buffer);
337                  del_fvec(note_buffer2);
338          }
339  }
340  if (usedoubled)    {
341    del_aubio_onsetdetection(o2);
342    del_fvec(onset2);
343  }
344  del_aubio_onsetdetection(o);
345  del_aubio_peakpicker(parms);
346  del_aubio_pvoc(pv);
347  del_fvec(obuf);
348  del_fvec(ibuf);
349  del_cvec(fftgrain);
350  del_fvec(onset);
351  del_fvec(woodblock);
352  aubio_cleanup();
353}
354
355void 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
402    flush_process(process_func, print);
403    del_aubio_sndfile(file);
404
405    if (output_filename != NULL)
406      del_aubio_sndfile(fileout);
407
408  }
409}
410
411void flush_process(aubio_process_func_t process_func, aubio_print_func_t print){
412  uint i,j;
413  for (i = 0; i < channels; i++) {
414    for (j = 0; j < obuf->length; j++) {
415      fvec_write_sample(obuf,0.,i,j);
416    }
417  }
418  for (i = 0; (signed)i < frames_delay; i++) {
419    process_func(ibuf->data, obuf->data, overlap_size);
420    print(); 
421  }
422}
423
424
425void send_noteon(int pitch, int velo)
426{
427    smpl_t mpitch = floor(aubio_freqtomidi(pitch)+.5);
428    /* we should check if we use midi here, not jack */
429#if ALSA_SUPPORT
430    if (usejack) {
431        if (velo==0) {
432            aubio_midi_event_set_type(event,NOTE_OFF);
433        } else {
434            aubio_midi_event_set_type(event,NOTE_ON);
435        }
436        aubio_midi_event_set_channel(event,0);
437        aubio_midi_event_set_pitch(event,mpitch);
438        aubio_midi_event_set_velocity(event,velo);
439        aubio_midi_direct_output(mdriver,event);
440    } else 
441#endif
442    if (!verbose)
443    {
444        if (velo==0) {
445            outmsg("%f\n",frames*overlap_size/(float)samplerate);
446        } else {
447            outmsg("%f\t%f\t", mpitch,
448                        frames*overlap_size/(float)samplerate);
449        }
450    }
451}
452
453
454void note_append(fvec_t * note_buffer, smpl_t curnote) {
455  uint_t i = 0;
456  for (i = 0; i < note_buffer->length - 1; i++) { 
457      note_buffer->data[0][i] = note_buffer->data[0][i+1];
458  }
459  note_buffer->data[0][note_buffer->length - 1] = curnote;
460  return;
461}
462
463uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
464  uint_t i = 0;
465  for (i = 0; i < note_buffer->length; i++) { 
466      note_buffer2->data[0][i] = note_buffer->data[0][i];
467  }
468  return vec_median(note_buffer2);
469}
470
471#if LASH_SUPPORT
472
473void * lash_thread_main(void *data __attribute__((unused)))
474{
475        printf("LASH thread running\n");
476
477        while (!lash_main())
478                usleep(1000);
479
480        printf("LASH thread finished\n");
481        return NULL;
482}
483
484int lash_main(void) {
485        lash_event_t *lash_event;
486        lash_config_t *lash_config;
487
488        while ((lash_event = lash_get_event(aubio_lash_client))) {
489                switch (lash_event_get_type(lash_event)) {
490                case LASH_Quit:
491                        lash_event_destroy(lash_event);
492      exit(1);
493      return 1;
494                case LASH_Restore_Data_Set:
495                        lash_send_event(aubio_lash_client, lash_event);
496                        break;
497                case LASH_Save_Data_Set:
498                        save_data();
499                        lash_send_event(aubio_lash_client, lash_event);
500                        break;
501                case LASH_Server_Lost:
502                        return 1;
503                default:
504                        printf("%s: received unknown LASH event of type %d",
505                                   __FUNCTION__, lash_event_get_type(lash_event));
506                        lash_event_destroy(lash_event);
507      break;
508                }
509        }
510
511        while ((lash_config = lash_get_config(aubio_lash_client))) {
512                restore_data(lash_config);
513                lash_config_destroy(lash_config);
514        }
515
516        return 0;
517}
518
519void save_data() {
520        lash_config_t *lash_config;
521
522        lash_config = lash_config_new_with_key("threshold");
523        lash_config_set_value_double(lash_config, threshold);
524        lash_send_config(aubio_lash_client, lash_config);
525
526}
527
528void restore_data(lash_config_t * lash_config) {
529        const char *lash_key;
530
531        lash_key = lash_config_get_key(lash_config);
532
533        if (strcmp(lash_key, "threshold") == 0) {
534                threshold = lash_config_get_value_double(lash_config);
535                return;
536        }
537
538}
539
540#endif /* LASH_SUPPORT */
541
Note: See TracBrowser for help on using the repository browser.