source: examples/utils.c @ 71482a9

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

adding basic lash support to examples
adding basic lash support to examples

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