source: examples/aubionotes.c @ a0fd4e4

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since a0fd4e4 was a0fd4e4, checked in by Paul Brossier <piem@altern.org>, 19 years ago

merge aubionotes and aubionotesmedian

add make_audio_plot to gnuplot.py
adds autocorrelation to mathutils
adds Makefile.in to junk files

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