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 /* HAVE_LASH */
28 const char * output_filename = NULL;
29 const char * input_filename = NULL;
30 const char * onset_filename = AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff";
38 /* energy,specdiff,hfc,complexdomain,phase */
39 aubio_onsetdetection_type type_onset = aubio_onset_kl;
40 aubio_onsetdetection_type type_onset2 = aubio_onset_complex;
41 smpl_t threshold = 0.3;
42 smpl_t silence = -90.;
43 uint_t buffer_size = 512; //1024;
44 uint_t overlap_size = 256; //512;
46 uint_t samplerate = 44100;
49 aubio_sndfile_t * file = NULL;
50 aubio_sndfile_t * fileout = NULL;
57 aubio_onsetdetection_t *o;
58 aubio_onsetdetection_t *o2;
62 aubio_pickpeak_t * parms;
67 aubio_pitchdetection_t * pitchdet;
68 aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; // aubio_pitch_mcomb
69 aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
72 fvec_t * note_buffer = NULL;
73 fvec_t * note_buffer2 = NULL;
78 aubio_midi_player_t * mplay;
79 aubio_midi_driver_t * mdriver;
80 aubio_midi_event_t * event;
88 /* badly redeclare some things */
89 aubio_onsetdetection_type type_onset;
92 const char * prog_name;
94 void usage (FILE * stream, int exit_code)
96 fprintf(stream, "usage: %s [ options ] \n", prog_name);
98 " -h --help Display this message.\n"
99 " -v --verbose Be verbose.\n"
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 " -B --bufsize Set buffer size.\n"
108 " -H --hopsize Set hopsize.\n"
109 " -a --averaging Use averaging.\n"
114 int parse_args (int argc, char **argv) {
115 const char *options = "hvjo:i:O:t:s:p:B:H:a";
117 struct option long_options[] =
119 {"help" , 0, NULL, 'h'},
120 {"verbose" , 0, NULL, 'v'},
121 {"jack" , 0, NULL, 'j'},
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'},
127 {"pitch" , 1, NULL, 'p'},
128 {"averaging", 0, NULL, 'a'},
129 {"bufsize", 1, NULL, 'B'},
130 {"hopsize", 1, NULL, 'H'},
134 lash_args = lash_extract_args(&argc, &argv);
135 #endif /* HAVE_LASH */
142 next_option = getopt_long (argc, argv, options,
144 switch (next_option) {
146 output_filename = optarg;
149 input_filename = optarg;
154 case 'v': /* verbose */
160 case 'O': /*onset type*/
161 if (strcmp(optarg,"energy") == 0)
162 type_onset = aubio_onset_energy;
163 else if (strcmp(optarg,"specdiff") == 0)
164 type_onset = aubio_onset_specdiff;
165 else if (strcmp(optarg,"hfc") == 0)
166 type_onset = aubio_onset_hfc;
167 else if (strcmp(optarg,"complexdomain") == 0)
168 type_onset = aubio_onset_complex;
169 else if (strcmp(optarg,"complex") == 0)
170 type_onset = aubio_onset_complex;
171 else if (strcmp(optarg,"phase") == 0)
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;
177 else if (strcmp(optarg,"specflux") == 0)
178 type_onset = aubio_onset_specflux;
180 errmsg("unknown onset type.\n");
185 case 's': /* threshold value for onset */
186 silence = (smpl_t)atof(optarg);
188 case 't': /* threshold value for onset */
189 threshold = (smpl_t)atof(optarg);
191 if (!isfinite(threshold)) {
192 debug("could not get threshold.\n");
198 if (strcmp(optarg,"mcomb") == 0)
199 type_pitch = aubio_pitch_mcomb;
200 else if (strcmp(optarg,"yinfft") == 0)
201 type_pitch = aubio_pitch_yin;
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;
209 errmsg("unknown pitch type.\n");
217 buffer_size = atoi(optarg);
220 overlap_size = atoi(optarg);
222 case '?': /* unknown options */
225 case -1: /* done with options */
227 default: /*something else unexpected */
228 fprintf(stderr, "Error parsing option '%c'\n", next_option);
232 while (next_option != -1);
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 );
242 debug ("Jack input output\n");
245 debug ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n");
253 void examples_common_init(int argc,char ** argv) {
256 aubio_sndfile_t * onsetfile = NULL;
257 /* parse command line arguments */
258 parse_args(argc, argv);
260 woodblock = new_fvec(buffer_size,1);
261 if (output_filename || usejack) {
262 /* dummy assignement to keep egcs happy */
263 isonset = (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
264 (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
265 (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
266 if (onsetfile == NULL) {
267 outmsg("Could not find woodblock.aiff\n");
272 /* read the output sound once */
273 aubio_sndfile_read(onsetfile, overlap_size, woodblock);
278 debug("Opening files ...\n");
279 file = new_aubio_sndfile_ro (input_filename);
281 outmsg("Could not open input file %s.\n", input_filename);
284 if (verbose) aubio_sndfile_info(file);
285 channels = aubio_sndfile_channels(file);
286 samplerate = aubio_sndfile_samplerate(file);
287 if (output_filename != NULL)
288 fileout = new_aubio_sndfile_wo(file, output_filename);
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__);
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);
303 pthread_create(&lash_thread, NULL, lash_thread_main, NULL);
306 #endif /* HAVE_LASH */
308 ibuf = new_fvec(overlap_size, channels);
309 obuf = new_fvec(overlap_size, channels);
310 fftgrain = new_cvec(buffer_size, channels);
313 pitchdet = new_aubio_pitchdetection(buffer_size*4,
314 overlap_size, channels, samplerate, type_pitch, mode_pitch);
315 aubio_pitchdetection_set_yinthresh(pitchdet, 0.7);
318 note_buffer = new_fvec(median, 1);
319 note_buffer2= new_fvec(median, 1);
323 pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
325 parms = new_aubio_peakpicker(threshold);
326 o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
327 onset = new_fvec(1, channels);
329 o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
330 onset2 = new_fvec(1 , channels);
336 void examples_common_del(void){
338 send_noteon(curnote,0);
339 del_aubio_pitchdetection(pitchdet);
341 del_fvec(note_buffer);
342 del_fvec(note_buffer2);
346 del_aubio_onsetdetection(o2);
349 del_aubio_onsetdetection(o);
350 del_aubio_peakpicker(parms);
360 void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
363 aubio_jack_t * jack_setup;
364 debug("Jack init ...\n");
365 jack_setup = new_aubio_jack(channels, channels,
367 (aubio_process_func_t)process_func);
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();
375 debug("Jack activation ...\n");
376 aubio_jack_activate(jack_setup);
377 debug("Processing (Ctrl+C to quit) ...\n");
379 aubio_jack_close(jack_setup);
381 send_noteon(curnote,0);
382 del_aubio_midi_driver(mdriver);
386 outmsg("Compiled without jack output, exiting.\n");
391 debug("Processing ...\n");
395 while ((signed)overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
398 process_func(ibuf->data, obuf->data, overlap_size);
400 if (output_filename != NULL) {
401 aubio_sndfile_write(fileout,overlap_size,obuf);
406 debug("Processed %d frames of %d samples.\n", frames, buffer_size);
408 flush_process(process_func, print);
409 del_aubio_sndfile(file);
411 if (output_filename != NULL)
412 del_aubio_sndfile(fileout);
417 void flush_process(aubio_process_func_t process_func, aubio_print_func_t print){
419 for (i = 0; i < channels; i++) {
420 for (j = 0; j < obuf->length; j++) {
421 fvec_write_sample(obuf,0.,i,j);
424 for (i = 0; (signed)i < frames_delay; i++) {
425 process_func(ibuf->data, obuf->data, overlap_size);
431 void send_noteon(int pitch, int velo)
433 smpl_t mpitch = floor(aubio_freqtomidi(pitch)+.5);
434 /* we should check if we use midi here, not jack */
438 aubio_midi_event_set_type(event,NOTE_OFF);
440 aubio_midi_event_set_type(event,NOTE_ON);
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);
451 outmsg("%f\n",frames*overlap_size/(float)samplerate);
453 outmsg("%f\t%f\t", mpitch,
454 frames*overlap_size/(float)samplerate);
460 void note_append(fvec_t * note_buffer, smpl_t curnote) {
462 for (i = 0; i < note_buffer->length - 1; i++) {
463 note_buffer->data[0][i] = note_buffer->data[0][i+1];
465 note_buffer->data[0][note_buffer->length - 1] = curnote;
469 uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
471 for (i = 0; i < note_buffer->length; i++) {
472 note_buffer2->data[0][i] = note_buffer->data[0][i];
474 return vec_median(note_buffer2);
479 void * lash_thread_main(void *data __attribute__((unused)))
481 printf("LASH thread running\n");
486 printf("LASH thread finished\n");
490 int lash_main(void) {
491 lash_event_t *lash_event;
492 lash_config_t *lash_config;
494 while ((lash_event = lash_get_event(aubio_lash_client))) {
495 switch (lash_event_get_type(lash_event)) {
497 lash_event_destroy(lash_event);
500 case LASH_Restore_Data_Set:
501 lash_send_event(aubio_lash_client, lash_event);
503 case LASH_Save_Data_Set:
505 lash_send_event(aubio_lash_client, lash_event);
507 case LASH_Server_Lost:
510 printf("%s: received unknown LASH event of type %d",
511 __FUNCTION__, lash_event_get_type(lash_event));
512 lash_event_destroy(lash_event);
517 while ((lash_config = lash_get_config(aubio_lash_client))) {
518 restore_data(lash_config);
519 lash_config_destroy(lash_config);
526 lash_config_t *lash_config;
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);
534 void restore_data(lash_config_t * lash_config) {
535 const char *lash_key;
537 lash_key = lash_config_get_key(lash_config);
539 if (strcmp(lash_key, "threshold") == 0) {
540 threshold = lash_config_get_value_double(lash_config);
546 #endif /* HAVE_LASH */