source: examples/utils.c @ fb615eb

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

added pitch option to utils.c

  • Property mode set to 100644
File size: 13.5 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 type_pitch = aubio_pitch_schmitt; // 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                                        debug("could not get 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,"yin") == 0) 
186                                        type_pitch = aubio_pitch_yin;
187                                else if (strcmp(optarg,"schmitt") == 0) 
188                                        type_pitch = aubio_pitch_schmitt;
189                                else if (strcmp(optarg,"fcomb") == 0) 
190                                        type_pitch = aubio_pitch_fcomb;
191                                else {
192                                        debug("could not get pitch type.\n");
193                                        abort();
194                                }
195                                break;
196                        case 'a':
197                                averaging = 1;
198                                break; 
199                        case 'H':
200                                overlap_size = atoi(optarg);
201                                break;
202                        case '?': /* unknown options */
203                                usage(stderr, 1);
204                                break;
205                        case -1: /* done with options */
206                                break;
207                        default: /*something else unexpected */
208                                abort ();
209                }
210        }
211        while (next_option != -1);
212
213        if (input_filename != NULL) {
214                debug ("Input file : %s\n", input_filename );
215        } else if (input_filename != NULL && output_filename != NULL) {
216                debug ("Input file : %s\n", input_filename );
217                debug ("Output file : %s\n", output_filename );
218        } else {
219                if (JACK_SUPPORT)
220                {
221                        debug ("Jack input output\n");
222                        usejack = 1;
223                } else {
224                        debug ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
225                        exit(1);
226                }
227        }
228        return 0;
229}
230
231void examples_common_init(int argc,char ** argv) {
232
233
234  aubio_sndfile_t * onsetfile = NULL;
235  /* parse command line arguments */
236  parse_args(argc, argv);
237
238  woodblock = new_fvec(buffer_size,1);
239  if (output_filename || usejack) {
240          (onsetfile = new_aubio_sndfile_ro(onset_filename)) ||
241                  (onsetfile = new_aubio_sndfile_ro("sounds/woodblock.aiff")) ||
242                  (onsetfile = new_aubio_sndfile_ro("../sounds/woodblock.aiff"));
243  }
244  if (onsetfile) {
245          /* read the output sound once */
246          aubio_sndfile_read(onsetfile, overlap_size, woodblock);
247  }
248
249  if(!usejack)
250  {
251    debug("Opening files ...\n");
252    file = new_aubio_sndfile_ro (input_filename);
253    if (verbose) aubio_sndfile_info(file);
254    channels = aubio_sndfile_channels(file);
255    samplerate = aubio_sndfile_samplerate(file);
256    if (output_filename != NULL)
257      fileout = new_aubio_sndfile_wo(file, output_filename);
258  }
259
260  ibuf      = new_fvec(overlap_size, channels);
261  obuf      = new_fvec(overlap_size, channels);
262  fftgrain  = new_cvec(buffer_size, channels);
263
264  if (usepitch) {
265    pitchdet = new_aubio_pitchdetection(buffer_size*4, 
266                    overlap_size, channels, samplerate, type_pitch, mode_pitch);
267 
268  if (median) {
269          note_buffer = new_fvec(median, 1);
270          note_buffer2= new_fvec(median, 1);
271  }
272  }
273  /* phase vocoder */
274  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
275  /* onsets */
276  parms = new_aubio_peakpicker(threshold);
277  o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
278  onset = new_fvec(1, channels);
279  if (usedoubled)    {
280    o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
281    onset2 = new_fvec(1 , channels);
282  }
283
284}
285
286
287void examples_common_del(void){
288  if (usepitch) {
289          send_noteon(curnote,0);
290          del_aubio_pitchdetection(pitchdet);
291          if (median) {
292                  del_fvec(note_buffer);
293                  del_fvec(note_buffer2);
294          }
295  }
296  del_aubio_pvoc(pv);
297  del_fvec(obuf);
298  del_fvec(ibuf);
299  del_cvec(fftgrain);
300  del_fvec(onset);
301}
302
303void examples_common_process(aubio_process_func_t process_func, aubio_print_func_t print ){
304  if(usejack) {
305#if JACK_SUPPORT
306    aubio_jack_t * jack_setup;
307    debug("Jack init ...\n");
308    jack_setup = new_aubio_jack(channels, channels,
309          (aubio_process_func_t)process_func);
310    if (usepitch) {
311            debug("Midi init ...\n");
312            mplay = new_aubio_midi_player();
313            mdriver = new_aubio_midi_driver("alsa_seq",
314                            (handle_midi_event_func_t)aubio_midi_send_event, mplay);
315            event = new_aubio_midi_event();
316    }
317    debug("Jack activation ...\n");
318    aubio_jack_activate(jack_setup);
319    debug("Processing (Ctrl+C to quit) ...\n");
320    pause();
321    aubio_jack_close(jack_setup);
322    if (usepitch) {
323            send_noteon(curnote,0);
324            del_aubio_midi_driver(mdriver);
325    }
326#else
327    usage(stderr, 1);
328    outmsg("Compiled without jack output, exiting.\n");
329#endif
330
331  } else {
332    /* phasevoc */
333    debug("Processing ...\n");
334
335    frames = 0;
336
337    while (overlap_size == aubio_sndfile_read(file, overlap_size, ibuf))
338    {
339      isonset=0;
340      process_func(ibuf->data, obuf->data, overlap_size);
341      print(); 
342      if (output_filename != NULL) {
343        aubio_sndfile_write(fileout,overlap_size,obuf);
344      }
345      frames++;
346    }
347
348    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
349    del_aubio_sndfile(file);
350
351    if (output_filename != NULL)
352      del_aubio_sndfile(fileout);
353
354  }
355}
356
357
358
359void send_noteon(int pitch, int velo)
360{
361    smpl_t mpitch = (FLOOR)(aubio_freqtomidi(pitch)+.5);
362    /* we should check if we use midi here, not jack */
363#if ALSA_SUPPORT
364    if (usejack) {
365        if (velo==0) {
366            aubio_midi_event_set_type(event,NOTE_OFF);
367        } else {
368            aubio_midi_event_set_type(event,NOTE_ON);
369        }
370        aubio_midi_event_set_channel(event,0);
371        aubio_midi_event_set_pitch(event,mpitch);
372        aubio_midi_event_set_velocity(event,velo);
373        aubio_midi_direct_output(mdriver,event);
374    } else 
375#endif
376    if (!verbose)
377    {
378        if (velo==0) {
379            outmsg("%f\n",frames*overlap_size/(float)samplerate);
380        } else {
381            outmsg("%f\t%f\t", mpitch,
382                        frames*overlap_size/(float)samplerate);
383        }
384    }
385}
386
387
388void note_append(fvec_t * note_buffer, smpl_t curnote) {
389  uint_t i = 0;
390  for (i = 0; i < note_buffer->length - 1; i++) { 
391      note_buffer->data[0][i] = note_buffer->data[0][i+1];
392  }
393  note_buffer->data[0][note_buffer->length - 1] = curnote;
394  return;
395}
396
397uint_t get_note(fvec_t *note_buffer, fvec_t *note_buffer2){
398  uint_t i = 0;
399  for (i = 0; i < note_buffer->length; i++) { 
400      note_buffer2->data[0][i] = note_buffer->data[0][i];
401  }
402  return vec_median(note_buffer2);
403}
404
Note: See TracBrowser for help on using the repository browser.