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