From fad6b895bb3e48ad88b76d72baf0820ed31663f8 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Tue, 9 Jan 2024 18:14:32 +0300 Subject: [PATCH] b/c...py: extract sample rate conv. optimizations --- bytebeat_compiler.py | 23 ++++++++++++++++++++--- src/template.c | 7 +++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/bytebeat_compiler.py b/bytebeat_compiler.py index ce4a3f4..f82abf0 100644 --- a/bytebeat_compiler.py +++ b/bytebeat_compiler.py @@ -93,6 +93,13 @@ if __name__ == "__main__": help="bit depth") parser.add_argument("-s", "--signed", default=False, action="store_true", help="is signed?") + parser.add_argument("-R", "--precalculate-ratio", default=False, + action="store_true", + help="precalculate sample ratio to speed up rendering (may produce " + "inaccurate results") + parser.add_argument("-m", "--faster-sample-ratio-math", default=False, + action="store_true", + help="faster sample ratio math (implies argument -R)") parser.add_argument("-f", "--floating-point", default=False, action="store_true", help="use floating point as the return type") parser.add_argument("-c", "--channels", default=1, type=int, @@ -125,10 +132,17 @@ if __name__ == "__main__": if not args.no_return: # Insert return statement bytebeat_contents = f"return {bytebeat_contents}" + original_sample_rate = args.sample_rate final_sample_rate_code = "" - if not args.final_sample_rate is None: - sample_rate_ratio = args.sample_rate / args.final_sample_rate - final_sample_rate_code = f"w *= {sample_rate_ratio}L;" + if args.faster_sample_ratio_math: + args.precalculate_ratio = True + if not args.final_sample_rate is None and not args.precalculate_ratio: + if args.faster_sample_ratio_math: + sample_rate_ratio = args.sample_rate / args.final_sample_rate + final_sample_rate_code = f"w *= {sample_rate_ratio}L;" + else: + sample_rate_ratio = args.final_sample_rate / args.sample_rate + final_sample_rate_code = f"w /= {sample_rate_ratio}L;" args.sample_rate = args.final_sample_rate samples = 0 @@ -163,9 +177,12 @@ if __name__ == "__main__": rewrite_file(PATHS["substitute"], substitute_vars({ "bytebeat_contents": bytebeat_contents, "sample_rate": args.sample_rate, + "original_sample_rate": original_sample_rate, "final_sample_rate_code": final_sample_rate_code, "bit_depth": args.bit_depth, "is_signed": "1" if args.signed else "0", + "precalculated_ratio": "1" if args.precalculate_ratio else "0", + "faster_sample_ratio_math": "1" if args.precalculate_ratio else "0", "fp_return_type": "1" if args.floating_point else "0", "channels": args.channels, "length": samples, diff --git a/src/template.c b/src/template.c index d486856..4cbae3a 100644 --- a/src/template.c +++ b/src/template.c @@ -20,6 +20,7 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING; #define SAMPLE_RATE `sample_rate` +#define ORIGINAL_SAMPLE_RATE `original_sample_rate` #define BIT_DEPTH `bit_depth` #define IS_SIGNED `is_signed` #define CHANNELS `channels` @@ -38,6 +39,8 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING; #define PRODUCT (LENGTH * CHANNELS) #define FREQUENCY_OF_STATUS_REPORTING 5000 +#define PRECALCULATED_RATIO `precalculated_ratio` + #define BIT_DEPTH_LIMITER ((1 << BIT_DEPTH) - 1) #define PCM_COEFFICIENT ((1 << (BIT_DEPTH - 1)) - 1) #define unsigned_to_signed(x) (x - PCM_COEFFICIENT) @@ -63,7 +66,11 @@ SAMPLE_TYPE #endif bytebeat(long double w) { +#if PRECALCULATED_RATIO `final_sample_rate_code` +#else + w /= ((long double) SAMPLE_RATE) / ((long double) ORIGINAL_SAMPLE_RATE); +#endif uintmax_t t = (uintmax_t) w; `bytebeat_contents`; }