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