diff --git a/bytebeat_compiler.py b/bytebeat_compiler.py index fdc8274..a70f36e 100644 --- a/bytebeat_compiler.py +++ b/bytebeat_compiler.py @@ -74,8 +74,12 @@ if __name__ == "__main__": help="is signed?") parser.add_argument("-c", "--channels", default=1, type=int, help="amount of channels") - parser.add_argument("-t", "--seconds", default=30, type=int, - help="length (seconds)") + parser.add_argument("-t", "--seconds", default=None, type=int, + help="length in seconds (samples = sample rate * seconds) : " + "default = 30 seconds") + parser.add_argument("-l", "--samples", default=None, type=int, + help="length in samples (adds to `-t`; supports negative numbers) : " + "default = +0 samples") parser.add_argument("-a", "--no-return", default=False, action="store_true", help="do not insert return statement before the code") parser.add_argument("-q", "--silent", default=False, action="store_true", @@ -102,6 +106,35 @@ if __name__ == "__main__": final_sample_rate_code = f"w *= {sample_rate_ratio}L;" args.sample_rate = args.final_sample_rate + samples = 0 + while True: + no_seconds = args.seconds is None or args.seconds == 0 + no_samples = args.samples is None or args.samples == 0 + seconds_exist = not no_seconds + samples_exist = not no_samples + + if seconds_exist and args.seconds < 0: + print("CLI: Count of seconds can't be less than zero.") + raise SystemExit + + if no_seconds and samples_exist: + samples = args.samples + elif seconds_exist and samples_exist: + samples = args.seconds * args.sample_rate + args.samples + elif seconds_exist and no_samples: + samples = args.seconds * args.sample_rate + elif no_seconds and no_samples: + args.seconds = 30 # default + continue + else: + print("CLI: Incorrect seconds/samples length format.") + raise SystemExit + break + + if samples <= 0: + print("CLI: Count of samples should be greater than zero.") + raise SystemExit + substitute = read_file(PATHS["template"]) substitute = substitute_var("bytebeat_contents", bytebeat_contents, substitute) @@ -115,8 +148,8 @@ if __name__ == "__main__": "1" if args.signed else "0", substitute) substitute = substitute_var("channels", args.channels, substitute) - substitute = substitute_var("seconds", - args.seconds, substitute) + substitute = substitute_var("length", + samples, substitute) substitute = substitute_var("silent_mode", "true" if args.silent else "false", substitute) substitute = substitute_var("verbose_mode", diff --git a/src/template.c b/src/template.c index a7d1cc2..20804cb 100644 --- a/src/template.c +++ b/src/template.c @@ -21,7 +21,7 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING; #define BIT_DEPTH `bit_depth` #define IS_SIGNED `is_signed` #define CHANNELS `channels` -#define SECONDS `seconds` +#define LENGTH `length` #if BIT_DEPTH <= 8 # define SAMPLE_TYPE uint8_t @@ -33,7 +33,7 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING; # endif #endif -#define PRODUCT (SAMPLE_RATE * SECONDS * CHANNELS) +#define PRODUCT (LENGTH * CHANNELS) #define FREQUENCY_OF_STATUS_REPORTING 5000 #define BIT_DEPTH_LIMITER ((1 << BIT_DEPTH) - 1) @@ -75,6 +75,9 @@ main(void) #endif #if !SILENT_MODE + const uintmax_t seconds = LENGTH / SAMPLE_RATE, + samples = LENGTH % SAMPLE_RATE; + printf( "\n" "Sample rate: %d Hz\n" @@ -87,16 +90,24 @@ main(void) IS_SIGNED ? "" : "un", BIT_DEPTH); - if (SECONDS >= 3600) - printf( - "%d:%02d:%02d", - SECONDS / 3600, - (SECONDS / 60) % 60, - SECONDS % 60); - else if (SECONDS >= 60) - printf("%d:%02d", SECONDS / 60, SECONDS % 60); - else - printf("%d seconds", SECONDS); + if (seconds > 0) { + if (seconds >= 3600) + printf( + "%" PRIuMAX ":%02" PRIuMAX ":%02" PRIuMAX, + seconds / 3600, + (seconds / 60) % 60, + seconds % 60); + else if (seconds >= 60) + printf("%" PRIuMAX ":%02" PRIuMAX, seconds / 60, seconds % 60); + else + printf("%" PRIuMAX " seconds", seconds); + } + + if (seconds > 0 && samples > 0) + printf(" + "); + + if (samples > 0) + printf("%" PRIuMAX " samples", samples); printf( #if VERBOSE_MODE