source: examples/parse_args.h @ 71bc8b0

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampleryinfft+
Last change on this file since 71bc8b0 was 71bc8b0, checked in by Tres Seaver <tseaver@palladion.com>, 7 years ago

aubioonset: user-set MIDI tap note / velocity values.

  • Property mode set to 100644
File size: 10.5 KB
Line 
1/*
2  Copyright (C) 2003-2013 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#include "config.h"
22
23#ifdef HAVE_GETOPT_H
24#include <getopt.h>
25#endif
26
27extern int verbose;
28// input / output
29extern int usejack;
30extern char_t *source_uri;
31extern char_t *sink_uri;
32// general stuff
33extern uint_t samplerate;
34extern uint_t buffer_size;
35extern uint_t hop_size;
36// onset stuff
37extern char_t * onset_method;
38extern smpl_t onset_threshold;
39extern smpl_t onset_minioi;
40// pitch stuff
41extern char_t * pitch_method;
42extern char_t * pitch_unit;
43extern smpl_t pitch_tolerance;
44// time stuff
45extern uint_t time_format;
46// tempo stuff
47extern char_t * tempo_method;
48// more general stuff
49extern smpl_t silence_threshold;
50extern uint_t mix_input;
51// midi tap
52extern smpl_t miditap_note;
53extern smpl_t miditap_velo;
54
55extern uint_t force_overwrite;
56
57// functions defined in utils.c
58extern void examples_common_init (int argc, char **argv);
59extern void examples_common_del (void);
60extern void examples_common_process (aubio_process_func_t process_func,
61    aubio_print_func_t print);
62int parse_args (int argc, char **argv);
63
64// internal stuff
65extern int blocks;
66
67extern fvec_t *ibuf;
68extern fvec_t *obuf;
69
70const char *prog_name;
71
72void usage (FILE * stream, int exit_code);
73
74void usage (FILE * stream, int exit_code)
75{
76#ifdef HAVE_GETOPT_H
77  fprintf (stream, "usage: %s [ options ] \n", prog_name);
78  fprintf (stream,
79      "       -i      --input            input file\n"
80#ifdef PROG_HAS_OUTPUT
81      "       -o      --output           output file\n"
82#endif
83      "       -r      --samplerate       select samplerate\n"
84      "                 use 0 to use input source samplerate, or 32000 to force 32kHz\n"
85      "       -B      --bufsize          set buffer size\n"
86      "                 number of frames to run the analysis on\n"
87      "       -H      --hopsize          set hopsize\n"
88      "                 number of frames to read from source before each analysis\n"
89#ifdef PROG_HAS_ONSET
90      "       -O      --onset            select onset detection algorithm\n"
91      "                 <default|energy|hfc|complex|phase|specdiff|kl|mkl|specflux>;\n"
92      "                 default=hfc\n"
93      "       -t      --onset-threshold  set onset detection threshold\n"
94      "                 a value between 0.1 (more detections) and 1 (less); default=0.3\n"
95      "       -M      --minioi           set minimum inter-onset interval\n"
96      "                 a value in second; default=0.012\n"
97#endif /* PROG_HAS_ONSET */
98#ifdef PROG_HAS_PITCH
99      "       -p      --pitch            select pitch detection algorithm\n"
100      "                 <default|yinfft|yin|mcomb|fcomb|schmitt>; default=yinfft\n"
101      "       -u      --pitch-unit       select pitch output unit\n"
102      "                 <default|freq|hertz|Hz|midi|cent|bin>; default=freq\n"
103      "       -l      --pitch-tolerance  select pitch tolerance\n"
104      "                 (yin, yinfft only) a value between 0.1 and 0.7; default=0.3\n"
105#endif /* PROG_HAS_PITCH */
106#ifdef PROG_HAS_SILENCE
107      "       -s      --silence          select silence threshold\n"
108      "                 a value in dB, for instance -70, or -100; default=-90\n"
109#endif /* PROG_HAS_SILENCE */
110      "       -T      --time-format      select time values output format\n"
111      "                 (samples, ms, seconds) default=seconds\n"
112#ifdef PROG_HAS_OUTPUT
113      "       -m      --mix-input        mix input signal with output signal\n"
114      "                 input signal will be added to output synthesis\n"
115      "       -f      --force-overwrite  overwrite output file if needed\n"
116      "                 do not fail if output file already exists\n"
117#endif /* PROG_HAS_OUTPUT */
118#ifdef PROG_HAS_JACK
119      "       -j      --jack             use Jack\n"
120      "       -n      --miditap-note     MIDI note\n"
121      "       -V      --miditap-velo     MIDI velocity\n"
122#endif /* PROG_HAS_JACK */
123      "       -v      --verbose          be verbose\n"
124      "       -h      --help             display this message\n"
125      );
126#else /* HAVE_GETOPT_H */
127  fprintf (stream, "warning: compiled with getopt.h, no argument parsing\n");
128  fprintf (stream, "usage: %s <filename> \n", prog_name);
129#endif /* HAVE_GETOPT_H */
130  exit (exit_code);
131}
132
133int
134parse_args (int argc, char **argv)
135{
136#ifdef HAVE_GETOPT_H
137  const char *options = "hv"
138    "i:r:B:H:"
139#ifdef PROG_HAS_JACK
140    "jn:V:"
141#endif /* PROG_HAS_JACK */
142#ifdef PROG_HAS_OUTPUT
143    "o:"
144#endif /* PROG_HAS_OUTPUT */
145#ifdef PROG_HAS_ONSET
146    "O:t:M:"
147#endif /* PROG_HAS_ONSET */
148#ifdef PROG_HAS_PITCH
149    "p:u:l:"
150#endif /* PROG_HAS_PITCH */
151    "T:"
152#ifdef PROG_HAS_SILENCE
153    "s:"
154#endif /* PROG_HAS_SILENCE */
155#ifdef PROG_HAS_OUTPUT
156    "mf"
157#endif /* PROG_HAS_OUTPUT */
158    ;
159  int next_option;
160  struct option long_options[] = {
161    {"help",                  0, NULL, 'h'},
162    {"verbose",               0, NULL, 'v'},
163    {"input",                 1, NULL, 'i'},
164    {"samplerate",            1, NULL, 'r'},
165    {"bufsize",               1, NULL, 'B'},
166    {"hopsize",               1, NULL, 'H'},
167#ifdef PROG_HAS_JACK
168    {"jack",                  0, NULL, 'j'},
169    {"miditap-note",          1, NULL, 'n'},
170    {"miditap-velo",          1, NULL, 'V'},
171#endif /* PROG_HAS_JACK */
172#ifdef PROG_HAS_OUTPUT
173    {"output",                1, NULL, 'o'},
174#endif /* PROG_HAS_OUTPUT */
175#ifdef PROG_HAS_ONSET
176    {"onset",                 1, NULL, 'O'},
177    {"onset-threshold",       1, NULL, 't'},
178    {"onset-minioi",          1, NULL, 'M'},
179#endif /* PROG_HAS_ONSET */
180#ifdef PROG_HAS_PITCH
181    {"pitch",                 1, NULL, 'p'},
182    {"pitch-unit",            1, NULL, 'u'},
183    {"pitch-tolerance",       1, NULL, 'l'},
184#endif /* PROG_HAS_PITCH */
185#ifdef PROG_HAS_SILENCE
186    {"silence",               1, NULL, 's'},
187#endif /* PROG_HAS_SILENCE */
188    {"time-format",           1, NULL, 'T'},
189#ifdef PROG_HAS_OUTPUT
190    {"mix-input",             0, NULL, 'm'},
191    {"force-overwrite",       0, NULL, 'f'},
192#endif /* PROG_HAS_OUTPUT */
193    {NULL,                    0, NULL, 0}
194  };
195#endif /* HAVE_GETOPT_H */
196  prog_name = argv[0];
197  if (argc < 1) {
198    usage (stderr, 1);
199    return -1;
200  }
201#ifdef HAVE_GETOPT_H
202  do {
203    next_option = getopt_long (argc, argv, options, long_options, NULL);
204    switch (next_option) {
205      case 'h':                /* help */
206        usage (stdout, 0);
207        return -1;
208      case 'v':                /* verbose */
209        verbose = 1;
210        break;
211      case 'j':
212        usejack = 1;
213        break;
214      case 'n':
215        miditap_note = atoi (optarg);
216        break;
217      case 'V':
218        miditap_velo = atoi (optarg);
219        break;
220      case 'i':
221        source_uri = optarg;
222        break;
223      case 'o':
224        sink_uri = optarg;
225        break;
226      case 'f':                /* force_overwrite flag */
227        force_overwrite = 1;
228        break;
229      case 'r':
230        samplerate = atoi (optarg);
231        break;
232      case 'B':
233        buffer_size = atoi (optarg);
234        break;
235      case 'H':
236        hop_size = atoi (optarg);
237        break;
238      case 'O':                /*onset method */
239        onset_method = optarg;
240        break;
241      case 't':                /* threshold value for onset */
242        onset_threshold = (smpl_t) atof (optarg);
243        break;
244      case 'M':                /* minimum inter-onset-interval */
245        onset_minioi = (smpl_t) atof (optarg);
246        break;
247      case 'p':
248        pitch_method = optarg;
249        break;
250      case 'u':
251        pitch_unit = optarg;
252        break;
253      case 'l':
254        pitch_tolerance = (smpl_t) atof (optarg);
255        break;
256      case 'T':
257        if (strcmp (optarg, "samples") == 0) {
258          time_format = 2;
259        } else if (strcmp (optarg, "ms") == 0) {
260          time_format = 1;
261        } else if (strcmp (optarg, "seconds") == 0) {
262          time_format = 0;
263        } else {
264          errmsg ("Warning: did not get '%s' time-format string\n", optarg);
265        }
266        break;
267      case 's':                /* silence threshold */
268        silence_threshold = (smpl_t) atof (optarg);
269        break;
270      case 'm':                /* mix_input flag */
271        mix_input = 1;
272        break;
273      case '?':                /* unknown options */
274        usage (stderr, 1);
275        break;
276      case -1:                 /* done with options */
277        break;
278      default:                 /*something else unexpected */
279        fprintf (stderr, "Error parsing option '%c'\n", next_option);
280        abort ();
281    }
282  }
283  while (next_option != -1);
284#else /* HAVE_GETOPT_H */
285  int optind = 1;
286#endif /* HAVE_GETOPT_H */
287
288  // if unique, use the non option argument as the source
289  if ( source_uri == NULL ) {
290    if (argc - optind == 1) {
291      source_uri = argv[optind];
292    } else if ( argc - optind > 1 ) {
293      errmsg ("Error: too many non-option arguments `%s'\n", argv[argc - 1]);
294      usage ( stderr, 1 );
295    }
296  } else if ( argc - optind > 0 ) {
297    errmsg ("Error: extra non-option argument %s\n", argv[optind]);
298    usage ( stderr, 1 );
299  }
300
301  // if no source, show a message
302  if (source_uri == NULL) {
303#ifdef PROG_HAS_JACK
304#if HAVE_JACK
305    verbmsg("No input source given, using jack\n");
306    usejack = 1;
307#else
308    errmsg("Error: no arguments given (and no available audio input)\n");
309    usage ( stderr, 1 );
310#endif /* HAVE_JACK */
311#else
312    errmsg("Error: no arguments given\n");
313    usage ( stderr, 1 );
314#endif /* PROG_HAS_JACK */
315  }
316
317  if ((sint_t)hop_size < 1) {
318    errmsg("Error: got hop_size %d, but can not be < 1\n", hop_size);
319    usage ( stderr, 1 );
320  } else if ((sint_t)buffer_size < 2) {
321    errmsg("Error: got buffer_size %d, but can not be < 2\n", buffer_size);
322    usage ( stderr, 1 );
323  } else if ((sint_t)buffer_size < (sint_t)hop_size) {
324    errmsg("Error: hop size (%d) is larger than win size (%d)\n",
325        hop_size, buffer_size);
326    usage ( stderr, 1 );
327  }
328
329  if ((sint_t)samplerate < 0) {
330    errmsg("Error: got samplerate %d, but can not be < 0\n", samplerate);
331    usage ( stderr, 1 );
332  }
333
334  return 0;
335}
Note: See TracBrowser for help on using the repository browser.