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 | |
---|
15 | /* not supported yet */ |
---|
16 | #ifdef LADCCA_SUPPORT |
---|
17 | #include <ladcca/ladcca.h> |
---|
18 | cca_client_t * aubio_cca_client; |
---|
19 | #endif /* LADCCA_SUPPORT */ |
---|
20 | |
---|
21 | /* settings */ |
---|
22 | const char * output_filename = NULL; |
---|
23 | const char * input_filename = NULL; |
---|
24 | const char * onset_filename = AUBIO_PREFIX "/share/sounds/" PACKAGE "/woodblock.aiff"; |
---|
25 | int frames = 0; |
---|
26 | int verbose = 0; |
---|
27 | int usejack = 0; |
---|
28 | int usedoubled = 1; |
---|
29 | |
---|
30 | |
---|
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; |
---|
38 | uint_t channels = 1; |
---|
39 | uint_t samplerate = 44100; |
---|
40 | |
---|
41 | |
---|
42 | aubio_sndfile_t * file = NULL; |
---|
43 | aubio_sndfile_t * fileout = NULL; |
---|
44 | |
---|
45 | aubio_pvoc_t * pv; |
---|
46 | fvec_t * ibuf; |
---|
47 | fvec_t * obuf; |
---|
48 | cvec_t * fftgrain; |
---|
49 | fvec_t * woodblock; |
---|
50 | aubio_onsetdetection_t *o; |
---|
51 | aubio_onsetdetection_t *o2; |
---|
52 | fvec_t *onset; |
---|
53 | fvec_t *onset2; |
---|
54 | int isonset = 0; |
---|
55 | aubio_pickpeak_t * parms; |
---|
56 | |
---|
57 | |
---|
58 | /* pitch objects */ |
---|
59 | smpl_t pitch = 0.; |
---|
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; |
---|
63 | uint_t median = 6; |
---|
64 | |
---|
65 | fvec_t * note_buffer = NULL; |
---|
66 | fvec_t * note_buffer2 = NULL; |
---|
67 | smpl_t curlevel = 0.; |
---|
68 | smpl_t maxonset = 0.; |
---|
69 | |
---|
70 | /* midi objects */ |
---|
71 | aubio_midi_player_t * mplay; |
---|
72 | aubio_midi_driver_t * mdriver; |
---|
73 | aubio_midi_event_t * event; |
---|
74 | |
---|
75 | smpl_t curnote = 0.; |
---|
76 | smpl_t newnote = 0.; |
---|
77 | uint_t isready = 0; |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | /* badly redeclare some things */ |
---|
82 | aubio_onsetdetection_type type_onset; |
---|
83 | smpl_t threshold; |
---|
84 | smpl_t averaging; |
---|
85 | const char * prog_name; |
---|
86 | |
---|
87 | void usage (FILE * stream, int exit_code) |
---|
88 | { |
---|
89 | fprintf(stream, "usage: %s [ options ] \n", prog_name); |
---|
90 | fprintf(stream, |
---|
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" |
---|
101 | ); |
---|
102 | exit(exit_code); |
---|
103 | } |
---|
104 | |
---|
105 | int parse_args (int argc, char **argv) { |
---|
106 | const char *options = "hvjo:i:O:t:s:p:H:a"; |
---|
107 | int next_option; |
---|
108 | struct option long_options[] = |
---|
109 | { |
---|
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'}, |
---|
121 | {NULL , 0, NULL, 0} |
---|
122 | }; |
---|
123 | prog_name = argv[0]; |
---|
124 | if( argc < 1 ) { |
---|
125 | usage (stderr, 1); |
---|
126 | return -1; |
---|
127 | } |
---|
128 | do { |
---|
129 | next_option = getopt_long (argc, argv, options, |
---|
130 | long_options, NULL); |
---|
131 | switch (next_option) { |
---|
132 | case 'o': |
---|
133 | output_filename = optarg; |
---|
134 | break; |
---|
135 | case 'i': |
---|
136 | input_filename = optarg; |
---|
137 | break; |
---|
138 | case 'h': /* help */ |
---|
139 | usage (stdout, 0); |
---|
140 | return -1; |
---|
141 | case 'v': /* verbose */ |
---|
142 | verbose = 1; |
---|
143 | break; |
---|
144 | case 'j': |
---|
145 | usejack = 1; |
---|
146 | break; |
---|
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; |
---|
164 | else { |
---|
165 | errmsg("unknown onset type.\n"); |
---|
166 | abort(); |
---|
167 | } |
---|
168 | usedoubled = 0; |
---|
169 | break; |
---|
170 | case 's': /* threshold value for onset */ |
---|
171 | threshold2 = (smpl_t)atof(optarg); |
---|
172 | break; |
---|
173 | case 't': /* threshold value for onset */ |
---|
174 | threshold = (smpl_t)atof(optarg); |
---|
175 | /* |
---|
176 | if (!isfinite(threshold)) { |
---|
177 | debug("could not get threshold.\n"); |
---|
178 | abort(); |
---|
179 | } |
---|
180 | */ |
---|
181 | break; |
---|
182 | case 'p': |
---|
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; |
---|
191 | else { |
---|
192 | errmsg("unknown pitch type.\n"); |
---|
193 | abort(); |
---|
194 | } |
---|
195 | break; |
---|
196 | case 'a': |
---|
197 | averaging = 1; |
---|
198 | break; |
---|
199 | case 'H': |
---|
200 | overlap_size = atoi(optarg); |
---|
201 | break; |
---|
202 | case '?': /* unknown options */ |
---|
203 | usage(stderr, 1); |
---|
204 | break; |
---|
205 | case -1: /* done with options */ |
---|
206 | break; |
---|
207 | default: /*something else unexpected */ |
---|
208 | abort (); |
---|
209 | } |
---|
210 | } |
---|
211 | while (next_option != -1); |
---|
212 | |
---|
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 ); |
---|
218 | } else { |
---|
219 | if (JACK_SUPPORT) |
---|
220 | { |
---|
221 | debug ("Jack input output\n"); |
---|
222 | usejack = 1; |
---|
223 | } else { |
---|
224 | debug ("Error: Could not switch to jack mode\n aubio was compiled without jack support\n"); |
---|
225 | exit(1); |
---|
226 | } |
---|
227 | } |
---|
228 | return 0; |
---|
229 | } |
---|
230 | |
---|
231 | void examples_common_init(int argc,char ** argv) { |
---|
232 | |
---|
233 | |
---|
234 | aubio_sndfile_t * onsetfile = NULL; |
---|
235 | /* parse command line arguments */ |
---|
236 | parse_args(argc, argv); |
---|
237 | |
---|
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")); |
---|
244 | } |
---|
245 | if (onsetfile) { |
---|
246 | /* read the output sound once */ |
---|
247 | aubio_sndfile_read(onsetfile, overlap_size, woodblock); |
---|
248 | } |
---|
249 | |
---|
250 | if(!usejack) |
---|
251 | { |
---|
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); |
---|
259 | } |
---|
260 | |
---|
261 | ibuf = new_fvec(overlap_size, channels); |
---|
262 | obuf = new_fvec(overlap_size, channels); |
---|
263 | fftgrain = new_cvec(buffer_size, channels); |
---|
264 | |
---|
265 | if (usepitch) { |
---|
266 | pitchdet = new_aubio_pitchdetection(buffer_size*4, |
---|
267 | overlap_size, channels, samplerate, type_pitch, mode_pitch); |
---|
268 | |
---|
269 | if (median) { |
---|
270 | note_buffer = new_fvec(median, 1); |
---|
271 | note_buffer2= new_fvec(median, 1); |
---|
272 | } |
---|
273 | } |
---|
274 | /* phase vocoder */ |
---|
275 | pv = new_aubio_pvoc(buffer_size, overlap_size, channels); |
---|
276 | /* onsets */ |
---|
277 | parms = new_aubio_peakpicker(threshold); |
---|
278 | o = new_aubio_onsetdetection(type_onset,buffer_size,channels); |
---|
279 | onset = new_fvec(1, channels); |
---|
280 | if (usedoubled) { |
---|
281 | o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels); |
---|
282 | onset2 = new_fvec(1 , channels); |
---|
283 | } |
---|
284 | |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | void examples_common_del(void){ |
---|
289 | if (usepitch) { |
---|
290 | send_noteon(curnote,0); |
---|
291 | del_aubio_pitchdetection(pitchdet); |
---|
292 | if (median) { |
---|
293 | del_fvec(note_buffer); |
---|
294 | del_fvec(note_buffer2); |
---|
295 | } |
---|
296 | } |
---|
297 | del_aubio_pvoc(pv); |
---|
298 | del_fvec(obuf); |
---|
299 | del_fvec(ibuf); |
---|
300 | del_cvec(fftgrain); |
---|
301 | del_fvec(onset); |
---|
302 | } |
---|
303 | |
---|
304 | void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){ |
---|
305 | if(usejack) { |
---|
306 | #if JACK_SUPPORT |
---|
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); |
---|
311 | if (usepitch) { |
---|
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(); |
---|
317 | } |
---|
318 | debug("Jack activation ...\n"); |
---|
319 | aubio_jack_activate(jack_setup); |
---|
320 | debug("Processing (Ctrl+C to quit) ...\n"); |
---|
321 | pause(); |
---|
322 | aubio_jack_close(jack_setup); |
---|
323 | if (usepitch) { |
---|
324 | send_noteon(curnote,0); |
---|
325 | del_aubio_midi_driver(mdriver); |
---|
326 | } |
---|
327 | #else |
---|
328 | usage(stderr, 1); |
---|
329 | outmsg("Compiled without jack output, exiting.\n"); |
---|
330 | #endif |
---|
331 | |
---|
332 | } else { |
---|
333 | /* phasevoc */ |
---|
334 | debug("Processing ...\n"); |
---|
335 | |
---|
336 | frames = 0; |
---|
337 | |
---|
338 | while (overlap_size == aubio_sndfile_read(file, overlap_size, ibuf)) |
---|
339 | { |
---|
340 | isonset=0; |
---|
341 | process_func(ibuf->data, obuf->data, overlap_size); |
---|
342 | print(); |
---|
343 | if (output_filename != NULL) { |
---|
344 | aubio_sndfile_write(fileout,overlap_size,obuf); |
---|
345 | } |
---|
346 | frames++; |
---|
347 | } |
---|
348 | |
---|
349 | debug("Processed %d frames of %d samples.\n", frames, buffer_size); |
---|
350 | del_aubio_sndfile(file); |
---|
351 | |
---|
352 | if (output_filename != NULL) |
---|
353 | del_aubio_sndfile(fileout); |
---|
354 | |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | |
---|
360 | void send_noteon(int pitch, int velo) |
---|
361 | { |
---|
362 | smpl_t mpitch = (FLOOR)(aubio_freqtomidi(pitch)+.5); |
---|
363 | /* we should check if we use midi here, not jack */ |
---|
364 | #if ALSA_SUPPORT |
---|
365 | if (usejack) { |
---|
366 | if (velo==0) { |
---|
367 | aubio_midi_event_set_type(event,NOTE_OFF); |
---|
368 | } else { |
---|
369 | aubio_midi_event_set_type(event,NOTE_ON); |
---|
370 | } |
---|
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); |
---|
375 | } else |
---|
376 | #endif |
---|
377 | if (!verbose) |
---|
378 | { |
---|
379 | if (velo==0) { |
---|
380 | outmsg("%f\n",frames*overlap_size/(float)samplerate); |
---|
381 | } else { |
---|
382 | outmsg("%f\t%f\t", mpitch, |
---|
383 | frames*overlap_size/(float)samplerate); |
---|
384 | } |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | |
---|
389 | void note_append(fvec_t * note_buffer, smpl_t curnote) { |
---|
390 | uint_t i = 0; |
---|
391 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
392 | note_buffer->data[0][i] = note_buffer->data[0][i+1]; |
---|
393 | } |
---|
394 | note_buffer->data[0][note_buffer->length - 1] = curnote; |
---|
395 | return; |
---|
396 | } |
---|
397 | |
---|
398 | uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){ |
---|
399 | uint_t i = 0; |
---|
400 | for (i = 0; i < note_buffer->length; i++) { |
---|
401 | note_buffer2->data[0][i] = note_buffer->data[0][i]; |
---|
402 | } |
---|
403 | return vec_median(note_buffer2); |
---|
404 | } |
---|
405 | |
---|