1
0

b/c...py: extract sample rate conv. optimizations

This commit is contained in:
Intel A80486DX2-66 2024-01-09 18:14:32 +03:00
parent bd53ac920f
commit fad6b895bb
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 27 additions and 3 deletions

View File

@ -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:
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,

View File

@ -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`;
}