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