source: examples/utils.c @ d3c5282

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since d3c5282 was 7c6c806d, checked in by Amaury Hazan <mahmoudax@gmail.org>, 17 years ago

minor changes

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