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 = "/usr/share/sounds/aubio/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 mode = aubio_pitch_yin; // aubio_pitch_mcomb
64 fvec_t * note_buffer = NULL;
65 fvec_t * note_buffer2 = NULL;
70 aubio_midi_player_t * mplay;
71 aubio_midi_driver_t * mdriver;
72 aubio_midi_event_t * event;
80 /* badly redeclare some things */
81 aubio_onsetdetection_type type_onset;
84 const char * prog_name;
86 void usage (FILE * stream, int exit_code)
88 fprintf(stream, "usage: %s [ options ] \n", prog_name);
90 " -j --jack Use Jack.\n"
91 " -o --output Output type.\n"
92 " -i --input Input type.\n"
93 " -h --help Display this message.\n"
94 " -v --verbose Print verbose message.\n"
99 int parse_args (int argc, char **argv) {
100 const char *options = "hvjo:i:O:t:s:H:a";
102 struct option long_options[] =
104 {"help" , 0, NULL, 'h'},
105 {"verbose" , 0, NULL, 'v'},
106 {"jack" , 0, NULL, 'j'},
107 {"output" , 1, NULL, 'o'},
108 {"input" , 1, NULL, 'i'},
109 {"onset" , 1, NULL, 'O'},
110 {"threshold", 1, NULL, 't'},
111 {"silence" , 1, NULL, 's'},
112 {"averaging", 0, NULL, 'a'},
113 {"hopsize", 1, NULL, 'H'},
122 next_option = getopt_long (argc, argv, options,
124 switch (next_option) {
126 output_filename = optarg;
129 input_filename = optarg;
134 case 'v': /* verbose */
137 case 'j': /* verbose */
140 case 'O': /*onset type*/
141 if (strcmp(optarg,"energy") == 0)
142 type_onset = aubio_onset_energy;
143 else if (strcmp(optarg,"specdiff") == 0)
144 type_onset = aubio_onset_specdiff;
145 else if (strcmp(optarg,"hfc") == 0)
146 type_onset = aubio_onset_hfc;
147 else if (strcmp(optarg,"complexdomain") == 0)
148 type_onset = aubio_onset_complex;
149 else if (strcmp(optarg,"complex") == 0)
150 type_onset = aubio_onset_complex;
151 else if (strcmp(optarg,"phase") == 0)
152 type_onset = aubio_onset_phase;
153 else if (strcmp(optarg,"mkl") == 0)
154 type_onset = aubio_onset_mkl;
155 else if (strcmp(optarg,"kl") == 0)
156 type_onset = aubio_onset_kl;
158 debug("could not get onset type.\n");
163 case 's': /* threshold value for onset */
164 threshold2 = (smpl_t)atof(optarg);
166 case 't': /* threshold value for onset */
167 threshold = (smpl_t)atof(optarg);
169 if (!isfinite(threshold)) {
170 debug("could not get threshold.\n");
179 overlap_size = atoi(optarg);
181 case '?': /* unknown options */
184 case -1: /* done with options */
186 default: /*something else unexpected */
190 while (next_option != -1);
192 if (input_filename != NULL) {
193 debug ("Input file : %s\n", input_filename );
194 } else if (input_filename != NULL && output_filename != NULL) {
195 debug ("Input file : %s\n", input_filename );
196 debug ("Output file : %s\n", output_filename );
200 debug ("Jack input output\n");
203 debug ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n");
210 void examples_common_init(int argc,char ** argv) {
213 aubio_sndfile_t * onsetfile = NULL;
214 /* parse command line arguments */
215 parse_args(argc, argv);
217 woodblock = new_fvec(buffer_size,1);
218 if (output_filename || usejack) {
219 (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
220 (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
221 (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
224 /* read the output sound once */
225 aubio_sndfile_read(onsetfile, overlap_size, woodblock);
230 debug("Opening files ...\n");
231 file = new_aubio_sndfile_ro (input_filename);
232 if (verbose) aubio_sndfile_info(file);
233 channels = aubio_sndfile_channels(file);
234 samplerate = aubio_sndfile_samplerate(file);
235 if (output_filename != NULL)
236 fileout = new_aubio_sndfile_wo(file, output_filename);
239 ibuf = new_fvec(overlap_size, channels);
240 obuf = new_fvec(overlap_size, channels);
241 fftgrain = new_cvec(buffer_size, channels);
244 pitchdet = new_aubio_pitchdetection(buffer_size*4,
245 overlap_size, channels, samplerate, mode, aubio_pitchm_freq);
248 note_buffer = new_fvec(median, 1);
249 note_buffer2= new_fvec(median, 1);
253 pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
255 parms = new_aubio_peakpicker(threshold);
256 o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
257 onset = new_fvec(1, channels);
259 o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
260 onset2 = new_fvec(1 , channels);
266 void examples_common_del(void){
268 send_noteon(curnote,0);
269 del_aubio_pitchdetection(pitchdet);
271 del_fvec(note_buffer);
272 del_fvec(note_buffer2);
282 void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
285 aubio_jack_t * jack_setup;
286 debug("Jack init ...\n");
287 jack_setup = new_aubio_jack(channels, channels,
288 (aubio_process_func_t)process_func);
290 debug("Midi init ...\n");
291 mplay = new_aubio_midi_player();
292 mdriver = new_aubio_midi_driver("alsa_seq",
293 (handle_midi_event_func_t)aubio_midi_send_event, mplay);
294 event = new_aubio_midi_event();
296 debug("Jack activation ...\n");
297 aubio_jack_activate(jack_setup);
298 debug("Processing (Ctrl+C to quit) ...\n");
300 aubio_jack_close(jack_setup);
302 send_noteon(curnote,0);
303 del_aubio_midi_driver(mdriver);
307 outmsg("Compiled without jack output, exiting.\n");
312 debug("Processing ...\n");
316 while (overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
319 process_func(ibuf->data, obuf->data, overlap_size);
321 if (output_filename != NULL) {
322 aubio_sndfile_write(fileout,overlap_size,obuf);
327 debug("Processed %d frames of %d samples.\n", frames, buffer_size);
328 del_aubio_sndfile(file);
330 if (output_filename != NULL)
331 del_aubio_sndfile(fileout);
338 void send_noteon(int pitch, int velo)
340 smpl_t mpitch = (FLOOR)(aubio_freqtomidi(pitch)+.5);
341 /* we should check if we use midi here, not jack */
345 aubio_midi_event_set_type(event,NOTE_OFF);
347 aubio_midi_event_set_type(event,NOTE_ON);
349 aubio_midi_event_set_channel(event,0);
350 aubio_midi_event_set_pitch(event,mpitch);
351 aubio_midi_event_set_velocity(event,velo);
352 aubio_midi_direct_output(mdriver,event);
358 outmsg("%f\n",frames*overlap_size/(float)samplerate);
360 outmsg("%f\t%f\t", mpitch,
361 frames*overlap_size/(float)samplerate);
367 void note_append(fvec_t * note_buffer, smpl_t curnote) {
369 for (i = 0; i < note_buffer->length - 1; i++) {
370 note_buffer->data[0][i] = note_buffer->data[0][i+1];
372 note_buffer->data[0][note_buffer->length - 1] = curnote;
376 uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
378 for (i = 0; i < note_buffer->length; i++) {
379 note_buffer2->data[0][i] = note_buffer->data[0][i];
381 return vec_median(note_buffer2);