source: examples/utils.c @ 16e0f16

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

examples/utils.{c,h}: move flush_process prototype to .h

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