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 | " -s --silence select silence threshold\n" |
---|
104 | " a value in dB, for instance -70, or -100; default=-90\n" |
---|
105 | " -T --time-format select time values output format\n" |
---|
106 | " (samples, ms, seconds) default=seconds\n" |
---|
107 | #ifdef PROG_HAS_OUTPUT |
---|
108 | " -m --mix-input mix input signal with output signal\n" |
---|
109 | " input signal will be added to output synthesis\n" |
---|
110 | " -f --force-overwrite overwrite output file if needed\n" |
---|
111 | " do not fail if output file already exists\n" |
---|
112 | #endif |
---|
113 | #ifdef PROG_HAS_JACK |
---|
114 | " -j --jack use Jack\n" |
---|
115 | #endif |
---|
116 | " -v --verbose be verbose\n" |
---|
117 | " -h --help display this message\n" |
---|
118 | ); |
---|
119 | #else /* HAVE_GETOPT_H */ |
---|
120 | fprintf (stream, "warning: compiled with getopt.h, no argument parsing\n"); |
---|
121 | fprintf (stream, "usage: %s <filename> \n", prog_name); |
---|
122 | #endif /* HAVE_GETOPT_H */ |
---|
123 | exit (exit_code); |
---|
124 | } |
---|
125 | |
---|
126 | int |
---|
127 | parse_args (int argc, char **argv) |
---|
128 | { |
---|
129 | #ifdef HAVE_GETOPT_H |
---|
130 | const char *options = "hv" |
---|
131 | "i:r:B:H:" |
---|
132 | #ifdef PROG_HAS_JACK |
---|
133 | "j" |
---|
134 | #endif /* PROG_HAS_JACK */ |
---|
135 | #ifdef PROG_HAS_OUTPUT |
---|
136 | "o:" |
---|
137 | #endif /* PROG_HAS_OUTPUT */ |
---|
138 | #ifdef PROG_HAS_ONSET |
---|
139 | "O:t:" |
---|
140 | #endif /* PROG_HAS_ONSET */ |
---|
141 | #ifdef PROG_HAS_PITCH |
---|
142 | "p:u:l:" |
---|
143 | #endif /* PROG_HAS_PITCH */ |
---|
144 | "T:" |
---|
145 | "s:mf"; |
---|
146 | int next_option; |
---|
147 | struct option long_options[] = { |
---|
148 | {"help", 0, NULL, 'h'}, |
---|
149 | {"verbose", 0, NULL, 'v'}, |
---|
150 | {"input", 1, NULL, 'i'}, |
---|
151 | {"samplerate", 1, NULL, 'r'}, |
---|
152 | {"bufsize", 1, NULL, 'B'}, |
---|
153 | {"hopsize", 1, NULL, 'H'}, |
---|
154 | #ifdef PROG_HAS_JACK |
---|
155 | {"jack", 0, NULL, 'j'}, |
---|
156 | #endif /* PROG_HAS_JACK */ |
---|
157 | #ifdef PROG_HAS_OUTPUT |
---|
158 | {"output", 1, NULL, 'o'}, |
---|
159 | #endif /* PROG_HAS_OUTPUT */ |
---|
160 | #ifdef PROG_HAS_ONSET |
---|
161 | {"onset", 1, NULL, 'O'}, |
---|
162 | {"onset-threshold", 1, NULL, 't'}, |
---|
163 | #endif /* PROG_HAS_ONSET */ |
---|
164 | #ifdef PROG_HAS_PITCH |
---|
165 | {"pitch", 1, NULL, 'p'}, |
---|
166 | {"pitch-unit", 1, NULL, 'u'}, |
---|
167 | {"pitch-tolerance", 1, NULL, 'l'}, |
---|
168 | #endif /* PROG_HAS_PITCH */ |
---|
169 | {"silence", 1, NULL, 's'}, |
---|
170 | {"time-format", 1, NULL, 'T'}, |
---|
171 | {"mix-input", 0, NULL, 'm'}, |
---|
172 | {"force-overwrite", 0, NULL, 'f'}, |
---|
173 | {NULL, 0, NULL, 0} |
---|
174 | }; |
---|
175 | #endif /* HAVE_GETOPT_H */ |
---|
176 | prog_name = argv[0]; |
---|
177 | if (argc < 1) { |
---|
178 | usage (stderr, 1); |
---|
179 | return -1; |
---|
180 | } |
---|
181 | #ifdef HAVE_GETOPT_H |
---|
182 | do { |
---|
183 | next_option = getopt_long (argc, argv, options, long_options, NULL); |
---|
184 | switch (next_option) { |
---|
185 | case 'h': /* help */ |
---|
186 | usage (stdout, 0); |
---|
187 | return -1; |
---|
188 | case 'v': /* verbose */ |
---|
189 | verbose = 1; |
---|
190 | break; |
---|
191 | case 'j': |
---|
192 | usejack = 1; |
---|
193 | break; |
---|
194 | case 'i': |
---|
195 | source_uri = optarg; |
---|
196 | break; |
---|
197 | case 'o': |
---|
198 | sink_uri = optarg; |
---|
199 | break; |
---|
200 | case 'f': /* force_overwrite flag */ |
---|
201 | force_overwrite = 1; |
---|
202 | break; |
---|
203 | case 'r': |
---|
204 | samplerate = atoi (optarg); |
---|
205 | break; |
---|
206 | case 'B': |
---|
207 | buffer_size = atoi (optarg); |
---|
208 | break; |
---|
209 | case 'H': |
---|
210 | hop_size = atoi (optarg); |
---|
211 | break; |
---|
212 | case 'O': /*onset method */ |
---|
213 | onset_method = optarg; |
---|
214 | break; |
---|
215 | case 't': /* threshold value for onset */ |
---|
216 | onset_threshold = (smpl_t) atof (optarg); |
---|
217 | break; |
---|
218 | case 'p': |
---|
219 | pitch_method = optarg; |
---|
220 | break; |
---|
221 | case 'u': |
---|
222 | pitch_unit = optarg; |
---|
223 | break; |
---|
224 | case 'l': |
---|
225 | pitch_tolerance = (smpl_t) atof (optarg); |
---|
226 | break; |
---|
227 | case 'T': |
---|
228 | if (strcmp (optarg, "samples") == 0) { |
---|
229 | time_format = 2; |
---|
230 | } else if (strcmp (optarg, "ms") == 0) { |
---|
231 | time_format = 1; |
---|
232 | } else if (strcmp (optarg, "seconds") == 0) { |
---|
233 | time_format = 0; |
---|
234 | } else { |
---|
235 | errmsg ("Warning: did not get '%s' time-format string\n", optarg); |
---|
236 | } |
---|
237 | break; |
---|
238 | case 's': /* silence threshold */ |
---|
239 | silence_threshold = (smpl_t) atof (optarg); |
---|
240 | break; |
---|
241 | case 'm': /* mix_input flag */ |
---|
242 | mix_input = 1; |
---|
243 | break; |
---|
244 | case '?': /* unknown options */ |
---|
245 | usage (stderr, 1); |
---|
246 | break; |
---|
247 | case -1: /* done with options */ |
---|
248 | break; |
---|
249 | default: /*something else unexpected */ |
---|
250 | fprintf (stderr, "Error parsing option '%c'\n", next_option); |
---|
251 | abort (); |
---|
252 | } |
---|
253 | } |
---|
254 | while (next_option != -1); |
---|
255 | #else /* HAVE_GETOPT_H */ |
---|
256 | int optind = 1; |
---|
257 | #endif /* HAVE_GETOPT_H */ |
---|
258 | |
---|
259 | // if unique, use the non option argument as the source |
---|
260 | if ( source_uri == NULL ) { |
---|
261 | if (argc - optind == 1) { |
---|
262 | source_uri = argv[optind]; |
---|
263 | } else if ( argc - optind > 1 ) { |
---|
264 | errmsg ("Error: too many non-option arguments `%s'\n", argv[argc - 1]); |
---|
265 | usage ( stderr, 1 ); |
---|
266 | } |
---|
267 | } else if ( argc - optind > 0 ) { |
---|
268 | errmsg ("Error: extra non-option argument %s\n", argv[optind]); |
---|
269 | usage ( stderr, 1 ); |
---|
270 | } |
---|
271 | |
---|
272 | // if no source, show a message |
---|
273 | if (source_uri == NULL) { |
---|
274 | #ifdef PROG_HAS_JACK |
---|
275 | #if HAVE_JACK |
---|
276 | verbmsg("No input source given, using jack\n"); |
---|
277 | usejack = 1; |
---|
278 | #else |
---|
279 | errmsg("Error: no arguments given (and no available audio input)\n"); |
---|
280 | usage ( stderr, 1 ); |
---|
281 | #endif /* HAVE_JACK */ |
---|
282 | #else |
---|
283 | errmsg("Error: no arguments given\n"); |
---|
284 | usage ( stderr, 1 ); |
---|
285 | #endif /* PROG_HAS_JACK */ |
---|
286 | } |
---|
287 | |
---|
288 | if ((sint_t)hop_size < 1) { |
---|
289 | errmsg("Error: got hop_size %d, but can not be < 1\n", hop_size); |
---|
290 | usage ( stderr, 1 ); |
---|
291 | } else if ((sint_t)buffer_size < 2) { |
---|
292 | errmsg("Error: got buffer_size %d, but can not be < 2\n", buffer_size); |
---|
293 | usage ( stderr, 1 ); |
---|
294 | } else if ((sint_t)buffer_size < (sint_t)hop_size) { |
---|
295 | errmsg("Error: hop size (%d) is larger than win size (%d)\n", |
---|
296 | hop_size, buffer_size); |
---|
297 | usage ( stderr, 1 ); |
---|
298 | } |
---|
299 | |
---|
300 | if ((sint_t)samplerate < 0) { |
---|
301 | errmsg("Error: got samplerate %d, but can not be < 0\n", samplerate); |
---|
302 | usage ( stderr, 1 ); |
---|
303 | } |
---|
304 | |
---|
305 | return 0; |
---|
306 | } |
---|