DSF2FLAC
|
00001 /* 00002 File autogenerated by gengetopt version 2.22.5 00003 generated with the following command: 00004 gengetopt 00005 00006 The developers of gengetopt consider the fixed text that goes in all 00007 gengetopt output files to be in the public domain: 00008 we make no copyright claims on it. 00009 */ 00010 00011 /* If we use autoconf. */ 00012 #ifdef HAVE_CONFIG_H 00013 #include "config.h" 00014 #endif 00015 00016 #include <stdio.h> 00017 #include <stdlib.h> 00018 #include <string.h> 00019 00020 #ifndef FIX_UNUSED 00021 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */ 00022 #endif 00023 00024 #include <getopt.h> 00025 00026 #include "cmdline.h" 00027 00028 const char *gengetopt_args_info_purpose = 00029 "Convert DSD audio files from DFF or DSF format into PCM flac audio."; 00030 00031 const char *gengetopt_args_info_usage = "Usage: dsf2flac [OPTIONS]..."; 00032 00033 const char *gengetopt_args_info_description = ""; 00034 00035 const char *gengetopt_args_info_help[] = 00036 { " -h, --help Print help and exit", 00037 " -V, --version Print version and exit", 00038 " -r, --samplerate=Hz Output sample rate (possible values=\"88200\", \n \"176400\", \"352800\" default=`88200')", 00039 " -b, --bits=bits Output bitdepth (possible values=\"16\", \"20\", \n \"24\" default=`24')", 00040 " -n, --nodither Don't add dither before quantization (default=off)", 00041 " -s, --scale=dB Scale adjustment. Raw DSD has a modulation depth of \n approximately 0.5 so with no scaling the PCM peak \n level is approximately -6dB below 0dBFs \n (default=`4')", 00042 " -i, --infile=filepath Input DSF or DFF file", 00043 " -o, --outfile=filepath Output FLAC file, if not specified the output file be \n the same as the input file with the extension \n changed", 00044 " -d, --dop Encode DSD data directly into FLAC file without \n conversion to PCM using DoP format (DSD over PCM) \n (default=off)", 00045 0 }; 00046 00047 typedef enum { 00048 ARG_NO, ARG_FLAG, ARG_STRING, ARG_INT, ARG_FLOAT 00049 } cmdline_parser_arg_type; 00050 00051 static 00052 void clear_given(struct gengetopt_args_info *args_info); 00053 static 00054 void clear_args(struct gengetopt_args_info *args_info); 00055 00056 static int 00057 cmdline_parser_internal(int argc, char **argv, 00058 struct gengetopt_args_info *args_info, 00059 struct cmdline_parser_params *params, const char *additional_error); 00060 00061 static int 00062 cmdline_parser_required2(struct gengetopt_args_info *args_info, 00063 const char *prog_name, const char *additional_error); 00064 00065 const char *cmdline_parser_samplerate_values[] = { "88200", "176400", "352800", 00066 0 }; /*< Possible values for samplerate. */ 00067 const char *cmdline_parser_bits_values[] = { "16", "20", "24", 0 }; /*< Possible values for bits. */ 00068 00069 static char * 00070 gengetopt_strdup(const char *s); 00071 00072 static 00073 void clear_given(struct gengetopt_args_info *args_info) { 00074 args_info->help_given = 0; 00075 args_info->version_given = 0; 00076 args_info->samplerate_given = 0; 00077 args_info->bits_given = 0; 00078 args_info->nodither_given = 0; 00079 args_info->scale_given = 0; 00080 args_info->infile_given = 0; 00081 args_info->outfile_given = 0; 00082 args_info->dop_given = 0; 00083 } 00084 00085 static 00086 void clear_args(struct gengetopt_args_info *args_info) { 00087 FIX_UNUSED(args_info); 00088 args_info->samplerate_arg = 88200; 00089 args_info->samplerate_orig = NULL; 00090 args_info->bits_arg = 24; 00091 args_info->bits_orig = NULL; 00092 args_info->nodither_flag = 0; 00093 args_info->scale_arg = 4; 00094 args_info->scale_orig = NULL; 00095 args_info->infile_arg = NULL; 00096 args_info->infile_orig = NULL; 00097 args_info->outfile_arg = NULL; 00098 args_info->outfile_orig = NULL; 00099 args_info->dop_flag = 0; 00100 00101 } 00102 00103 static 00104 void init_args_info(struct gengetopt_args_info *args_info) { 00105 00106 args_info->help_help = gengetopt_args_info_help[0]; 00107 args_info->version_help = gengetopt_args_info_help[1]; 00108 args_info->samplerate_help = gengetopt_args_info_help[2]; 00109 args_info->bits_help = gengetopt_args_info_help[3]; 00110 args_info->nodither_help = gengetopt_args_info_help[4]; 00111 args_info->scale_help = gengetopt_args_info_help[5]; 00112 args_info->infile_help = gengetopt_args_info_help[6]; 00113 args_info->outfile_help = gengetopt_args_info_help[7]; 00114 args_info->dop_help = gengetopt_args_info_help[8]; 00115 00116 } 00117 00118 void cmdline_parser_print_version(void) { 00119 printf("%s %s\n", 00120 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? 00121 CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), 00122 CMDLINE_PARSER_VERSION); 00123 } 00124 00125 static void print_help_common(void) { 00126 cmdline_parser_print_version(); 00127 00128 if (strlen(gengetopt_args_info_purpose) > 0) 00129 printf("\n%s\n", gengetopt_args_info_purpose); 00130 00131 if (strlen(gengetopt_args_info_usage) > 0) 00132 printf("\n%s\n", gengetopt_args_info_usage); 00133 00134 printf("\n"); 00135 00136 if (strlen(gengetopt_args_info_description) > 0) 00137 printf("%s\n\n", gengetopt_args_info_description); 00138 } 00139 00140 void cmdline_parser_print_help(void) { 00141 int i = 0; 00142 print_help_common(); 00143 while (gengetopt_args_info_help[i]) 00144 printf("%s\n", gengetopt_args_info_help[i++]); 00145 } 00146 00147 void cmdline_parser_init(struct gengetopt_args_info *args_info) { 00148 clear_given(args_info); 00149 clear_args(args_info); 00150 init_args_info(args_info); 00151 } 00152 00153 void cmdline_parser_params_init(struct cmdline_parser_params *params) { 00154 if (params) { 00155 params->override = 0; 00156 params->initialize = 1; 00157 params->check_required = 1; 00158 params->check_ambiguity = 0; 00159 params->print_errors = 1; 00160 } 00161 } 00162 00163 struct cmdline_parser_params * 00164 cmdline_parser_params_create(void) { 00165 struct cmdline_parser_params *params = 00166 (struct cmdline_parser_params *) malloc( 00167 sizeof(struct cmdline_parser_params)); 00168 cmdline_parser_params_init(params); 00169 return params; 00170 } 00171 00172 static void free_string_field(char **s) { 00173 if (*s) { 00174 free(*s); 00175 *s = 0; 00176 } 00177 } 00178 00179 static void cmdline_parser_release(struct gengetopt_args_info *args_info) { 00180 00181 free_string_field(&(args_info->samplerate_orig)); 00182 free_string_field(&(args_info->bits_orig)); 00183 free_string_field(&(args_info->scale_orig)); 00184 free_string_field(&(args_info->infile_arg)); 00185 free_string_field(&(args_info->infile_orig)); 00186 free_string_field(&(args_info->outfile_arg)); 00187 free_string_field(&(args_info->outfile_orig)); 00188 00189 clear_given(args_info); 00190 } 00191 00199 static int check_possible_values(const char *val, const char *values[]) { 00200 int i, found, last; 00201 size_t len; 00202 00203 if (!val) /* otherwise strlen() crashes below */ 00204 return -1; /* -1 means no argument for the option */ 00205 00206 found = last = 0; 00207 00208 for (i = 0, len = strlen(val); values[i]; ++i) { 00209 if (strncmp(val, values[i], len) == 0) { 00210 ++found; 00211 last = i; 00212 if (strlen(values[i]) == len) 00213 return i; /* exact macth no need to check more */ 00214 } 00215 } 00216 00217 if (found == 1) /* one match: OK */ 00218 return last; 00219 00220 return (found ? -2 : -1); /* return many values or none matched */ 00221 } 00222 00223 static void write_into_file(FILE *outfile, const char *opt, const char *arg, 00224 const char *values[]) { 00225 int found = -1; 00226 if (arg) { 00227 if (values) { 00228 found = check_possible_values(arg, values); 00229 } 00230 if (found >= 0) 00231 fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]); 00232 else 00233 fprintf(outfile, "%s=\"%s\"\n", opt, arg); 00234 } else { 00235 fprintf(outfile, "%s\n", opt); 00236 } 00237 } 00238 00239 int cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info) { 00240 int i = 0; 00241 00242 if (!outfile) { 00243 fprintf(stderr, "%s: cannot dump options to stream\n", 00244 CMDLINE_PARSER_PACKAGE); 00245 return EXIT_FAILURE; 00246 } 00247 00248 if (args_info->help_given) 00249 write_into_file(outfile, "help", 0, 0); 00250 if (args_info->version_given) 00251 write_into_file(outfile, "version", 0, 0); 00252 if (args_info->samplerate_given) 00253 write_into_file(outfile, "samplerate", args_info->samplerate_orig, 00254 cmdline_parser_samplerate_values); 00255 if (args_info->bits_given) 00256 write_into_file(outfile, "bits", args_info->bits_orig, 00257 cmdline_parser_bits_values); 00258 if (args_info->nodither_given) 00259 write_into_file(outfile, "nodither", 0, 0); 00260 if (args_info->scale_given) 00261 write_into_file(outfile, "scale", args_info->scale_orig, 0); 00262 if (args_info->infile_given) 00263 write_into_file(outfile, "infile", args_info->infile_orig, 0); 00264 if (args_info->outfile_given) 00265 write_into_file(outfile, "outfile", args_info->outfile_orig, 0); 00266 if (args_info->dop_given) 00267 write_into_file(outfile, "dop", 0, 0); 00268 00269 i = EXIT_SUCCESS; 00270 return i; 00271 } 00272 00273 int cmdline_parser_file_save(const char *filename, 00274 struct gengetopt_args_info *args_info) { 00275 FILE *outfile; 00276 int i = 0; 00277 00278 outfile = fopen(filename, "w"); 00279 00280 if (!outfile) { 00281 fprintf(stderr, "%s: cannot open file for writing: %s\n", 00282 CMDLINE_PARSER_PACKAGE, filename); 00283 return EXIT_FAILURE; 00284 } 00285 00286 i = cmdline_parser_dump(outfile, args_info); 00287 fclose(outfile); 00288 00289 return i; 00290 } 00291 00292 void cmdline_parser_free(struct gengetopt_args_info *args_info) { 00293 cmdline_parser_release(args_info); 00294 } 00295 00297 char * 00298 gengetopt_strdup(const char *s) { 00299 char *result = 0; 00300 if (!s) 00301 return result; 00302 00303 result = (char*) malloc(strlen(s) + 1); 00304 if (result == (char*) 0) 00305 return (char*) 0; 00306 strcpy(result, s); 00307 return result; 00308 } 00309 00310 int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info) { 00311 return cmdline_parser2(argc, argv, args_info, 0, 1, 1); 00312 } 00313 00314 int cmdline_parser_ext(int argc, char **argv, 00315 struct gengetopt_args_info *args_info, 00316 struct cmdline_parser_params *params) { 00317 int result; 00318 result = cmdline_parser_internal(argc, argv, args_info, params, 0); 00319 00320 if (result == EXIT_FAILURE) { 00321 cmdline_parser_free(args_info); 00322 exit(EXIT_FAILURE); 00323 } 00324 00325 return result; 00326 } 00327 00328 int cmdline_parser2(int argc, char **argv, 00329 struct gengetopt_args_info *args_info, int override, int initialize, 00330 int check_required) { 00331 int result; 00332 struct cmdline_parser_params params; 00333 00334 params.override = override; 00335 params.initialize = initialize; 00336 params.check_required = check_required; 00337 params.check_ambiguity = 0; 00338 params.print_errors = 1; 00339 00340 result = cmdline_parser_internal(argc, argv, args_info, ¶ms, 0); 00341 00342 if (result == EXIT_FAILURE) { 00343 cmdline_parser_free(args_info); 00344 exit(EXIT_FAILURE); 00345 } 00346 00347 return result; 00348 } 00349 00350 int cmdline_parser_required(struct gengetopt_args_info *args_info, 00351 const char *prog_name) { 00352 int result = EXIT_SUCCESS; 00353 00354 if (cmdline_parser_required2(args_info, prog_name, 0) > 0) 00355 result = EXIT_FAILURE; 00356 00357 if (result == EXIT_FAILURE) { 00358 cmdline_parser_free(args_info); 00359 exit(EXIT_FAILURE); 00360 } 00361 00362 return result; 00363 } 00364 00365 int cmdline_parser_required2(struct gengetopt_args_info *args_info, 00366 const char *prog_name, const char *additional_error) { 00367 int error = 0; 00368 FIX_UNUSED(additional_error); 00369 00370 /* checks for required options */ 00371 if (!args_info->infile_given) { 00372 fprintf(stderr, "%s: '--infile' ('-i') option required%s\n", prog_name, 00373 (additional_error ? additional_error : "")); 00374 error = 1; 00375 } 00376 00377 /* checks for dependences among options */ 00378 00379 return error; 00380 } 00381 00382 static char *package_name = 0; 00383 00402 static 00403 int update_arg(void *field, char **orig_field, unsigned int *field_given, 00404 unsigned int *prev_given, char *value, const char *possible_values[], 00405 const char *default_value, cmdline_parser_arg_type arg_type, 00406 int check_ambiguity, int override, int no_free, int multiple_option, 00407 const char *long_opt, char short_opt, const char *additional_error) { 00408 char *stop_char = 0; 00409 const char *val = value; 00410 int found; 00411 char **string_field; 00412 FIX_UNUSED(field); 00413 00414 stop_char = 0; 00415 found = 0; 00416 00417 if (!multiple_option && prev_given 00418 && (*prev_given || (check_ambiguity && *field_given))) { 00419 if (short_opt != '-') 00420 fprintf(stderr, 00421 "%s: `--%s' (`-%c') option given more than once%s\n", 00422 package_name, long_opt, short_opt, 00423 (additional_error ? additional_error : "")); 00424 else 00425 fprintf(stderr, "%s: `--%s' option given more than once%s\n", 00426 package_name, long_opt, 00427 (additional_error ? additional_error : "")); 00428 return 1; /* failure */ 00429 } 00430 00431 if (possible_values 00432 && (found = check_possible_values((value ? value : default_value), 00433 possible_values)) < 0) { 00434 if (short_opt != '-') 00435 fprintf(stderr, 00436 "%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n", 00437 package_name, (found == -2) ? "ambiguous" : "invalid", 00438 value, long_opt, short_opt, 00439 (additional_error ? additional_error : "")); 00440 else 00441 fprintf(stderr, "%s: %s argument, \"%s\", for option `--%s'%s\n", 00442 package_name, (found == -2) ? "ambiguous" : "invalid", 00443 value, long_opt, 00444 (additional_error ? additional_error : "")); 00445 return 1; /* failure */ 00446 } 00447 00448 if (field_given && *field_given && !override) 00449 return 0; 00450 if (prev_given) 00451 (*prev_given)++; 00452 if (field_given) 00453 (*field_given)++; 00454 if (possible_values) 00455 val = possible_values[found]; 00456 00457 switch (arg_type) { 00458 case ARG_FLAG: 00459 *((int *) field) = !*((int *) field); 00460 break; 00461 case ARG_INT: 00462 if (val) 00463 *((int *) field) = strtol(val, &stop_char, 0); 00464 break; 00465 case ARG_FLOAT: 00466 if (val) 00467 *((float *) field) = (float) strtod(val, &stop_char); 00468 break; 00469 case ARG_STRING: 00470 if (val) { 00471 string_field = (char **) field; 00472 if (!no_free && *string_field) 00473 free(*string_field); /* free previous string */ 00474 *string_field = gengetopt_strdup(val); 00475 } 00476 break; 00477 default: 00478 break; 00479 }; 00480 00481 /* check numeric conversion */ 00482 switch (arg_type) { 00483 case ARG_INT: 00484 case ARG_FLOAT: 00485 if (val && !(stop_char && *stop_char == '\0')) { 00486 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, 00487 val); 00488 return 1; /* failure */ 00489 } 00490 break; 00491 default: 00492 ; 00493 }; 00494 00495 /* store the original value */ 00496 switch (arg_type) { 00497 case ARG_NO: 00498 case ARG_FLAG: 00499 break; 00500 default: 00501 if (value && orig_field) { 00502 if (no_free) { 00503 *orig_field = value; 00504 } else { 00505 if (*orig_field) 00506 free(*orig_field); /* free previous string */ 00507 *orig_field = gengetopt_strdup(value); 00508 } 00509 } 00510 }; 00511 00512 return 0; /* OK */ 00513 } 00514 00515 int cmdline_parser_internal(int argc, char **argv, 00516 struct gengetopt_args_info *args_info, 00517 struct cmdline_parser_params *params, const char *additional_error) { 00518 int c; /* Character of the parsed option. */ 00519 00520 int error = 0; 00521 struct gengetopt_args_info local_args_info; 00522 00523 int override; 00524 int initialize; 00525 int check_required; 00526 int check_ambiguity; 00527 00528 package_name = argv[0]; 00529 00530 override = params->override; 00531 initialize = params->initialize; 00532 check_required = params->check_required; 00533 check_ambiguity = params->check_ambiguity; 00534 00535 if (initialize) 00536 cmdline_parser_init(args_info); 00537 00538 cmdline_parser_init(&local_args_info); 00539 00540 optarg = 0; 00541 optind = 0; 00542 opterr = params->print_errors; 00543 optopt = '?'; 00544 00545 while (1) { 00546 int option_index = 0; 00547 00548 static struct option long_options[] = { { "help", 0, NULL, 'h' }, { 00549 "version", 0, NULL, 'V' }, { "samplerate", 1, NULL, 'r' }, { 00550 "bits", 1, NULL, 'b' }, { "nodither", 0, NULL, 'n' }, { "scale", 00551 1, NULL, 's' }, { "infile", 1, NULL, 'i' }, { "outfile", 1, 00552 NULL, 'o' }, { "dop", 0, NULL, 'd' }, { 0, 0, 0, 0 } }; 00553 00554 c = getopt_long(argc, argv, "hVr:b:ns:i:o:d", long_options, 00555 &option_index); 00556 00557 if (c == -1) 00558 break; /* Exit from `while (1)' loop. */ 00559 00560 switch (c) { 00561 case 'h': /* Print help and exit. */ 00562 cmdline_parser_print_help(); 00563 cmdline_parser_free(&local_args_info); 00564 exit(EXIT_SUCCESS); 00565 00566 case 'V': /* Print version and exit. */ 00567 cmdline_parser_print_version(); 00568 cmdline_parser_free(&local_args_info); 00569 exit(EXIT_SUCCESS); 00570 00571 case 'r': /* Output sample rate. */ 00572 00573 if (update_arg((void *) &(args_info->samplerate_arg), 00574 &(args_info->samplerate_orig), 00575 &(args_info->samplerate_given), 00576 &(local_args_info.samplerate_given), optarg, 00577 cmdline_parser_samplerate_values, "88200", ARG_INT, 00578 check_ambiguity, override, 0, 0, "samplerate", 'r', 00579 additional_error)) 00580 goto failure; 00581 00582 break; 00583 case 'b': /* Output bitdepth. */ 00584 00585 if (update_arg((void *) &(args_info->bits_arg), 00586 &(args_info->bits_orig), &(args_info->bits_given), 00587 &(local_args_info.bits_given), optarg, 00588 cmdline_parser_bits_values, "24", ARG_INT, check_ambiguity, 00589 override, 0, 0, "bits", 'b', additional_error)) 00590 goto failure; 00591 00592 break; 00593 case 'n': /* Don't add dither before quantization. */ 00594 00595 if (update_arg((void *) &(args_info->nodither_flag), 0, 00596 &(args_info->nodither_given), 00597 &(local_args_info.nodither_given), optarg, 0, 0, ARG_FLAG, 00598 check_ambiguity, override, 1, 0, "nodither", 'n', 00599 additional_error)) 00600 goto failure; 00601 00602 break; 00603 case 's': /* Scale adjustment. Raw DSD has a modulation depth of approximately 0.5 so with no scaling the PCM peak level is approximately -6dB below 0dBFs. */ 00604 00605 if (update_arg((void *) &(args_info->scale_arg), 00606 &(args_info->scale_orig), &(args_info->scale_given), 00607 &(local_args_info.scale_given), optarg, 0, "4", ARG_FLOAT, 00608 check_ambiguity, override, 0, 0, "scale", 's', 00609 additional_error)) 00610 goto failure; 00611 00612 break; 00613 case 'i': /* Input DSF or DFF file. */ 00614 00615 if (update_arg((void *) &(args_info->infile_arg), 00616 &(args_info->infile_orig), &(args_info->infile_given), 00617 &(local_args_info.infile_given), optarg, 0, 0, ARG_STRING, 00618 check_ambiguity, override, 0, 0, "infile", 'i', 00619 additional_error)) 00620 goto failure; 00621 00622 break; 00623 case 'o': /* Output FLAC file, if not specified the output file be the same as the input file with the extension changed. */ 00624 00625 if (update_arg((void *) &(args_info->outfile_arg), 00626 &(args_info->outfile_orig), &(args_info->outfile_given), 00627 &(local_args_info.outfile_given), optarg, 0, 0, ARG_STRING, 00628 check_ambiguity, override, 0, 0, "outfile", 'o', 00629 additional_error)) 00630 goto failure; 00631 00632 break; 00633 case 'd': /* Encode DSD data directly into FLAC file without conversion to PCM using DoP format (DSD over PCM). */ 00634 00635 if (update_arg((void *) &(args_info->dop_flag), 0, 00636 &(args_info->dop_given), &(local_args_info.dop_given), 00637 optarg, 0, 0, ARG_FLAG, check_ambiguity, override, 1, 0, 00638 "dop", 'd', additional_error)) 00639 goto failure; 00640 00641 break; 00642 00643 case 0: /* Long option with no short option */ 00644 case '?': /* Invalid option. */ 00645 /* `getopt_long' already printed an error message. */ 00646 goto failure; 00647 00648 default: /* bug: option not considered. */ 00649 fprintf(stderr, "%s: option unknown: %c%s\n", 00650 CMDLINE_PARSER_PACKAGE, c, 00651 (additional_error ? additional_error : "")); 00652 abort(); 00653 } /* switch */ 00654 } /* while */ 00655 00656 if (check_required) { 00657 error += cmdline_parser_required2(args_info, argv[0], additional_error); 00658 } 00659 00660 cmdline_parser_release(&local_args_info); 00661 00662 if (error) 00663 return (EXIT_FAILURE); 00664 00665 return 0; 00666 00667 failure: 00668 00669 cmdline_parser_release(&local_args_info); 00670 return (EXIT_FAILURE); 00671 }