source: examples/parse_args.h @ e9e2a87

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

examples/: define aubio_process_func_t once in utils.h

  • Property mode set to 100644
File size: 7.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// tempo stuff
38extern char_t * tempo_method;
39// more general stuff
40extern smpl_t silence_threshold;
41extern uint_t mix_input;
42
43// functions defined in utils.c
44extern void examples_common_init (int argc, char **argv);
45extern void examples_common_del (void);
46extern void examples_common_process (aubio_process_func_t process_func,
47    aubio_print_func_t print);
48
49// internal stuff
50extern int blocks;
51
52extern fvec_t *ibuf;
53extern fvec_t *obuf;
54
55const char *prog_name;
56
57void
58usage (FILE * stream, int exit_code)
59{
60  fprintf (stream, "usage: %s [ options ] \n", prog_name);
61  fprintf (stream,
62      "       -i      --input            input type\n"
63#ifdef PROG_HAS_OUTPUT
64      "       -o      --output           output type\n"
65#endif
66      "       -r      --samplerate       select samplerate\n"
67      "       -B      --bufsize          set buffer size\n"
68      "       -H      --hopsize          set hopsize\n"
69#ifdef PROG_HAS_ONSET
70      "       -O      --onset            select onset detection algorithm\n"
71      "       -t      --onset-threshold  set onset detection threshold\n"
72#endif /* PROG_HAS_ONSET */
73#ifdef PROG_HAS_PITCH
74      "       -p      --pitch            select pitch detection algorithm\n"
75      "       -u      --pitch-unit       select pitch output unit\n"
76      "       -l      --pitch-tolerance  select pitch tolerance\n"
77#endif /* PROG_HAS_PITCH */
78      "       -s      --silence          select silence threshold\n"
79#ifdef PROG_HAS_OUTPUT
80      "       -m      --mix-input        mix input signal with output signal\n"
81#endif
82#ifdef PROG_HAS_JACK
83      "       -j      --jack             use Jack\n"
84#endif
85      "       -v      --verbose          be verbose\n"
86      "       -h      --help             display this message\n"
87      );
88  exit (exit_code);
89}
90
91int
92parse_args (int argc, char **argv)
93{
94  const char *options = "hv"
95    "i:r:B:H:"
96#ifdef PROG_HAS_JACK
97    "j"
98#endif /* PROG_HAS_JACK */
99#ifdef PROG_HAS_OUTPUT
100    "o:"
101#endif /* PROG_HAS_OUTPUT */
102#ifdef PROG_HAS_ONSET
103    "O:t:"
104#endif /* PROG_HAS_ONSET */
105#ifdef PROG_HAS_PITCH
106    "p:u:l:"
107#endif /* PROG_HAS_PITCH */
108    "s:m";
109  int next_option;
110  struct option long_options[] = {
111    {"help",                  0, NULL, 'h'},
112    {"verbose",               0, NULL, 'v'},
113    {"input",                 1, NULL, 'i'},
114    {"samplerate",            1, NULL, 'r'},
115    {"bufsize",               1, NULL, 'B'},
116    {"hopsize",               1, NULL, 'H'},
117#ifdef PROG_HAS_JACK
118    {"jack",                  0, NULL, 'j'},
119#endif /* PROG_HAS_JACK */
120#ifdef PROG_HAS_OUTPUT
121    {"output",                1, NULL, 'o'},
122#endif /* PROG_HAS_OUTPUT */
123#ifdef PROG_HAS_ONSET
124    {"onset",                 1, NULL, 'O'},
125    {"onset-threshold",       1, NULL, 't'},
126#endif /* PROG_HAS_ONSET */
127#ifdef PROG_HAS_PITCH
128    {"pitch",                 1, NULL, 'p'},
129    {"pitch-unit",            1, NULL, 'u'},
130    {"pitch-tolerance",       1, NULL, 'l'},
131#endif /* PROG_HAS_PITCH */
132    {"silence",               1, NULL, 's'},
133    {"mix-input",             0, NULL, 'm'},
134    {NULL,                    0, NULL, 0}
135  };
136  prog_name = argv[0];
137  if (argc < 1) {
138    usage (stderr, 1);
139    return -1;
140  }
141  do {
142    next_option = getopt_long (argc, argv, options, long_options, NULL);
143    switch (next_option) {
144      case 'h':                /* help */
145        usage (stdout, 0);
146        return -1;
147      case 'v':                /* verbose */
148        verbose = 1;
149        break;
150      case 'j':
151        usejack = 1;
152        break;
153      case 'i':
154        source_uri = optarg;
155        break;
156      case 'o':
157        sink_uri = optarg;
158        break;
159      case 'r':
160        samplerate = atoi (optarg);
161        break;
162      case 'B':
163        buffer_size = atoi (optarg);
164        break;
165      case 'H':
166        hop_size = atoi (optarg);
167        break;
168      case 'O':                /*onset type */
169        onset_method = optarg;
170        break;
171      case 't':                /* threshold value for onset */
172        onset_threshold = (smpl_t) atof (optarg);
173        break;
174      case 'p':
175        pitch_method = optarg;
176        break;
177      case 'u':
178        pitch_unit = optarg;
179        break;
180      case 'l':
181        pitch_tolerance = (smpl_t) atof (optarg);
182        break;
183      case 's':                /* silence threshold */
184        silence_threshold = (smpl_t) atof (optarg);
185        break;
186      case 'm':                /* mix_input flag */
187        mix_input = 1;
188        break;
189      case '?':                /* unknown options */
190        usage (stderr, 1);
191        break;
192      case -1:                 /* done with options */
193        break;
194      default:                 /*something else unexpected */
195        fprintf (stderr, "Error parsing option '%c'\n", next_option);
196        abort ();
197    }
198  }
199  while (next_option != -1);
200
201  // if unique, use the non option argument as the source
202  if ( source_uri == NULL ) {
203    if (argc - optind == 1) {
204      source_uri = argv[optind];
205    } else if ( argc - optind > 1 ) {
206      errmsg ("Error: too many non-option arguments `%s'\n", argv[argc - 1]);
207      usage ( stderr, 1 );
208    }
209  } else if ( argc - optind > 0 ) {
210    errmsg ("Error: extra non-option argument %s\n", argv[optind]);
211    usage ( stderr, 1 );
212  }
213
214  // if no source, show a message
215  if (source_uri == NULL) {
216#ifdef PROG_HAS_JACK
217#if HAVE_JACK
218    verbmsg("No input source given, using jack\n");
219    usejack = 1;
220#else
221    errmsg("Error: no arguments given (and no available audio input)\n");
222    usage ( stderr, 1 );
223#endif /* HAVE_JACK */
224#else
225    errmsg("Error: no arguments given\n");
226    usage ( stderr, 1 );
227#endif /* PROG_HAS_JACK */
228  }
229
230  if (hop_size < 1) {
231    errmsg("Error: got hop_size %d, but can not be < 1\n", hop_size);
232    usage ( stderr, 1 );
233  } else if (buffer_size < 2) {
234    errmsg("Error: got buffer_size %d, but can not be < 2\n", buffer_size);
235    usage ( stderr, 1 );
236  } else if (buffer_size < hop_size + 1) {
237    errmsg("Error: hop size (%d) is larger than or equal to win size (%d)\n",
238        buffer_size, hop_size);
239    usage ( stderr, 1 );
240  }
241
242  return 0;
243}
Note: See TracBrowser for help on using the repository browser.