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