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