[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 | |
---|
| 21 | #include "utils.h" |
---|
| 22 | |
---|
[b511fa9] | 23 | #ifdef HAVE_LASH |
---|
[71482a9] | 24 | #include <lash/lash.h> |
---|
| 25 | #include <pthread.h> |
---|
[50bc5f2] | 26 | lash_client_t *aubio_lash_client; |
---|
| 27 | lash_args_t *lash_args; |
---|
| 28 | void *lash_thread_main (void *data); |
---|
[71482a9] | 29 | int lash_main (void); |
---|
| 30 | void save_data (void); |
---|
[50bc5f2] | 31 | void restore_data (lash_config_t * lash_config); |
---|
[71482a9] | 32 | pthread_t lash_thread; |
---|
[b511fa9] | 33 | #endif /* HAVE_LASH */ |
---|
[96fb8ad] | 34 | |
---|
[bd2f2ab] | 35 | /* settings */ |
---|
[50bc5f2] | 36 | const char *output_filename = NULL; |
---|
| 37 | const char *input_filename = NULL; |
---|
| 38 | const char *onset_filename = |
---|
| 39 | AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff"; |
---|
[bd2f2ab] | 40 | int frames = 0; |
---|
| 41 | int verbose = 0; |
---|
| 42 | int usejack = 0; |
---|
| 43 | int usedoubled = 1; |
---|
[e24378a] | 44 | int frames_delay = 0; |
---|
[bd2f2ab] | 45 | |
---|
| 46 | |
---|
| 47 | /* energy,specdiff,hfc,complexdomain,phase */ |
---|
[50bc5f2] | 48 | aubio_onsetdetection_type type_onset = aubio_onset_kl; |
---|
[5cf415f] | 49 | aubio_onsetdetection_type type_onset2 = aubio_onset_complex; |
---|
[50bc5f2] | 50 | smpl_t threshold = 0.3; |
---|
| 51 | smpl_t silence = -90.; |
---|
| 52 | uint_t buffer_size = 512; //1024; |
---|
| 53 | uint_t overlap_size = 256; //512; |
---|
| 54 | uint_t channels = 1; |
---|
| 55 | uint_t samplerate = 44100; |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | aubio_sndfile_t *file = NULL; |
---|
| 59 | aubio_sndfile_t *fileout = NULL; |
---|
| 60 | |
---|
| 61 | aubio_pvoc_t *pv; |
---|
| 62 | fvec_t *ibuf; |
---|
| 63 | fvec_t *obuf; |
---|
| 64 | cvec_t *fftgrain; |
---|
| 65 | fvec_t *woodblock; |
---|
[bd2f2ab] | 66 | aubio_onsetdetection_t *o; |
---|
| 67 | aubio_onsetdetection_t *o2; |
---|
| 68 | fvec_t *onset; |
---|
| 69 | fvec_t *onset2; |
---|
| 70 | int isonset = 0; |
---|
[50bc5f2] | 71 | aubio_pickpeak_t *parms; |
---|
[bd2f2ab] | 72 | |
---|
| 73 | |
---|
| 74 | /* pitch objects */ |
---|
[50bc5f2] | 75 | smpl_t pitch = 0.; |
---|
| 76 | aubio_pitchdetection_t *pitchdet; |
---|
| 77 | aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft; // aubio_pitch_mcomb |
---|
[fb615eb] | 78 | aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq; |
---|
[50bc5f2] | 79 | uint_t median = 6; |
---|
[bd2f2ab] | 80 | |
---|
[50bc5f2] | 81 | fvec_t *note_buffer = NULL; |
---|
| 82 | fvec_t *note_buffer2 = NULL; |
---|
| 83 | smpl_t curlevel = 0.; |
---|
| 84 | smpl_t maxonset = 0.; |
---|
[bd2f2ab] | 85 | |
---|
| 86 | smpl_t curnote = 0.; |
---|
| 87 | smpl_t newnote = 0.; |
---|
| 88 | uint_t isready = 0; |
---|
| 89 | |
---|
| 90 | |
---|
[96fb8ad] | 91 | |
---|
| 92 | /* badly redeclare some things */ |
---|
| 93 | aubio_onsetdetection_type type_onset; |
---|
| 94 | smpl_t threshold; |
---|
| 95 | smpl_t averaging; |
---|
[50bc5f2] | 96 | const char *prog_name; |
---|
[96fb8ad] | 97 | |
---|
[50bc5f2] | 98 | void |
---|
| 99 | usage (FILE * stream, int exit_code) |
---|
[96fb8ad] | 100 | { |
---|
[50bc5f2] | 101 | fprintf (stream, "usage: %s [ options ] \n", prog_name); |
---|
| 102 | fprintf (stream, |
---|
| 103 | " -h --help Display this message.\n" |
---|
| 104 | " -v --verbose Be verbose.\n" |
---|
| 105 | " -j --jack Use Jack.\n" |
---|
| 106 | " -o --output Output type.\n" |
---|
| 107 | " -i --input Input type.\n" |
---|
| 108 | " -O --onset Select onset detection algorithm.\n" |
---|
| 109 | " -t --threshold Set onset detection threshold.\n" |
---|
| 110 | " -s --silence Select silence threshold.\n" |
---|
| 111 | " -p --pitch Select pitch detection algorithm.\n" |
---|
| 112 | " -B --bufsize Set buffer size.\n" |
---|
| 113 | " -H --hopsize Set hopsize.\n" |
---|
| 114 | " -a --averaging Use averaging.\n"); |
---|
| 115 | exit (exit_code); |
---|
[96fb8ad] | 116 | } |
---|
| 117 | |
---|
[50bc5f2] | 118 | int |
---|
| 119 | parse_args (int argc, char **argv) |
---|
| 120 | { |
---|
| 121 | const char *options = "hvjo:i:O:t:s:p:B:H:a"; |
---|
| 122 | int next_option; |
---|
| 123 | struct option long_options[] = { |
---|
| 124 | {"help", 0, NULL, 'h'}, |
---|
| 125 | {"verbose", 0, NULL, 'v'}, |
---|
| 126 | {"jack", 0, NULL, 'j'}, |
---|
| 127 | {"output", 1, NULL, 'o'}, |
---|
| 128 | {"input", 1, NULL, 'i'}, |
---|
| 129 | {"onset", 1, NULL, 'O'}, |
---|
| 130 | {"threshold", 1, NULL, 't'}, |
---|
| 131 | {"silence", 1, NULL, 's'}, |
---|
| 132 | {"pitch", 1, NULL, 'p'}, |
---|
| 133 | {"averaging", 0, NULL, 'a'}, |
---|
| 134 | {"bufsize", 1, NULL, 'B'}, |
---|
| 135 | {"hopsize", 1, NULL, 'H'}, |
---|
| 136 | {NULL, 0, NULL, 0} |
---|
| 137 | }; |
---|
[b511fa9] | 138 | #ifdef HAVE_LASH |
---|
[50bc5f2] | 139 | lash_args = lash_extract_args (&argc, &argv); |
---|
[b511fa9] | 140 | #endif /* HAVE_LASH */ |
---|
[50bc5f2] | 141 | prog_name = argv[0]; |
---|
| 142 | if (argc < 1) { |
---|
| 143 | usage (stderr, 1); |
---|
| 144 | return -1; |
---|
| 145 | } |
---|
| 146 | do { |
---|
| 147 | next_option = getopt_long (argc, argv, options, long_options, NULL); |
---|
| 148 | switch (next_option) { |
---|
| 149 | case 'o': |
---|
| 150 | output_filename = optarg; |
---|
| 151 | break; |
---|
| 152 | case 'i': |
---|
| 153 | input_filename = optarg; |
---|
| 154 | break; |
---|
| 155 | case 'h': /* help */ |
---|
| 156 | usage (stdout, 0); |
---|
| 157 | return -1; |
---|
| 158 | case 'v': /* verbose */ |
---|
| 159 | verbose = 1; |
---|
| 160 | break; |
---|
| 161 | case 'j': |
---|
| 162 | usejack = 1; |
---|
| 163 | break; |
---|
| 164 | case 'O': /*onset type */ |
---|
| 165 | if (strcmp (optarg, "energy") == 0) |
---|
| 166 | type_onset = aubio_onset_energy; |
---|
| 167 | else if (strcmp (optarg, "specdiff") == 0) |
---|
| 168 | type_onset = aubio_onset_specdiff; |
---|
| 169 | else if (strcmp (optarg, "hfc") == 0) |
---|
| 170 | type_onset = aubio_onset_hfc; |
---|
| 171 | else if (strcmp (optarg, "complexdomain") == 0) |
---|
| 172 | type_onset = aubio_onset_complex; |
---|
| 173 | else if (strcmp (optarg, "complex") == 0) |
---|
| 174 | type_onset = aubio_onset_complex; |
---|
| 175 | else if (strcmp (optarg, "phase") == 0) |
---|
| 176 | type_onset = aubio_onset_phase; |
---|
| 177 | else if (strcmp (optarg, "mkl") == 0) |
---|
| 178 | type_onset = aubio_onset_mkl; |
---|
| 179 | else if (strcmp (optarg, "kl") == 0) |
---|
| 180 | type_onset = aubio_onset_kl; |
---|
| 181 | else if (strcmp (optarg, "specflux") == 0) |
---|
| 182 | type_onset = aubio_onset_specflux; |
---|
| 183 | else { |
---|
| 184 | errmsg ("unknown onset type.\n"); |
---|
| 185 | abort (); |
---|
[96fb8ad] | 186 | } |
---|
[50bc5f2] | 187 | usedoubled = 0; |
---|
| 188 | break; |
---|
| 189 | case 's': /* threshold value for onset */ |
---|
| 190 | silence = (smpl_t) atof (optarg); |
---|
| 191 | break; |
---|
| 192 | case 't': /* threshold value for onset */ |
---|
| 193 | threshold = (smpl_t) atof (optarg); |
---|
| 194 | /* |
---|
| 195 | if (!isfinite(threshold)) { |
---|
| 196 | debug("could not get threshold.\n"); |
---|
| 197 | abort(); |
---|
| 198 | } |
---|
| 199 | */ |
---|
| 200 | break; |
---|
| 201 | case 'p': |
---|
| 202 | if (strcmp (optarg, "mcomb") == 0) |
---|
| 203 | type_pitch = aubio_pitch_mcomb; |
---|
| 204 | else if (strcmp (optarg, "yinfft") == 0) |
---|
| 205 | type_pitch = aubio_pitch_yin; |
---|
| 206 | else if (strcmp (optarg, "yin") == 0) |
---|
| 207 | type_pitch = aubio_pitch_yin; |
---|
| 208 | else if (strcmp (optarg, "schmitt") == 0) |
---|
| 209 | type_pitch = aubio_pitch_schmitt; |
---|
| 210 | else if (strcmp (optarg, "fcomb") == 0) |
---|
| 211 | type_pitch = aubio_pitch_fcomb; |
---|
| 212 | else { |
---|
| 213 | errmsg ("unknown pitch type.\n"); |
---|
| 214 | abort (); |
---|
[fb615eb] | 215 | } |
---|
[50bc5f2] | 216 | break; |
---|
| 217 | case 'a': |
---|
| 218 | averaging = 1; |
---|
| 219 | break; |
---|
| 220 | case 'B': |
---|
| 221 | buffer_size = atoi (optarg); |
---|
| 222 | break; |
---|
| 223 | case 'H': |
---|
| 224 | overlap_size = atoi (optarg); |
---|
| 225 | break; |
---|
| 226 | case '?': /* unknown options */ |
---|
| 227 | usage (stderr, 1); |
---|
| 228 | break; |
---|
| 229 | case -1: /* done with options */ |
---|
| 230 | break; |
---|
| 231 | default: /*something else unexpected */ |
---|
| 232 | fprintf (stderr, "Error parsing option '%c'\n", next_option); |
---|
| 233 | abort (); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | while (next_option != -1); |
---|
[71482a9] | 237 | |
---|
[50bc5f2] | 238 | if (input_filename != NULL) { |
---|
| 239 | debug ("Input file : %s\n", input_filename); |
---|
| 240 | } else if (input_filename != NULL && output_filename != NULL) { |
---|
| 241 | debug ("Input file : %s\n", input_filename); |
---|
| 242 | debug ("Output file : %s\n", output_filename); |
---|
| 243 | } else { |
---|
[ebbf5a0] | 244 | #if HAVE_JACK |
---|
| 245 | debug ("Jack input output\n"); |
---|
| 246 | usejack = 1; |
---|
| 247 | #else |
---|
| 248 | debug |
---|
| 249 | ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n"); |
---|
| 250 | exit (1); |
---|
| 251 | #endif |
---|
[50bc5f2] | 252 | } |
---|
| 253 | |
---|
| 254 | return 0; |
---|
[96fb8ad] | 255 | } |
---|
| 256 | |
---|
[50bc5f2] | 257 | void |
---|
| 258 | examples_common_init (int argc, char **argv) |
---|
| 259 | { |
---|
[bd2f2ab] | 260 | |
---|
| 261 | |
---|
[50bc5f2] | 262 | aubio_sndfile_t *onsetfile = NULL; |
---|
[bd2f2ab] | 263 | /* parse command line arguments */ |
---|
[50bc5f2] | 264 | parse_args (argc, argv); |
---|
[bd2f2ab] | 265 | |
---|
[50bc5f2] | 266 | woodblock = new_fvec (buffer_size, 1); |
---|
[f382ac6] | 267 | if (output_filename || usejack) { |
---|
[50bc5f2] | 268 | /* dummy assignement to keep egcs happy */ |
---|
| 269 | isonset = (onsetfile = new_aubio_sndfile_ro (onset_filename)) || |
---|
| 270 | (onsetfile = new_aubio_sndfile_ro ("sounds/woodblock.aiff")) || |
---|
| 271 | (onsetfile = new_aubio_sndfile_ro ("../sounds/woodblock.aiff")); |
---|
| 272 | if (onsetfile == NULL) { |
---|
| 273 | outmsg ("Could not find woodblock.aiff\n"); |
---|
| 274 | exit (1); |
---|
| 275 | } |
---|
[b1f723d] | 276 | } |
---|
| 277 | if (onsetfile) { |
---|
[50bc5f2] | 278 | /* read the output sound once */ |
---|
| 279 | aubio_sndfile_read (onsetfile, overlap_size, woodblock); |
---|
[f382ac6] | 280 | } |
---|
| 281 | |
---|
[50bc5f2] | 282 | if (!usejack) { |
---|
| 283 | debug ("Opening files ...\n"); |
---|
[5e9c68a] | 284 | file = new_aubio_sndfile_ro (input_filename); |
---|
[9af07aa] | 285 | if (file == NULL) { |
---|
[50bc5f2] | 286 | outmsg ("Could not open input file %s.\n", input_filename); |
---|
| 287 | exit (1); |
---|
[9af07aa] | 288 | } |
---|
[50bc5f2] | 289 | if (verbose) |
---|
| 290 | aubio_sndfile_info (file); |
---|
| 291 | channels = aubio_sndfile_channels (file); |
---|
| 292 | samplerate = aubio_sndfile_samplerate (file); |
---|
[bd2f2ab] | 293 | if (output_filename != NULL) |
---|
[50bc5f2] | 294 | fileout = new_aubio_sndfile_wo (file, output_filename); |
---|
[bd2f2ab] | 295 | } |
---|
[b511fa9] | 296 | #ifdef HAVE_LASH |
---|
[71482a9] | 297 | else { |
---|
[50bc5f2] | 298 | aubio_lash_client = lash_init (lash_args, argv[0], |
---|
| 299 | LASH_Config_Data_Set | LASH_Terminal, LASH_PROTOCOL (2, 0)); |
---|
[71482a9] | 300 | if (!aubio_lash_client) { |
---|
[50bc5f2] | 301 | fprintf (stderr, "%s: could not initialise lash\n", __FUNCTION__); |
---|
[71482a9] | 302 | } |
---|
| 303 | /* tell the lash server our client id */ |
---|
[50bc5f2] | 304 | if (lash_enabled (aubio_lash_client)) { |
---|
| 305 | lash_event_t *event = |
---|
| 306 | (lash_event_t *) lash_event_new_with_type (LASH_Client_Name); |
---|
| 307 | lash_event_set_string (event, "aubio"); |
---|
| 308 | lash_send_event (aubio_lash_client, event); |
---|
| 309 | pthread_create (&lash_thread, NULL, lash_thread_main, NULL); |
---|
[71482a9] | 310 | } |
---|
| 311 | } |
---|
[b511fa9] | 312 | #endif /* HAVE_LASH */ |
---|
[bd2f2ab] | 313 | |
---|
[50bc5f2] | 314 | ibuf = new_fvec (overlap_size, channels); |
---|
| 315 | obuf = new_fvec (overlap_size, channels); |
---|
| 316 | fftgrain = new_cvec (buffer_size, channels); |
---|
[bd2f2ab] | 317 | |
---|
| 318 | if (usepitch) { |
---|
[50bc5f2] | 319 | pitchdet = new_aubio_pitchdetection (buffer_size * 4, |
---|
[4c67dc8] | 320 | overlap_size, channels, samplerate, type_pitch, mode_pitch); |
---|
[50bc5f2] | 321 | aubio_pitchdetection_set_yinthresh (pitchdet, 0.7); |
---|
[4c67dc8] | 322 | |
---|
| 323 | if (median) { |
---|
[50bc5f2] | 324 | note_buffer = new_fvec (median, 1); |
---|
| 325 | note_buffer2 = new_fvec (median, 1); |
---|
[4c67dc8] | 326 | } |
---|
[bd2f2ab] | 327 | } |
---|
| 328 | /* phase vocoder */ |
---|
[50bc5f2] | 329 | pv = new_aubio_pvoc (buffer_size, overlap_size, channels); |
---|
[bd2f2ab] | 330 | /* onsets */ |
---|
[50bc5f2] | 331 | parms = new_aubio_peakpicker (threshold); |
---|
| 332 | o = new_aubio_onsetdetection (type_onset, buffer_size, channels); |
---|
| 333 | onset = new_fvec (1, channels); |
---|
| 334 | if (usedoubled) { |
---|
| 335 | o2 = new_aubio_onsetdetection (type_onset2, buffer_size, channels); |
---|
| 336 | onset2 = new_fvec (1, channels); |
---|
[bd2f2ab] | 337 | } |
---|
| 338 | |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | |
---|
[50bc5f2] | 342 | void |
---|
| 343 | examples_common_del (void) |
---|
| 344 | { |
---|
[bd2f2ab] | 345 | if (usepitch) { |
---|
[50bc5f2] | 346 | send_noteon (curnote, 0); |
---|
| 347 | del_aubio_pitchdetection (pitchdet); |
---|
| 348 | if (median) { |
---|
| 349 | del_fvec (note_buffer); |
---|
| 350 | del_fvec (note_buffer2); |
---|
| 351 | } |
---|
[bd2f2ab] | 352 | } |
---|
[50bc5f2] | 353 | if (usedoubled) { |
---|
| 354 | del_aubio_onsetdetection (o2); |
---|
| 355 | del_fvec (onset2); |
---|
[b91fe6e] | 356 | } |
---|
[50bc5f2] | 357 | del_aubio_onsetdetection (o); |
---|
| 358 | del_aubio_peakpicker (parms); |
---|
| 359 | del_aubio_pvoc (pv); |
---|
| 360 | del_fvec (obuf); |
---|
| 361 | del_fvec (ibuf); |
---|
| 362 | del_cvec (fftgrain); |
---|
| 363 | del_fvec (onset); |
---|
| 364 | del_fvec (woodblock); |
---|
| 365 | aubio_cleanup (); |
---|
[bd2f2ab] | 366 | } |
---|
| 367 | |
---|
[ebbf5a0] | 368 | #if HAVE_JACK |
---|
| 369 | aubio_jack_t *jack_setup; |
---|
| 370 | #endif |
---|
| 371 | |
---|
[50bc5f2] | 372 | void |
---|
| 373 | examples_common_process (aubio_process_func_t process_func, |
---|
| 374 | aubio_print_func_t print) |
---|
| 375 | { |
---|
| 376 | if (usejack) { |
---|
[ebbf5a0] | 377 | |
---|
[b511fa9] | 378 | #if HAVE_JACK |
---|
[50bc5f2] | 379 | debug ("Jack init ...\n"); |
---|
| 380 | jack_setup = new_aubio_jack (channels, channels, |
---|
[ebbf5a0] | 381 | 0, 1, (aubio_process_func_t) process_func); |
---|
[50bc5f2] | 382 | debug ("Jack activation ...\n"); |
---|
| 383 | aubio_jack_activate (jack_setup); |
---|
| 384 | debug ("Processing (Ctrl+C to quit) ...\n"); |
---|
| 385 | pause (); |
---|
| 386 | aubio_jack_close (jack_setup); |
---|
[bd2f2ab] | 387 | #else |
---|
[50bc5f2] | 388 | usage (stderr, 1); |
---|
| 389 | outmsg ("Compiled without jack output, exiting.\n"); |
---|
[bd2f2ab] | 390 | #endif |
---|
| 391 | |
---|
| 392 | } else { |
---|
| 393 | /* phasevoc */ |
---|
[50bc5f2] | 394 | debug ("Processing ...\n"); |
---|
[bd2f2ab] | 395 | |
---|
| 396 | frames = 0; |
---|
| 397 | |
---|
[50bc5f2] | 398 | while ((signed) overlap_size == aubio_sndfile_read (file, overlap_size, |
---|
| 399 | ibuf)) { |
---|
| 400 | isonset = 0; |
---|
| 401 | process_func (ibuf->data, obuf->data, overlap_size); |
---|
| 402 | print (); |
---|
[bd2f2ab] | 403 | if (output_filename != NULL) { |
---|
[50bc5f2] | 404 | aubio_sndfile_write (fileout, overlap_size, obuf); |
---|
[bd2f2ab] | 405 | } |
---|
| 406 | frames++; |
---|
| 407 | } |
---|
| 408 | |
---|
[50bc5f2] | 409 | debug ("Processed %d frames of %d samples.\n", frames, buffer_size); |
---|
[e24378a] | 410 | |
---|
[50bc5f2] | 411 | flush_process (process_func, print); |
---|
| 412 | del_aubio_sndfile (file); |
---|
[bd2f2ab] | 413 | |
---|
| 414 | if (output_filename != NULL) |
---|
[50bc5f2] | 415 | del_aubio_sndfile (fileout); |
---|
[bd2f2ab] | 416 | |
---|
| 417 | } |
---|
| 418 | } |
---|
| 419 | |
---|
[50bc5f2] | 420 | void |
---|
| 421 | flush_process (aubio_process_func_t process_func, aubio_print_func_t print) |
---|
| 422 | { |
---|
| 423 | uint_t i, j; |
---|
[e24378a] | 424 | for (i = 0; i < channels; i++) { |
---|
| 425 | for (j = 0; j < obuf->length; j++) { |
---|
[50bc5f2] | 426 | fvec_write_sample (obuf, 0., i, j); |
---|
[e24378a] | 427 | } |
---|
| 428 | } |
---|
[50bc5f2] | 429 | for (i = 0; (signed) i < frames_delay; i++) { |
---|
| 430 | process_func (ibuf->data, obuf->data, overlap_size); |
---|
| 431 | print (); |
---|
[e24378a] | 432 | } |
---|
| 433 | } |
---|
[bd2f2ab] | 434 | |
---|
[50bc5f2] | 435 | void |
---|
| 436 | send_noteon (int pitch, int velo) |
---|
[bd2f2ab] | 437 | { |
---|
[50bc5f2] | 438 | smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5); |
---|
[ebbf5a0] | 439 | #if HAVE_JACK |
---|
| 440 | jack_midi_event_t ev; |
---|
| 441 | ev.size = 3; |
---|
| 442 | ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME |
---|
| 443 | ev.time = 0; |
---|
[50bc5f2] | 444 | if (usejack) { |
---|
[ebbf5a0] | 445 | ev.buffer[2] = velo; |
---|
| 446 | ev.buffer[1] = mpitch; |
---|
[50bc5f2] | 447 | if (velo == 0) { |
---|
[ebbf5a0] | 448 | ev.buffer[0] = 0x80; /* note off */ |
---|
[50bc5f2] | 449 | } else { |
---|
[ebbf5a0] | 450 | ev.buffer[0] = 0x90; /* note on */ |
---|
[50bc5f2] | 451 | } |
---|
[ebbf5a0] | 452 | aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev); |
---|
[50bc5f2] | 453 | } else |
---|
[bd2f2ab] | 454 | #endif |
---|
[50bc5f2] | 455 | if (!verbose) { |
---|
| 456 | if (velo == 0) { |
---|
| 457 | outmsg ("%f\n", frames * overlap_size / (float) samplerate); |
---|
| 458 | } else { |
---|
| 459 | outmsg ("%f\t%f\t", mpitch, frames * overlap_size / (float) samplerate); |
---|
[bd2f2ab] | 460 | } |
---|
[50bc5f2] | 461 | } |
---|
[bd2f2ab] | 462 | } |
---|
| 463 | |
---|
| 464 | |
---|
[50bc5f2] | 465 | void |
---|
| 466 | note_append (fvec_t * note_buffer, smpl_t curnote) |
---|
| 467 | { |
---|
[bd2f2ab] | 468 | uint_t i = 0; |
---|
[50bc5f2] | 469 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
| 470 | note_buffer->data[0][i] = note_buffer->data[0][i + 1]; |
---|
[bd2f2ab] | 471 | } |
---|
| 472 | note_buffer->data[0][note_buffer->length - 1] = curnote; |
---|
| 473 | return; |
---|
| 474 | } |
---|
| 475 | |
---|
[50bc5f2] | 476 | uint_t |
---|
| 477 | get_note (fvec_t * note_buffer, fvec_t * note_buffer2) |
---|
| 478 | { |
---|
[bd2f2ab] | 479 | uint_t i = 0; |
---|
[50bc5f2] | 480 | for (i = 0; i < note_buffer->length; i++) { |
---|
| 481 | note_buffer2->data[0][i] = note_buffer->data[0][i]; |
---|
[bd2f2ab] | 482 | } |
---|
[50bc5f2] | 483 | return vec_median (note_buffer2); |
---|
[bd2f2ab] | 484 | } |
---|
| 485 | |
---|
[b511fa9] | 486 | #if HAVE_LASH |
---|
[71482a9] | 487 | |
---|
[50bc5f2] | 488 | void * |
---|
| 489 | lash_thread_main (void *data __attribute__ ((unused))) |
---|
[71482a9] | 490 | { |
---|
[50bc5f2] | 491 | printf ("LASH thread running\n"); |
---|
[71482a9] | 492 | |
---|
[50bc5f2] | 493 | while (!lash_main ()) |
---|
| 494 | usleep (1000); |
---|
[71482a9] | 495 | |
---|
[50bc5f2] | 496 | printf ("LASH thread finished\n"); |
---|
| 497 | return NULL; |
---|
[71482a9] | 498 | } |
---|
| 499 | |
---|
[50bc5f2] | 500 | int |
---|
| 501 | lash_main (void) |
---|
| 502 | { |
---|
| 503 | lash_event_t *lash_event; |
---|
| 504 | lash_config_t *lash_config; |
---|
| 505 | |
---|
| 506 | while ((lash_event = lash_get_event (aubio_lash_client))) { |
---|
| 507 | switch (lash_event_get_type (lash_event)) { |
---|
| 508 | case LASH_Quit: |
---|
| 509 | lash_event_destroy (lash_event); |
---|
| 510 | exit (1); |
---|
| 511 | return 1; |
---|
| 512 | case LASH_Restore_Data_Set: |
---|
| 513 | lash_send_event (aubio_lash_client, lash_event); |
---|
| 514 | break; |
---|
| 515 | case LASH_Save_Data_Set: |
---|
| 516 | save_data (); |
---|
| 517 | lash_send_event (aubio_lash_client, lash_event); |
---|
| 518 | break; |
---|
| 519 | case LASH_Server_Lost: |
---|
| 520 | return 1; |
---|
| 521 | default: |
---|
| 522 | printf ("%s: received unknown LASH event of type %d", |
---|
| 523 | __FUNCTION__, lash_event_get_type (lash_event)); |
---|
| 524 | lash_event_destroy (lash_event); |
---|
| 525 | break; |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | while ((lash_config = lash_get_config (aubio_lash_client))) { |
---|
| 530 | restore_data (lash_config); |
---|
| 531 | lash_config_destroy (lash_config); |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | return 0; |
---|
[71482a9] | 535 | } |
---|
| 536 | |
---|
[50bc5f2] | 537 | void |
---|
| 538 | save_data () |
---|
| 539 | { |
---|
| 540 | lash_config_t *lash_config; |
---|
[71482a9] | 541 | |
---|
[50bc5f2] | 542 | lash_config = lash_config_new_with_key ("threshold"); |
---|
| 543 | lash_config_set_value_double (lash_config, threshold); |
---|
| 544 | lash_send_config (aubio_lash_client, lash_config); |
---|
[71482a9] | 545 | |
---|
| 546 | } |
---|
| 547 | |
---|
[50bc5f2] | 548 | void |
---|
| 549 | restore_data (lash_config_t * lash_config) |
---|
| 550 | { |
---|
| 551 | const char *lash_key; |
---|
[71482a9] | 552 | |
---|
[50bc5f2] | 553 | lash_key = lash_config_get_key (lash_config); |
---|
[71482a9] | 554 | |
---|
[50bc5f2] | 555 | if (strcmp (lash_key, "threshold") == 0) { |
---|
| 556 | threshold = lash_config_get_value_double (lash_config); |
---|
| 557 | return; |
---|
| 558 | } |
---|
[71482a9] | 559 | |
---|
| 560 | } |
---|
| 561 | |
---|
[b511fa9] | 562 | #endif /* HAVE_LASH */ |
---|