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