Files
Triforce-Source/tests/test_audio_late.py
ruckus7382sfgsgdf b527e29adb Triforce v0.8.5
A Randomizer page in the launcher, a new title screen with Mods and Save Select tabs, scaling changes applied in game, the Jabu-Jabu bars fixed, renderer frame-time work and the brightness-flash fix, and fixes from the 0.8.4 reports.
2026-11-28 10:54:41 -06:00

102 lines
5.5 KiB
Python

"""The mixer's count of late refills (host/nn_audio.c au_late; no game, no output device).
A playing voice that ran out of the game's wave buffers and is given one again within half a second played silence in
between: the game's audio thread came late. The title screen's sound stuttered with its movie and the log had no line for
it. The two rules are cut out of nn_audio.c unchanged and run: a voice is marked dry at the first mix frame it plays
nothing in and has nothing queued, the mark holds while it stays dry and goes when it plays or has a buffer again; a
buffer appended to a playing, dry voice counts the frames in between, up to half a second (longer is a new sound on the
voice), and nothing for a voice that is not playing or was fed in time. Then the wiring is read: the mixer marks every
voice it renders, the append and a state change clear the mark, the device-behind drop is counted, and the mixer thread
writes one line per 2000 mix frames that had any.
python -I -B tests/test_audio_late.py [--cc gcc]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cc", "gcc"]
ROOT = Path(__file__).resolve().parents[1]
MAIN = r'''
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); return 1; } } while (0)
int main(void) {
/* (the mark: 0 while the voice plays or has buffers; the first dry frame, kept while dry) */
CHECK(audio_late_mark(0, 1, 0, 7) == 0 && audio_late_mark(0, 0, 2, 7) == 0 && audio_late_mark(0, 1, 3, 7) == 0);
CHECK(audio_late_mark(0, 0, 0, 7) == 7 && audio_late_mark(7, 0, 0, 8) == 7 && audio_late_mark(7, 0, 0, 900) == 7);
CHECK(audio_late_mark(7, 1, 0, 9) == 0 && audio_late_mark(7, 0, 1, 9) == 0);
/* (the gap: dry at frame 7, fed before frame 8 is mixed = one frame of silence; half a second at most) */
CHECK(audio_late_gap(1, 7, 8) == 1 && audio_late_gap(1, 7, 27) == 20 && audio_late_gap(1, 7, 107) == 100);
CHECK(audio_late_gap(1, 7, 108) == 0 && audio_late_gap(1, 7, 5000) == 0);
CHECK(audio_late_gap(1, 0, 8) == 0); /* fed in time */
CHECK(audio_late_gap(0, 7, 8) == 0); /* not playing: a stopped voice's next sound */
CHECK(audio_late_gap(1, 7, 7) == 0); /* (never: the mixer counts on before an append can run) */
CHECK(AUDIO_LATE_MOST * 5 == 500);
return 0;
}
'''
def check(cond, what):
if not cond:
raise SystemExit("FAIL: " + what)
def block(source, marker):
start = source.find(marker)
check(start >= 0, "%s is missing" % marker)
end = source.index("{", start) + 1
depth = 1
while depth:
depth += (source[end] == "{") - (source[end] == "}")
end += 1
return source[start:end]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--cc", default=os.environ.get("CC", "gcc"))
args = parser.parse_args()
source = (ROOT / "host/nn_audio.c").read_text(encoding="utf-8")
rules = "\n".join(["#include <stdint.h>", "#include <stdio.h>", "enum { AUDIO_LATE_MOST = 100 };",
block(source, "static inline int64_t audio_late_mark("), block(source, "static inline int64_t audio_late_gap("), MAIN])
check("enum { AUDIO_LATE_MOST = 100 };" in source, "AUDIO_LATE_MOST is not 100 mix frames in nn_audio.c")
with tempfile.TemporaryDirectory() as tmp:
src, exe = Path(tmp) / "audio_late_test.c", Path(tmp) / "audio_late_test.exe"
src.write_text(rules, encoding="utf-8")
built = subprocess.run([args.cc, "-std=c11", "-Wall", "-Werror", "-o", str(exe), str(src)], capture_output=True, text=True)
check(built.returncode == 0, "the rules do not compile:\n" + built.stderr)
ran = subprocess.run([str(exe)], capture_output=True, text=True)
check(ran.returncode == 0, "the rules: " + ran.stderr.strip())
# the wiring
render = block(source, "static void render_voice(")
check("v->dry_at = audio_late_mark(v->dry_at, any, v->nwb, au_mix_frame);" in render, "render_voice does not mark the voice")
apply = block(source, "static void apply(")
append = apply[apply.index("case OP_VOICE_APPEND:"):]
append = append[:append.index("break;")]
check("audio_late_gap(v->state == PLAY_STARTED, v->dry_at, au_mix_frame)" in append, "the append does not count the gap")
check("v->dry_at = 0;" in append, "the append does not clear the mark")
check(append.index("audio_late_gap(") < append.index("v->wb[v->nwb++]"), "the gap is read after the buffer is queued")
state = apply[apply.index("case OP_VOICE_STATE:"):apply.index("case OP_VOICE_APPEND:")]
check("if (v->state != k->i0) v->dry_at = 0;" in state, "a state change does not clear the mark")
output = block(source, "static void output_frame(")
behind = output[output.index("the device is behind"):]
check("out_dropped++;" in behind[:120], "the device-behind drop is not counted")
loop = block(source, "static DWORD WINAPI renderer_main(")
check("++au_mix_frame % 2000 == 0" in loop, "the mixer does not count its frames")
check(loop.index("render_frame(pcm") < loop.index("++au_mix_frame") < loop.index("au_unlock();"),
"the frame count moves outside the mixer's lock (apply() reads it under the lock)")
check("au_late_report(out_dropped), out_dropped = 0;" in loop, "the report is not written from the mixer thread")
report = block(source, "static void au_late_report(")
check("if (au_late.gaps || dropped)" in report and "late refills" in report, "the report writes when there was nothing")
print("ok")
if __name__ == "__main__":
main()