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