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