source: examples/utils.c @ edca23f

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

add aubio_ to pitch detection modes
add aubio_ to pitch detection modes

  • Property mode set to 100644
File size: 8.9 KB
Line 
1
2#include "aubio.h"
3
4#ifndef JACK_SUPPORT
5#define JACK_SUPPORT 0
6#endif
7
8#include <getopt.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include <math.h> /* for isfinite */
13#include "utils.h"
14
15/* not supported yet */
16#ifdef LADCCA_SUPPORT
17#include <ladcca/ladcca.h>
18cca_client_t * aubio_cca_client;
19#endif /* LADCCA_SUPPORT */
20
21/* settings */
22const char * output_filename = NULL;
23const char * input_filename  = NULL;
24const char * onset_filename  = "/usr/share/sounds/aubio/woodblock.aiff";
25int frames = 0;
26int verbose = 0;
27int usejack = 0;
28int usedoubled = 1;
29
30
31/* energy,specdiff,hfc,complexdomain,phase */
32aubio_onsetdetection_type type_onset  = hfc;
33aubio_onsetdetection_type type_onset2 = complexdomain;
34smpl_t threshold                      = 0.3;
35smpl_t threshold2                     = -90.;
36uint_t buffer_size                    = 1024;
37uint_t overlap_size                   = 512;
38uint_t channels                       = 1;
39uint_t samplerate                     = 44100;
40
41
42aubio_file_t * file = NULL;
43aubio_file_t * fileout = NULL;
44
45aubio_pvoc_t * pv;
46fvec_t * ibuf;
47fvec_t * obuf;
48cvec_t * fftgrain;
49fvec_t * woodblock;
50aubio_onsetdetection_t *o;
51aubio_onsetdetection_t *o2;
52fvec_t *onset;
53fvec_t *onset2;
54int isonset = 0;
55aubio_pickpeak_t * parms;
56
57
58/* pitch objects */
59smpl_t pitch               = 0.;
60aubio_pitchdetection_t * pitchdet;
61aubio_pitchdetection_type mode = aubio_yin; // aubio_mcomb
62uint_t median         = 6;
63
64fvec_t * note_buffer  = NULL;
65fvec_t * note_buffer2 = NULL;
66smpl_t curlevel       = 0.;
67smpl_t maxonset       = 0.;
68
69/* midi objects */
70aubio_midi_player_t * mplay; 
71aubio_midi_driver_t * mdriver; 
72aubio_midi_event_t  * event;
73
74smpl_t curnote = 0.;
75smpl_t newnote = 0.;
76uint_t isready = 0;
77
78
79
80/* badly redeclare some things */
81aubio_onsetdetection_type type_onset;
82smpl_t threshold;
83smpl_t averaging;
84const char * prog_name;
85
86void usage (FILE * stream, int exit_code)
87{
88        fprintf(stream, "usage: %s [ options ] \n", prog_name);
89        fprintf(stream, 
90                        "       -j      --jack          Use Jack.\n"
91                        "       -o      --output        Output type.\n"
92                        "       -i      --input         Input type.\n"
93                        "       -h      --help          Display this message.\n"
94                        "       -v      --verbose       Print verbose message.\n"
95                        );
96        exit(exit_code);
97}
98
99int parse_args (int argc, char **argv) {
100        const char *options = "hvjo:i:O:t:a";
101        int next_option;
102        struct option long_options[] =
103        {
104                {"help",                0, NULL, 'h'},
105                {"verbose",     0, NULL, 'v'},
106                {"jack",                0, NULL, 'j'},
107                {"output",      0, NULL, 'o'},
108                {"input",       0, NULL, 'i'},
109                {"onset",       0, NULL, 'O'},
110                {"threshold",   0, NULL, 't'},
111                {"averaging",   0, NULL, 'a'},
112                {NULL,                  0, NULL, 0}
113        };
114        prog_name = argv[0];   
115        if( argc < 1 ) {
116                usage (stderr, 1);
117                return -1;
118        }
119        do {
120                next_option = getopt_long (argc, argv, options, 
121                                                                        long_options, NULL);
122                switch (next_option) {
123                        case 'o':
124                                output_filename = optarg;
125                                break;
126                        case 'i':
127                                input_filename = optarg;
128                                break;
129                        case 'h':       /* help */
130                                usage (stdout, 0);
131                                return -1;
132                        case 'v':               /* verbose */
133                                verbose = 1;
134                                break;
135                        case 'j':               /* verbose */
136                                usejack = 1;
137                                break;
138      case 'O':   /*onset type*/
139        if (strcmp(optarg,"energy") == 0) 
140          type_onset = energy;
141        else if (strcmp(optarg,"specdiff") == 0) 
142          type_onset = specdiff;
143        else if (strcmp(optarg,"hfc") == 0) 
144          type_onset = hfc;
145        else if (strcmp(optarg,"complexdomain") == 0) 
146          type_onset = complexdomain;
147        else if (strcmp(optarg,"phase") == 0) 
148          type_onset = phase;
149        else {
150          debug("could not get onset type.\n");
151          abort();
152        }
153        usedoubled = 0;
154        break;
155      case 't':   /* threshold value for onset */
156        threshold = (smpl_t)atof(optarg);
157        /*
158        if (!isfinite(threshold)) {
159          debug("could not get threshold.\n");
160          abort();
161        }
162        */
163        break;
164      case 'a':
165        averaging = 1;
166        break; 
167                        case '?':       /* unknown options */
168                                usage(stderr, 1);
169        break;
170                        case -1:                /* done with options */
171                                break;
172                        default:                /*something else unexpected */
173                                abort ();
174                }
175        }
176        while (next_option != -1);
177
178        if (input_filename != NULL) {
179                debug ("Input file : %s\n", input_filename );
180        } else if (input_filename != NULL && output_filename != NULL) {
181                debug ("Input file : %s\n", input_filename );
182                debug ("Output file : %s\n", output_filename );
183        } else {
184                if (JACK_SUPPORT)
185                {
186                        debug ("Jack input output\n");
187                        usejack = 1;
188                } else {
189                        debug ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
190                        exit(1);
191                }
192        }       
193        return 0;
194}
195
196void examples_common_init(int argc,char ** argv) {
197
198
199  aubio_file_t * onsetfile = new_file_ro(onset_filename);
200  /* parse command line arguments */
201  parse_args(argc, argv);
202
203  if(!usejack)
204  {
205    debug("Opening files ...\n");
206    file = new_file_ro (input_filename);
207    if (verbose) file_info(file);
208    channels = aubio_file_channels(file);
209    samplerate = aubio_file_samplerate(file);
210    if (output_filename != NULL)
211      fileout = new_file_wo(file, output_filename);
212  }
213
214  ibuf      = new_fvec(overlap_size, channels);
215  obuf      = new_fvec(overlap_size, channels);
216  woodblock = new_fvec(buffer_size,1);
217  fftgrain  = new_cvec(buffer_size, channels);
218
219  if (usepitch) {
220    pitchdet = new_aubio_pitchdetection(buffer_size*4, 
221                    overlap_size, channels, samplerate, mode, aubio_freq);
222 
223  if (median) {
224          note_buffer = new_fvec(median, 1);
225          note_buffer2= new_fvec(median, 1);
226  }
227  }
228  /* read the output sound once */
229  file_read(onsetfile, overlap_size, woodblock);
230  /* phase vocoder */
231  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
232  /* onsets */
233  parms = new_aubio_peakpicker(threshold);
234  o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
235  onset = new_fvec(1, channels);
236  if (usedoubled)    {
237    o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
238    onset2 = new_fvec(1 , channels);
239  }
240
241}
242
243
244void examples_common_del(void){
245  if (usepitch) {
246          send_noteon(curnote,0);
247          del_aubio_pitchdetection(pitchdet);
248          if (median) {
249                  del_fvec(note_buffer);
250                  del_fvec(note_buffer2);
251          }
252  }
253  del_aubio_pvoc(pv);
254  del_fvec(obuf);
255  del_fvec(ibuf);
256  del_cvec(fftgrain);
257  del_fvec(onset);
258}
259
260void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
261  if(usejack) {
262#ifdef JACK_SUPPORT
263    aubio_jack_t * jack_setup;
264    debug("Jack init ...\n");
265    jack_setup = new_aubio_jack(channels, channels,
266          (aubio_process_func_t)process_func);
267    if (usepitch) {
268            debug("Midi init ...\n");
269            mplay = new_aubio_midi_player();
270            mdriver = new_aubio_midi_driver("alsa_seq",
271                            (handle_midi_event_func_t)aubio_midi_send_event, mplay);
272            event = new_aubio_midi_event();
273    }
274    debug("Jack activation ...\n");
275    aubio_jack_activate(jack_setup);
276    debug("Processing (Ctrl+C to quit) ...\n");
277    pause();
278    aubio_jack_close(jack_setup);
279    if (usepitch) {
280            send_noteon(curnote,0);
281            del_aubio_midi_driver(mdriver);
282    }
283#else
284    usage(stderr, 1);
285    outmsg("Compiled without jack output, exiting.\n");
286#endif
287
288  } else {
289    /* phasevoc */
290    debug("Processing ...\n");
291
292    frames = 0;
293
294    while (overlap_size == file_read(file, overlap_size, ibuf))
295    {
296      isonset=0;
297      process_func(ibuf->data, obuf->data, overlap_size);
298      print(); 
299      if (output_filename != NULL) {
300        file_write(fileout,overlap_size,obuf);
301      }
302      frames++;
303    }
304
305    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
306    del_file(file);
307
308    if (output_filename != NULL)
309      del_file(fileout);
310
311  }
312}
313
314
315
316void send_noteon(int pitch, int velo)
317{
318    smpl_t mpitch = (FLOOR)(freqtomidi(pitch)+.5);
319    /* we should check if we use midi here, not jack */
320#if ALSA_SUPPORT
321    if (usejack) {
322        if (velo==0) {
323            aubio_midi_event_set_type(event,NOTE_OFF);
324        } else {
325            aubio_midi_event_set_type(event,NOTE_ON);
326        }
327        aubio_midi_event_set_channel(event,0);
328        aubio_midi_event_set_pitch(event,mpitch);
329        aubio_midi_event_set_velocity(event,velo);
330        aubio_midi_direct_output(mdriver,event);
331    } else 
332#endif
333    if (!verbose)
334    {
335        if (velo==0) {
336            outmsg("%f\n",frames*overlap_size/(float)samplerate);
337        } else {
338            outmsg("%f\t%f\t", mpitch,
339                        frames*overlap_size/(float)samplerate);
340        }
341    }
342}
343
344
345void note_append(fvec_t * note_buffer, smpl_t curnote) {
346  uint_t i = 0;
347  for (i = 0; i < note_buffer->length - 1; i++) { 
348      note_buffer->data[0][i] = note_buffer->data[0][i+1];
349  }
350  note_buffer->data[0][note_buffer->length - 1] = curnote;
351  return;
352}
353
354uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
355  uint_t i = 0;
356  for (i = 0; i < note_buffer->length; i++) { 
357      note_buffer2->data[0][i] = note_buffer->data[0][i];
358  }
359  return vec_median(note_buffer2);
360}
361
Note: See TracBrowser for help on using the repository browser.