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