source: examples/parse_args.h @ df7be43

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

examples/parse_args.h: only parse time format string once, warn if unknown

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