2 Copyright (C) 2003 Paul Brossier
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.
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.
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.
28 #include <math.h> //required for FLOORF :(
31 const char * output_filename = NULL;
32 const char * input_filename = NULL;
33 const char * onset_filename = "/usr/share/sounds/aubio/woodblock.aiff";
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;
47 uint_t samplerate = 44100;
56 aubio_onsetdetection_t *o;
57 aubio_onsetdetection_t *o2;
61 aubio_pickpeak_t * parms;
65 cvec_t * fftpitch = NULL;
66 aubio_filter_t * filter = NULL;
67 aubio_pvoc_t * pvi = NULL;
68 aubio_pitchmcomb_t * p = NULL;
70 fvec_t * note_buffer = NULL;
71 fvec_t * note_buffer2 = NULL;
72 uint_t medianfiltlen = 6;
77 aubio_midi_player_t * mplay;
78 aubio_midi_driver_t * mdriver;
79 aubio_midi_event_t * event;
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)
84 /* we should check if we use midi here, not jack */
88 aubio_midi_event_set_type(event,NOTE_OFF);
90 aubio_midi_event_set_type(event,NOTE_ON);
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);
100 outmsg("%f\n",frames*overlap_size/(float)samplerate);
102 outmsg("%d\t%f\t",pitch,frames*overlap_size/(float)samplerate);
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.*/
111 void note_append(fvec_t * note_buffer, smpl_t curnote);
112 void note_append(fvec_t * note_buffer, smpl_t curnote) {
114 for (i = 0; i < note_buffer->length - 1; i++) {
115 note_buffer->data[0][i] = note_buffer->data[0][i+1];
117 note_buffer->data[0][note_buffer->length - 1] = curnote;
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){
124 for (i = 0; i < note_buffer->length; i++) {
125 note_buffer2->data[0][i] = note_buffer->data[0][i];
127 return vec_median(note_buffer2);
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++) {
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);
150 if (pos == overlap_size-1) {
152 aubio_pvoc_do (pv,ibuf, fftgrain);
153 aubio_onsetdetection(o,fftgrain, onset);
155 aubio_onsetdetection(o2,fftgrain, onset2);
156 onset->data[0][0] *= onset2->data[0][0];
158 isonset = aubio_peakpick_pimrt(onset,parms);
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);
167 //outmsg("%f %f\n", pitch, newnote);
169 /* curlevel is negatif or 1 if silence */
170 curlevel = aubio_level_detection(ibuf, threshold2);
172 /* test for silence */
173 if (curlevel == 1.) {
177 send_noteon(mdriver,curnote,0);
180 for (pos = 0; pos < overlap_size; pos++){
181 obuf->data[0][pos] = woodblock->data[0][pos];
185 // if (curlevel != 1) {
186 // if (bufpos == note_buffer->length)
187 // curnote = aubio_getnote(note_buffer);
192 if (isready == note_buffer->length)
194 //outmsg("%d, %f\n", isready, curnote);
195 send_noteon(mdriver,curnote,0);
197 newnote = get_note(note_buffer, note_buffer2);
199 /* get and send new one */
201 send_noteon(mdriver,curnote,0);
204 send_noteon(mdriver,curnote,127+(int)FLOOR(curlevel));
207 for (pos = 0; pos < overlap_size; pos++)
208 obuf->data[0][pos] = 0.;
210 /* end of block loop */
211 //aubio_pvoc_rdo(pv,fftgrain, obuf);
212 pos = -1; /* so it will be zero next j loop */
219 int main(int argc, char **argv) {
221 aubio_file_t * file = NULL;
222 aubio_file_t * fileout = NULL;
224 aubio_file_t * onsetfile = new_file_ro(onset_filename);
225 /* parse command line arguments */
226 parse_args(argc, argv);
230 debug("Opening files ...\n");
231 file = new_file_ro (input_filename);
233 channels = aubio_file_channels(file);
234 if (output_filename != NULL)
235 fileout = new_file_wo(file, output_filename);
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);
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);
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);
254 debug("Phase voc init ... \n");
255 pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
258 parms = new_aubio_peakpicker(threshold);
259 o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
260 onset = new_fvec(1, channels);
262 o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
263 onset2 = new_fvec(1 , channels);
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);
274 mplay = new_aubio_midi_player();
276 mdriver = new_aubio_midi_driver("alsa_seq",
277 (handle_midi_event_func_t)aubio_midi_send_event, mplay);
279 event = new_aubio_midi_event();
281 debug("Jack activation ...\n");
282 aubio_jack_activate(jack_setup);
283 debug("Processing (Ctrl+C to quit) ...\n");
285 send_noteon(mdriver,curnote,0);
286 aubio_jack_close(jack_setup);
287 del_aubio_midi_driver(mdriver);
289 outmsg("Compiled without jack output, exiting.");
294 debug("Processing ...\n");
298 while (overlap_size == file_read(file, overlap_size, ibuf))
301 aubio_process(ibuf->data, obuf->data, overlap_size);
302 if (output_filename != NULL) {
303 file_write(fileout,overlap_size,obuf);
307 send_noteon(mdriver,curnote,0);
309 debug("Processed %d frames of %d samples.\n", frames, buffer_size);
312 if (output_filename != NULL)
324 del_fvec(note_buffer);
325 del_fvec(note_buffer2);
327 debug("End of program.\n");