source: examples/aubionotes.c @ 20f9748

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

moved midi functions to ext/

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