diff options
author | Chris Robinson <[email protected]> | 2017-08-20 04:30:53 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2017-08-20 04:30:53 -0700 |
commit | 55643d6370430a73bbf0c4ce8d7a2e689b9891d9 (patch) | |
tree | 86de62cabeab690c13bb2d5cffa9b3152f6b95bf /utils/makehrtf.c | |
parent | 051828344ecaeff0140034693e3f63355f490e9b (diff) |
Use getopt to handle options in makehrtf
Diffstat (limited to 'utils/makehrtf.c')
-rw-r--r-- | utils/makehrtf.c | 161 |
1 files changed, 89 insertions, 72 deletions
diff --git a/utils/makehrtf.c b/utils/makehrtf.c index 344754e9..e7dd34c3 100644 --- a/utils/makehrtf.c +++ b/utils/makehrtf.c @@ -71,6 +71,11 @@ #ifdef HAVE_STRINGS_H #include <strings.h> #endif +#ifdef HAVE_GETOPT +#include <unistd.h> +#else +#include "getopt.h" +#endif // Rely (if naively) on OpenAL's header for the types used for serialization. #include "AL/al.h" @@ -2911,26 +2916,26 @@ static void PrintHelp(const char *argv0, FILE *ofile) fprintf(ofile, " Defaults output to: ./oalsoft_hrtf_%%r.mhr\n"); fprintf(ofile, " -h, --help Displays this help information.\n\n"); fprintf(ofile, "Options:\n"); - fprintf(ofile, " -r=<rate> Change the data set sample rate to the specified value and\n"); + fprintf(ofile, " -r <rate> Change the data set sample rate to the specified value and\n"); fprintf(ofile, " resample the HRIRs accordingly.\n"); - fprintf(ofile, " -f=<points> Override the FFT window size (default: %u).\n", DEFAULT_FFTSIZE); - fprintf(ofile, " -e={on|off} Toggle diffuse-field equalization (default: %s).\n", (DEFAULT_EQUALIZE ? "on" : "off")); - fprintf(ofile, " -s={on|off} Toggle surface-weighted diffuse-field average (default: %s).\n", (DEFAULT_SURFACE ? "on" : "off")); - fprintf(ofile, " -l={<dB>|none} Specify a limit to the magnitude range of the diffuse-field\n"); + fprintf(ofile, " -f <points> Override the FFT window size (default: %u).\n", DEFAULT_FFTSIZE); + fprintf(ofile, " -e {on|off} Toggle diffuse-field equalization (default: %s).\n", (DEFAULT_EQUALIZE ? "on" : "off")); + fprintf(ofile, " -s {on|off} Toggle surface-weighted diffuse-field average (default: %s).\n", (DEFAULT_SURFACE ? "on" : "off")); + fprintf(ofile, " -l {<dB>|none} Specify a limit to the magnitude range of the diffuse-field\n"); fprintf(ofile, " average (default: %.2f).\n", DEFAULT_LIMIT); - fprintf(ofile, " -w=<points> Specify the size of the truncation window that's applied\n"); + fprintf(ofile, " -w <points> Specify the size of the truncation window that's applied\n"); fprintf(ofile, " after minimum-phase reconstruction (default: %u).\n", DEFAULT_TRUNCSIZE); - fprintf(ofile, " -d={dataset| Specify the model used for calculating the head-delay timing\n"); + fprintf(ofile, " -d {dataset| Specify the model used for calculating the head-delay timing\n"); fprintf(ofile, " sphere} values (default: %s).\n", ((DEFAULT_HEAD_MODEL == HM_DATASET) ? "dataset" : "sphere")); - fprintf(ofile, " -c=<size> Use a customized head radius measured ear-to-ear in meters.\n"); - fprintf(ofile, " -i=<filename> Specify an HRIR definition file to use (defaults to stdin).\n"); - fprintf(ofile, " -o=<filename> Specify an output file. Overrides command-selected default.\n"); + fprintf(ofile, " -c <size> Use a customized head radius measured ear-to-ear in meters.\n"); + fprintf(ofile, " -i <filename> Specify an HRIR definition file to use (defaults to stdin).\n"); + fprintf(ofile, " -o <filename> Specify an output file. Overrides command-selected default.\n"); fprintf(ofile, " Use of '%%r' will be substituted with the data set sample rate.\n"); } #ifdef _WIN32 #define main my_main -int main(int argc, const char *argv[]); +int main(int argc, char *argv[]); static char **arglist; static void cleanup_arglist(void) @@ -2950,12 +2955,12 @@ int wmain(int argc, const wchar_t *wargv[]) for(i = 0;i < argc;i++) arglist[i] = ToUTF8(wargv[i]); - return main(argc, (const char**)arglist); + return main(argc, arglist); } #endif // Standard command line dispatch. -int main(int argc, const char *argv[]) +int main(int argc, char *argv[]) { const char *inName = NULL, *outName = NULL; OutputFormatT outFormat; @@ -2967,13 +2972,13 @@ int main(int argc, const char *argv[]) uint truncSize; double radius; double limit; - int argi; + int opt; if(argc < 2 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { fprintf(stdout, "HRTF Processing and Composition Utility\n\n"); PrintHelp(argv[0], stdout); - return 0; + exit(EXIT_SUCCESS); } if(strcmp(argv[1], "--make-mhr") == 0 || strcmp(argv[1], "-m") == 0) @@ -2985,7 +2990,7 @@ int main(int argc, const char *argv[]) { fprintf(stderr, "Error: Invalid command '%s'.\n\n", argv[1]); PrintHelp(argv[0], stderr); - return -1; + exit(EXIT_FAILURE); } outRate = 0; @@ -2998,112 +3003,124 @@ int main(int argc, const char *argv[]) radius = DEFAULT_CUSTOM_RADIUS; experimental = 0; - argi = 2; - while(argi < argc) + optind = 2; + while((opt=getopt(argc, argv, "r:f:e:s:k:w:d:c:e:i:o:xh")) != -1) { - if(strncmp(argv[argi], "-r=", 3) == 0) + switch(opt) { - outRate = strtoul(&argv[argi][3], &end, 10); + case 'r': + outRate = strtoul(optarg, &end, 10); if(end[0] != '\0' || outRate < MIN_RATE || outRate > MAX_RATE) { fprintf(stderr, "Error: Expected a value from %u to %u for '-r'.\n", MIN_RATE, MAX_RATE); - return -1; + exit(EXIT_FAILURE); } - } - else if(strncmp(argv[argi], "-f=", 3) == 0) - { - fftSize = strtoul(&argv[argi][3], &end, 10); + break; + + case 'f': + fftSize = strtoul(optarg, &end, 10); if(end[0] != '\0' || (fftSize&(fftSize-1)) || fftSize < MIN_FFTSIZE || fftSize > MAX_FFTSIZE) { fprintf(stderr, "Error: Expected a power-of-two value from %u to %u for '-f'.\n", MIN_FFTSIZE, MAX_FFTSIZE); - return -1; + exit(EXIT_FAILURE); } - } - else if(strncmp(argv[argi], "-e=", 3) == 0) - { - if(strcmp(&argv[argi][3], "on") == 0) + break; + + case 'e': + if(strcmp(optarg, "on") == 0) equalize = 1; - else if(strcmp(&argv[argi][3], "off") == 0) + else if(strcmp(optarg, "off") == 0) equalize = 0; else { fprintf(stderr, "Error: Expected 'on' or 'off' for '-e'.\n"); - return -1; + exit(EXIT_FAILURE); } - } - else if(strncmp(argv[argi], "-s=", 3) == 0) - { - if(strcmp(&argv[argi][3], "on") == 0) + break; + + case 's': + if(strcmp(optarg, "on") == 0) surface = 1; - else if(strcmp(&argv[argi][3], "off") == 0) + else if(strcmp(optarg, "off") == 0) surface = 0; else { fprintf(stderr, "Error: Expected 'on' or 'off' for '-s'.\n"); - return -1; + exit(EXIT_FAILURE); } - } - else if(strncmp(argv[argi], "-l=", 3) == 0) - { - if(strcmp(&argv[argi][3], "none") == 0) + break; + + case 'l': + if(strcmp(optarg, "none") == 0) limit = 0.0; else { - limit = strtod(&argv[argi] [3], &end); + limit = strtod(optarg, &end); if(end[0] != '\0' || limit < MIN_LIMIT || limit > MAX_LIMIT) { fprintf(stderr, "Error: Expected 'none' or a value from %.2f to %.2f for '-l'.\n", MIN_LIMIT, MAX_LIMIT); - return -1; + exit(EXIT_FAILURE); } } - } - else if(strncmp(argv[argi], "-w=", 3) == 0) - { - truncSize = strtoul(&argv[argi][3], &end, 10); + break; + + case 'w': + truncSize = strtoul(optarg, &end, 10); if(end[0] != '\0' || truncSize < MIN_TRUNCSIZE || truncSize > MAX_TRUNCSIZE || (truncSize%MOD_TRUNCSIZE)) { fprintf(stderr, "Error: Expected a value from %u to %u in multiples of %u for '-w'.\n", MIN_TRUNCSIZE, MAX_TRUNCSIZE, MOD_TRUNCSIZE); - return -1; + exit(EXIT_FAILURE); } - } - else if(strncmp(argv[argi], "-d=", 3) == 0) - { - if(strcmp(&argv[argi][3], "dataset") == 0) + break; + + case 'd': + if(strcmp(optarg, "dataset") == 0) model = HM_DATASET; - else if(strcmp(&argv[argi][3], "sphere") == 0) + else if(strcmp(optarg, "sphere") == 0) model = HM_SPHERE; else { fprintf(stderr, "Error: Expected 'dataset' or 'sphere' for '-d'.\n"); - return -1; + exit(EXIT_FAILURE); } - } - else if(strncmp(argv[argi], "-c=", 3) == 0) - { - radius = strtod(&argv[argi][3], &end); + break; + + case 'c': + radius = strtod(optarg, &end); if(end[0] != '\0' || radius < MIN_CUSTOM_RADIUS || radius > MAX_CUSTOM_RADIUS) { fprintf(stderr, "Error: Expected a value from %.2f to %.2f for '-c'.\n", MIN_CUSTOM_RADIUS, MAX_CUSTOM_RADIUS); - return -1; + exit(EXIT_FAILURE); } - } - else if(strncmp(argv[argi], "-i=", 3) == 0) - inName = &argv[argi][3]; - else if(strncmp(argv[argi], "-o=", 3) == 0) - outName = &argv[argi][3]; - else if(strcmp(argv[argi], "--experimental") == 0) + break; + + case 'i': + inName = optarg; + break; + + case 'o': + outName = optarg; + break; + + case 'x': experimental = 1; - else - { - fprintf(stderr, "Error: Invalid option '%s'.\n", argv[argi]); - return -1; + break; + + case 'h': + PrintHelp(argv[0], stdout); + exit(EXIT_SUCCESS); + + default: /* '?' */ + PrintHelp(argv[0], stderr); + exit(EXIT_FAILURE); } - argi++; } + if(!ProcessDefinition(inName, outRate, fftSize, equalize, surface, limit, truncSize, model, radius, outFormat, experimental, outName)) return -1; fprintf(stdout, "Operation completed.\n"); - return 0; + + return EXIT_SUCCESS; } |