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
RevLine 
[96fb8ad]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
[71482a9]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);
[55c6da4]23void restore_data(lash_config_t * lash_config);
[71482a9]24pthread_t lash_thread;
25#endif /* LASH_SUPPORT */
[96fb8ad]26
[bd2f2ab]27/* settings */
28const char * output_filename = NULL;
29const char * input_filename  = NULL;
[6b233fc]30const char * onset_filename  = AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff";
[bd2f2ab]31int frames = 0;
32int verbose = 0;
33int usejack = 0;
34int usedoubled = 1;
35
36
37/* energy,specdiff,hfc,complexdomain,phase */
[5cf415f]38aubio_onsetdetection_type type_onset  = aubio_onset_kl;
39aubio_onsetdetection_type type_onset2 = aubio_onset_complex;
[bd2f2ab]40smpl_t threshold                      = 0.3;
[660cad22]41smpl_t silence                        = -90.;
[2d7b65a]42uint_t buffer_size                    = 512; //1024;
43uint_t overlap_size                   = 256; //512;
[bd2f2ab]44uint_t channels                       = 1;
45uint_t samplerate                     = 44100;
46
47
[5e9c68a]48aubio_sndfile_t * file = NULL;
49aubio_sndfile_t * fileout = NULL;
[bd2f2ab]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
[7c6c806d]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
[bd2f2ab]77
78/* pitch objects */
79smpl_t pitch               = 0.;
80aubio_pitchdetection_t * pitchdet;
[4c67dc8]81aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; // aubio_pitch_mcomb
[fb615eb]82aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
[bd2f2ab]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
[96fb8ad]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{
[ee0cc27b]109        fprintf(stream, "usage: %s [ options ] \n", prog_name);
110        fprintf(stream, 
[fb615eb]111                        "       -h      --help          Display this message.\n"
[3a323d0]112                        "       -v      --verbose       Be verbose.\n"
[fb615eb]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"
[ee0cc27b]122               );
123        exit(exit_code);
[96fb8ad]124}
125
126int parse_args (int argc, char **argv) {
[fb615eb]127        const char *options = "hvjo:i:O:t:s:p:H:a";
[ee0cc27b]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'},
[13d57e8]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'},
[fb615eb]139                {"pitch"    , 1, NULL, 'p'},
[ee0cc27b]140                {"averaging", 0, NULL, 'a'},
[13d57e8]141                {"hopsize",   1, NULL, 'H'},
[ee0cc27b]142                {NULL       , 0, NULL, 0}
143        };
[71482a9]144#ifdef LASH_SUPPORT
145        lash_args = lash_extract_args(&argc, &argv);
146#endif /* LASH_SUPPORT */
[fb615eb]147        prog_name = argv[0];
[ee0cc27b]148        if( argc < 1 ) {
149                usage (stderr, 1);
150                return -1;
[96fb8ad]151        }
[ee0cc27b]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;
[fb615eb]162                        case 'h': /* help */
[ee0cc27b]163                                usage (stdout, 0);
164                                return -1;
[fb615eb]165                        case 'v': /* verbose */
[ee0cc27b]166                                verbose = 1;
167                                break;
[fb615eb]168                        case 'j':
[ee0cc27b]169                                usejack = 1;
170                                break;
171                        case 'O':   /*onset type*/
172                                if (strcmp(optarg,"energy") == 0) 
[5cf415f]173                                        type_onset = aubio_onset_energy;
[ee0cc27b]174                                else if (strcmp(optarg,"specdiff") == 0) 
[5cf415f]175                                        type_onset = aubio_onset_specdiff;
[ee0cc27b]176                                else if (strcmp(optarg,"hfc") == 0) 
[5cf415f]177                                        type_onset = aubio_onset_hfc;
[ee0cc27b]178                                else if (strcmp(optarg,"complexdomain") == 0) 
[5cf415f]179                                        type_onset = aubio_onset_complex;
180                                else if (strcmp(optarg,"complex") == 0) 
181                                        type_onset = aubio_onset_complex;
[ee0cc27b]182                                else if (strcmp(optarg,"phase") == 0) 
[5cf415f]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;
[ee0cc27b]188                                else {
[92e4028]189                                        errmsg("unknown onset type.\n");
[ee0cc27b]190                                        abort();
191                                }
192                                usedoubled = 0;
193                                break;
194                        case 's':   /* threshold value for onset */
[660cad22]195                                silence = (smpl_t)atof(optarg);
[ee0cc27b]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;
[fb615eb]206                        case 'p':
207                                if (strcmp(optarg,"mcomb") == 0) 
208                                        type_pitch = aubio_pitch_mcomb;
[4c67dc8]209                                else if (strcmp(optarg,"yinfft") == 0) 
210                                        type_pitch = aubio_pitch_yin;
[fb615eb]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 {
[92e4028]218                                        errmsg("unknown pitch type.\n");
[fb615eb]219                                        abort();
220                                }
221                                break;
[ee0cc27b]222                        case 'a':
223                                averaging = 1;
224                                break; 
[0ce9acc3]225                        case 'H':
226                                overlap_size = atoi(optarg);
227                                break;
[fb615eb]228                        case '?': /* unknown options */
[ee0cc27b]229                                usage(stderr, 1);
230                                break;
[fb615eb]231                        case -1: /* done with options */
[ee0cc27b]232                                break;
[fb615eb]233                        default: /*something else unexpected */
[ee0cc27b]234                                abort ();
235                }
[96fb8ad]236        }
[ee0cc27b]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                }
[fb615eb]253        }
[71482a9]254
[ee0cc27b]255        return 0;
[96fb8ad]256}
257
[bd2f2ab]258void examples_common_init(int argc,char ** argv) {
259
260
[b1f723d]261  aubio_sndfile_t * onsetfile = NULL;
[bd2f2ab]262  /* parse command line arguments */
263  parse_args(argc, argv);
264
[f382ac6]265  woodblock = new_fvec(buffer_size,1);
266  if (output_filename || usejack) {
[6b233fc]267          /* dummy assignement to keep egcs happy */
268          isonset = (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
[5e9c68a]269                  (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
270                  (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
[9af07aa]271          if (onsetfile == NULL) {
272            outmsg("Could not find woodblock.aiff\n");
273            exit(1);
274          }
[b1f723d]275  }
276  if (onsetfile) {
[f382ac6]277          /* read the output sound once */
[5e9c68a]278          aubio_sndfile_read(onsetfile, overlap_size, woodblock);
[f382ac6]279  }
280
[bd2f2ab]281  if(!usejack)
282  {
283    debug("Opening files ...\n");
[5e9c68a]284    file = new_aubio_sndfile_ro (input_filename);
[9af07aa]285    if (file == NULL) {
286      outmsg("Could not open input file %s.\n", input_filename);
287      exit(1);
288    }
[5e9c68a]289    if (verbose) aubio_sndfile_info(file);
290    channels = aubio_sndfile_channels(file);
291    samplerate = aubio_sndfile_samplerate(file);
[bd2f2ab]292    if (output_filename != NULL)
[5e9c68a]293      fileout = new_aubio_sndfile_wo(file, output_filename);
[bd2f2ab]294  }
[71482a9]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);
[8e3a067]308      pthread_create(&lash_thread, NULL, lash_thread_main, NULL);
[71482a9]309    }
310  }
311#endif /* LASH_SUPPORT */
[bd2f2ab]312
313  ibuf      = new_fvec(overlap_size, channels);
314  obuf      = new_fvec(overlap_size, channels);
315  fftgrain  = new_cvec(buffer_size, channels);
316
[7c6c806d]317  //init for mfcc process
318  fftgrain_dct= new_cvec(n_filters, channels);
319
[bd2f2ab]320  if (usepitch) {
[edca23f]321    pitchdet = new_aubio_pitchdetection(buffer_size*4, 
[4c67dc8]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    }
[bd2f2ab]329  }
330  /* phase vocoder */
331  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
[7c6c806d]332 
333  // dct phase vocoder
334  //TODO: check size
335  fft_dct = new_aubio_mfft(n_filters, channels);
336
[bd2f2ab]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  }
[b91fe6e]358  if (usedoubled)    {
359    del_aubio_onsetdetection(o2);
360    del_fvec(onset2);
361  }
362  del_aubio_onsetdetection(o);
363  del_aubio_peakpicker(parms);
[bd2f2ab]364  del_aubio_pvoc(pv);
365  del_fvec(obuf);
366  del_fvec(ibuf);
367  del_cvec(fftgrain);
368  del_fvec(onset);
[b91fe6e]369  del_fvec(woodblock);
[7c6c806d]370 
371  //mffc related
372  del_aubio_mfft(fft_dct);
373  del_cvec(fftgrain_dct);
374 
[b91fe6e]375  aubio_cleanup();
[bd2f2ab]376}
377
378void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
379  if(usejack) {
[5a1ff62]380#if JACK_SUPPORT
[bd2f2ab]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
[af35ed0]412    while ((signed)overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
[bd2f2ab]413    {
414      isonset=0;
415      process_func(ibuf->data, obuf->data, overlap_size);
416      print(); 
417      if (output_filename != NULL) {
[5e9c68a]418        aubio_sndfile_write(fileout,overlap_size,obuf);
[bd2f2ab]419      }
420      frames++;
421    }
422
423    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
[5e9c68a]424    del_aubio_sndfile(file);
[bd2f2ab]425
426    if (output_filename != NULL)
[5e9c68a]427      del_aubio_sndfile(fileout);
[bd2f2ab]428
429  }
430}
431
432
433
434void send_noteon(int pitch, int velo)
435{
[144aff7]436    smpl_t mpitch = floor(aubio_freqtomidi(pitch)+.5);
[bd2f2ab]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
[71482a9]480#if LASH_SUPPORT
481
[633fb32]482void * lash_thread_main(void *data __attribute__((unused)))
[71482a9]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) {
[55c6da4]494        lash_event_t *lash_event;
495        lash_config_t *lash_config;
[71482a9]496
[55c6da4]497        while ((lash_event = lash_get_event(aubio_lash_client))) {
498                switch (lash_event_get_type(lash_event)) {
[71482a9]499                case LASH_Quit:
[55c6da4]500                        lash_event_destroy(lash_event);
[71482a9]501      exit(1);
502      return 1;
503                case LASH_Restore_Data_Set:
[55c6da4]504                        lash_send_event(aubio_lash_client, lash_event);
[71482a9]505                        break;
506                case LASH_Save_Data_Set:
507                        save_data();
[55c6da4]508                        lash_send_event(aubio_lash_client, lash_event);
[71482a9]509                        break;
510                case LASH_Server_Lost:
511                        return 1;
512                default:
513                        printf("%s: received unknown LASH event of type %d",
[55c6da4]514                                   __FUNCTION__, lash_event_get_type(lash_event));
515                        lash_event_destroy(lash_event);
[71482a9]516      break;
517                }
518        }
519
[55c6da4]520        while ((lash_config = lash_get_config(aubio_lash_client))) {
521                restore_data(lash_config);
522                lash_config_destroy(lash_config);
[71482a9]523        }
524
525        return 0;
526}
527
528void save_data() {
[55c6da4]529        lash_config_t *lash_config;
[71482a9]530
[55c6da4]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);
[71482a9]534
535}
536
[55c6da4]537void restore_data(lash_config_t * lash_config) {
538        const char *lash_key;
[71482a9]539
[55c6da4]540        lash_key = lash_config_get_key(lash_config);
[71482a9]541
[55c6da4]542        if (strcmp(lash_key, "threshold") == 0) {
543                threshold = lash_config_get_value_double(lash_config);
[71482a9]544                return;
545        }
546
547}
548
549#endif /* LASH_SUPPORT */
550
Note: See TracBrowser for help on using the repository browser.