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.
24 #include <math.h> // how do i do a floorf with a mask again ?
29 const char * output_filename = NULL;
30 const char * input_filename = NULL;
31 const char * onset_filename = "/usr/share/sounds/aubio/woodblock.aiff";
37 /* energy,specdiff,hfc,complexdomain,phase */
38 aubio_onsetdetection_type type_onset = hfc;
39 aubio_onsetdetection_type type_onset2 = hfc;
40 smpl_t threshold = 0.3;
41 smpl_t threshold2 = -90.;
42 uint_t buffer_size = 1024;
43 uint_t overlap_size = 512;
45 uint_t samplerate = 44100;
54 aubio_onsetdetection_t *o;
55 aubio_onsetdetection_t *o2;
59 aubio_pickpeak_t * parms;
64 aubio_pitchdetection_t * pitchdet;
70 aubio_midi_player_t * mplay;
71 aubio_midi_driver_t * mdriver;
72 aubio_midi_event_t * event;
74 void send_noteon(aubio_midi_driver_t * d, int pitch, int velo);
75 void send_noteon(aubio_midi_driver_t * d, int pitch, int velo)
77 /* we should check if we use midi here, not jack */
81 aubio_midi_event_set_type(event,NOTE_OFF);
83 aubio_midi_event_set_type(event,NOTE_ON);
85 aubio_midi_event_set_channel(event,0);
86 aubio_midi_event_set_pitch(event,pitch);
87 aubio_midi_event_set_velocity(event,velo);
88 aubio_midi_direct_output(mdriver,event);
93 outmsg("%f\n",frames*overlap_size/(float)samplerate);
95 outmsg("%d\t%f\t",pitch,frames*overlap_size/(float)samplerate);
100 int aubio_process(float **input, float **output, int nframes);
101 int aubio_process(float **input, float **output, int nframes) {
102 unsigned int i; /*channels*/
103 unsigned int j; /*frames*/
104 unsigned int pos = 0; /*frames%dspblocksize*/
105 for (j=0;j<nframes;j++) {
107 for (i=0;i<channels;i++) {
108 /* write input to datanew */
109 fvec_write_sample(ibuf, input[i][j], i, pos);
110 /* put synthnew in output */
111 output[i][j] = fvec_read_sample(obuf, i, pos);
115 if (pos == overlap_size-1) {
117 aubio_pvoc_do (pv,ibuf, fftgrain);
118 aubio_onsetdetection(o,fftgrain, onset);
120 aubio_onsetdetection(o2,fftgrain, onset2);
121 onset->data[0][0] *= onset2->data[0][0];
123 isonset = aubio_peakpick_pimrt(onset,parms);
125 pitch = aubio_pitchdetection(pitchdet,ibuf);
127 /* curlevel is negatif or 1 if silence */
128 curlevel = aubio_level_detection(ibuf, threshold2);
130 /* test for silence */
131 if (curlevel == 1.) {
134 send_noteon(mdriver,curnote,0);
137 send_noteon(mdriver,curnote,0);
138 //curnote = (int)FLOOR(bintomidi(pitch,samplerate,buffer_size*4) + .5);
139 curnote = (int)FLOOR(freqtomidi(pitch) + .5);
140 /* get and send new one */
142 send_noteon(mdriver,curnote,0);
145 send_noteon(mdriver,curnote,127+(int)FLOOR(curlevel));
147 for (pos = 0; pos < overlap_size; pos++){
148 obuf->data[0][pos] = woodblock->data[0][pos];
152 for (pos = 0; pos < overlap_size; pos++)
153 obuf->data[0][pos] = 0.;
155 /* end of block loop */
156 pos = -1; /* so it will be zero next j loop */
163 int main(int argc, char **argv) {
165 aubio_file_t * file = NULL;
166 aubio_file_t * fileout = NULL;
168 aubio_file_t * onsetfile = new_file_ro(onset_filename);
169 /* parse command line arguments */
170 parse_args(argc, argv);
174 debug("Opening files ...\n");
175 file = new_file_ro (input_filename);
177 samplerate = aubio_file_samplerate(file);
178 channels = aubio_file_channels(file);
179 if (output_filename != NULL)
180 fileout = new_file_wo(file, output_filename);
183 ibuf = new_fvec(overlap_size, channels);
184 obuf = new_fvec(overlap_size, channels);
185 woodblock = new_fvec(buffer_size,1);
186 fftgrain = new_cvec(buffer_size, channels);
188 pitchdet = new_aubio_pitchdetection(buffer_size*4, overlap_size, channels, samplerate, mcomb, freq);
189 //pitchdet = new_aubio_pitchdetection(buffer_size*2, overlap_size, channels, samplerate, yin, freq);
191 /* read the output sound once */
192 file_read(onsetfile, overlap_size, woodblock);
194 pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
196 parms = new_aubio_peakpicker(threshold);
197 o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
198 onset = new_fvec(1, channels);
200 o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
201 onset2 = new_fvec(1 , channels);
206 aubio_jack_t * jack_setup;
207 debug("Midi init ...\n");
208 debug("Jack init ...\n");
209 jack_setup = new_aubio_jack(channels, channels,
210 (aubio_process_func_t)aubio_process);
212 mplay = new_aubio_midi_player();
214 mdriver = new_aubio_midi_driver("alsa_seq",
215 (handle_midi_event_func_t)aubio_midi_send_event, mplay);
217 event = new_aubio_midi_event();
219 debug("Jack activation ...\n");
220 aubio_jack_activate(jack_setup);
221 debug("Processing (Ctrl+C to quit) ...\n");
223 send_noteon(mdriver,curnote,0);
224 aubio_jack_close(jack_setup);
225 del_aubio_midi_driver(mdriver);
228 outmsg("Compiled without jack output, exiting.\n");
233 debug("Processing ...\n");
237 while (overlap_size == file_read(file, overlap_size, ibuf))
240 aubio_process(ibuf->data, obuf->data, overlap_size);
241 if (output_filename != NULL) {
242 file_write(fileout,overlap_size,obuf);
246 send_noteon(mdriver,curnote,0);
248 debug("Processed %d frames of %d samples.\n", frames, buffer_size);
251 if (output_filename != NULL)
260 del_aubio_pitchdetection(pitchdet);
263 debug("End of program.\n");