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> |
---|
18 | lash_client_t * aubio_lash_client; |
---|
19 | lash_args_t * lash_args; |
---|
20 | void * lash_thread_main (void * data); |
---|
21 | int lash_main (void); |
---|
22 | void save_data (void); |
---|
23 | void restore_data(lash_config_t * lash_config); |
---|
24 | pthread_t lash_thread; |
---|
25 | #endif /* LASH_SUPPORT */ |
---|
26 | |
---|
27 | /* settings */ |
---|
28 | const char * output_filename = NULL; |
---|
29 | const char * input_filename = NULL; |
---|
30 | const char * onset_filename = AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff"; |
---|
31 | int frames = 0; |
---|
32 | int verbose = 0; |
---|
33 | int usejack = 0; |
---|
34 | int usedoubled = 1; |
---|
35 | |
---|
36 | |
---|
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; |
---|
43 | // uint_t overlap_size = 256; |
---|
44 | uint_t buffer_size = 1024; |
---|
45 | uint_t overlap_size = 512; |
---|
46 | // uint_t buffer_size = 2048; |
---|
47 | // uint_t overlap_size = 1024; |
---|
48 | uint_t channels = 1; |
---|
49 | uint_t samplerate = 44100; |
---|
50 | |
---|
51 | |
---|
52 | aubio_sndfile_t * file = NULL; |
---|
53 | aubio_sndfile_t * fileout = NULL; |
---|
54 | |
---|
55 | aubio_pvoc_t * pv; |
---|
56 | fvec_t * ibuf; |
---|
57 | fvec_t * obuf; |
---|
58 | cvec_t * fftgrain; |
---|
59 | fvec_t * woodblock; |
---|
60 | aubio_onsetdetection_t *o; |
---|
61 | aubio_onsetdetection_t *o2; |
---|
62 | fvec_t *onset; |
---|
63 | fvec_t *onset2; |
---|
64 | int isonset = 0; |
---|
65 | aubio_pickpeak_t * parms; |
---|
66 | |
---|
67 | /* pitch objects */ |
---|
68 | smpl_t pitch = 0.; |
---|
69 | aubio_pitchdetection_t * pitchdet; |
---|
70 | aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; // aubio_pitch_mcomb |
---|
71 | aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq; |
---|
72 | uint_t median = 6; |
---|
73 | |
---|
74 | fvec_t * note_buffer = NULL; |
---|
75 | fvec_t * note_buffer2 = NULL; |
---|
76 | smpl_t curlevel = 0.; |
---|
77 | smpl_t maxonset = 0.; |
---|
78 | |
---|
79 | /* midi objects */ |
---|
80 | aubio_midi_player_t * mplay; |
---|
81 | aubio_midi_driver_t * mdriver; |
---|
82 | aubio_midi_event_t * event; |
---|
83 | |
---|
84 | smpl_t curnote = 0.; |
---|
85 | smpl_t newnote = 0.; |
---|
86 | uint_t isready = 0; |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | /* badly redeclare some things */ |
---|
91 | aubio_onsetdetection_type type_onset; |
---|
92 | smpl_t threshold; |
---|
93 | smpl_t averaging; |
---|
94 | const char * prog_name; |
---|
95 | |
---|
96 | void usage (FILE * stream, int exit_code) |
---|
97 | { |
---|
98 | fprintf(stream, "usage: %s [ options ] \n", prog_name); |
---|
99 | fprintf(stream, |
---|
100 | " -h --help Display this message.\n" |
---|
101 | " -v --verbose Be verbose.\n" |
---|
102 | " -j --jack Use Jack.\n" |
---|
103 | " -o --output Output type.\n" |
---|
104 | " -i --input Input type.\n" |
---|
105 | " -O --onset Select onset detection algorithm.\n" |
---|
106 | " -t --threshold Set onset detection threshold.\n" |
---|
107 | " -s --silence Select silence threshold.\n" |
---|
108 | " -p --pitch Select pitch detection algorithm.\n" |
---|
109 | " -H --hopsize Set hopsize.\n" |
---|
110 | " -a --averaging Use averaging.\n" |
---|
111 | ); |
---|
112 | exit(exit_code); |
---|
113 | } |
---|
114 | |
---|
115 | int parse_args (int argc, char **argv) { |
---|
116 | const char *options = "hvjo:i:O:t:s:p:H:a"; |
---|
117 | int next_option; |
---|
118 | struct option long_options[] = |
---|
119 | { |
---|
120 | {"help" , 0, NULL, 'h'}, |
---|
121 | {"verbose" , 0, NULL, 'v'}, |
---|
122 | {"jack" , 0, NULL, 'j'}, |
---|
123 | {"output" , 1, NULL, 'o'}, |
---|
124 | {"input" , 1, NULL, 'i'}, |
---|
125 | {"onset" , 1, NULL, 'O'}, |
---|
126 | {"threshold", 1, NULL, 't'}, |
---|
127 | {"silence" , 1, NULL, 's'}, |
---|
128 | {"pitch" , 1, NULL, 'p'}, |
---|
129 | {"averaging", 0, NULL, 'a'}, |
---|
130 | {"hopsize", 1, NULL, 'H'}, |
---|
131 | {NULL , 0, NULL, 0} |
---|
132 | }; |
---|
133 | #ifdef LASH_SUPPORT |
---|
134 | lash_args = lash_extract_args(&argc, &argv); |
---|
135 | #endif /* LASH_SUPPORT */ |
---|
136 | prog_name = argv[0]; |
---|
137 | if( argc < 1 ) { |
---|
138 | usage (stderr, 1); |
---|
139 | return -1; |
---|
140 | } |
---|
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; |
---|
151 | case 'h': /* help */ |
---|
152 | usage (stdout, 0); |
---|
153 | return -1; |
---|
154 | case 'v': /* verbose */ |
---|
155 | verbose = 1; |
---|
156 | break; |
---|
157 | case 'j': |
---|
158 | usejack = 1; |
---|
159 | break; |
---|
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 { |
---|
178 | errmsg("unknown onset type.\n"); |
---|
179 | abort(); |
---|
180 | } |
---|
181 | usedoubled = 0; |
---|
182 | break; |
---|
183 | case 's': /* threshold value for onset */ |
---|
184 | silence = (smpl_t)atof(optarg); |
---|
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; |
---|
195 | case 'p': |
---|
196 | if (strcmp(optarg,"mcomb") == 0) |
---|
197 | type_pitch = aubio_pitch_mcomb; |
---|
198 | else if (strcmp(optarg,"yinfft") == 0) |
---|
199 | type_pitch = aubio_pitch_yin; |
---|
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 { |
---|
207 | errmsg("unknown pitch type.\n"); |
---|
208 | abort(); |
---|
209 | } |
---|
210 | break; |
---|
211 | case 'a': |
---|
212 | averaging = 1; |
---|
213 | break; |
---|
214 | case 'H': |
---|
215 | overlap_size = atoi(optarg); |
---|
216 | break; |
---|
217 | case '?': /* unknown options */ |
---|
218 | usage(stderr, 1); |
---|
219 | break; |
---|
220 | case -1: /* done with options */ |
---|
221 | break; |
---|
222 | default: /*something else unexpected */ |
---|
223 | abort (); |
---|
224 | } |
---|
225 | } |
---|
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 | } |
---|
242 | } |
---|
243 | |
---|
244 | return 0; |
---|
245 | } |
---|
246 | |
---|
247 | void examples_common_init(int argc,char ** argv) { |
---|
248 | |
---|
249 | |
---|
250 | aubio_sndfile_t * onsetfile = NULL; |
---|
251 | /* parse command line arguments */ |
---|
252 | parse_args(argc, argv); |
---|
253 | |
---|
254 | woodblock = new_fvec(buffer_size,1); |
---|
255 | if (output_filename || usejack) { |
---|
256 | /* dummy assignement to keep egcs happy */ |
---|
257 | isonset = (onsetfile = new_aubio_sndfile_ro(onset_filename)) || |
---|
258 | (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) || |
---|
259 | (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff")); |
---|
260 | if (onsetfile == NULL) { |
---|
261 | outmsg("Could not find woodblock.aiff\n"); |
---|
262 | exit(1); |
---|
263 | } |
---|
264 | } |
---|
265 | if (onsetfile) { |
---|
266 | /* read the output sound once */ |
---|
267 | aubio_sndfile_read(onsetfile, overlap_size, woodblock); |
---|
268 | } |
---|
269 | |
---|
270 | if(!usejack) |
---|
271 | { |
---|
272 | debug("Opening files ...\n"); |
---|
273 | file = new_aubio_sndfile_ro (input_filename); |
---|
274 | if (file == NULL) { |
---|
275 | outmsg("Could not open input file %s.\n", input_filename); |
---|
276 | exit(1); |
---|
277 | } |
---|
278 | if (verbose) aubio_sndfile_info(file); |
---|
279 | channels = aubio_sndfile_channels(file); |
---|
280 | samplerate = aubio_sndfile_samplerate(file); |
---|
281 | if (output_filename != NULL) |
---|
282 | fileout = new_aubio_sndfile_wo(file, output_filename); |
---|
283 | } |
---|
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); |
---|
297 | pthread_create(&lash_thread, NULL, lash_thread_main, NULL); |
---|
298 | } |
---|
299 | } |
---|
300 | #endif /* LASH_SUPPORT */ |
---|
301 | |
---|
302 | ibuf = new_fvec(overlap_size, channels); |
---|
303 | obuf = new_fvec(overlap_size, channels); |
---|
304 | fftgrain = new_cvec(buffer_size, channels); |
---|
305 | |
---|
306 | |
---|
307 | if (usepitch) { |
---|
308 | pitchdet = new_aubio_pitchdetection(buffer_size*4, |
---|
309 | overlap_size, channels, samplerate, type_pitch, mode_pitch); |
---|
310 | aubio_pitchdetection_set_yinthresh(pitchdet, 0.7); |
---|
311 | |
---|
312 | if (median) { |
---|
313 | note_buffer = new_fvec(median, 1); |
---|
314 | note_buffer2= new_fvec(median, 1); |
---|
315 | } |
---|
316 | } |
---|
317 | /* phase vocoder */ |
---|
318 | pv = new_aubio_pvoc(buffer_size, overlap_size, channels); |
---|
319 | |
---|
320 | /* onsets */ |
---|
321 | parms = new_aubio_peakpicker(threshold); |
---|
322 | o = new_aubio_onsetdetection(type_onset,buffer_size,channels); |
---|
323 | onset = new_fvec(1, channels); |
---|
324 | if (usedoubled) { |
---|
325 | o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels); |
---|
326 | onset2 = new_fvec(1 , channels); |
---|
327 | } |
---|
328 | |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | void examples_common_del(void){ |
---|
333 | if (usepitch) { |
---|
334 | send_noteon(curnote,0); |
---|
335 | del_aubio_pitchdetection(pitchdet); |
---|
336 | if (median) { |
---|
337 | del_fvec(note_buffer); |
---|
338 | del_fvec(note_buffer2); |
---|
339 | } |
---|
340 | } |
---|
341 | if (usedoubled) { |
---|
342 | del_aubio_onsetdetection(o2); |
---|
343 | del_fvec(onset2); |
---|
344 | } |
---|
345 | del_aubio_onsetdetection(o); |
---|
346 | del_aubio_peakpicker(parms); |
---|
347 | del_aubio_pvoc(pv); |
---|
348 | del_fvec(obuf); |
---|
349 | del_fvec(ibuf); |
---|
350 | del_cvec(fftgrain); |
---|
351 | del_fvec(onset); |
---|
352 | del_fvec(woodblock); |
---|
353 | |
---|
354 | aubio_cleanup(); |
---|
355 | } |
---|
356 | |
---|
357 | void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){ |
---|
358 | if(usejack) { |
---|
359 | #if JACK_SUPPORT |
---|
360 | aubio_jack_t * jack_setup; |
---|
361 | debug("Jack init ...\n"); |
---|
362 | jack_setup = new_aubio_jack(channels, channels, |
---|
363 | (aubio_process_func_t)process_func); |
---|
364 | if (usepitch) { |
---|
365 | debug("Midi init ...\n"); |
---|
366 | mplay = new_aubio_midi_player(); |
---|
367 | mdriver = new_aubio_midi_driver("alsa_seq", |
---|
368 | (handle_midi_event_func_t)aubio_midi_send_event, mplay); |
---|
369 | event = new_aubio_midi_event(); |
---|
370 | } |
---|
371 | debug("Jack activation ...\n"); |
---|
372 | aubio_jack_activate(jack_setup); |
---|
373 | debug("Processing (Ctrl+C to quit) ...\n"); |
---|
374 | pause(); |
---|
375 | aubio_jack_close(jack_setup); |
---|
376 | if (usepitch) { |
---|
377 | send_noteon(curnote,0); |
---|
378 | del_aubio_midi_driver(mdriver); |
---|
379 | } |
---|
380 | #else |
---|
381 | usage(stderr, 1); |
---|
382 | outmsg("Compiled without jack output, exiting.\n"); |
---|
383 | #endif |
---|
384 | |
---|
385 | } else { |
---|
386 | /* phasevoc */ |
---|
387 | debug("Processing ...\n"); |
---|
388 | |
---|
389 | frames = 0; |
---|
390 | |
---|
391 | while ((signed)overlap_size == aubio_sndfile_read(file, overlap_size, ibuf)) |
---|
392 | { |
---|
393 | isonset=0; |
---|
394 | process_func(ibuf->data, obuf->data, overlap_size); |
---|
395 | print(); |
---|
396 | if (output_filename != NULL) { |
---|
397 | aubio_sndfile_write(fileout,overlap_size,obuf); |
---|
398 | } |
---|
399 | frames++; |
---|
400 | } |
---|
401 | |
---|
402 | debug("Processed %d frames of %d samples.\n", frames, buffer_size); |
---|
403 | del_aubio_sndfile(file); |
---|
404 | |
---|
405 | if (output_filename != NULL) |
---|
406 | del_aubio_sndfile(fileout); |
---|
407 | |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | |
---|
413 | void send_noteon(int pitch, int velo) |
---|
414 | { |
---|
415 | smpl_t mpitch = floor(aubio_freqtomidi(pitch)+.5); |
---|
416 | /* we should check if we use midi here, not jack */ |
---|
417 | #if ALSA_SUPPORT |
---|
418 | if (usejack) { |
---|
419 | if (velo==0) { |
---|
420 | aubio_midi_event_set_type(event,NOTE_OFF); |
---|
421 | } else { |
---|
422 | aubio_midi_event_set_type(event,NOTE_ON); |
---|
423 | } |
---|
424 | aubio_midi_event_set_channel(event,0); |
---|
425 | aubio_midi_event_set_pitch(event,mpitch); |
---|
426 | aubio_midi_event_set_velocity(event,velo); |
---|
427 | aubio_midi_direct_output(mdriver,event); |
---|
428 | } else |
---|
429 | #endif |
---|
430 | if (!verbose) |
---|
431 | { |
---|
432 | if (velo==0) { |
---|
433 | outmsg("%f\n",frames*overlap_size/(float)samplerate); |
---|
434 | } else { |
---|
435 | outmsg("%f\t%f\t", mpitch, |
---|
436 | frames*overlap_size/(float)samplerate); |
---|
437 | } |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | |
---|
442 | void note_append(fvec_t * note_buffer, smpl_t curnote) { |
---|
443 | uint_t i = 0; |
---|
444 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
445 | note_buffer->data[0][i] = note_buffer->data[0][i+1]; |
---|
446 | } |
---|
447 | note_buffer->data[0][note_buffer->length - 1] = curnote; |
---|
448 | return; |
---|
449 | } |
---|
450 | |
---|
451 | uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){ |
---|
452 | uint_t i = 0; |
---|
453 | for (i = 0; i < note_buffer->length; i++) { |
---|
454 | note_buffer2->data[0][i] = note_buffer->data[0][i]; |
---|
455 | } |
---|
456 | return vec_median(note_buffer2); |
---|
457 | } |
---|
458 | |
---|
459 | #if LASH_SUPPORT |
---|
460 | |
---|
461 | void * lash_thread_main(void *data __attribute__((unused))) |
---|
462 | { |
---|
463 | printf("LASH thread running\n"); |
---|
464 | |
---|
465 | while (!lash_main()) |
---|
466 | usleep(1000); |
---|
467 | |
---|
468 | printf("LASH thread finished\n"); |
---|
469 | return NULL; |
---|
470 | } |
---|
471 | |
---|
472 | int lash_main(void) { |
---|
473 | lash_event_t *lash_event; |
---|
474 | lash_config_t *lash_config; |
---|
475 | |
---|
476 | while ((lash_event = lash_get_event(aubio_lash_client))) { |
---|
477 | switch (lash_event_get_type(lash_event)) { |
---|
478 | case LASH_Quit: |
---|
479 | lash_event_destroy(lash_event); |
---|
480 | exit(1); |
---|
481 | return 1; |
---|
482 | case LASH_Restore_Data_Set: |
---|
483 | lash_send_event(aubio_lash_client, lash_event); |
---|
484 | break; |
---|
485 | case LASH_Save_Data_Set: |
---|
486 | save_data(); |
---|
487 | lash_send_event(aubio_lash_client, lash_event); |
---|
488 | break; |
---|
489 | case LASH_Server_Lost: |
---|
490 | return 1; |
---|
491 | default: |
---|
492 | printf("%s: received unknown LASH event of type %d", |
---|
493 | __FUNCTION__, lash_event_get_type(lash_event)); |
---|
494 | lash_event_destroy(lash_event); |
---|
495 | break; |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | while ((lash_config = lash_get_config(aubio_lash_client))) { |
---|
500 | restore_data(lash_config); |
---|
501 | lash_config_destroy(lash_config); |
---|
502 | } |
---|
503 | |
---|
504 | return 0; |
---|
505 | } |
---|
506 | |
---|
507 | void save_data() { |
---|
508 | lash_config_t *lash_config; |
---|
509 | |
---|
510 | lash_config = lash_config_new_with_key("threshold"); |
---|
511 | lash_config_set_value_double(lash_config, threshold); |
---|
512 | lash_send_config(aubio_lash_client, lash_config); |
---|
513 | |
---|
514 | } |
---|
515 | |
---|
516 | void restore_data(lash_config_t * lash_config) { |
---|
517 | const char *lash_key; |
---|
518 | |
---|
519 | lash_key = lash_config_get_key(lash_config); |
---|
520 | |
---|
521 | if (strcmp(lash_key, "threshold") == 0) { |
---|
522 | threshold = lash_config_get_value_double(lash_config); |
---|
523 | return; |
---|
524 | } |
---|
525 | |
---|
526 | } |
---|
527 | |
---|
528 | #endif /* LASH_SUPPORT */ |
---|
529 | |
---|