12 #include <math.h> /* for isfinite */
15 /* not supported yet */
17 #include <ladcca/ladcca.h>
18 cca_client_t * aubio_cca_client;
19 #endif /* LADCCA_SUPPORT */
22 const char * output_filename = NULL;
23 const char * input_filename = NULL;
24 const char * onset_filename = AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff";
31 /* energy,specdiff,hfc,complexdomain,phase */
32 aubio_onsetdetection_type type_onset = aubio_onset_kl;
33 aubio_onsetdetection_type type_onset2 = aubio_onset_complex;
34 smpl_t threshold = 0.3;
35 smpl_t threshold2 = -90.;
36 uint_t buffer_size = 512; //1024;
37 uint_t overlap_size = 256; //512;
39 uint_t samplerate = 44100;
42 aubio_sndfile_t * file = NULL;
43 aubio_sndfile_t * fileout = NULL;
50 aubio_onsetdetection_t *o;
51 aubio_onsetdetection_t *o2;
55 aubio_pickpeak_t * parms;
60 aubio_pitchdetection_t * pitchdet;
61 aubio_pitchdetection_type type_pitch = aubio_pitch_schmitt; // aubio_pitch_mcomb
62 aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq;
65 fvec_t * note_buffer = NULL;
66 fvec_t * note_buffer2 = NULL;
71 aubio_midi_player_t * mplay;
72 aubio_midi_driver_t * mdriver;
73 aubio_midi_event_t * event;
81 /* badly redeclare some things */
82 aubio_onsetdetection_type type_onset;
85 const char * prog_name;
87 void usage (FILE * stream, int exit_code)
89 fprintf(stream, "usage: %s [ options ] \n", prog_name);
91 " -h --help Display this message.\n"
92 " -j --jack Use Jack.\n"
93 " -o --output Output type.\n"
94 " -i --input Input type.\n"
95 " -O --onset Select onset detection algorithm.\n"
96 " -t --threshold Set onset detection threshold.\n"
97 " -s --silence Select silence threshold.\n"
98 " -p --pitch Select pitch detection algorithm.\n"
99 " -H --hopsize Set hopsize.\n"
100 " -a --averaging Use averaging.\n"
105 int parse_args (int argc, char **argv) {
106 const char *options = "hvjo:i:O:t:s:p:H:a";
108 struct option long_options[] =
110 {"help" , 0, NULL, 'h'},
111 {"verbose" , 0, NULL, 'v'},
112 {"jack" , 0, NULL, 'j'},
113 {"output" , 1, NULL, 'o'},
114 {"input" , 1, NULL, 'i'},
115 {"onset" , 1, NULL, 'O'},
116 {"threshold", 1, NULL, 't'},
117 {"silence" , 1, NULL, 's'},
118 {"pitch" , 1, NULL, 'p'},
119 {"averaging", 0, NULL, 'a'},
120 {"hopsize", 1, NULL, 'H'},
129 next_option = getopt_long (argc, argv, options,
131 switch (next_option) {
133 output_filename = optarg;
136 input_filename = optarg;
141 case 'v': /* verbose */
147 case 'O': /*onset type*/
148 if (strcmp(optarg,"energy") == 0)
149 type_onset = aubio_onset_energy;
150 else if (strcmp(optarg,"specdiff") == 0)
151 type_onset = aubio_onset_specdiff;
152 else if (strcmp(optarg,"hfc") == 0)
153 type_onset = aubio_onset_hfc;
154 else if (strcmp(optarg,"complexdomain") == 0)
155 type_onset = aubio_onset_complex;
156 else if (strcmp(optarg,"complex") == 0)
157 type_onset = aubio_onset_complex;
158 else if (strcmp(optarg,"phase") == 0)
159 type_onset = aubio_onset_phase;
160 else if (strcmp(optarg,"mkl") == 0)
161 type_onset = aubio_onset_mkl;
162 else if (strcmp(optarg,"kl") == 0)
163 type_onset = aubio_onset_kl;
165 debug("could not get onset type.\n");
170 case 's': /* threshold value for onset */
171 threshold2 = (smpl_t)atof(optarg);
173 case 't': /* threshold value for onset */
174 threshold = (smpl_t)atof(optarg);
176 if (!isfinite(threshold)) {
177 debug("could not get threshold.\n");
183 if (strcmp(optarg,"mcomb") == 0)
184 type_pitch = aubio_pitch_mcomb;
185 else if (strcmp(optarg,"yin") == 0)
186 type_pitch = aubio_pitch_yin;
187 else if (strcmp(optarg,"schmitt") == 0)
188 type_pitch = aubio_pitch_schmitt;
189 else if (strcmp(optarg,"fcomb") == 0)
190 type_pitch = aubio_pitch_fcomb;
192 debug("could not get pitch type.\n");
200 overlap_size = atoi(optarg);
202 case '?': /* unknown options */
205 case -1: /* done with options */
207 default: /*something else unexpected */
211 while (next_option != -1);
213 if (input_filename != NULL) {
214 debug ("Input file : %s\n", input_filename );
215 } else if (input_filename != NULL && output_filename != NULL) {
216 debug ("Input file : %s\n", input_filename );
217 debug ("Output file : %s\n", output_filename );
221 debug ("Jack input output\n");
224 debug ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n");
231 void examples_common_init(int argc,char ** argv) {
234 aubio_sndfile_t * onsetfile = NULL;
235 /* parse command line arguments */
236 parse_args(argc, argv);
238 woodblock = new_fvec(buffer_size,1);
239 if (output_filename || usejack) {
240 /* dummy assignement to keep egcs happy */
241 isonset = (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
242 (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
243 (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
246 /* read the output sound once */
247 aubio_sndfile_read(onsetfile, overlap_size, woodblock);
252 debug("Opening files ...\n");
253 file = new_aubio_sndfile_ro (input_filename);
254 if (verbose) aubio_sndfile_info(file);
255 channels = aubio_sndfile_channels(file);
256 samplerate = aubio_sndfile_samplerate(file);
257 if (output_filename != NULL)
258 fileout = new_aubio_sndfile_wo(file, output_filename);
261 ibuf = new_fvec(overlap_size, channels);
262 obuf = new_fvec(overlap_size, channels);
263 fftgrain = new_cvec(buffer_size, channels);
266 pitchdet = new_aubio_pitchdetection(buffer_size*4,
267 overlap_size, channels, samplerate, type_pitch, mode_pitch);
270 note_buffer = new_fvec(median, 1);
271 note_buffer2= new_fvec(median, 1);
275 pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
277 parms = new_aubio_peakpicker(threshold);
278 o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
279 onset = new_fvec(1, channels);
281 o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
282 onset2 = new_fvec(1 , channels);
288 void examples_common_del(void){
290 send_noteon(curnote,0);
291 del_aubio_pitchdetection(pitchdet);
293 del_fvec(note_buffer);
294 del_fvec(note_buffer2);
304 void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
307 aubio_jack_t * jack_setup;
308 debug("Jack init ...\n");
309 jack_setup = new_aubio_jack(channels, channels,
310 (aubio_process_func_t)process_func);
312 debug("Midi init ...\n");
313 mplay = new_aubio_midi_player();
314 mdriver = new_aubio_midi_driver("alsa_seq",
315 (handle_midi_event_func_t)aubio_midi_send_event, mplay);
316 event = new_aubio_midi_event();
318 debug("Jack activation ...\n");
319 aubio_jack_activate(jack_setup);
320 debug("Processing (Ctrl+C to quit) ...\n");
322 aubio_jack_close(jack_setup);
324 send_noteon(curnote,0);
325 del_aubio_midi_driver(mdriver);
329 outmsg("Compiled without jack output, exiting.\n");
334 debug("Processing ...\n");
338 while (overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
341 process_func(ibuf->data, obuf->data, overlap_size);
343 if (output_filename != NULL) {
344 aubio_sndfile_write(fileout,overlap_size,obuf);
349 debug("Processed %d frames of %d samples.\n", frames, buffer_size);
350 del_aubio_sndfile(file);
352 if (output_filename != NULL)
353 del_aubio_sndfile(fileout);
360 void send_noteon(int pitch, int velo)
362 smpl_t mpitch = (FLOOR)(aubio_freqtomidi(pitch)+.5);
363 /* we should check if we use midi here, not jack */
367 aubio_midi_event_set_type(event,NOTE_OFF);
369 aubio_midi_event_set_type(event,NOTE_ON);
371 aubio_midi_event_set_channel(event,0);
372 aubio_midi_event_set_pitch(event,mpitch);
373 aubio_midi_event_set_velocity(event,velo);
374 aubio_midi_direct_output(mdriver,event);
380 outmsg("%f\n",frames*overlap_size/(float)samplerate);
382 outmsg("%f\t%f\t", mpitch,
383 frames*overlap_size/(float)samplerate);
389 void note_append(fvec_t * note_buffer, smpl_t curnote) {
391 for (i = 0; i < note_buffer->length - 1; i++) {
392 note_buffer->data[0][i] = note_buffer->data[0][i+1];
394 note_buffer->data[0][note_buffer->length - 1] = curnote;
398 uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
400 for (i = 0; i < note_buffer->length; i++) {
401 note_buffer2->data[0][i] = note_buffer->data[0][i];
403 return vec_median(note_buffer2);