[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 | |
---|
[d4c5de7] | 21 | /** |
---|
| 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 */ |
---|
[50bc5f2] | 44 | const char *output_filename = NULL; |
---|
| 45 | const char *input_filename = NULL; |
---|
| 46 | const char *onset_filename = |
---|
| 47 | AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff"; |
---|
[bd2f2ab] | 48 | int frames = 0; |
---|
| 49 | int verbose = 0; |
---|
| 50 | int usejack = 0; |
---|
[e24378a] | 51 | int frames_delay = 0; |
---|
[bd2f2ab] | 52 | |
---|
| 53 | |
---|
[d4c5de7] | 54 | char_t * pitch_unit = "default"; |
---|
| 55 | char_t * pitch_mode = "default"; |
---|
| 56 | |
---|
[bd2f2ab] | 57 | /* energy,specdiff,hfc,complexdomain,phase */ |
---|
[b4f5967] | 58 | char_t * onset_mode = "default"; |
---|
[40acfbc] | 59 | smpl_t threshold = 0.0; // leave unset, only set as asked |
---|
[50bc5f2] | 60 | smpl_t silence = -90.; |
---|
| 61 | uint_t buffer_size = 512; //1024; |
---|
| 62 | uint_t overlap_size = 256; //512; |
---|
| 63 | uint_t channels = 1; |
---|
| 64 | uint_t samplerate = 44100; |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | aubio_sndfile_t *file = NULL; |
---|
| 68 | aubio_sndfile_t *fileout = NULL; |
---|
| 69 | |
---|
| 70 | fvec_t *ibuf; |
---|
| 71 | fvec_t *obuf; |
---|
| 72 | fvec_t *woodblock; |
---|
[96fb8ad] | 73 | |
---|
| 74 | /* badly redeclare some things */ |
---|
| 75 | smpl_t threshold; |
---|
| 76 | smpl_t averaging; |
---|
[50bc5f2] | 77 | const char *prog_name; |
---|
[96fb8ad] | 78 | |
---|
[d4c5de7] | 79 | void flush_process (aubio_process_func_t process_func, |
---|
| 80 | aubio_print_func_t print); |
---|
| 81 | |
---|
[50bc5f2] | 82 | void |
---|
| 83 | usage (FILE * stream, int exit_code) |
---|
[96fb8ad] | 84 | { |
---|
[50bc5f2] | 85 | fprintf (stream, "usage: %s [ options ] \n", prog_name); |
---|
| 86 | fprintf (stream, |
---|
| 87 | " -h --help Display this message.\n" |
---|
| 88 | " -v --verbose Be verbose.\n" |
---|
| 89 | " -j --jack Use Jack.\n" |
---|
| 90 | " -o --output Output type.\n" |
---|
| 91 | " -i --input Input type.\n" |
---|
| 92 | " -O --onset Select onset detection algorithm.\n" |
---|
| 93 | " -t --threshold Set onset detection threshold.\n" |
---|
| 94 | " -s --silence Select silence threshold.\n" |
---|
| 95 | " -p --pitch Select pitch detection algorithm.\n" |
---|
| 96 | " -B --bufsize Set buffer size.\n" |
---|
| 97 | " -H --hopsize Set hopsize.\n" |
---|
| 98 | " -a --averaging Use averaging.\n"); |
---|
| 99 | exit (exit_code); |
---|
[96fb8ad] | 100 | } |
---|
| 101 | |
---|
[50bc5f2] | 102 | int |
---|
| 103 | parse_args (int argc, char **argv) |
---|
| 104 | { |
---|
| 105 | const char *options = "hvjo:i:O:t:s:p:B:H:a"; |
---|
| 106 | int next_option; |
---|
| 107 | struct option long_options[] = { |
---|
| 108 | {"help", 0, NULL, 'h'}, |
---|
| 109 | {"verbose", 0, NULL, 'v'}, |
---|
| 110 | {"jack", 0, NULL, 'j'}, |
---|
| 111 | {"output", 1, NULL, 'o'}, |
---|
| 112 | {"input", 1, NULL, 'i'}, |
---|
| 113 | {"onset", 1, NULL, 'O'}, |
---|
| 114 | {"threshold", 1, NULL, 't'}, |
---|
| 115 | {"silence", 1, NULL, 's'}, |
---|
| 116 | {"pitch", 1, NULL, 'p'}, |
---|
| 117 | {"averaging", 0, NULL, 'a'}, |
---|
| 118 | {"bufsize", 1, NULL, 'B'}, |
---|
| 119 | {"hopsize", 1, NULL, 'H'}, |
---|
| 120 | {NULL, 0, NULL, 0} |
---|
| 121 | }; |
---|
[b511fa9] | 122 | #ifdef HAVE_LASH |
---|
[50bc5f2] | 123 | lash_args = lash_extract_args (&argc, &argv); |
---|
[b511fa9] | 124 | #endif /* HAVE_LASH */ |
---|
[50bc5f2] | 125 | prog_name = argv[0]; |
---|
| 126 | if (argc < 1) { |
---|
| 127 | usage (stderr, 1); |
---|
| 128 | return -1; |
---|
| 129 | } |
---|
| 130 | do { |
---|
| 131 | next_option = getopt_long (argc, argv, options, long_options, NULL); |
---|
| 132 | switch (next_option) { |
---|
| 133 | case 'o': |
---|
| 134 | output_filename = optarg; |
---|
| 135 | break; |
---|
| 136 | case 'i': |
---|
| 137 | input_filename = optarg; |
---|
| 138 | break; |
---|
| 139 | case 'h': /* help */ |
---|
| 140 | usage (stdout, 0); |
---|
| 141 | return -1; |
---|
| 142 | case 'v': /* verbose */ |
---|
| 143 | verbose = 1; |
---|
| 144 | break; |
---|
| 145 | case 'j': |
---|
| 146 | usejack = 1; |
---|
| 147 | break; |
---|
| 148 | case 'O': /*onset type */ |
---|
[b4f5967] | 149 | onset_mode = optarg; |
---|
[50bc5f2] | 150 | break; |
---|
[40acfbc] | 151 | case 's': /* silence value for onset */ |
---|
[50bc5f2] | 152 | silence = (smpl_t) atof (optarg); |
---|
| 153 | break; |
---|
| 154 | case 't': /* threshold value for onset */ |
---|
| 155 | threshold = (smpl_t) atof (optarg); |
---|
| 156 | break; |
---|
| 157 | case 'p': |
---|
[fe163ad] | 158 | pitch_mode = optarg; |
---|
[50bc5f2] | 159 | break; |
---|
| 160 | case 'a': |
---|
| 161 | averaging = 1; |
---|
| 162 | break; |
---|
| 163 | case 'B': |
---|
| 164 | buffer_size = atoi (optarg); |
---|
| 165 | break; |
---|
| 166 | case 'H': |
---|
| 167 | overlap_size = atoi (optarg); |
---|
| 168 | break; |
---|
| 169 | case '?': /* unknown options */ |
---|
| 170 | usage (stderr, 1); |
---|
| 171 | break; |
---|
| 172 | case -1: /* done with options */ |
---|
| 173 | break; |
---|
| 174 | default: /*something else unexpected */ |
---|
| 175 | fprintf (stderr, "Error parsing option '%c'\n", next_option); |
---|
| 176 | abort (); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | while (next_option != -1); |
---|
[71482a9] | 180 | |
---|
[50bc5f2] | 181 | if (input_filename != NULL) { |
---|
| 182 | debug ("Input file : %s\n", input_filename); |
---|
| 183 | } else if (input_filename != NULL && output_filename != NULL) { |
---|
| 184 | debug ("Input file : %s\n", input_filename); |
---|
| 185 | debug ("Output file : %s\n", output_filename); |
---|
| 186 | } else { |
---|
[ebbf5a0] | 187 | #if HAVE_JACK |
---|
| 188 | debug ("Jack input output\n"); |
---|
| 189 | usejack = 1; |
---|
| 190 | #else |
---|
| 191 | debug |
---|
| 192 | ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n"); |
---|
| 193 | exit (1); |
---|
| 194 | #endif |
---|
[50bc5f2] | 195 | } |
---|
| 196 | |
---|
| 197 | return 0; |
---|
[96fb8ad] | 198 | } |
---|
| 199 | |
---|
[50bc5f2] | 200 | void |
---|
| 201 | examples_common_init (int argc, char **argv) |
---|
| 202 | { |
---|
[bd2f2ab] | 203 | |
---|
[d4c5de7] | 204 | uint_t found_wood = 0; |
---|
[bd2f2ab] | 205 | |
---|
[50bc5f2] | 206 | aubio_sndfile_t *onsetfile = NULL; |
---|
[bd2f2ab] | 207 | /* parse command line arguments */ |
---|
[50bc5f2] | 208 | parse_args (argc, argv); |
---|
[bd2f2ab] | 209 | |
---|
[50bc5f2] | 210 | woodblock = new_fvec (buffer_size, 1); |
---|
[f382ac6] | 211 | if (output_filename || usejack) { |
---|
[50bc5f2] | 212 | /* dummy assignement to keep egcs happy */ |
---|
[d4c5de7] | 213 | found_wood = (onsetfile = new_aubio_sndfile_ro (onset_filename)) || |
---|
[50bc5f2] | 214 | (onsetfile = new_aubio_sndfile_ro ("sounds/woodblock.aiff")) || |
---|
| 215 | (onsetfile = new_aubio_sndfile_ro ("../sounds/woodblock.aiff")); |
---|
| 216 | if (onsetfile == NULL) { |
---|
| 217 | outmsg ("Could not find woodblock.aiff\n"); |
---|
| 218 | exit (1); |
---|
| 219 | } |
---|
[b1f723d] | 220 | } |
---|
| 221 | if (onsetfile) { |
---|
[50bc5f2] | 222 | /* read the output sound once */ |
---|
| 223 | aubio_sndfile_read (onsetfile, overlap_size, woodblock); |
---|
[f382ac6] | 224 | } |
---|
| 225 | |
---|
[50bc5f2] | 226 | if (!usejack) { |
---|
| 227 | debug ("Opening files ...\n"); |
---|
[5e9c68a] | 228 | file = new_aubio_sndfile_ro (input_filename); |
---|
[9af07aa] | 229 | if (file == NULL) { |
---|
[50bc5f2] | 230 | outmsg ("Could not open input file %s.\n", input_filename); |
---|
| 231 | exit (1); |
---|
[9af07aa] | 232 | } |
---|
[50bc5f2] | 233 | if (verbose) |
---|
| 234 | aubio_sndfile_info (file); |
---|
| 235 | channels = aubio_sndfile_channels (file); |
---|
| 236 | samplerate = aubio_sndfile_samplerate (file); |
---|
[bd2f2ab] | 237 | if (output_filename != NULL) |
---|
[50bc5f2] | 238 | fileout = new_aubio_sndfile_wo (file, output_filename); |
---|
[bd2f2ab] | 239 | } |
---|
[b511fa9] | 240 | #ifdef HAVE_LASH |
---|
[71482a9] | 241 | else { |
---|
[50bc5f2] | 242 | aubio_lash_client = lash_init (lash_args, argv[0], |
---|
| 243 | LASH_Config_Data_Set | LASH_Terminal, LASH_PROTOCOL (2, 0)); |
---|
[71482a9] | 244 | if (!aubio_lash_client) { |
---|
[50bc5f2] | 245 | fprintf (stderr, "%s: could not initialise lash\n", __FUNCTION__); |
---|
[71482a9] | 246 | } |
---|
| 247 | /* tell the lash server our client id */ |
---|
[50bc5f2] | 248 | if (lash_enabled (aubio_lash_client)) { |
---|
| 249 | lash_event_t *event = |
---|
| 250 | (lash_event_t *) lash_event_new_with_type (LASH_Client_Name); |
---|
| 251 | lash_event_set_string (event, "aubio"); |
---|
| 252 | lash_send_event (aubio_lash_client, event); |
---|
| 253 | pthread_create (&lash_thread, NULL, lash_thread_main, NULL); |
---|
[71482a9] | 254 | } |
---|
| 255 | } |
---|
[b511fa9] | 256 | #endif /* HAVE_LASH */ |
---|
[bd2f2ab] | 257 | |
---|
[50bc5f2] | 258 | ibuf = new_fvec (overlap_size, channels); |
---|
| 259 | obuf = new_fvec (overlap_size, channels); |
---|
[bd2f2ab] | 260 | |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
[50bc5f2] | 264 | void |
---|
| 265 | examples_common_del (void) |
---|
| 266 | { |
---|
| 267 | del_fvec (ibuf); |
---|
[d4c5de7] | 268 | del_fvec (obuf); |
---|
[50bc5f2] | 269 | del_fvec (woodblock); |
---|
| 270 | aubio_cleanup (); |
---|
[bd2f2ab] | 271 | } |
---|
| 272 | |
---|
[ebbf5a0] | 273 | #if HAVE_JACK |
---|
| 274 | aubio_jack_t *jack_setup; |
---|
| 275 | #endif |
---|
| 276 | |
---|
[50bc5f2] | 277 | void |
---|
| 278 | examples_common_process (aubio_process_func_t process_func, |
---|
| 279 | aubio_print_func_t print) |
---|
| 280 | { |
---|
| 281 | if (usejack) { |
---|
[ebbf5a0] | 282 | |
---|
[b511fa9] | 283 | #if HAVE_JACK |
---|
[50bc5f2] | 284 | debug ("Jack init ...\n"); |
---|
| 285 | jack_setup = new_aubio_jack (channels, channels, |
---|
[ebbf5a0] | 286 | 0, 1, (aubio_process_func_t) process_func); |
---|
[50bc5f2] | 287 | debug ("Jack activation ...\n"); |
---|
| 288 | aubio_jack_activate (jack_setup); |
---|
| 289 | debug ("Processing (Ctrl+C to quit) ...\n"); |
---|
| 290 | pause (); |
---|
| 291 | aubio_jack_close (jack_setup); |
---|
[bd2f2ab] | 292 | #else |
---|
[50bc5f2] | 293 | usage (stderr, 1); |
---|
| 294 | outmsg ("Compiled without jack output, exiting.\n"); |
---|
[bd2f2ab] | 295 | #endif |
---|
| 296 | |
---|
| 297 | } else { |
---|
| 298 | /* phasevoc */ |
---|
[50bc5f2] | 299 | debug ("Processing ...\n"); |
---|
[bd2f2ab] | 300 | |
---|
| 301 | frames = 0; |
---|
| 302 | |
---|
[50bc5f2] | 303 | while ((signed) overlap_size == aubio_sndfile_read (file, overlap_size, |
---|
| 304 | ibuf)) { |
---|
| 305 | process_func (ibuf->data, obuf->data, overlap_size); |
---|
| 306 | print (); |
---|
[bd2f2ab] | 307 | if (output_filename != NULL) { |
---|
[50bc5f2] | 308 | aubio_sndfile_write (fileout, overlap_size, obuf); |
---|
[bd2f2ab] | 309 | } |
---|
| 310 | frames++; |
---|
| 311 | } |
---|
| 312 | |
---|
[50bc5f2] | 313 | debug ("Processed %d frames of %d samples.\n", frames, buffer_size); |
---|
[e24378a] | 314 | |
---|
[50bc5f2] | 315 | flush_process (process_func, print); |
---|
| 316 | del_aubio_sndfile (file); |
---|
[bd2f2ab] | 317 | |
---|
| 318 | if (output_filename != NULL) |
---|
[50bc5f2] | 319 | del_aubio_sndfile (fileout); |
---|
[bd2f2ab] | 320 | |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
[50bc5f2] | 324 | void |
---|
| 325 | flush_process (aubio_process_func_t process_func, aubio_print_func_t print) |
---|
| 326 | { |
---|
[05a3479] | 327 | uint_t i; |
---|
[847b374] | 328 | fvec_zeros(obuf); |
---|
[50bc5f2] | 329 | for (i = 0; (signed) i < frames_delay; i++) { |
---|
| 330 | process_func (ibuf->data, obuf->data, overlap_size); |
---|
| 331 | print (); |
---|
[e24378a] | 332 | } |
---|
| 333 | } |
---|
[bd2f2ab] | 334 | |
---|
[50bc5f2] | 335 | void |
---|
| 336 | send_noteon (int pitch, int velo) |
---|
[bd2f2ab] | 337 | { |
---|
[50bc5f2] | 338 | smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5); |
---|
[ebbf5a0] | 339 | #if HAVE_JACK |
---|
| 340 | jack_midi_event_t ev; |
---|
| 341 | ev.size = 3; |
---|
| 342 | ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME |
---|
| 343 | ev.time = 0; |
---|
[50bc5f2] | 344 | if (usejack) { |
---|
[ebbf5a0] | 345 | ev.buffer[2] = velo; |
---|
| 346 | ev.buffer[1] = mpitch; |
---|
[50bc5f2] | 347 | if (velo == 0) { |
---|
[ebbf5a0] | 348 | ev.buffer[0] = 0x80; /* note off */ |
---|
[50bc5f2] | 349 | } else { |
---|
[ebbf5a0] | 350 | ev.buffer[0] = 0x90; /* note on */ |
---|
[50bc5f2] | 351 | } |
---|
[ebbf5a0] | 352 | aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev); |
---|
[50bc5f2] | 353 | } else |
---|
[bd2f2ab] | 354 | #endif |
---|
[50bc5f2] | 355 | if (!verbose) { |
---|
| 356 | if (velo == 0) { |
---|
| 357 | outmsg ("%f\n", frames * overlap_size / (float) samplerate); |
---|
| 358 | } else { |
---|
| 359 | outmsg ("%f\t%f\t", mpitch, frames * overlap_size / (float) samplerate); |
---|
[bd2f2ab] | 360 | } |
---|
[50bc5f2] | 361 | } |
---|
[bd2f2ab] | 362 | } |
---|
| 363 | |
---|
| 364 | |
---|
[b511fa9] | 365 | #if HAVE_LASH |
---|
[71482a9] | 366 | |
---|
[50bc5f2] | 367 | void * |
---|
| 368 | lash_thread_main (void *data __attribute__ ((unused))) |
---|
[71482a9] | 369 | { |
---|
[50bc5f2] | 370 | printf ("LASH thread running\n"); |
---|
[71482a9] | 371 | |
---|
[50bc5f2] | 372 | while (!lash_main ()) |
---|
| 373 | usleep (1000); |
---|
[71482a9] | 374 | |
---|
[50bc5f2] | 375 | printf ("LASH thread finished\n"); |
---|
| 376 | return NULL; |
---|
[71482a9] | 377 | } |
---|
| 378 | |
---|
[50bc5f2] | 379 | int |
---|
| 380 | lash_main (void) |
---|
| 381 | { |
---|
| 382 | lash_event_t *lash_event; |
---|
| 383 | lash_config_t *lash_config; |
---|
| 384 | |
---|
| 385 | while ((lash_event = lash_get_event (aubio_lash_client))) { |
---|
| 386 | switch (lash_event_get_type (lash_event)) { |
---|
| 387 | case LASH_Quit: |
---|
| 388 | lash_event_destroy (lash_event); |
---|
| 389 | exit (1); |
---|
| 390 | return 1; |
---|
| 391 | case LASH_Restore_Data_Set: |
---|
| 392 | lash_send_event (aubio_lash_client, lash_event); |
---|
| 393 | break; |
---|
| 394 | case LASH_Save_Data_Set: |
---|
| 395 | save_data (); |
---|
| 396 | lash_send_event (aubio_lash_client, lash_event); |
---|
| 397 | break; |
---|
| 398 | case LASH_Server_Lost: |
---|
| 399 | return 1; |
---|
| 400 | default: |
---|
| 401 | printf ("%s: received unknown LASH event of type %d", |
---|
| 402 | __FUNCTION__, lash_event_get_type (lash_event)); |
---|
| 403 | lash_event_destroy (lash_event); |
---|
| 404 | break; |
---|
| 405 | } |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | while ((lash_config = lash_get_config (aubio_lash_client))) { |
---|
| 409 | restore_data (lash_config); |
---|
| 410 | lash_config_destroy (lash_config); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | return 0; |
---|
[71482a9] | 414 | } |
---|
| 415 | |
---|
[50bc5f2] | 416 | void |
---|
| 417 | save_data () |
---|
| 418 | { |
---|
| 419 | lash_config_t *lash_config; |
---|
[71482a9] | 420 | |
---|
[50bc5f2] | 421 | lash_config = lash_config_new_with_key ("threshold"); |
---|
| 422 | lash_config_set_value_double (lash_config, threshold); |
---|
| 423 | lash_send_config (aubio_lash_client, lash_config); |
---|
[71482a9] | 424 | |
---|
| 425 | } |
---|
| 426 | |
---|
[50bc5f2] | 427 | void |
---|
| 428 | restore_data (lash_config_t * lash_config) |
---|
| 429 | { |
---|
| 430 | const char *lash_key; |
---|
[71482a9] | 431 | |
---|
[50bc5f2] | 432 | lash_key = lash_config_get_key (lash_config); |
---|
[71482a9] | 433 | |
---|
[50bc5f2] | 434 | if (strcmp (lash_key, "threshold") == 0) { |
---|
| 435 | threshold = lash_config_get_value_double (lash_config); |
---|
| 436 | return; |
---|
| 437 | } |
---|
[71482a9] | 438 | |
---|
| 439 | } |
---|
| 440 | |
---|
[b511fa9] | 441 | #endif /* HAVE_LASH */ |
---|