1 | /* |
---|
2 | Copyright (C) 2003 Paul Brossier |
---|
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. |
---|
8 | |
---|
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. |
---|
13 | |
---|
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 | */ |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <stdarg.h> |
---|
22 | #include <getopt.h> |
---|
23 | #include <unistd.h> |
---|
24 | #include "aubio.h" |
---|
25 | #include "aubioext.h" |
---|
26 | #include "utils.h" |
---|
27 | |
---|
28 | #include <math.h> //required for FLOORF :( |
---|
29 | |
---|
30 | /* settings */ |
---|
31 | const char * output_filename = NULL; |
---|
32 | const char * input_filename = NULL; |
---|
33 | const char * onset_filename = "/usr/share/sounds/aubio/woodblock.aiff"; |
---|
34 | int verbose = 0; |
---|
35 | int usejack = 0; |
---|
36 | int usedoubled = 1; |
---|
37 | int usemidi = 1; |
---|
38 | |
---|
39 | /* energy,specdiff,hfc,complexdomain,phase */ |
---|
40 | aubio_onsetdetection_type type_onset = hfc; |
---|
41 | aubio_onsetdetection_type type_onset2 = complexdomain; |
---|
42 | smpl_t threshold = 0.3; |
---|
43 | smpl_t threshold2 = -90.; |
---|
44 | uint_t buffer_size = 1024; |
---|
45 | uint_t overlap_size = 512; |
---|
46 | uint_t channels = 1; |
---|
47 | uint_t samplerate = 44100; |
---|
48 | |
---|
49 | /* global objects */ |
---|
50 | int frames; |
---|
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 | /* pitch objects */ |
---|
64 | smpl_t pitch = 0.; |
---|
65 | cvec_t * fftpitch = NULL; |
---|
66 | aubio_filter_t * filter = NULL; |
---|
67 | aubio_pvoc_t * pvi = NULL; |
---|
68 | aubio_pitchmcomb_t * p = NULL; |
---|
69 | |
---|
70 | fvec_t * note_buffer = NULL; |
---|
71 | fvec_t * note_buffer2 = NULL; |
---|
72 | uint_t medianfiltlen = 6; |
---|
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 | void send_noteon(aubio_midi_driver_t * d, int pitch, int velo); |
---|
82 | void send_noteon(aubio_midi_driver_t * d, int pitch, int velo) |
---|
83 | { |
---|
84 | /* we should check if we use midi here, not jack */ |
---|
85 | #if ALSA_SUPPORT |
---|
86 | if (usejack) { |
---|
87 | if (velo==0) { |
---|
88 | aubio_midi_event_set_type(event,NOTE_OFF); |
---|
89 | } else { |
---|
90 | aubio_midi_event_set_type(event,NOTE_ON); |
---|
91 | } |
---|
92 | aubio_midi_event_set_channel(event,0); |
---|
93 | aubio_midi_event_set_pitch(event,pitch); |
---|
94 | aubio_midi_event_set_velocity(event,velo); |
---|
95 | aubio_midi_direct_output(mdriver,event); |
---|
96 | } else |
---|
97 | #endif |
---|
98 | { |
---|
99 | if (velo==0) { |
---|
100 | outmsg("%f\n",frames*overlap_size/(float)samplerate); |
---|
101 | } else { |
---|
102 | outmsg("%d\t%f\t",pitch,frames*overlap_size/(float)samplerate); |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** append new note candidate to the note_buffer and return filtered value. we |
---|
109 | * need to copy the input array as vec_median destroy its input data.*/ |
---|
110 | |
---|
111 | void note_append(fvec_t * note_buffer, smpl_t curnote); |
---|
112 | void note_append(fvec_t * note_buffer, smpl_t curnote) { |
---|
113 | uint_t i = 0; |
---|
114 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
115 | note_buffer->data[0][i] = note_buffer->data[0][i+1]; |
---|
116 | } |
---|
117 | note_buffer->data[0][note_buffer->length - 1] = curnote; |
---|
118 | return; |
---|
119 | } |
---|
120 | |
---|
121 | uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2); |
---|
122 | uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){ |
---|
123 | uint_t i = 0; |
---|
124 | for (i = 0; i < note_buffer->length; i++) { |
---|
125 | note_buffer2->data[0][i] = note_buffer->data[0][i]; |
---|
126 | } |
---|
127 | return vec_median(note_buffer2); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | smpl_t curnote = 0.; |
---|
132 | smpl_t newnote = 0.; |
---|
133 | uint_t isready = 0; |
---|
134 | |
---|
135 | int aubio_process(float **input, float **output, int nframes); |
---|
136 | int aubio_process(float **input, float **output, int nframes) { |
---|
137 | unsigned int i; /*channels*/ |
---|
138 | unsigned int j; /*frames*/ |
---|
139 | unsigned int pos = 0; /*frames%dspblocksize*/ |
---|
140 | for (j=0;j<nframes;j++) { |
---|
141 | if(usejack) { |
---|
142 | for (i=0;i<channels;i++) { |
---|
143 | /* write input to datanew */ |
---|
144 | fvec_write_sample(ibuf, input[i][j], i, pos); |
---|
145 | /* put synthnew in output */ |
---|
146 | output[i][j] = fvec_read_sample(obuf, i, pos); |
---|
147 | } |
---|
148 | } |
---|
149 | /*time for fft*/ |
---|
150 | if (pos == overlap_size-1) { |
---|
151 | /* block loop */ |
---|
152 | aubio_pvoc_do (pv,ibuf, fftgrain); |
---|
153 | aubio_onsetdetection(o,fftgrain, onset); |
---|
154 | if (usedoubled) { |
---|
155 | aubio_onsetdetection(o2,fftgrain, onset2); |
---|
156 | onset->data[0][0] *= onset2->data[0][0]; |
---|
157 | } |
---|
158 | isonset = aubio_peakpick_pimrt(onset,parms); |
---|
159 | |
---|
160 | aubio_filter_do(filter,ibuf); |
---|
161 | aubio_filter_do(filter,ibuf); |
---|
162 | aubio_pvoc_do(pvi,ibuf, fftpitch); |
---|
163 | pitch = aubio_pitchmcomb_detect(p,fftpitch); |
---|
164 | newnote = (FLOOR)(bintomidi(pitch,samplerate,buffer_size*4)+.5); |
---|
165 | note_append(note_buffer, newnote); |
---|
166 | |
---|
167 | //outmsg("%f %f\n", pitch, newnote); |
---|
168 | |
---|
169 | /* curlevel is negatif or 1 if silence */ |
---|
170 | curlevel = aubio_level_detection(ibuf, threshold2); |
---|
171 | if (isonset) { |
---|
172 | /* test for silence */ |
---|
173 | if (curlevel == 1.) { |
---|
174 | isonset=0; |
---|
175 | isready=0; |
---|
176 | /* send note off */ |
---|
177 | send_noteon(mdriver,curnote,0); |
---|
178 | } else { |
---|
179 | isready = 1; |
---|
180 | for (pos = 0; pos < overlap_size; pos++){ |
---|
181 | obuf->data[0][pos] = woodblock->data[0][pos]; |
---|
182 | } |
---|
183 | } |
---|
184 | } else { |
---|
185 | // if (curlevel != 1) { |
---|
186 | // if (bufpos == note_buffer->length) |
---|
187 | // curnote = aubio_getnote(note_buffer); |
---|
188 | // } |
---|
189 | // |
---|
190 | if (isready > 0) |
---|
191 | isready++; |
---|
192 | if (isready == note_buffer->length) |
---|
193 | { |
---|
194 | //outmsg("%d, %f\n", isready, curnote); |
---|
195 | send_noteon(mdriver,curnote,0); |
---|
196 | /* kill old note */ |
---|
197 | newnote = get_note(note_buffer, note_buffer2); |
---|
198 | curnote = newnote; |
---|
199 | /* get and send new one */ |
---|
200 | /*if (curnote<45){ |
---|
201 | send_noteon(mdriver,curnote,0); |
---|
202 | } else {*/ |
---|
203 | if (curnote>45){ |
---|
204 | send_noteon(mdriver,curnote,127+(int)FLOOR(curlevel)); |
---|
205 | } |
---|
206 | } |
---|
207 | for (pos = 0; pos < overlap_size; pos++) |
---|
208 | obuf->data[0][pos] = 0.; |
---|
209 | } |
---|
210 | /* end of block loop */ |
---|
211 | //aubio_pvoc_rdo(pv,fftgrain, obuf); |
---|
212 | pos = -1; /* so it will be zero next j loop */ |
---|
213 | } |
---|
214 | pos++; |
---|
215 | } |
---|
216 | return 1; |
---|
217 | } |
---|
218 | |
---|
219 | int main(int argc, char **argv) { |
---|
220 | |
---|
221 | aubio_file_t * file = NULL; |
---|
222 | aubio_file_t * fileout = NULL; |
---|
223 | |
---|
224 | aubio_file_t * onsetfile = new_file_ro(onset_filename); |
---|
225 | /* parse command line arguments */ |
---|
226 | parse_args(argc, argv); |
---|
227 | |
---|
228 | if(!usejack) |
---|
229 | { |
---|
230 | debug("Opening files ...\n"); |
---|
231 | file = new_file_ro (input_filename); |
---|
232 | file_info(file); |
---|
233 | channels = aubio_file_channels(file); |
---|
234 | if (output_filename != NULL) |
---|
235 | fileout = new_file_wo(file, output_filename); |
---|
236 | } |
---|
237 | |
---|
238 | ibuf = new_fvec(overlap_size, channels); |
---|
239 | obuf = new_fvec(overlap_size, channels); |
---|
240 | woodblock = new_fvec(buffer_size,1); |
---|
241 | fftgrain = new_cvec(buffer_size, channels); |
---|
242 | |
---|
243 | pvi = new_aubio_pvoc(buffer_size*4, overlap_size, channels); |
---|
244 | fftpitch = new_cvec(buffer_size*4, channels); |
---|
245 | filter = new_aubio_adsgn_filter((smpl_t)samplerate); |
---|
246 | p = new_aubio_pitchmcomb(buffer_size*4,channels); |
---|
247 | |
---|
248 | note_buffer = new_fvec(medianfiltlen, 1); |
---|
249 | note_buffer2= new_fvec(medianfiltlen, 1); |
---|
250 | /* read the output sound once */ |
---|
251 | file_read(onsetfile, overlap_size, woodblock); |
---|
252 | |
---|
253 | /* phase vocoder */ |
---|
254 | debug("Phase voc init ... \n"); |
---|
255 | pv = new_aubio_pvoc(buffer_size, overlap_size, channels); |
---|
256 | |
---|
257 | /* onsets */ |
---|
258 | parms = new_aubio_peakpicker(threshold); |
---|
259 | o = new_aubio_onsetdetection(type_onset,buffer_size,channels); |
---|
260 | onset = new_fvec(1, channels); |
---|
261 | if (usedoubled) { |
---|
262 | o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels); |
---|
263 | onset2 = new_fvec(1 , channels); |
---|
264 | } |
---|
265 | |
---|
266 | if(usejack) { |
---|
267 | #ifdef JACK_SUPPORT |
---|
268 | aubio_jack_t * jack_setup; |
---|
269 | debug("Midi init ...\n"); |
---|
270 | debug("Jack init ...\n"); |
---|
271 | jack_setup = new_aubio_jack(channels, channels, |
---|
272 | (aubio_process_func_t)aubio_process); |
---|
273 | |
---|
274 | mplay = new_aubio_midi_player(); |
---|
275 | |
---|
276 | mdriver = new_aubio_midi_driver("alsa_seq", |
---|
277 | (handle_midi_event_func_t)aubio_midi_send_event, mplay); |
---|
278 | |
---|
279 | event = new_aubio_midi_event(); |
---|
280 | |
---|
281 | debug("Jack activation ...\n"); |
---|
282 | aubio_jack_activate(jack_setup); |
---|
283 | debug("Processing (Ctrl+C to quit) ...\n"); |
---|
284 | pause(); |
---|
285 | send_noteon(mdriver,curnote,0); |
---|
286 | aubio_jack_close(jack_setup); |
---|
287 | del_aubio_midi_driver(mdriver); |
---|
288 | #else |
---|
289 | outmsg("Compiled without jack output, exiting."); |
---|
290 | #endif |
---|
291 | |
---|
292 | } else { |
---|
293 | /* phasevoc */ |
---|
294 | debug("Processing ...\n"); |
---|
295 | |
---|
296 | frames = 0; |
---|
297 | |
---|
298 | while (overlap_size == file_read(file, overlap_size, ibuf)) |
---|
299 | { |
---|
300 | isonset=0; |
---|
301 | aubio_process(ibuf->data, obuf->data, overlap_size); |
---|
302 | if (output_filename != NULL) { |
---|
303 | file_write(fileout,overlap_size,obuf); |
---|
304 | } |
---|
305 | frames++; |
---|
306 | } |
---|
307 | send_noteon(mdriver,curnote,0); |
---|
308 | |
---|
309 | debug("Processed %d frames of %d samples.\n", frames, buffer_size); |
---|
310 | del_file(file); |
---|
311 | |
---|
312 | if (output_filename != NULL) |
---|
313 | del_file(fileout); |
---|
314 | |
---|
315 | } |
---|
316 | |
---|
317 | del_aubio_pvoc(pv); |
---|
318 | del_fvec(obuf); |
---|
319 | del_fvec(ibuf); |
---|
320 | del_cvec(fftgrain); |
---|
321 | del_cvec(fftpitch); |
---|
322 | del_aubio_pvoc(pvi); |
---|
323 | del_fvec(onset); |
---|
324 | del_fvec(note_buffer); |
---|
325 | del_fvec(note_buffer2); |
---|
326 | |
---|
327 | debug("End of program.\n"); |
---|
328 | fflush(stderr); |
---|
329 | return 0; |
---|
330 | } |
---|
331 | |
---|