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