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