source: examples/utils.c @ b1f723d

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

fix segfault when file not found

  • Property mode set to 100644
File size: 12.1 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  = aubio_onset_kl;
33aubio_onsetdetection_type type_onset2 = aubio_onset_complex;
34smpl_t threshold                      = 0.3;
35smpl_t threshold2                     = -90.;
36uint_t buffer_size                    = 512; //1024;
37uint_t overlap_size                   = 256; //512;
38uint_t channels                       = 1;
39uint_t samplerate                     = 44100;
40
41
42aubio_sndfile_t * file = NULL;
43aubio_sndfile_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_pitch_yin; // aubio_pitch_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:s:H: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                {"silence"  , 0, NULL, 's'},
112                {"averaging", 0, NULL, 'a'},
113                {"hopsize",   0, NULL, 'H'},
114                {NULL       , 0, NULL, 0}
115        };
116        prog_name = argv[0];   
117        if( argc < 1 ) {
118                usage (stderr, 1);
119                return -1;
120        }
121        do {
122                next_option = getopt_long (argc, argv, options, 
123                                long_options, NULL);
124                switch (next_option) {
125                        case 'o':
126                                output_filename = optarg;
127                                break;
128                        case 'i':
129                                input_filename = optarg;
130                                break;
131                        case 'h':       /* help */
132                                usage (stdout, 0);
133                                return -1;
134                        case 'v':               /* verbose */
135                                verbose = 1;
136                                break;
137                        case 'j':               /* verbose */
138                                usejack = 1;
139                                break;
140                        case 'O':   /*onset type*/
141                                if (strcmp(optarg,"energy") == 0) 
142                                        type_onset = aubio_onset_energy;
143                                else if (strcmp(optarg,"specdiff") == 0) 
144                                        type_onset = aubio_onset_specdiff;
145                                else if (strcmp(optarg,"hfc") == 0) 
146                                        type_onset = aubio_onset_hfc;
147                                else if (strcmp(optarg,"complexdomain") == 0) 
148                                        type_onset = aubio_onset_complex;
149                                else if (strcmp(optarg,"complex") == 0) 
150                                        type_onset = aubio_onset_complex;
151                                else if (strcmp(optarg,"phase") == 0) 
152                                        type_onset = aubio_onset_phase;
153                                else if (strcmp(optarg,"mkl") == 0) 
154                                        type_onset = aubio_onset_mkl;
155                                else if (strcmp(optarg,"kl") == 0) 
156                                        type_onset = aubio_onset_kl;
157                                else {
158                                        debug("could not get onset type.\n");
159                                        abort();
160                                }
161                                usedoubled = 0;
162                                break;
163                        case 's':   /* threshold value for onset */
164                                threshold2 = (smpl_t)atof(optarg);
165                                break;
166                        case 't':   /* threshold value for onset */
167                                threshold = (smpl_t)atof(optarg);
168                                /*
169                                   if (!isfinite(threshold)) {
170                                   debug("could not get threshold.\n");
171                                   abort();
172                                   }
173                                   */
174                                break;
175                        case 'a':
176                                averaging = 1;
177                                break; 
178                        case 'H':
179                                overlap_size = atoi(optarg);
180                                break;
181                        case '?':       /* unknown options */
182                                usage(stderr, 1);
183                                break;
184                        case -1:                /* done with options */
185                                break;
186                        default:                /*something else unexpected */
187                                abort ();
188                }
189        }
190        while (next_option != -1);
191
192        if (input_filename != NULL) {
193                debug ("Input file : %s\n", input_filename );
194        } else if (input_filename != NULL && output_filename != NULL) {
195                debug ("Input file : %s\n", input_filename );
196                debug ("Output file : %s\n", output_filename );
197        } else {
198                if (JACK_SUPPORT)
199                {
200                        debug ("Jack input output\n");
201                        usejack = 1;
202                } else {
203                        debug ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
204                        exit(1);
205                }
206        }       
207        return 0;
208}
209
210void examples_common_init(int argc,char ** argv) {
211
212
213  aubio_sndfile_t * onsetfile = NULL;
214  /* parse command line arguments */
215  parse_args(argc, argv);
216
217  woodblock = new_fvec(buffer_size,1);
218  if (output_filename || usejack) {
219          (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
220                  (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
221                  (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
222  }
223  if (onsetfile) {
224          /* read the output sound once */
225          aubio_sndfile_read(onsetfile, overlap_size, woodblock);
226  }
227
228  if(!usejack)
229  {
230    debug("Opening files ...\n");
231    file = new_aubio_sndfile_ro (input_filename);
232    if (verbose) aubio_sndfile_info(file);
233    channels = aubio_sndfile_channels(file);
234    samplerate = aubio_sndfile_samplerate(file);
235    if (output_filename != NULL)
236      fileout = new_aubio_sndfile_wo(file, output_filename);
237  }
238
239  ibuf      = new_fvec(overlap_size, channels);
240  obuf      = new_fvec(overlap_size, channels);
241  fftgrain  = new_cvec(buffer_size, channels);
242
243  if (usepitch) {
244    pitchdet = new_aubio_pitchdetection(buffer_size*4, 
245                    overlap_size, channels, samplerate, mode, aubio_pitchm_freq);
246 
247  if (median) {
248          note_buffer = new_fvec(median, 1);
249          note_buffer2= new_fvec(median, 1);
250  }
251  }
252  /* phase vocoder */
253  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
254  /* onsets */
255  parms = new_aubio_peakpicker(threshold);
256  o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
257  onset = new_fvec(1, channels);
258  if (usedoubled)    {
259    o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
260    onset2 = new_fvec(1 , channels);
261  }
262
263}
264
265
266void examples_common_del(void){
267  if (usepitch) {
268          send_noteon(curnote,0);
269          del_aubio_pitchdetection(pitchdet);
270          if (median) {
271                  del_fvec(note_buffer);
272                  del_fvec(note_buffer2);
273          }
274  }
275  del_aubio_pvoc(pv);
276  del_fvec(obuf);
277  del_fvec(ibuf);
278  del_cvec(fftgrain);
279  del_fvec(onset);
280}
281
282void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
283  if(usejack) {
284#if JACK_SUPPORT
285    aubio_jack_t * jack_setup;
286    debug("Jack init ...\n");
287    jack_setup = new_aubio_jack(channels, channels,
288          (aubio_process_func_t)process_func);
289    if (usepitch) {
290            debug("Midi init ...\n");
291            mplay = new_aubio_midi_player();
292            mdriver = new_aubio_midi_driver("alsa_seq",
293                            (handle_midi_event_func_t)aubio_midi_send_event, mplay);
294            event = new_aubio_midi_event();
295    }
296    debug("Jack activation ...\n");
297    aubio_jack_activate(jack_setup);
298    debug("Processing (Ctrl+C to quit) ...\n");
299    pause();
300    aubio_jack_close(jack_setup);
301    if (usepitch) {
302            send_noteon(curnote,0);
303            del_aubio_midi_driver(mdriver);
304    }
305#else
306    usage(stderr, 1);
307    outmsg("Compiled without jack output, exiting.\n");
308#endif
309
310  } else {
311    /* phasevoc */
312    debug("Processing ...\n");
313
314    frames = 0;
315
316    while (overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
317    {
318      isonset=0;
319      process_func(ibuf->data, obuf->data, overlap_size);
320      print(); 
321      if (output_filename != NULL) {
322        aubio_sndfile_write(fileout,overlap_size,obuf);
323      }
324      frames++;
325    }
326
327    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
328    del_aubio_sndfile(file);
329
330    if (output_filename != NULL)
331      del_aubio_sndfile(fileout);
332
333  }
334}
335
336
337
338void send_noteon(int pitch, int velo)
339{
340    smpl_t mpitch = (FLOOR)(aubio_freqtomidi(pitch)+.5);
341    /* we should check if we use midi here, not jack */
342#if ALSA_SUPPORT
343    if (usejack) {
344        if (velo==0) {
345            aubio_midi_event_set_type(event,NOTE_OFF);
346        } else {
347            aubio_midi_event_set_type(event,NOTE_ON);
348        }
349        aubio_midi_event_set_channel(event,0);
350        aubio_midi_event_set_pitch(event,mpitch);
351        aubio_midi_event_set_velocity(event,velo);
352        aubio_midi_direct_output(mdriver,event);
353    } else 
354#endif
355    if (!verbose)
356    {
357        if (velo==0) {
358            outmsg("%f\n",frames*overlap_size/(float)samplerate);
359        } else {
360            outmsg("%f\t%f\t", mpitch,
361                        frames*overlap_size/(float)samplerate);
362        }
363    }
364}
365
366
367void note_append(fvec_t * note_buffer, smpl_t curnote) {
368  uint_t i = 0;
369  for (i = 0; i < note_buffer->length - 1; i++) { 
370      note_buffer->data[0][i] = note_buffer->data[0][i+1];
371  }
372  note_buffer->data[0][note_buffer->length - 1] = curnote;
373  return;
374}
375
376uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
377  uint_t i = 0;
378  for (i = 0; i < note_buffer->length; i++) { 
379      note_buffer2->data[0][i] = note_buffer->data[0][i];
380  }
381  return vec_median(note_buffer2);
382}
383
Note: See TracBrowser for help on using the repository browser.