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