[50bc5f2] | 1 | /* |
---|
| 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[50bc5f2] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[50bc5f2] | 6 | aubio is free software: you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | (at your option) any later version. |
---|
| 10 | |
---|
| 11 | aubio is distributed in the hope that it will be useful, |
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | GNU General Public License for more details. |
---|
| 15 | |
---|
| 16 | You should have received a copy of the GNU General Public License |
---|
| 17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | |
---|
| 19 | */ |
---|
[96fb8ad] | 20 | |
---|
[9430dfd] | 21 | /** |
---|
[d4c5de7] | 22 | |
---|
| 23 | This file includes some tools common to all examples. Code specific to the |
---|
| 24 | algorithm performed by each program should go in the source file of that |
---|
| 25 | program instead. |
---|
| 26 | |
---|
| 27 | */ |
---|
| 28 | |
---|
[96fb8ad] | 29 | #include "utils.h" |
---|
| 30 | |
---|
[b511fa9] | 31 | #ifdef HAVE_LASH |
---|
[71482a9] | 32 | #include <lash/lash.h> |
---|
| 33 | #include <pthread.h> |
---|
[50bc5f2] | 34 | lash_client_t *aubio_lash_client; |
---|
| 35 | lash_args_t *lash_args; |
---|
| 36 | void *lash_thread_main (void *data); |
---|
[71482a9] | 37 | int lash_main (void); |
---|
| 38 | void save_data (void); |
---|
[50bc5f2] | 39 | void restore_data (lash_config_t * lash_config); |
---|
[71482a9] | 40 | pthread_t lash_thread; |
---|
[b511fa9] | 41 | #endif /* HAVE_LASH */ |
---|
[96fb8ad] | 42 | |
---|
[bd2f2ab] | 43 | /* settings */ |
---|
[5a66677] | 44 | const char *sink_uri = NULL; |
---|
| 45 | const char *source_uri = NULL; |
---|
[bd2f2ab] | 46 | int frames = 0; |
---|
| 47 | int verbose = 0; |
---|
| 48 | int usejack = 0; |
---|
[e24378a] | 49 | int frames_delay = 0; |
---|
[bd2f2ab] | 50 | |
---|
| 51 | |
---|
[d4c5de7] | 52 | char_t * pitch_unit = "default"; |
---|
| 53 | char_t * pitch_mode = "default"; |
---|
| 54 | |
---|
[bd2f2ab] | 55 | /* energy,specdiff,hfc,complexdomain,phase */ |
---|
[b4f5967] | 56 | char_t * onset_mode = "default"; |
---|
[40acfbc] | 57 | smpl_t threshold = 0.0; // leave unset, only set as asked |
---|
[50bc5f2] | 58 | smpl_t silence = -90.; |
---|
| 59 | uint_t buffer_size = 512; //1024; |
---|
| 60 | uint_t overlap_size = 256; //512; |
---|
| 61 | uint_t samplerate = 44100; |
---|
| 62 | |
---|
[5a66677] | 63 | aubio_source_t *this_source = NULL; |
---|
| 64 | aubio_sink_t *this_sink = NULL; |
---|
[50bc5f2] | 65 | |
---|
| 66 | fvec_t *ibuf; |
---|
| 67 | fvec_t *obuf; |
---|
[96fb8ad] | 68 | |
---|
| 69 | /* badly redeclare some things */ |
---|
| 70 | smpl_t threshold; |
---|
| 71 | smpl_t averaging; |
---|
[50bc5f2] | 72 | const char *prog_name; |
---|
[96fb8ad] | 73 | |
---|
[d4c5de7] | 74 | void flush_process (aubio_process_func_t process_func, |
---|
| 75 | aubio_print_func_t print); |
---|
| 76 | |
---|
[50bc5f2] | 77 | void |
---|
| 78 | usage (FILE * stream, int exit_code) |
---|
[96fb8ad] | 79 | { |
---|
[50bc5f2] | 80 | fprintf (stream, "usage: %s [ options ] \n", prog_name); |
---|
| 81 | fprintf (stream, |
---|
| 82 | " -h --help Display this message.\n" |
---|
| 83 | " -v --verbose Be verbose.\n" |
---|
| 84 | " -j --jack Use Jack.\n" |
---|
| 85 | " -o --output Output type.\n" |
---|
| 86 | " -i --input Input type.\n" |
---|
| 87 | " -O --onset Select onset detection algorithm.\n" |
---|
| 88 | " -t --threshold Set onset detection threshold.\n" |
---|
| 89 | " -s --silence Select silence threshold.\n" |
---|
| 90 | " -p --pitch Select pitch detection algorithm.\n" |
---|
| 91 | " -B --bufsize Set buffer size.\n" |
---|
| 92 | " -H --hopsize Set hopsize.\n" |
---|
| 93 | " -a --averaging Use averaging.\n"); |
---|
| 94 | exit (exit_code); |
---|
[96fb8ad] | 95 | } |
---|
| 96 | |
---|
[50bc5f2] | 97 | int |
---|
| 98 | parse_args (int argc, char **argv) |
---|
| 99 | { |
---|
| 100 | const char *options = "hvjo:i:O:t:s:p:B:H:a"; |
---|
| 101 | int next_option; |
---|
| 102 | struct option long_options[] = { |
---|
| 103 | {"help", 0, NULL, 'h'}, |
---|
| 104 | {"verbose", 0, NULL, 'v'}, |
---|
| 105 | {"jack", 0, NULL, 'j'}, |
---|
| 106 | {"output", 1, NULL, 'o'}, |
---|
| 107 | {"input", 1, NULL, 'i'}, |
---|
| 108 | {"onset", 1, NULL, 'O'}, |
---|
| 109 | {"threshold", 1, NULL, 't'}, |
---|
| 110 | {"silence", 1, NULL, 's'}, |
---|
| 111 | {"pitch", 1, NULL, 'p'}, |
---|
| 112 | {"averaging", 0, NULL, 'a'}, |
---|
| 113 | {"bufsize", 1, NULL, 'B'}, |
---|
| 114 | {"hopsize", 1, NULL, 'H'}, |
---|
| 115 | {NULL, 0, NULL, 0} |
---|
| 116 | }; |
---|
[b511fa9] | 117 | #ifdef HAVE_LASH |
---|
[50bc5f2] | 118 | lash_args = lash_extract_args (&argc, &argv); |
---|
[b511fa9] | 119 | #endif /* HAVE_LASH */ |
---|
[50bc5f2] | 120 | prog_name = argv[0]; |
---|
| 121 | if (argc < 1) { |
---|
| 122 | usage (stderr, 1); |
---|
| 123 | return -1; |
---|
| 124 | } |
---|
| 125 | do { |
---|
| 126 | next_option = getopt_long (argc, argv, options, long_options, NULL); |
---|
| 127 | switch (next_option) { |
---|
| 128 | case 'o': |
---|
[5a66677] | 129 | sink_uri = optarg; |
---|
[50bc5f2] | 130 | break; |
---|
| 131 | case 'i': |
---|
[5a66677] | 132 | source_uri = optarg; |
---|
[50bc5f2] | 133 | break; |
---|
| 134 | case 'h': /* help */ |
---|
| 135 | usage (stdout, 0); |
---|
| 136 | return -1; |
---|
| 137 | case 'v': /* verbose */ |
---|
| 138 | verbose = 1; |
---|
| 139 | break; |
---|
| 140 | case 'j': |
---|
| 141 | usejack = 1; |
---|
| 142 | break; |
---|
| 143 | case 'O': /*onset type */ |
---|
[b4f5967] | 144 | onset_mode = optarg; |
---|
[50bc5f2] | 145 | break; |
---|
[40acfbc] | 146 | case 's': /* silence value for onset */ |
---|
[50bc5f2] | 147 | silence = (smpl_t) atof (optarg); |
---|
| 148 | break; |
---|
| 149 | case 't': /* threshold value for onset */ |
---|
| 150 | threshold = (smpl_t) atof (optarg); |
---|
| 151 | break; |
---|
| 152 | case 'p': |
---|
[fe163ad] | 153 | pitch_mode = optarg; |
---|
[50bc5f2] | 154 | break; |
---|
| 155 | case 'a': |
---|
| 156 | averaging = 1; |
---|
| 157 | break; |
---|
| 158 | case 'B': |
---|
| 159 | buffer_size = atoi (optarg); |
---|
| 160 | break; |
---|
| 161 | case 'H': |
---|
| 162 | overlap_size = atoi (optarg); |
---|
| 163 | break; |
---|
| 164 | case '?': /* unknown options */ |
---|
| 165 | usage (stderr, 1); |
---|
| 166 | break; |
---|
| 167 | case -1: /* done with options */ |
---|
| 168 | break; |
---|
| 169 | default: /*something else unexpected */ |
---|
| 170 | fprintf (stderr, "Error parsing option '%c'\n", next_option); |
---|
| 171 | abort (); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | while (next_option != -1); |
---|
[71482a9] | 175 | |
---|
[d795463] | 176 | if ( source_uri == NULL ) { |
---|
| 177 | if (argc - optind == 1) { |
---|
| 178 | source_uri = argv[optind]; |
---|
| 179 | } else if ( argc - optind > 1 ) { |
---|
| 180 | errmsg ("Error: too many non-option arguments `%s'\n", argv[argc - 1]); |
---|
| 181 | usage ( stderr, 1 ); |
---|
| 182 | } |
---|
| 183 | } else if ( argc - optind > 0 ) { |
---|
| 184 | errmsg ("Error: extra non-option argument %s\n", argv[optind]); |
---|
| 185 | usage ( stderr, 1 ); |
---|
| 186 | } |
---|
| 187 | |
---|
[5a66677] | 188 | if (source_uri != NULL) { |
---|
| 189 | debug ("Input file : %s\n", source_uri); |
---|
| 190 | } else if (source_uri != NULL && sink_uri != NULL) { |
---|
| 191 | debug ("Input file : %s\n", source_uri); |
---|
| 192 | debug ("Output file : %s\n", sink_uri); |
---|
[50bc5f2] | 193 | } else { |
---|
[ebbf5a0] | 194 | #if HAVE_JACK |
---|
| 195 | debug ("Jack input output\n"); |
---|
| 196 | usejack = 1; |
---|
| 197 | #else |
---|
[a4bf052] | 198 | errmsg("Error: no arguments given (and no available audio input)\n"); |
---|
| 199 | usage ( stderr, 1 ); |
---|
[ebbf5a0] | 200 | #endif |
---|
[50bc5f2] | 201 | } |
---|
| 202 | |
---|
| 203 | return 0; |
---|
[96fb8ad] | 204 | } |
---|
| 205 | |
---|
[50bc5f2] | 206 | void |
---|
| 207 | examples_common_init (int argc, char **argv) |
---|
| 208 | { |
---|
[bd2f2ab] | 209 | |
---|
| 210 | /* parse command line arguments */ |
---|
[50bc5f2] | 211 | parse_args (argc, argv); |
---|
[bd2f2ab] | 212 | |
---|
[50bc5f2] | 213 | if (!usejack) { |
---|
| 214 | debug ("Opening files ...\n"); |
---|
[98874a6] | 215 | this_source = new_aubio_source ((char_t*)source_uri, 0, overlap_size); |
---|
[5a66677] | 216 | if (this_source == NULL) { |
---|
| 217 | outmsg ("Could not open input file %s.\n", source_uri); |
---|
[50bc5f2] | 218 | exit (1); |
---|
[9af07aa] | 219 | } |
---|
[98874a6] | 220 | samplerate = aubio_source_get_samplerate(this_source); |
---|
[5a66677] | 221 | if (sink_uri != NULL) { |
---|
| 222 | this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate); |
---|
[88fc249] | 223 | if (this_sink == NULL) { |
---|
| 224 | outmsg ("Could not open output file %s.\n", sink_uri); |
---|
| 225 | exit (1); |
---|
| 226 | } |
---|
[5a66677] | 227 | } |
---|
[bd2f2ab] | 228 | } |
---|
[b511fa9] | 229 | #ifdef HAVE_LASH |
---|
[71482a9] | 230 | else { |
---|
[50bc5f2] | 231 | aubio_lash_client = lash_init (lash_args, argv[0], |
---|
| 232 | LASH_Config_Data_Set | LASH_Terminal, LASH_PROTOCOL (2, 0)); |
---|
[71482a9] | 233 | if (!aubio_lash_client) { |
---|
[50bc5f2] | 234 | fprintf (stderr, "%s: could not initialise lash\n", __FUNCTION__); |
---|
[71482a9] | 235 | } |
---|
| 236 | /* tell the lash server our client id */ |
---|
[50bc5f2] | 237 | if (lash_enabled (aubio_lash_client)) { |
---|
| 238 | lash_event_t *event = |
---|
| 239 | (lash_event_t *) lash_event_new_with_type (LASH_Client_Name); |
---|
| 240 | lash_event_set_string (event, "aubio"); |
---|
| 241 | lash_send_event (aubio_lash_client, event); |
---|
| 242 | pthread_create (&lash_thread, NULL, lash_thread_main, NULL); |
---|
[71482a9] | 243 | } |
---|
| 244 | } |
---|
[b511fa9] | 245 | #endif /* HAVE_LASH */ |
---|
[bd2f2ab] | 246 | |
---|
[4621cd6] | 247 | ibuf = new_fvec (overlap_size); |
---|
| 248 | obuf = new_fvec (overlap_size); |
---|
[bd2f2ab] | 249 | |
---|
| 250 | } |
---|
| 251 | |
---|
[50bc5f2] | 252 | void |
---|
| 253 | examples_common_del (void) |
---|
| 254 | { |
---|
| 255 | del_fvec (ibuf); |
---|
[d4c5de7] | 256 | del_fvec (obuf); |
---|
[50bc5f2] | 257 | aubio_cleanup (); |
---|
[bd2f2ab] | 258 | } |
---|
| 259 | |
---|
[ebbf5a0] | 260 | #if HAVE_JACK |
---|
| 261 | aubio_jack_t *jack_setup; |
---|
| 262 | #endif |
---|
| 263 | |
---|
[50bc5f2] | 264 | void |
---|
| 265 | examples_common_process (aubio_process_func_t process_func, |
---|
| 266 | aubio_print_func_t print) |
---|
| 267 | { |
---|
[5a66677] | 268 | |
---|
| 269 | uint_t read = 0; |
---|
[50bc5f2] | 270 | if (usejack) { |
---|
[ebbf5a0] | 271 | |
---|
[b511fa9] | 272 | #if HAVE_JACK |
---|
[50bc5f2] | 273 | debug ("Jack init ...\n"); |
---|
[4621cd6] | 274 | jack_setup = new_aubio_jack (1, 1, |
---|
[ebbf5a0] | 275 | 0, 1, (aubio_process_func_t) process_func); |
---|
[50bc5f2] | 276 | debug ("Jack activation ...\n"); |
---|
| 277 | aubio_jack_activate (jack_setup); |
---|
| 278 | debug ("Processing (Ctrl+C to quit) ...\n"); |
---|
| 279 | pause (); |
---|
| 280 | aubio_jack_close (jack_setup); |
---|
[bd2f2ab] | 281 | #else |
---|
[50bc5f2] | 282 | usage (stderr, 1); |
---|
| 283 | outmsg ("Compiled without jack output, exiting.\n"); |
---|
[bd2f2ab] | 284 | #endif |
---|
| 285 | |
---|
| 286 | } else { |
---|
| 287 | /* phasevoc */ |
---|
[50bc5f2] | 288 | debug ("Processing ...\n"); |
---|
[bd2f2ab] | 289 | |
---|
| 290 | frames = 0; |
---|
| 291 | |
---|
[5a66677] | 292 | do { |
---|
| 293 | aubio_source_do (this_source, ibuf, &read); |
---|
[4621cd6] | 294 | process_func (&ibuf->data, &obuf->data, overlap_size); |
---|
[50bc5f2] | 295 | print (); |
---|
[5a66677] | 296 | if (this_sink) { |
---|
| 297 | aubio_sink_do (this_sink, obuf, overlap_size); |
---|
[bd2f2ab] | 298 | } |
---|
| 299 | frames++; |
---|
[5a66677] | 300 | } while (read == overlap_size); |
---|
[bd2f2ab] | 301 | |
---|
[50bc5f2] | 302 | debug ("Processed %d frames of %d samples.\n", frames, buffer_size); |
---|
[e24378a] | 303 | |
---|
[50bc5f2] | 304 | flush_process (process_func, print); |
---|
[5a66677] | 305 | del_aubio_source (this_source); |
---|
| 306 | del_aubio_sink (this_sink); |
---|
[bd2f2ab] | 307 | |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | |
---|
[50bc5f2] | 311 | void |
---|
| 312 | flush_process (aubio_process_func_t process_func, aubio_print_func_t print) |
---|
| 313 | { |
---|
[05a3479] | 314 | uint_t i; |
---|
[847b374] | 315 | fvec_zeros(obuf); |
---|
[50bc5f2] | 316 | for (i = 0; (signed) i < frames_delay; i++) { |
---|
[4621cd6] | 317 | process_func (&ibuf->data, &obuf->data, overlap_size); |
---|
[50bc5f2] | 318 | print (); |
---|
[e24378a] | 319 | } |
---|
| 320 | } |
---|
[bd2f2ab] | 321 | |
---|
[50bc5f2] | 322 | void |
---|
| 323 | send_noteon (int pitch, int velo) |
---|
[bd2f2ab] | 324 | { |
---|
[50bc5f2] | 325 | smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5); |
---|
[ebbf5a0] | 326 | #if HAVE_JACK |
---|
| 327 | jack_midi_event_t ev; |
---|
| 328 | ev.size = 3; |
---|
| 329 | ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME |
---|
| 330 | ev.time = 0; |
---|
[50bc5f2] | 331 | if (usejack) { |
---|
[ebbf5a0] | 332 | ev.buffer[2] = velo; |
---|
| 333 | ev.buffer[1] = mpitch; |
---|
[50bc5f2] | 334 | if (velo == 0) { |
---|
[ebbf5a0] | 335 | ev.buffer[0] = 0x80; /* note off */ |
---|
[50bc5f2] | 336 | } else { |
---|
[ebbf5a0] | 337 | ev.buffer[0] = 0x90; /* note on */ |
---|
[50bc5f2] | 338 | } |
---|
[ebbf5a0] | 339 | aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev); |
---|
[50bc5f2] | 340 | } else |
---|
[bd2f2ab] | 341 | #endif |
---|
[50bc5f2] | 342 | if (!verbose) { |
---|
| 343 | if (velo == 0) { |
---|
| 344 | outmsg ("%f\n", frames * overlap_size / (float) samplerate); |
---|
| 345 | } else { |
---|
| 346 | outmsg ("%f\t%f\t", mpitch, frames * overlap_size / (float) samplerate); |
---|
[bd2f2ab] | 347 | } |
---|
[50bc5f2] | 348 | } |
---|
[bd2f2ab] | 349 | } |
---|
| 350 | |
---|
| 351 | |
---|
[b511fa9] | 352 | #if HAVE_LASH |
---|
[71482a9] | 353 | |
---|
[50bc5f2] | 354 | void * |
---|
| 355 | lash_thread_main (void *data __attribute__ ((unused))) |
---|
[71482a9] | 356 | { |
---|
[50bc5f2] | 357 | printf ("LASH thread running\n"); |
---|
[71482a9] | 358 | |
---|
[50bc5f2] | 359 | while (!lash_main ()) |
---|
| 360 | usleep (1000); |
---|
[71482a9] | 361 | |
---|
[50bc5f2] | 362 | printf ("LASH thread finished\n"); |
---|
| 363 | return NULL; |
---|
[71482a9] | 364 | } |
---|
| 365 | |
---|
[50bc5f2] | 366 | int |
---|
| 367 | lash_main (void) |
---|
| 368 | { |
---|
| 369 | lash_event_t *lash_event; |
---|
| 370 | lash_config_t *lash_config; |
---|
| 371 | |
---|
| 372 | while ((lash_event = lash_get_event (aubio_lash_client))) { |
---|
| 373 | switch (lash_event_get_type (lash_event)) { |
---|
| 374 | case LASH_Quit: |
---|
| 375 | lash_event_destroy (lash_event); |
---|
| 376 | exit (1); |
---|
| 377 | return 1; |
---|
| 378 | case LASH_Restore_Data_Set: |
---|
| 379 | lash_send_event (aubio_lash_client, lash_event); |
---|
| 380 | break; |
---|
| 381 | case LASH_Save_Data_Set: |
---|
| 382 | save_data (); |
---|
| 383 | lash_send_event (aubio_lash_client, lash_event); |
---|
| 384 | break; |
---|
| 385 | case LASH_Server_Lost: |
---|
| 386 | return 1; |
---|
| 387 | default: |
---|
| 388 | printf ("%s: received unknown LASH event of type %d", |
---|
| 389 | __FUNCTION__, lash_event_get_type (lash_event)); |
---|
| 390 | lash_event_destroy (lash_event); |
---|
| 391 | break; |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | while ((lash_config = lash_get_config (aubio_lash_client))) { |
---|
| 396 | restore_data (lash_config); |
---|
| 397 | lash_config_destroy (lash_config); |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | return 0; |
---|
[71482a9] | 401 | } |
---|
| 402 | |
---|
[50bc5f2] | 403 | void |
---|
| 404 | save_data () |
---|
| 405 | { |
---|
| 406 | lash_config_t *lash_config; |
---|
[71482a9] | 407 | |
---|
[50bc5f2] | 408 | lash_config = lash_config_new_with_key ("threshold"); |
---|
| 409 | lash_config_set_value_double (lash_config, threshold); |
---|
| 410 | lash_send_config (aubio_lash_client, lash_config); |
---|
[71482a9] | 411 | |
---|
| 412 | } |
---|
| 413 | |
---|
[50bc5f2] | 414 | void |
---|
| 415 | restore_data (lash_config_t * lash_config) |
---|
| 416 | { |
---|
| 417 | const char *lash_key; |
---|
[71482a9] | 418 | |
---|
[50bc5f2] | 419 | lash_key = lash_config_get_key (lash_config); |
---|
[71482a9] | 420 | |
---|
[50bc5f2] | 421 | if (strcmp (lash_key, "threshold") == 0) { |
---|
| 422 | threshold = lash_config_get_value_double (lash_config); |
---|
| 423 | return; |
---|
| 424 | } |
---|
[71482a9] | 425 | |
---|
| 426 | } |
---|
| 427 | |
---|
[b511fa9] | 428 | #endif /* HAVE_LASH */ |
---|