source: examples/utils.c @ bccac6b

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

ext/jackio.{c,h}: add support for jack midi

  • Property mode set to 100644
File size: 17.4 KB
RevLine 
[96fb8ad]1
2#include "aubio.h"
3
[b511fa9]4#ifndef HAVE_JACK
5#define HAVE_JACK 0
[96fb8ad]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
[b511fa9]15#ifdef HAVE_LASH
[71482a9]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;
[b511fa9]25#endif /* HAVE_LASH */
[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;
[e24378a]35int frames_delay = 0;
[bd2f2ab]36
37
38/* energy,specdiff,hfc,complexdomain,phase */
[5cf415f]39aubio_onsetdetection_type type_onset  = aubio_onset_kl;
40aubio_onsetdetection_type type_onset2 = aubio_onset_complex;
[bd2f2ab]41smpl_t threshold                      = 0.3;
[660cad22]42smpl_t silence                        = -90.;
[2d7b65a]43uint_t buffer_size                    = 512; //1024;
44uint_t overlap_size                   = 256; //512;
[bd2f2ab]45uint_t channels                       = 1;
46uint_t samplerate                     = 44100;
47
48
[5e9c68a]49aubio_sndfile_t * file = NULL;
50aubio_sndfile_t * fileout = NULL;
[bd2f2ab]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;
[4c67dc8]68aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; // aubio_pitch_mcomb
[fb615eb]69aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
[bd2f2ab]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
[96fb8ad]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{
[ee0cc27b]96        fprintf(stream, "usage: %s [ options ] \n", prog_name);
97        fprintf(stream, 
[fb615eb]98                        "       -h      --help          Display this message.\n"
[3a323d0]99                        "       -v      --verbose       Be verbose.\n"
[fb615eb]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"
[75a0f40]107                        "       -B      --bufsize       Set buffer size.\n"
[fb615eb]108                        "       -H      --hopsize       Set hopsize.\n"
109                        "       -a      --averaging     Use averaging.\n"
[ee0cc27b]110               );
111        exit(exit_code);
[96fb8ad]112}
113
114int parse_args (int argc, char **argv) {
[75a0f40]115        const char *options = "hvjo:i:O:t:s:p:B:H:a";
[ee0cc27b]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'},
[13d57e8]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'},
[fb615eb]127                {"pitch"    , 1, NULL, 'p'},
[ee0cc27b]128                {"averaging", 0, NULL, 'a'},
[75a0f40]129                {"bufsize",   1, NULL, 'B'},
[13d57e8]130                {"hopsize",   1, NULL, 'H'},
[ee0cc27b]131                {NULL       , 0, NULL, 0}
132        };
[b511fa9]133#ifdef HAVE_LASH
[71482a9]134        lash_args = lash_extract_args(&argc, &argv);
[b511fa9]135#endif /* HAVE_LASH */
[fb615eb]136        prog_name = argv[0];
[ee0cc27b]137        if( argc < 1 ) {
138                usage (stderr, 1);
139                return -1;
[96fb8ad]140        }
[ee0cc27b]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;
[fb615eb]151                        case 'h': /* help */
[ee0cc27b]152                                usage (stdout, 0);
153                                return -1;
[fb615eb]154                        case 'v': /* verbose */
[ee0cc27b]155                                verbose = 1;
156                                break;
[fb615eb]157                        case 'j':
[ee0cc27b]158                                usejack = 1;
159                                break;
160                        case 'O':   /*onset type*/
161                                if (strcmp(optarg,"energy") == 0) 
[5cf415f]162                                        type_onset = aubio_onset_energy;
[ee0cc27b]163                                else if (strcmp(optarg,"specdiff") == 0) 
[5cf415f]164                                        type_onset = aubio_onset_specdiff;
[ee0cc27b]165                                else if (strcmp(optarg,"hfc") == 0) 
[5cf415f]166                                        type_onset = aubio_onset_hfc;
[ee0cc27b]167                                else if (strcmp(optarg,"complexdomain") == 0) 
[5cf415f]168                                        type_onset = aubio_onset_complex;
169                                else if (strcmp(optarg,"complex") == 0) 
170                                        type_onset = aubio_onset_complex;
[ee0cc27b]171                                else if (strcmp(optarg,"phase") == 0) 
[5cf415f]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;
[862d78f]177                                else if (strcmp(optarg,"specflux") == 0) 
178                                        type_onset = aubio_onset_specflux;
[ee0cc27b]179                                else {
[92e4028]180                                        errmsg("unknown onset type.\n");
[ee0cc27b]181                                        abort();
182                                }
183                                usedoubled = 0;
184                                break;
185                        case 's':   /* threshold value for onset */
[660cad22]186                                silence = (smpl_t)atof(optarg);
[ee0cc27b]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;
[fb615eb]197                        case 'p':
198                                if (strcmp(optarg,"mcomb") == 0) 
199                                        type_pitch = aubio_pitch_mcomb;
[4c67dc8]200                                else if (strcmp(optarg,"yinfft") == 0) 
201                                        type_pitch = aubio_pitch_yin;
[fb615eb]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 {
[92e4028]209                                        errmsg("unknown pitch type.\n");
[fb615eb]210                                        abort();
211                                }
212                                break;
[ee0cc27b]213                        case 'a':
214                                averaging = 1;
215                                break; 
[75a0f40]216                        case 'B':
217                                buffer_size = atoi(optarg);
218                                break;
[0ce9acc3]219                        case 'H':
220                                overlap_size = atoi(optarg);
221                                break;
[fb615eb]222                        case '?': /* unknown options */
[ee0cc27b]223                                usage(stderr, 1);
224                                break;
[fb615eb]225                        case -1: /* done with options */
[ee0cc27b]226                                break;
[fb615eb]227                        default: /*something else unexpected */
[75a0f40]228                                fprintf(stderr, "Error parsing option '%c'\n", next_option);
[ee0cc27b]229                                abort ();
230                }
[96fb8ad]231        }
[ee0cc27b]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 {
[b511fa9]240                if (HAVE_JACK)
[ee0cc27b]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                }
[fb615eb]248        }
[71482a9]249
[ee0cc27b]250        return 0;
[96fb8ad]251}
252
[bd2f2ab]253void examples_common_init(int argc,char ** argv) {
254
255
[b1f723d]256  aubio_sndfile_t * onsetfile = NULL;
[bd2f2ab]257  /* parse command line arguments */
258  parse_args(argc, argv);
259
[f382ac6]260  woodblock = new_fvec(buffer_size,1);
261  if (output_filename || usejack) {
[6b233fc]262          /* dummy assignement to keep egcs happy */
263          isonset = (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
[5e9c68a]264                  (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
265                  (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
[9af07aa]266          if (onsetfile == NULL) {
267            outmsg("Could not find woodblock.aiff\n");
268            exit(1);
269          }
[b1f723d]270  }
271  if (onsetfile) {
[f382ac6]272          /* read the output sound once */
[5e9c68a]273          aubio_sndfile_read(onsetfile, overlap_size, woodblock);
[f382ac6]274  }
275
[bd2f2ab]276  if(!usejack)
277  {
278    debug("Opening files ...\n");
[5e9c68a]279    file = new_aubio_sndfile_ro (input_filename);
[9af07aa]280    if (file == NULL) {
281      outmsg("Could not open input file %s.\n", input_filename);
282      exit(1);
283    }
[5e9c68a]284    if (verbose) aubio_sndfile_info(file);
285    channels = aubio_sndfile_channels(file);
286    samplerate = aubio_sndfile_samplerate(file);
[bd2f2ab]287    if (output_filename != NULL)
[5e9c68a]288      fileout = new_aubio_sndfile_wo(file, output_filename);
[bd2f2ab]289  }
[b511fa9]290#ifdef HAVE_LASH
[71482a9]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);
[8e3a067]303      pthread_create(&lash_thread, NULL, lash_thread_main, NULL);
[71482a9]304    }
305  }
[b511fa9]306#endif /* HAVE_LASH */
[bd2f2ab]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) {
[edca23f]313    pitchdet = new_aubio_pitchdetection(buffer_size*4, 
[4c67dc8]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    }
[bd2f2ab]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  }
[b91fe6e]345  if (usedoubled)    {
346    del_aubio_onsetdetection(o2);
347    del_fvec(onset2);
348  }
349  del_aubio_onsetdetection(o);
350  del_aubio_peakpicker(parms);
[bd2f2ab]351  del_aubio_pvoc(pv);
352  del_fvec(obuf);
353  del_fvec(ibuf);
354  del_cvec(fftgrain);
355  del_fvec(onset);
[b91fe6e]356  del_fvec(woodblock);
357  aubio_cleanup();
[bd2f2ab]358}
359
360void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
361  if(usejack) {
[b511fa9]362#if HAVE_JACK
[bd2f2ab]363    aubio_jack_t * jack_setup;
364    debug("Jack init ...\n");
365    jack_setup = new_aubio_jack(channels, channels,
[bccac6b]366          1, 1,
[bd2f2ab]367          (aubio_process_func_t)process_func);
368    if (usepitch) {
369            debug("Midi init ...\n");
370            mplay = new_aubio_midi_player();
371            mdriver = new_aubio_midi_driver("alsa_seq",
372                            (handle_midi_event_func_t)aubio_midi_send_event, mplay);
373            event = new_aubio_midi_event();
374    }
375    debug("Jack activation ...\n");
376    aubio_jack_activate(jack_setup);
377    debug("Processing (Ctrl+C to quit) ...\n");
378    pause();
379    aubio_jack_close(jack_setup);
380    if (usepitch) {
381            send_noteon(curnote,0);
382            del_aubio_midi_driver(mdriver);
383    }
384#else
385    usage(stderr, 1);
386    outmsg("Compiled without jack output, exiting.\n");
387#endif
388
389  } else {
390    /* phasevoc */
391    debug("Processing ...\n");
392
393    frames = 0;
394
[af35ed0]395    while ((signed)overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
[bd2f2ab]396    {
397      isonset=0;
398      process_func(ibuf->data, obuf->data, overlap_size);
399      print(); 
400      if (output_filename != NULL) {
[5e9c68a]401        aubio_sndfile_write(fileout,overlap_size,obuf);
[bd2f2ab]402      }
403      frames++;
404    }
405
406    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
[e24378a]407
408    flush_process(process_func, print);
[5e9c68a]409    del_aubio_sndfile(file);
[bd2f2ab]410
411    if (output_filename != NULL)
[5e9c68a]412      del_aubio_sndfile(fileout);
[bd2f2ab]413
414  }
415}
416
[e24378a]417void flush_process(aubio_process_func_t process_func, aubio_print_func_t print){
[ae7e547]418  uint_t i,j;
[e24378a]419  for (i = 0; i < channels; i++) {
420    for (j = 0; j < obuf->length; j++) {
421      fvec_write_sample(obuf,0.,i,j);
422    }
423  }
[6f10064]424  for (i = 0; (signed)i < frames_delay; i++) {
[e24378a]425    process_func(ibuf->data, obuf->data, overlap_size);
426    print(); 
427  }
428}
[bd2f2ab]429
430
431void send_noteon(int pitch, int velo)
432{
[144aff7]433    smpl_t mpitch = floor(aubio_freqtomidi(pitch)+.5);
[bd2f2ab]434    /* we should check if we use midi here, not jack */
[b511fa9]435#if HAVE_ALSA
[bd2f2ab]436    if (usejack) {
437        if (velo==0) {
438            aubio_midi_event_set_type(event,NOTE_OFF);
439        } else {
440            aubio_midi_event_set_type(event,NOTE_ON);
441        }
442        aubio_midi_event_set_channel(event,0);
443        aubio_midi_event_set_pitch(event,mpitch);
444        aubio_midi_event_set_velocity(event,velo);
445        aubio_midi_direct_output(mdriver,event);
446    } else 
447#endif
448    if (!verbose)
449    {
450        if (velo==0) {
451            outmsg("%f\n",frames*overlap_size/(float)samplerate);
452        } else {
453            outmsg("%f\t%f\t", mpitch,
454                        frames*overlap_size/(float)samplerate);
455        }
456    }
457}
458
459
460void note_append(fvec_t * note_buffer, smpl_t curnote) {
461  uint_t i = 0;
462  for (i = 0; i < note_buffer->length - 1; i++) { 
463      note_buffer->data[0][i] = note_buffer->data[0][i+1];
464  }
465  note_buffer->data[0][note_buffer->length - 1] = curnote;
466  return;
467}
468
469uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
470  uint_t i = 0;
471  for (i = 0; i < note_buffer->length; i++) { 
472      note_buffer2->data[0][i] = note_buffer->data[0][i];
473  }
474  return vec_median(note_buffer2);
475}
476
[b511fa9]477#if HAVE_LASH
[71482a9]478
[633fb32]479void * lash_thread_main(void *data __attribute__((unused)))
[71482a9]480{
481        printf("LASH thread running\n");
482
483        while (!lash_main())
484                usleep(1000);
485
486        printf("LASH thread finished\n");
487        return NULL;
488}
489
490int lash_main(void) {
[55c6da4]491        lash_event_t *lash_event;
492        lash_config_t *lash_config;
[71482a9]493
[55c6da4]494        while ((lash_event = lash_get_event(aubio_lash_client))) {
495                switch (lash_event_get_type(lash_event)) {
[71482a9]496                case LASH_Quit:
[55c6da4]497                        lash_event_destroy(lash_event);
[71482a9]498      exit(1);
499      return 1;
500                case LASH_Restore_Data_Set:
[55c6da4]501                        lash_send_event(aubio_lash_client, lash_event);
[71482a9]502                        break;
503                case LASH_Save_Data_Set:
504                        save_data();
[55c6da4]505                        lash_send_event(aubio_lash_client, lash_event);
[71482a9]506                        break;
507                case LASH_Server_Lost:
508                        return 1;
509                default:
510                        printf("%s: received unknown LASH event of type %d",
[55c6da4]511                                   __FUNCTION__, lash_event_get_type(lash_event));
512                        lash_event_destroy(lash_event);
[71482a9]513      break;
514                }
515        }
516
[55c6da4]517        while ((lash_config = lash_get_config(aubio_lash_client))) {
518                restore_data(lash_config);
519                lash_config_destroy(lash_config);
[71482a9]520        }
521
522        return 0;
523}
524
525void save_data() {
[55c6da4]526        lash_config_t *lash_config;
[71482a9]527
[55c6da4]528        lash_config = lash_config_new_with_key("threshold");
529        lash_config_set_value_double(lash_config, threshold);
530        lash_send_config(aubio_lash_client, lash_config);
[71482a9]531
532}
533
[55c6da4]534void restore_data(lash_config_t * lash_config) {
535        const char *lash_key;
[71482a9]536
[55c6da4]537        lash_key = lash_config_get_key(lash_config);
[71482a9]538
[55c6da4]539        if (strcmp(lash_key, "threshold") == 0) {
540                threshold = lash_config_get_value_double(lash_config);
[71482a9]541                return;
542        }
543
544}
545
[b511fa9]546#endif /* HAVE_LASH */
[71482a9]547
Note: See TracBrowser for help on using the repository browser.