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