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