12 #include <math.h> /* for isfinite */
16 #include <lash/lash.h>
18 lash_client_t * aubio_lash_client;
19 lash_args_t * lash_args;
20 void * lash_thread_main (void * data);
22 void save_data (void);
23 void restore_data(lash_config_t * lash_config);
24 pthread_t lash_thread;
25 #endif /* LASH_SUPPORT */
28 const char * output_filename = NULL;
29 const char * input_filename = NULL;
30 const char * onset_filename = AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff";
37 /* energy,specdiff,hfc,complexdomain,phase */
38 aubio_onsetdetection_type type_onset = aubio_onset_kl;
39 aubio_onsetdetection_type type_onset2 = aubio_onset_complex;
40 smpl_t threshold = 0.3;
41 smpl_t silence = -90.;
42 uint_t buffer_size = 512; //1024;
43 uint_t overlap_size = 256; //512;
45 uint_t samplerate = 44100;
48 aubio_sndfile_t * file = NULL;
49 aubio_sndfile_t * fileout = NULL;
56 aubio_onsetdetection_t *o;
57 aubio_onsetdetection_t *o2;
61 aubio_pickpeak_t * parms;
66 uint_t nyquist= samplerate / 2.;
68 smpl_t highfreq=18000.f;
70 aubio_mel_filter * mf;
72 // DCT mfft and result storage
74 cvec_t * fftgrain_dct;
75 smpl_t mfcc_outbuf[11];
80 aubio_pitchdetection_t * pitchdet;
81 aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; // aubio_pitch_mcomb
82 aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
85 fvec_t * note_buffer = NULL;
86 fvec_t * note_buffer2 = NULL;
91 aubio_midi_player_t * mplay;
92 aubio_midi_driver_t * mdriver;
93 aubio_midi_event_t * event;
101 /* badly redeclare some things */
102 aubio_onsetdetection_type type_onset;
105 const char * prog_name;
107 void usage (FILE * stream, int exit_code)
109 fprintf(stream, "usage: %s [ options ] \n", prog_name);
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"
126 int parse_args (int argc, char **argv) {
127 const char *options = "hvjo:i:O:t:s:p:H:a";
129 struct option long_options[] =
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'},
145 lash_args = lash_extract_args(&argc, &argv);
146 #endif /* LASH_SUPPORT */
153 next_option = getopt_long (argc, argv, options,
155 switch (next_option) {
157 output_filename = optarg;
160 input_filename = optarg;
165 case 'v': /* verbose */
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;
189 errmsg("unknown onset type.\n");
194 case 's': /* threshold value for onset */
195 silence = (smpl_t)atof(optarg);
197 case 't': /* threshold value for onset */
198 threshold = (smpl_t)atof(optarg);
200 if (!isfinite(threshold)) {
201 debug("could not get threshold.\n");
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;
218 errmsg("unknown pitch type.\n");
226 overlap_size = atoi(optarg);
228 case '?': /* unknown options */
231 case -1: /* done with options */
233 default: /*something else unexpected */
237 while (next_option != -1);
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 );
247 debug ("Jack input output\n");
250 debug ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n");
258 void examples_common_init(int argc,char ** argv) {
261 aubio_sndfile_t * onsetfile = NULL;
262 /* parse command line arguments */
263 parse_args(argc, argv);
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");
277 /* read the output sound once */
278 aubio_sndfile_read(onsetfile, overlap_size, woodblock);
283 debug("Opening files ...\n");
284 file = new_aubio_sndfile_ro (input_filename);
286 outmsg("Could not open input file %s.\n", input_filename);
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);
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__);
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);
311 #endif /* LASH_SUPPORT */
313 ibuf = new_fvec(overlap_size, channels);
314 obuf = new_fvec(overlap_size, channels);
315 fftgrain = new_cvec(buffer_size, channels);
317 //init for mfcc process
318 fftgrain_dct= new_cvec(n_filters, channels);
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);
326 note_buffer = new_fvec(median, 1);
327 note_buffer2= new_fvec(median, 1);
331 pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
335 fft_dct = new_aubio_mfft(n_filters, channels);
338 parms = new_aubio_peakpicker(threshold);
339 o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
340 onset = new_fvec(1, channels);
342 o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
343 onset2 = new_fvec(1 , channels);
349 void examples_common_del(void){
351 send_noteon(curnote,0);
352 del_aubio_pitchdetection(pitchdet);
354 del_fvec(note_buffer);
355 del_fvec(note_buffer2);
359 del_aubio_onsetdetection(o2);
362 del_aubio_onsetdetection(o);
363 del_aubio_peakpicker(parms);
372 del_aubio_mfft(fft_dct);
373 del_cvec(fftgrain_dct);
378 void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
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);
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();
392 debug("Jack activation ...\n");
393 aubio_jack_activate(jack_setup);
394 debug("Processing (Ctrl+C to quit) ...\n");
396 aubio_jack_close(jack_setup);
398 send_noteon(curnote,0);
399 del_aubio_midi_driver(mdriver);
403 outmsg("Compiled without jack output, exiting.\n");
408 debug("Processing ...\n");
412 while ((signed)overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
415 process_func(ibuf->data, obuf->data, overlap_size);
417 if (output_filename != NULL) {
418 aubio_sndfile_write(fileout,overlap_size,obuf);
423 debug("Processed %d frames of %d samples.\n", frames, buffer_size);
424 del_aubio_sndfile(file);
426 if (output_filename != NULL)
427 del_aubio_sndfile(fileout);
434 void send_noteon(int pitch, int velo)
436 smpl_t mpitch = floor(aubio_freqtomidi(pitch)+.5);
437 /* we should check if we use midi here, not jack */
441 aubio_midi_event_set_type(event,NOTE_OFF);
443 aubio_midi_event_set_type(event,NOTE_ON);
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);
454 outmsg("%f\n",frames*overlap_size/(float)samplerate);
456 outmsg("%f\t%f\t", mpitch,
457 frames*overlap_size/(float)samplerate);
463 void note_append(fvec_t * note_buffer, smpl_t curnote) {
465 for (i = 0; i < note_buffer->length - 1; i++) {
466 note_buffer->data[0][i] = note_buffer->data[0][i+1];
468 note_buffer->data[0][note_buffer->length - 1] = curnote;
472 uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
474 for (i = 0; i < note_buffer->length; i++) {
475 note_buffer2->data[0][i] = note_buffer->data[0][i];
477 return vec_median(note_buffer2);
482 void * lash_thread_main(void *data __attribute__((unused)))
484 printf("LASH thread running\n");
489 printf("LASH thread finished\n");
493 int lash_main(void) {
494 lash_event_t *lash_event;
495 lash_config_t *lash_config;
497 while ((lash_event = lash_get_event(aubio_lash_client))) {
498 switch (lash_event_get_type(lash_event)) {
500 lash_event_destroy(lash_event);
503 case LASH_Restore_Data_Set:
504 lash_send_event(aubio_lash_client, lash_event);
506 case LASH_Save_Data_Set:
508 lash_send_event(aubio_lash_client, lash_event);
510 case LASH_Server_Lost:
513 printf("%s: received unknown LASH event of type %d",
514 __FUNCTION__, lash_event_get_type(lash_event));
515 lash_event_destroy(lash_event);
520 while ((lash_config = lash_get_config(aubio_lash_client))) {
521 restore_data(lash_config);
522 lash_config_destroy(lash_config);
529 lash_config_t *lash_config;
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);
537 void restore_data(lash_config_t * lash_config) {
538 const char *lash_key;
540 lash_key = lash_config_get_key(lash_config);
542 if (strcmp(lash_key, "threshold") == 0) {
543 threshold = lash_config_get_value_double(lash_config);
549 #endif /* LASH_SUPPORT */