source: examples/utils.c @ 0b9e1da5

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

fix signed/unsigned mismatches in examples
fix signed/unsigned mismatches in examples

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