b/c...py: extract sample rate conv. optimizations
This commit is contained in:
parent
bd53ac920f
commit
fad6b895bb
@ -93,6 +93,13 @@ if __name__ == "__main__":
|
|||||||
help="bit depth")
|
help="bit depth")
|
||||||
parser.add_argument("-s", "--signed", default=False, action="store_true",
|
parser.add_argument("-s", "--signed", default=False, action="store_true",
|
||||||
help="is signed?")
|
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,
|
parser.add_argument("-f", "--floating-point", default=False,
|
||||||
action="store_true", help="use floating point as the return type")
|
action="store_true", help="use floating point as the return type")
|
||||||
parser.add_argument("-c", "--channels", default=1, type=int,
|
parser.add_argument("-c", "--channels", default=1, type=int,
|
||||||
@ -125,10 +132,17 @@ if __name__ == "__main__":
|
|||||||
if not args.no_return: # Insert return statement
|
if not args.no_return: # Insert return statement
|
||||||
bytebeat_contents = f"return {bytebeat_contents}"
|
bytebeat_contents = f"return {bytebeat_contents}"
|
||||||
|
|
||||||
|
original_sample_rate = args.sample_rate
|
||||||
final_sample_rate_code = ""
|
final_sample_rate_code = ""
|
||||||
if not args.final_sample_rate is None:
|
if args.faster_sample_ratio_math:
|
||||||
sample_rate_ratio = args.sample_rate / args.final_sample_rate
|
args.precalculate_ratio = True
|
||||||
final_sample_rate_code = f"w *= {sample_rate_ratio}L;"
|
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
|
args.sample_rate = args.final_sample_rate
|
||||||
|
|
||||||
samples = 0
|
samples = 0
|
||||||
@ -163,9 +177,12 @@ if __name__ == "__main__":
|
|||||||
rewrite_file(PATHS["substitute"], substitute_vars({
|
rewrite_file(PATHS["substitute"], substitute_vars({
|
||||||
"bytebeat_contents": bytebeat_contents,
|
"bytebeat_contents": bytebeat_contents,
|
||||||
"sample_rate": args.sample_rate,
|
"sample_rate": args.sample_rate,
|
||||||
|
"original_sample_rate": original_sample_rate,
|
||||||
"final_sample_rate_code": final_sample_rate_code,
|
"final_sample_rate_code": final_sample_rate_code,
|
||||||
"bit_depth": args.bit_depth,
|
"bit_depth": args.bit_depth,
|
||||||
"is_signed": "1" if args.signed else "0",
|
"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",
|
"fp_return_type": "1" if args.floating_point else "0",
|
||||||
"channels": args.channels,
|
"channels": args.channels,
|
||||||
"length": samples,
|
"length": samples,
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
const char* ANSI_CLEAR = __ANSI_CLEAR_STRING;
|
const char* ANSI_CLEAR = __ANSI_CLEAR_STRING;
|
||||||
|
|
||||||
#define SAMPLE_RATE `sample_rate`
|
#define SAMPLE_RATE `sample_rate`
|
||||||
|
#define ORIGINAL_SAMPLE_RATE `original_sample_rate`
|
||||||
#define BIT_DEPTH `bit_depth`
|
#define BIT_DEPTH `bit_depth`
|
||||||
#define IS_SIGNED `is_signed`
|
#define IS_SIGNED `is_signed`
|
||||||
#define CHANNELS `channels`
|
#define CHANNELS `channels`
|
||||||
@ -38,6 +39,8 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING;
|
|||||||
#define PRODUCT (LENGTH * CHANNELS)
|
#define PRODUCT (LENGTH * CHANNELS)
|
||||||
#define FREQUENCY_OF_STATUS_REPORTING 5000
|
#define FREQUENCY_OF_STATUS_REPORTING 5000
|
||||||
|
|
||||||
|
#define PRECALCULATED_RATIO `precalculated_ratio`
|
||||||
|
|
||||||
#define BIT_DEPTH_LIMITER ((1 << BIT_DEPTH) - 1)
|
#define BIT_DEPTH_LIMITER ((1 << BIT_DEPTH) - 1)
|
||||||
#define PCM_COEFFICIENT ((1 << (BIT_DEPTH - 1)) - 1)
|
#define PCM_COEFFICIENT ((1 << (BIT_DEPTH - 1)) - 1)
|
||||||
#define unsigned_to_signed(x) (x - PCM_COEFFICIENT)
|
#define unsigned_to_signed(x) (x - PCM_COEFFICIENT)
|
||||||
@ -63,7 +66,11 @@ SAMPLE_TYPE
|
|||||||
#endif
|
#endif
|
||||||
bytebeat(long double w)
|
bytebeat(long double w)
|
||||||
{
|
{
|
||||||
|
#if PRECALCULATED_RATIO
|
||||||
`final_sample_rate_code`
|
`final_sample_rate_code`
|
||||||
|
#else
|
||||||
|
w /= ((long double) SAMPLE_RATE) / ((long double) ORIGINAL_SAMPLE_RATE);
|
||||||
|
#endif
|
||||||
uintmax_t t = (uintmax_t) w;
|
uintmax_t t = (uintmax_t) w;
|
||||||
`bytebeat_contents`;
|
`bytebeat_contents`;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user