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