source: examples/aubionotes.c @ 1f40359

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

fix jack playing with short buffer (move pos)

(aubionotes.c and aubioonset.c)
added samplerate query to aubioonset.c

  • Property mode set to 100644
File size: 7.7 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> // how do i do a floorf with a mask again ?
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;
37
38/* energy,specdiff,hfc,complexdomain,phase */
39aubio_onsetdetection_type type_onset  = hfc;
40aubio_onsetdetection_type type_onset2 = hfc;
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 */
63int curnote                = 0;
64smpl_t pitch               = 0.;
65aubio_pitchdetection_t * pitchdet;
66int bufpos = 0;
67smpl_t curlevel = 0;
68smpl_t maxonset = 0.;
69
70/* midi objects */
71aubio_midi_player_t * mplay; 
72aubio_midi_driver_t * mdriver; 
73aubio_midi_event_t  * event;
74
75unsigned int pos = 0; /*frames%dspblocksize*/
76
77void send_noteon(aubio_midi_driver_t * d, int pitch, int velo);
78void send_noteon(aubio_midi_driver_t * d, int pitch, int velo)
79{
80    /* we should check if we use midi here, not jack */
81#if ALSA_SUPPORT
82    if (usejack) {
83        if (velo==0) {
84            aubio_midi_event_set_type(event,NOTE_OFF);
85        } else {
86            aubio_midi_event_set_type(event,NOTE_ON);
87        }
88        aubio_midi_event_set_channel(event,0);
89        aubio_midi_event_set_pitch(event,pitch);
90        aubio_midi_event_set_velocity(event,velo);
91        aubio_midi_direct_output(mdriver,event);
92    } else 
93#endif
94    {
95        if (velo==0) {
96            outmsg("%f\n",frames*overlap_size/(float)samplerate);
97        } else {
98            outmsg("%d\t%f\t",pitch,frames*overlap_size/(float)samplerate);
99        }
100    }
101}
102
103int aubio_process(float **input, float **output, int nframes);
104int aubio_process(float **input, float **output, int nframes) {
105  unsigned int i;       /*channels*/
106  unsigned int j;       /*frames*/
107  for (j=0;j<nframes;j++) {
108    if(usejack) {
109      for (i=0;i<channels;i++) {
110        /* write input to datanew */
111        fvec_write_sample(ibuf, input[i][j], i, pos);
112        /* put synthnew in output */
113        output[i][j] = fvec_read_sample(obuf, i, pos);
114      }
115    }
116    /*time for fft*/
117    if (pos == overlap_size-1) {         
118      /* block loop */
119      aubio_pvoc_do (pv,ibuf, fftgrain);
120      aubio_onsetdetection(o,fftgrain, onset);
121      if (usedoubled) {
122        aubio_onsetdetection(o2,fftgrain, onset2);
123        onset->data[0][0] *= onset2->data[0][0];
124      }
125      isonset = aubio_peakpick_pimrt(onset,parms);
126     
127      pitch = aubio_pitchdetection(pitchdet,ibuf);
128
129      /* curlevel is negatif or 1 if silence */
130      curlevel = aubio_level_detection(ibuf, threshold2);
131      if (isonset) {
132        /* test for silence */
133        if (curlevel == 1.) {
134          isonset=0;
135          /* send note off */
136          send_noteon(mdriver,curnote,0);
137        } else {
138          /* kill old note */
139          send_noteon(mdriver,curnote,0);
140          //curnote = (int)FLOOR(bintomidi(pitch,samplerate,buffer_size*4) + .5);
141          curnote = (int)FLOOR(freqtomidi(pitch) + .5);
142          /* get and send new one */
143          /*if (curnote<45){
144            send_noteon(mdriver,curnote,0);
145          } else {*/
146          if (curnote>45){
147            send_noteon(mdriver,curnote,127+(int)FLOOR(curlevel));
148          }
149          for (pos = 0; pos < overlap_size; pos++){
150            obuf->data[0][pos] = woodblock->data[0][pos];
151          }
152        }
153      } else {
154        for (pos = 0; pos < overlap_size; pos++)
155          obuf->data[0][pos] = 0.;
156      }
157      /* end of block loop */
158      pos = -1; /* so it will be zero next j loop */
159    }
160    pos++;
161  }
162  return 1;
163}
164
165int main(int argc, char **argv) {
166
167  aubio_file_t * file = NULL;
168  aubio_file_t * fileout = NULL;
169
170  aubio_file_t * onsetfile = new_file_ro(onset_filename);
171  /* parse command line arguments */
172  parse_args(argc, argv);
173
174  if(!usejack)
175  {
176    debug("Opening files ...\n");
177    file = new_file_ro (input_filename);
178    file_info(file);
179    samplerate = aubio_file_samplerate(file);
180    channels = aubio_file_channels(file);
181    if (output_filename != NULL)
182      fileout = new_file_wo(file, output_filename);
183  }
184
185  ibuf        = new_fvec(overlap_size, channels);
186  obuf        = new_fvec(overlap_size, channels);
187  woodblock   = new_fvec(buffer_size,1);
188  fftgrain    = new_cvec(buffer_size, channels);
189
190  pitchdet = new_aubio_pitchdetection(buffer_size*4, overlap_size, channels, samplerate, mcomb, freq);
191  //pitchdet = new_aubio_pitchdetection(buffer_size*2, overlap_size, channels, samplerate, yin, freq);
192 
193  /* read the output sound once */
194  file_read(onsetfile, overlap_size, woodblock);
195  /* phase vocoder */
196  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
197  /* onsets */
198  parms = new_aubio_peakpicker(threshold);
199  o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
200  onset = new_fvec(1, channels);
201  if (usedoubled)    {
202    o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
203    onset2 = new_fvec(1 , channels);
204  }
205
206  if(usejack) {
207#ifdef JACK_SUPPORT
208    aubio_jack_t * jack_setup;
209    debug("Midi init ...\n");
210    debug("Jack init ...\n");
211    jack_setup = new_aubio_jack(channels, channels,
212          (aubio_process_func_t)aubio_process);
213
214    mplay = new_aubio_midi_player();
215
216    mdriver = new_aubio_midi_driver("alsa_seq",
217        (handle_midi_event_func_t)aubio_midi_send_event, mplay);
218
219    event = new_aubio_midi_event();
220   
221    debug("Jack activation ...\n");
222    aubio_jack_activate(jack_setup);
223    debug("Processing (Ctrl+C to quit) ...\n");
224    pause();
225    send_noteon(mdriver,curnote,0);
226    aubio_jack_close(jack_setup);
227    del_aubio_midi_driver(mdriver);
228#else
229    usage(stderr, 1);
230    outmsg("Compiled without jack output, exiting.\n");
231#endif
232
233  } else {
234    /* phasevoc */
235    debug("Processing ...\n");
236
237    frames = 0;
238
239    while (overlap_size == file_read(file, overlap_size, ibuf))
240    {
241      isonset=0;
242      aubio_process(ibuf->data, obuf->data, overlap_size);
243      if (output_filename != NULL) {
244        file_write(fileout,overlap_size,obuf);
245      }
246      frames++;
247    }
248    send_noteon(mdriver,curnote,0);
249
250    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
251    del_file(file);
252
253    if (output_filename != NULL)
254      del_file(fileout);
255
256  }
257
258  del_aubio_pvoc(pv);
259  del_fvec(obuf);
260  del_fvec(ibuf);
261  del_cvec(fftgrain);
262  del_aubio_pitchdetection(pitchdet);
263  del_fvec(onset);
264
265  debug("End of program.\n");
266  fflush(stderr);
267  return 0;
268}
269
Note: See TracBrowser for help on using the repository browser.