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