source: examples/utils.c @ 98874a6

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

examples/utils.c: get samplerate from file

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/*
2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21/**
22
23  This file includes some tools common to all examples. Code specific to the
24  algorithm performed by each program should go in the source file of that
25  program instead.
26
27*/
28
29#include "utils.h"
30
31#ifdef HAVE_LASH
32#include <lash/lash.h>
33#include <pthread.h>
34lash_client_t *aubio_lash_client;
35lash_args_t *lash_args;
36void *lash_thread_main (void *data);
37int lash_main (void);
38void save_data (void);
39void restore_data (lash_config_t * lash_config);
40pthread_t lash_thread;
41#endif /* HAVE_LASH */
42
43/* settings */
44const char *sink_uri = NULL;
45const char *source_uri = NULL;
46int frames = 0;
47int verbose = 0;
48int usejack = 0;
49int frames_delay = 0;
50
51
52char_t * pitch_unit = "default";
53char_t * pitch_mode = "default";
54
55/* energy,specdiff,hfc,complexdomain,phase */
56char_t * onset_mode = "default";
57smpl_t threshold = 0.0;         // leave unset, only set as asked
58smpl_t silence = -90.;
59uint_t buffer_size = 512;       //1024;
60uint_t overlap_size = 256;      //512;
61uint_t samplerate = 44100;
62
63aubio_source_t *this_source = NULL;
64aubio_sink_t *this_sink = NULL;
65
66fvec_t *ibuf;
67fvec_t *obuf;
68fvec_t *woodblock;
69
70/* badly redeclare some things */
71smpl_t threshold;
72smpl_t averaging;
73const char *prog_name;
74
75void flush_process (aubio_process_func_t process_func,
76    aubio_print_func_t print);
77
78void
79usage (FILE * stream, int exit_code)
80{
81  fprintf (stream, "usage: %s [ options ] \n", prog_name);
82  fprintf (stream,
83      "       -h      --help          Display this message.\n"
84      "       -v      --verbose       Be verbose.\n"
85      "       -j      --jack          Use Jack.\n"
86      "       -o      --output        Output type.\n"
87      "       -i      --input         Input type.\n"
88      "       -O      --onset         Select onset detection algorithm.\n"
89      "       -t      --threshold     Set onset detection threshold.\n"
90      "       -s      --silence       Select silence threshold.\n"
91      "       -p      --pitch         Select pitch detection algorithm.\n"
92      "       -B      --bufsize       Set buffer size.\n"
93      "       -H      --hopsize       Set hopsize.\n"
94      "       -a      --averaging     Use averaging.\n");
95  exit (exit_code);
96}
97
98int
99parse_args (int argc, char **argv)
100{
101  const char *options = "hvjo:i:O:t:s:p:B:H:a";
102  int next_option;
103  struct option long_options[] = {
104    {"help", 0, NULL, 'h'},
105    {"verbose", 0, NULL, 'v'},
106    {"jack", 0, NULL, 'j'},
107    {"output", 1, NULL, 'o'},
108    {"input", 1, NULL, 'i'},
109    {"onset", 1, NULL, 'O'},
110    {"threshold", 1, NULL, 't'},
111    {"silence", 1, NULL, 's'},
112    {"pitch", 1, NULL, 'p'},
113    {"averaging", 0, NULL, 'a'},
114    {"bufsize", 1, NULL, 'B'},
115    {"hopsize", 1, NULL, 'H'},
116    {NULL, 0, NULL, 0}
117  };
118#ifdef HAVE_LASH
119  lash_args = lash_extract_args (&argc, &argv);
120#endif /* HAVE_LASH */
121  prog_name = argv[0];
122  if (argc < 1) {
123    usage (stderr, 1);
124    return -1;
125  }
126  do {
127    next_option = getopt_long (argc, argv, options, long_options, NULL);
128    switch (next_option) {
129      case 'o':
130        sink_uri = optarg;
131        break;
132      case 'i':
133        source_uri = optarg;
134        break;
135      case 'h':                /* help */
136        usage (stdout, 0);
137        return -1;
138      case 'v':                /* verbose */
139        verbose = 1;
140        break;
141      case 'j':
142        usejack = 1;
143        break;
144      case 'O':                /*onset type */
145        onset_mode = optarg;
146        break;
147      case 's':                /* silence value for onset */
148        silence = (smpl_t) atof (optarg);
149        break;
150      case 't':                /* threshold value for onset */
151        threshold = (smpl_t) atof (optarg);
152        break;
153      case 'p':
154        pitch_mode = optarg;
155        break;
156      case 'a':
157        averaging = 1;
158        break;
159      case 'B':
160        buffer_size = atoi (optarg);
161        break;
162      case 'H':
163        overlap_size = atoi (optarg);
164        break;
165      case '?':                /* unknown options */
166        usage (stderr, 1);
167        break;
168      case -1:                 /* done with options */
169        break;
170      default:                 /*something else unexpected */
171        fprintf (stderr, "Error parsing option '%c'\n", next_option);
172        abort ();
173    }
174  }
175  while (next_option != -1);
176
177  if (source_uri != NULL) {
178    debug ("Input file : %s\n", source_uri);
179  } else if (source_uri != NULL && sink_uri != NULL) {
180    debug ("Input file : %s\n", source_uri);
181    debug ("Output file : %s\n", sink_uri);
182  } else {
183#if HAVE_JACK
184    debug ("Jack input output\n");
185    usejack = 1;
186#else
187    debug
188        ("Error: Could not switch to jack mode\n   aubio was compiled without jack support\n");
189    exit (1);
190#endif
191  }
192
193  return 0;
194}
195
196void
197examples_common_init (int argc, char **argv)
198{
199
200  /* parse command line arguments */
201  parse_args (argc, argv);
202
203  if (!usejack) {
204    debug ("Opening files ...\n");
205    this_source = new_aubio_source ((char_t*)source_uri, 0, overlap_size);
206    if (this_source == NULL) {
207      outmsg ("Could not open input file %s.\n", source_uri);
208      exit (1);
209    }
210    samplerate = aubio_source_get_samplerate(this_source);
211    if (sink_uri != NULL) {
212      this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate);
213    }
214  }
215#ifdef HAVE_LASH
216  else {
217    aubio_lash_client = lash_init (lash_args, argv[0],
218        LASH_Config_Data_Set | LASH_Terminal, LASH_PROTOCOL (2, 0));
219    if (!aubio_lash_client) {
220      fprintf (stderr, "%s: could not initialise lash\n", __FUNCTION__);
221    }
222    /* tell the lash server our client id */
223    if (lash_enabled (aubio_lash_client)) {
224      lash_event_t *event =
225          (lash_event_t *) lash_event_new_with_type (LASH_Client_Name);
226      lash_event_set_string (event, "aubio");
227      lash_send_event (aubio_lash_client, event);
228      pthread_create (&lash_thread, NULL, lash_thread_main, NULL);
229    }
230  }
231#endif /* HAVE_LASH */
232
233  woodblock = new_fvec (overlap_size);
234  //TODO create woodblock sound
235
236  ibuf = new_fvec (overlap_size);
237  obuf = new_fvec (overlap_size);
238
239}
240
241void
242examples_common_del (void)
243{
244  del_fvec (ibuf);
245  del_fvec (obuf);
246  del_fvec (woodblock);
247  aubio_cleanup ();
248}
249
250#if HAVE_JACK
251aubio_jack_t *jack_setup;
252#endif
253
254void
255examples_common_process (aubio_process_func_t process_func,
256    aubio_print_func_t print)
257{
258
259  uint_t read = 0;
260  if (usejack) {
261
262#if HAVE_JACK
263    debug ("Jack init ...\n");
264    jack_setup = new_aubio_jack (1, 1,
265        0, 1, (aubio_process_func_t) process_func);
266    debug ("Jack activation ...\n");
267    aubio_jack_activate (jack_setup);
268    debug ("Processing (Ctrl+C to quit) ...\n");
269    pause ();
270    aubio_jack_close (jack_setup);
271#else
272    usage (stderr, 1);
273    outmsg ("Compiled without jack output, exiting.\n");
274#endif
275
276  } else {
277    /* phasevoc */
278    debug ("Processing ...\n");
279
280    frames = 0;
281
282    do {
283      aubio_source_do (this_source, ibuf, &read);
284      process_func (&ibuf->data, &obuf->data, overlap_size);
285      print ();
286      if (this_sink) {
287        aubio_sink_do (this_sink, obuf, overlap_size);
288      }
289      frames++;
290    } while (read == overlap_size);
291
292    debug ("Processed %d frames of %d samples.\n", frames, buffer_size);
293
294    flush_process (process_func, print);
295    del_aubio_source (this_source);
296    del_aubio_sink   (this_sink);
297
298  }
299}
300
301void
302flush_process (aubio_process_func_t process_func, aubio_print_func_t print)
303{
304  uint_t i;
305  fvec_zeros(obuf);
306  for (i = 0; (signed) i < frames_delay; i++) {
307    process_func (&ibuf->data, &obuf->data, overlap_size);
308    print ();
309  }
310}
311
312void
313send_noteon (int pitch, int velo)
314{
315  smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
316#if HAVE_JACK
317  jack_midi_event_t ev;
318  ev.size = 3;
319  ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
320  ev.time = 0;
321  if (usejack) {
322    ev.buffer[2] = velo;
323    ev.buffer[1] = mpitch;
324    if (velo == 0) {
325      ev.buffer[0] = 0x80;      /* note off */
326    } else {
327      ev.buffer[0] = 0x90;      /* note on */
328    }
329    aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev);
330  } else
331#endif
332  if (!verbose) {
333    if (velo == 0) {
334      outmsg ("%f\n", frames * overlap_size / (float) samplerate);
335    } else {
336      outmsg ("%f\t%f\t", mpitch, frames * overlap_size / (float) samplerate);
337    }
338  }
339}
340
341
342#if HAVE_LASH
343
344void *
345lash_thread_main (void *data __attribute__ ((unused)))
346{
347  printf ("LASH thread running\n");
348
349  while (!lash_main ())
350    usleep (1000);
351
352  printf ("LASH thread finished\n");
353  return NULL;
354}
355
356int
357lash_main (void)
358{
359  lash_event_t *lash_event;
360  lash_config_t *lash_config;
361
362  while ((lash_event = lash_get_event (aubio_lash_client))) {
363    switch (lash_event_get_type (lash_event)) {
364      case LASH_Quit:
365        lash_event_destroy (lash_event);
366        exit (1);
367        return 1;
368      case LASH_Restore_Data_Set:
369        lash_send_event (aubio_lash_client, lash_event);
370        break;
371      case LASH_Save_Data_Set:
372        save_data ();
373        lash_send_event (aubio_lash_client, lash_event);
374        break;
375      case LASH_Server_Lost:
376        return 1;
377      default:
378        printf ("%s: received unknown LASH event of type %d",
379            __FUNCTION__, lash_event_get_type (lash_event));
380        lash_event_destroy (lash_event);
381        break;
382    }
383  }
384
385  while ((lash_config = lash_get_config (aubio_lash_client))) {
386    restore_data (lash_config);
387    lash_config_destroy (lash_config);
388  }
389
390  return 0;
391}
392
393void
394save_data ()
395{
396  lash_config_t *lash_config;
397
398  lash_config = lash_config_new_with_key ("threshold");
399  lash_config_set_value_double (lash_config, threshold);
400  lash_send_config (aubio_lash_client, lash_config);
401
402}
403
404void
405restore_data (lash_config_t * lash_config)
406{
407  const char *lash_key;
408
409  lash_key = lash_config_get_key (lash_config);
410
411  if (strcmp (lash_key, "threshold") == 0) {
412    threshold = lash_config_get_value_double (lash_config);
413    return;
414  }
415
416}
417
418#endif /* HAVE_LASH */
Note: See TracBrowser for help on using the repository browser.