[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 | |
---|
[b49daf6] | 15 | /* not supported yet */ |
---|
[96fb8ad] | 16 | #ifdef LADCCA_SUPPORT |
---|
| 17 | #include <ladcca/ladcca.h> |
---|
| 18 | cca_client_t * aubio_cca_client; |
---|
| 19 | #endif /* LADCCA_SUPPORT */ |
---|
| 20 | |
---|
[bd2f2ab] | 21 | /* settings */ |
---|
| 22 | const char * output_filename = NULL; |
---|
| 23 | const char * input_filename = NULL; |
---|
[6b233fc] | 24 | const char * onset_filename = AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff"; |
---|
[bd2f2ab] | 25 | int frames = 0; |
---|
| 26 | int verbose = 0; |
---|
| 27 | int usejack = 0; |
---|
| 28 | int usedoubled = 1; |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | /* energy,specdiff,hfc,complexdomain,phase */ |
---|
[5cf415f] | 32 | aubio_onsetdetection_type type_onset = aubio_onset_kl; |
---|
| 33 | aubio_onsetdetection_type type_onset2 = aubio_onset_complex; |
---|
[bd2f2ab] | 34 | smpl_t threshold = 0.3; |
---|
| 35 | smpl_t threshold2 = -90.; |
---|
[2d7b65a] | 36 | uint_t buffer_size = 512; //1024; |
---|
| 37 | uint_t overlap_size = 256; //512; |
---|
[bd2f2ab] | 38 | uint_t channels = 1; |
---|
| 39 | uint_t samplerate = 44100; |
---|
| 40 | |
---|
| 41 | |
---|
[5e9c68a] | 42 | aubio_sndfile_t * file = NULL; |
---|
| 43 | aubio_sndfile_t * fileout = NULL; |
---|
[bd2f2ab] | 44 | |
---|
| 45 | aubio_pvoc_t * pv; |
---|
| 46 | fvec_t * ibuf; |
---|
| 47 | fvec_t * obuf; |
---|
| 48 | cvec_t * fftgrain; |
---|
| 49 | fvec_t * woodblock; |
---|
| 50 | aubio_onsetdetection_t *o; |
---|
| 51 | aubio_onsetdetection_t *o2; |
---|
| 52 | fvec_t *onset; |
---|
| 53 | fvec_t *onset2; |
---|
| 54 | int isonset = 0; |
---|
| 55 | aubio_pickpeak_t * parms; |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | /* pitch objects */ |
---|
| 59 | smpl_t pitch = 0.; |
---|
| 60 | aubio_pitchdetection_t * pitchdet; |
---|
[fb615eb] | 61 | aubio_pitchdetection_type type_pitch = aubio_pitch_schmitt; // aubio_pitch_mcomb |
---|
| 62 | aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq; |
---|
[bd2f2ab] | 63 | uint_t median = 6; |
---|
| 64 | |
---|
| 65 | fvec_t * note_buffer = NULL; |
---|
| 66 | fvec_t * note_buffer2 = NULL; |
---|
| 67 | smpl_t curlevel = 0.; |
---|
| 68 | smpl_t maxonset = 0.; |
---|
| 69 | |
---|
| 70 | /* midi objects */ |
---|
| 71 | aubio_midi_player_t * mplay; |
---|
| 72 | aubio_midi_driver_t * mdriver; |
---|
| 73 | aubio_midi_event_t * event; |
---|
| 74 | |
---|
| 75 | smpl_t curnote = 0.; |
---|
| 76 | smpl_t newnote = 0.; |
---|
| 77 | uint_t isready = 0; |
---|
| 78 | |
---|
| 79 | |
---|
[96fb8ad] | 80 | |
---|
| 81 | /* badly redeclare some things */ |
---|
| 82 | aubio_onsetdetection_type type_onset; |
---|
| 83 | smpl_t threshold; |
---|
| 84 | smpl_t averaging; |
---|
| 85 | const char * prog_name; |
---|
| 86 | |
---|
| 87 | void usage (FILE * stream, int exit_code) |
---|
| 88 | { |
---|
[ee0cc27b] | 89 | fprintf(stream, "usage: %s [ options ] \n", prog_name); |
---|
| 90 | fprintf(stream, |
---|
[fb615eb] | 91 | " -h --help Display this message.\n" |
---|
| 92 | " -j --jack Use Jack.\n" |
---|
| 93 | " -o --output Output type.\n" |
---|
| 94 | " -i --input Input type.\n" |
---|
| 95 | " -O --onset Select onset detection algorithm.\n" |
---|
| 96 | " -t --threshold Set onset detection threshold.\n" |
---|
| 97 | " -s --silence Select silence threshold.\n" |
---|
| 98 | " -p --pitch Select pitch detection algorithm.\n" |
---|
| 99 | " -H --hopsize Set hopsize.\n" |
---|
| 100 | " -a --averaging Use averaging.\n" |
---|
[ee0cc27b] | 101 | ); |
---|
| 102 | exit(exit_code); |
---|
[96fb8ad] | 103 | } |
---|
| 104 | |
---|
| 105 | int parse_args (int argc, char **argv) { |
---|
[fb615eb] | 106 | const char *options = "hvjo:i:O:t:s:p:H:a"; |
---|
[ee0cc27b] | 107 | int next_option; |
---|
| 108 | struct option long_options[] = |
---|
| 109 | { |
---|
| 110 | {"help" , 0, NULL, 'h'}, |
---|
| 111 | {"verbose" , 0, NULL, 'v'}, |
---|
| 112 | {"jack" , 0, NULL, 'j'}, |
---|
[13d57e8] | 113 | {"output" , 1, NULL, 'o'}, |
---|
| 114 | {"input" , 1, NULL, 'i'}, |
---|
| 115 | {"onset" , 1, NULL, 'O'}, |
---|
| 116 | {"threshold", 1, NULL, 't'}, |
---|
| 117 | {"silence" , 1, NULL, 's'}, |
---|
[fb615eb] | 118 | {"pitch" , 1, NULL, 'p'}, |
---|
[ee0cc27b] | 119 | {"averaging", 0, NULL, 'a'}, |
---|
[13d57e8] | 120 | {"hopsize", 1, NULL, 'H'}, |
---|
[ee0cc27b] | 121 | {NULL , 0, NULL, 0} |
---|
| 122 | }; |
---|
[fb615eb] | 123 | prog_name = argv[0]; |
---|
[ee0cc27b] | 124 | if( argc < 1 ) { |
---|
| 125 | usage (stderr, 1); |
---|
| 126 | return -1; |
---|
[96fb8ad] | 127 | } |
---|
[ee0cc27b] | 128 | do { |
---|
| 129 | next_option = getopt_long (argc, argv, options, |
---|
| 130 | long_options, NULL); |
---|
| 131 | switch (next_option) { |
---|
| 132 | case 'o': |
---|
| 133 | output_filename = optarg; |
---|
| 134 | break; |
---|
| 135 | case 'i': |
---|
| 136 | input_filename = optarg; |
---|
| 137 | break; |
---|
[fb615eb] | 138 | case 'h': /* help */ |
---|
[ee0cc27b] | 139 | usage (stdout, 0); |
---|
| 140 | return -1; |
---|
[fb615eb] | 141 | case 'v': /* verbose */ |
---|
[ee0cc27b] | 142 | verbose = 1; |
---|
| 143 | break; |
---|
[fb615eb] | 144 | case 'j': |
---|
[ee0cc27b] | 145 | usejack = 1; |
---|
| 146 | break; |
---|
| 147 | case 'O': /*onset type*/ |
---|
| 148 | if (strcmp(optarg,"energy") == 0) |
---|
[5cf415f] | 149 | type_onset = aubio_onset_energy; |
---|
[ee0cc27b] | 150 | else if (strcmp(optarg,"specdiff") == 0) |
---|
[5cf415f] | 151 | type_onset = aubio_onset_specdiff; |
---|
[ee0cc27b] | 152 | else if (strcmp(optarg,"hfc") == 0) |
---|
[5cf415f] | 153 | type_onset = aubio_onset_hfc; |
---|
[ee0cc27b] | 154 | else if (strcmp(optarg,"complexdomain") == 0) |
---|
[5cf415f] | 155 | type_onset = aubio_onset_complex; |
---|
| 156 | else if (strcmp(optarg,"complex") == 0) |
---|
| 157 | type_onset = aubio_onset_complex; |
---|
[ee0cc27b] | 158 | else if (strcmp(optarg,"phase") == 0) |
---|
[5cf415f] | 159 | type_onset = aubio_onset_phase; |
---|
| 160 | else if (strcmp(optarg,"mkl") == 0) |
---|
| 161 | type_onset = aubio_onset_mkl; |
---|
| 162 | else if (strcmp(optarg,"kl") == 0) |
---|
| 163 | type_onset = aubio_onset_kl; |
---|
[ee0cc27b] | 164 | else { |
---|
[92e4028] | 165 | errmsg("unknown onset type.\n"); |
---|
[ee0cc27b] | 166 | abort(); |
---|
| 167 | } |
---|
| 168 | usedoubled = 0; |
---|
| 169 | break; |
---|
| 170 | case 's': /* threshold value for onset */ |
---|
| 171 | threshold2 = (smpl_t)atof(optarg); |
---|
| 172 | break; |
---|
| 173 | case 't': /* threshold value for onset */ |
---|
| 174 | threshold = (smpl_t)atof(optarg); |
---|
| 175 | /* |
---|
| 176 | if (!isfinite(threshold)) { |
---|
| 177 | debug("could not get threshold.\n"); |
---|
| 178 | abort(); |
---|
| 179 | } |
---|
| 180 | */ |
---|
| 181 | break; |
---|
[fb615eb] | 182 | case 'p': |
---|
| 183 | if (strcmp(optarg,"mcomb") == 0) |
---|
| 184 | type_pitch = aubio_pitch_mcomb; |
---|
| 185 | else if (strcmp(optarg,"yin") == 0) |
---|
| 186 | type_pitch = aubio_pitch_yin; |
---|
| 187 | else if (strcmp(optarg,"schmitt") == 0) |
---|
| 188 | type_pitch = aubio_pitch_schmitt; |
---|
| 189 | else if (strcmp(optarg,"fcomb") == 0) |
---|
| 190 | type_pitch = aubio_pitch_fcomb; |
---|
| 191 | else { |
---|
[92e4028] | 192 | errmsg("unknown pitch type.\n"); |
---|
[fb615eb] | 193 | abort(); |
---|
| 194 | } |
---|
| 195 | break; |
---|
[ee0cc27b] | 196 | case 'a': |
---|
| 197 | averaging = 1; |
---|
| 198 | break; |
---|
[0ce9acc3] | 199 | case 'H': |
---|
| 200 | overlap_size = atoi(optarg); |
---|
| 201 | break; |
---|
[fb615eb] | 202 | case '?': /* unknown options */ |
---|
[ee0cc27b] | 203 | usage(stderr, 1); |
---|
| 204 | break; |
---|
[fb615eb] | 205 | case -1: /* done with options */ |
---|
[ee0cc27b] | 206 | break; |
---|
[fb615eb] | 207 | default: /*something else unexpected */ |
---|
[ee0cc27b] | 208 | abort (); |
---|
| 209 | } |
---|
[96fb8ad] | 210 | } |
---|
[ee0cc27b] | 211 | while (next_option != -1); |
---|
| 212 | |
---|
| 213 | if (input_filename != NULL) { |
---|
| 214 | debug ("Input file : %s\n", input_filename ); |
---|
| 215 | } else if (input_filename != NULL && output_filename != NULL) { |
---|
| 216 | debug ("Input file : %s\n", input_filename ); |
---|
| 217 | debug ("Output file : %s\n", output_filename ); |
---|
| 218 | } else { |
---|
| 219 | if (JACK_SUPPORT) |
---|
| 220 | { |
---|
| 221 | debug ("Jack input output\n"); |
---|
| 222 | usejack = 1; |
---|
| 223 | } else { |
---|
| 224 | debug ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n"); |
---|
| 225 | exit(1); |
---|
| 226 | } |
---|
[fb615eb] | 227 | } |
---|
[ee0cc27b] | 228 | return 0; |
---|
[96fb8ad] | 229 | } |
---|
| 230 | |
---|
[bd2f2ab] | 231 | void examples_common_init(int argc,char ** argv) { |
---|
| 232 | |
---|
| 233 | |
---|
[b1f723d] | 234 | aubio_sndfile_t * onsetfile = NULL; |
---|
[bd2f2ab] | 235 | /* parse command line arguments */ |
---|
| 236 | parse_args(argc, argv); |
---|
| 237 | |
---|
[f382ac6] | 238 | woodblock = new_fvec(buffer_size,1); |
---|
| 239 | if (output_filename || usejack) { |
---|
[6b233fc] | 240 | /* dummy assignement to keep egcs happy */ |
---|
| 241 | isonset = (onsetfile = new_aubio_sndfile_ro(onset_filename)) || |
---|
[5e9c68a] | 242 | (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) || |
---|
| 243 | (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff")); |
---|
[b1f723d] | 244 | } |
---|
| 245 | if (onsetfile) { |
---|
[f382ac6] | 246 | /* read the output sound once */ |
---|
[5e9c68a] | 247 | aubio_sndfile_read(onsetfile, overlap_size, woodblock); |
---|
[f382ac6] | 248 | } |
---|
| 249 | |
---|
[bd2f2ab] | 250 | if(!usejack) |
---|
| 251 | { |
---|
| 252 | debug("Opening files ...\n"); |
---|
[5e9c68a] | 253 | file = new_aubio_sndfile_ro (input_filename); |
---|
| 254 | if (verbose) aubio_sndfile_info(file); |
---|
| 255 | channels = aubio_sndfile_channels(file); |
---|
| 256 | samplerate = aubio_sndfile_samplerate(file); |
---|
[bd2f2ab] | 257 | if (output_filename != NULL) |
---|
[5e9c68a] | 258 | fileout = new_aubio_sndfile_wo(file, output_filename); |
---|
[bd2f2ab] | 259 | } |
---|
| 260 | |
---|
| 261 | ibuf = new_fvec(overlap_size, channels); |
---|
| 262 | obuf = new_fvec(overlap_size, channels); |
---|
| 263 | fftgrain = new_cvec(buffer_size, channels); |
---|
| 264 | |
---|
| 265 | if (usepitch) { |
---|
[edca23f] | 266 | pitchdet = new_aubio_pitchdetection(buffer_size*4, |
---|
[fb615eb] | 267 | overlap_size, channels, samplerate, type_pitch, mode_pitch); |
---|
[bd2f2ab] | 268 | |
---|
| 269 | if (median) { |
---|
| 270 | note_buffer = new_fvec(median, 1); |
---|
| 271 | note_buffer2= new_fvec(median, 1); |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | /* phase vocoder */ |
---|
| 275 | pv = new_aubio_pvoc(buffer_size, overlap_size, channels); |
---|
| 276 | /* onsets */ |
---|
| 277 | parms = new_aubio_peakpicker(threshold); |
---|
| 278 | o = new_aubio_onsetdetection(type_onset,buffer_size,channels); |
---|
| 279 | onset = new_fvec(1, channels); |
---|
| 280 | if (usedoubled) { |
---|
| 281 | o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels); |
---|
| 282 | onset2 = new_fvec(1 , channels); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | void examples_common_del(void){ |
---|
| 289 | if (usepitch) { |
---|
| 290 | send_noteon(curnote,0); |
---|
| 291 | del_aubio_pitchdetection(pitchdet); |
---|
| 292 | if (median) { |
---|
| 293 | del_fvec(note_buffer); |
---|
| 294 | del_fvec(note_buffer2); |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | del_aubio_pvoc(pv); |
---|
| 298 | del_fvec(obuf); |
---|
| 299 | del_fvec(ibuf); |
---|
| 300 | del_cvec(fftgrain); |
---|
| 301 | del_fvec(onset); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){ |
---|
| 305 | if(usejack) { |
---|
[5a1ff62] | 306 | #if JACK_SUPPORT |
---|
[bd2f2ab] | 307 | aubio_jack_t * jack_setup; |
---|
| 308 | debug("Jack init ...\n"); |
---|
| 309 | jack_setup = new_aubio_jack(channels, channels, |
---|
| 310 | (aubio_process_func_t)process_func); |
---|
| 311 | if (usepitch) { |
---|
| 312 | debug("Midi init ...\n"); |
---|
| 313 | mplay = new_aubio_midi_player(); |
---|
| 314 | mdriver = new_aubio_midi_driver("alsa_seq", |
---|
| 315 | (handle_midi_event_func_t)aubio_midi_send_event, mplay); |
---|
| 316 | event = new_aubio_midi_event(); |
---|
| 317 | } |
---|
| 318 | debug("Jack activation ...\n"); |
---|
| 319 | aubio_jack_activate(jack_setup); |
---|
| 320 | debug("Processing (Ctrl+C to quit) ...\n"); |
---|
| 321 | pause(); |
---|
| 322 | aubio_jack_close(jack_setup); |
---|
| 323 | if (usepitch) { |
---|
| 324 | send_noteon(curnote,0); |
---|
| 325 | del_aubio_midi_driver(mdriver); |
---|
| 326 | } |
---|
| 327 | #else |
---|
| 328 | usage(stderr, 1); |
---|
| 329 | outmsg("Compiled without jack output, exiting.\n"); |
---|
| 330 | #endif |
---|
| 331 | |
---|
| 332 | } else { |
---|
| 333 | /* phasevoc */ |
---|
| 334 | debug("Processing ...\n"); |
---|
| 335 | |
---|
| 336 | frames = 0; |
---|
| 337 | |
---|
[5e9c68a] | 338 | while (overlap_size == aubio_sndfile_read(file, overlap_size, ibuf)) |
---|
[bd2f2ab] | 339 | { |
---|
| 340 | isonset=0; |
---|
| 341 | process_func(ibuf->data, obuf->data, overlap_size); |
---|
| 342 | print(); |
---|
| 343 | if (output_filename != NULL) { |
---|
[5e9c68a] | 344 | aubio_sndfile_write(fileout,overlap_size,obuf); |
---|
[bd2f2ab] | 345 | } |
---|
| 346 | frames++; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | debug("Processed %d frames of %d samples.\n", frames, buffer_size); |
---|
[5e9c68a] | 350 | del_aubio_sndfile(file); |
---|
[bd2f2ab] | 351 | |
---|
| 352 | if (output_filename != NULL) |
---|
[5e9c68a] | 353 | del_aubio_sndfile(fileout); |
---|
[bd2f2ab] | 354 | |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | |
---|
| 359 | |
---|
| 360 | void send_noteon(int pitch, int velo) |
---|
| 361 | { |
---|
[28d8c4a] | 362 | smpl_t mpitch = (FLOOR)(aubio_freqtomidi(pitch)+.5); |
---|
[bd2f2ab] | 363 | /* we should check if we use midi here, not jack */ |
---|
| 364 | #if ALSA_SUPPORT |
---|
| 365 | if (usejack) { |
---|
| 366 | if (velo==0) { |
---|
| 367 | aubio_midi_event_set_type(event,NOTE_OFF); |
---|
| 368 | } else { |
---|
| 369 | aubio_midi_event_set_type(event,NOTE_ON); |
---|
| 370 | } |
---|
| 371 | aubio_midi_event_set_channel(event,0); |
---|
| 372 | aubio_midi_event_set_pitch(event,mpitch); |
---|
| 373 | aubio_midi_event_set_velocity(event,velo); |
---|
| 374 | aubio_midi_direct_output(mdriver,event); |
---|
| 375 | } else |
---|
| 376 | #endif |
---|
| 377 | if (!verbose) |
---|
| 378 | { |
---|
| 379 | if (velo==0) { |
---|
| 380 | outmsg("%f\n",frames*overlap_size/(float)samplerate); |
---|
| 381 | } else { |
---|
| 382 | outmsg("%f\t%f\t", mpitch, |
---|
| 383 | frames*overlap_size/(float)samplerate); |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | |
---|
| 389 | void note_append(fvec_t * note_buffer, smpl_t curnote) { |
---|
| 390 | uint_t i = 0; |
---|
| 391 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
| 392 | note_buffer->data[0][i] = note_buffer->data[0][i+1]; |
---|
| 393 | } |
---|
| 394 | note_buffer->data[0][note_buffer->length - 1] = curnote; |
---|
| 395 | return; |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){ |
---|
| 399 | uint_t i = 0; |
---|
| 400 | for (i = 0; i < note_buffer->length; i++) { |
---|
| 401 | note_buffer2->data[0][i] = note_buffer->data[0][i]; |
---|
| 402 | } |
---|
| 403 | return vec_median(note_buffer2); |
---|
| 404 | } |
---|
| 405 | |
---|