source: examples/aubionotesmedian.c @ 33bf500

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

import 0.1.7.1

  • Property mode set to 100644
File size: 9.3 KB
RevLine 
[96fb8ad]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 "utils.h"
26
27#include <math.h> //required for FLOORF :(
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;
37
38/* energy,specdiff,hfc,complexdomain,phase */
39aubio_onsetdetection_type type_onset  = hfc;
40aubio_onsetdetection_type type_onset2 = complexdomain;
41smpl_t threshold                      = 0.3;
42smpl_t threshold2                     = -90.;
43uint_t buffer_size                    = 1024;
44uint_t overlap_size                   = 512;
45uint_t channels                       = 1;
46uint_t samplerate                     = 44100;
47
48/* global objects */
49int frames;
50aubio_pvoc_t * pv;
51fvec_t * ibuf;
52fvec_t * obuf;
53cvec_t * fftgrain;
54fvec_t * woodblock;
55aubio_onsetdetection_t *o;
56aubio_onsetdetection_t *o2;
57fvec_t *onset;
58fvec_t *onset2;
59int isonset = 0;
60aubio_pickpeak_t * parms;
61
62/* pitch objects */
63smpl_t pitch               = 0.;
64cvec_t * fftpitch          = NULL;
65aubio_filter_t * filter    = NULL;
66aubio_pvoc_t * pvi         = NULL;
67aubio_pitchmcomb_t * p = NULL;
68
69fvec_t * note_buffer = NULL;
70fvec_t * note_buffer2 = NULL;
71uint_t medianfiltlen = 6;
72smpl_t curlevel = 0;
73smpl_t maxonset = 0.;
74
75/* midi objects */
76aubio_midi_player_t * mplay; 
77aubio_midi_driver_t * mdriver; 
78aubio_midi_event_t  * event;
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  unsigned int pos = 0; /*frames%dspblocksize*/
139  for (j=0;j<nframes;j++) {
140    if(usejack) {
141      for (i=0;i<channels;i++) {
142        /* write input to datanew */
143        fvec_write_sample(ibuf, input[i][j], i, pos);
144        /* put synthnew in output */
145        output[i][j] = fvec_read_sample(obuf, i, pos);
146      }
147    }
148    /*time for fft*/
149    if (pos == overlap_size-1) {         
150      /* block loop */
151      aubio_pvoc_do (pv,ibuf, fftgrain);
152      aubio_onsetdetection(o,fftgrain, onset);
153      if (usedoubled) {
154        aubio_onsetdetection(o2,fftgrain, onset2);
155        onset->data[0][0] *= onset2->data[0][0];
156      }
157      isonset = aubio_peakpick_pimrt(onset,parms);
158     
159      aubio_filter_do(filter,ibuf);
160      aubio_filter_do(filter,ibuf);
161      aubio_pvoc_do(pvi,ibuf, fftpitch);
162      pitch = aubio_pitchmcomb_detect(p,fftpitch);
163      newnote = (FLOOR)(bintomidi(pitch,samplerate,buffer_size*4)+.5);
164      note_append(note_buffer, newnote);
165
166      //outmsg("%f    %f\n", pitch, newnote);
167
168      /* curlevel is negatif or 1 if silence */
169      curlevel = aubio_level_detection(ibuf, threshold2);
170      if (isonset) {
171        /* test for silence */
172        if (curlevel == 1.) {
173          isonset=0;
174          isready=0;
175          /* send note off */
176          send_noteon(mdriver,curnote,0);
177        } else {
178          isready = 1;
179          for (pos = 0; pos < overlap_size; pos++){
180            obuf->data[0][pos] = woodblock->data[0][pos];
181          }
182        }
183      } else {
184//        if (curlevel != 1) {
185//          if (bufpos == note_buffer->length)
186//            curnote = aubio_getnote(note_buffer);
187//        }
188//
189        if (isready > 0)
190            isready++;
191        if (isready == note_buffer->length)
192        {
193            //outmsg("%d, %f\n", isready, curnote);
194            send_noteon(mdriver,curnote,0);
195        /* kill old note */
196            newnote = get_note(note_buffer, note_buffer2);
197            curnote = newnote;
198        /* get and send new one */
199        /*if (curnote<45){
200          send_noteon(mdriver,curnote,0);
201        } else {*/
202            if (curnote>45){
203                send_noteon(mdriver,curnote,127+(int)FLOOR(curlevel));
204            }
205        }
206        for (pos = 0; pos < overlap_size; pos++)
207          obuf->data[0][pos] = 0.;
208      }
209      /* end of block loop */
210      //aubio_pvoc_rdo(pv,fftgrain, obuf);
211      pos = -1; /* so it will be zero next j loop */
212    }
213    pos++;
214  }
215  return 1;
216}
217
218int main(int argc, char **argv) {
219
220  aubio_file_t * file = NULL;
221  aubio_file_t * fileout = NULL;
222
223  aubio_file_t * onsetfile = new_file_ro(onset_filename);
224  /* parse command line arguments */
225  parse_args(argc, argv);
226
227  if(!usejack)
228  {
229    debug("Opening files ...\n");
230    file = new_file_ro (input_filename);
231    file_info(file);
232    channels = aubio_file_channels(file);
233    if (output_filename != NULL)
234      fileout = new_file_wo(file, output_filename);
235  }
236
237  ibuf        = new_fvec(overlap_size, channels);
238  obuf        = new_fvec(overlap_size, channels);
239  woodblock   = new_fvec(buffer_size,1);
240  fftgrain    = new_cvec(buffer_size, channels);
241
242  pvi      = new_aubio_pvoc(buffer_size*4, overlap_size, channels);
243  fftpitch = new_cvec(buffer_size*4, channels);
244  filter   = new_aubio_adsgn_filter((smpl_t)samplerate);
245  p        = new_aubio_pitchmcomb(buffer_size*4,channels);
246
247  note_buffer = new_fvec(medianfiltlen, 1);
248  note_buffer2= new_fvec(medianfiltlen, 1);
249  /* read the output sound once */
250  file_read(onsetfile, overlap_size, woodblock);
251
252  /* phase vocoder */
253  debug("Phase voc init ... \n");
254  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
255
256  /* onsets */
257  parms = new_aubio_peakpicker(threshold);
258  o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
259  onset = new_fvec(1, channels);
260  if (usedoubled)    {
261    o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
262    onset2 = new_fvec(1 , channels);
263  }
264
265  if(usejack) {
266#ifdef JACK_SUPPORT
267    aubio_jack_t * jack_setup;
268    debug("Midi init ...\n");
269    debug("Jack init ...\n");
270    jack_setup = new_aubio_jack(channels, channels,
271          (aubio_process_func_t)aubio_process);
272
273    mplay = new_aubio_midi_player();
274
275    mdriver = new_aubio_midi_driver("alsa_seq",
276        (handle_midi_event_func_t)aubio_midi_send_event, mplay);
277
278    event = new_aubio_midi_event();
279   
280    debug("Jack activation ...\n");
281    aubio_jack_activate(jack_setup);
282    debug("Processing (Ctrl+C to quit) ...\n");
283    pause();
284    send_noteon(mdriver,curnote,0);
285    aubio_jack_close(jack_setup);
286    del_aubio_midi_driver(mdriver);
287#else
288    outmsg("Compiled without jack output, exiting.");
289#endif
290
291  } else {
292    /* phasevoc */
293    debug("Processing ...\n");
294
295    frames = 0;
296
297    while (overlap_size == file_read(file, overlap_size, ibuf))
298    {
299      isonset=0;
300      aubio_process(ibuf->data, obuf->data, overlap_size);
301      if (output_filename != NULL) {
302        file_write(fileout,overlap_size,obuf);
303      }
304      frames++;
305    }
306    send_noteon(mdriver,curnote,0);
307
308    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
309    del_file(file);
310
311    if (output_filename != NULL)
312      del_file(fileout);
313
314  }
315
316  del_aubio_pvoc(pv);
317  del_fvec(obuf);
318  del_fvec(ibuf);
319  del_cvec(fftgrain);
320  del_cvec(fftpitch);
321  del_aubio_pvoc(pvi);
322  del_fvec(onset);
323  del_fvec(note_buffer);
324  del_fvec(note_buffer2);
325
326  debug("End of program.\n");
327  fflush(stderr);
328  return 0;
329}
330
Note: See TracBrowser for help on using the repository browser.