Gitea #51 ROOT CAUSE: LoopAtWill was mapped to "loop forever", arming 224 of 603 samples as endless OpenAL sources

SAURON's "coolant flush sound glitched and perma" is one instance of a systemic
defect.  SetupPatch (L4AUDLVL.cpp) decided looping from the SAMPLE flag alone:

    AL_LOOPING = (info.loop != ForceStatic)

That armed every non-ForceStatic sample -- 224 of the 603 authored zones,
including unmistakable one-shots -- as an OpenAL source that never ends on its
own.  Any such sound whose note-off never arrives plays forever.  Measured 1946
LoopAtWill x Transient arming events in one short session.

The sample flag expresses PERMISSION; the SOURCE decides.  Three facts [T1]:

  * the enum's own comments (L4AUDLVL.h:14-19) -- LoopAtWill = "will play once
    OR LOOP AS DESIRED", ForceStatic = "plays only once EVEN IF LOOPED" (a
    veto), LoopAlways = "ramp up and then down";
  * LoopAtWill is enum value 0, i.e. the DEFAULT an unauthored sample receives
    (WTPresets.cpp:37) -- it cannot mean "loop forever";
  * LoopAlways, the real always-loop, is used by ZERO shipped samples.

"As desired" is the source's authored AudioRenderType (Transient=one-shot vs
Sustained=held), streamed from AUDIO*.RES (AUDLVL.cpp:28) and already consulted
by the renderer (L4AUDRND.cpp:567, AUDREND.cpp:184).  SetupPatch now takes it,
threaded from all three L4AudioSource call sites (Direct/Dynamic3D/Static3D).
New rule: LoopAlways->1, ForceStatic->0, LoopAtWill->the source's render type.

MEASURED before changing behaviour (BT_LOOP_AUDIT=1, scratchpad/loopaudit.py):

    LoopAtWill  x Transient -> loop=0  (was 1)   1946 events
    LoopAtWill  x Sustained -> loop=1  unchanged    65 events
    ForceStatic x Transient -> loop=0  unchanged   292 events

The engine loops (EngineAccel07_z0..z2, EngineMotor01, EnginePower01) are all
LoopAtWill/Sustained and PRESERVED -- no regression there.  All 24 reclassified
samples are genuine one-shots: laser charge/fire/explosion/loaded, missile
loading, engine shift, coolant pressure inc/dec.

A/B PROOF (scratchpad/loopab.py, same session both arms, only BT_LOOP_LEGACY
differs), asking the engine's own BT_AUDIO_DUMP what is still playing with
loop=1 long after every release:

    [legacy] EnginePower01, CoolantPresInc03_z2, CoolantPresDcr03_z2
    [fixed]  EnginePower01

The two stuck coolant sources are the reported symptom.  They were eventually
reclaimed when a later trigger reused the source, which is why the bug was
intermittent -- the "perma" case is when nothing else retriggers it.  The fix
removes the mechanism.

BT_LOOP_LEGACY=1 restores the old rule for a field A/B without a rebuild.  The
layers to listen to are the beam sustains: LaserA/CSustain* are authored
LoopAtWill on a TRANSIENT source, so they now end with the sample instead of
looping until note-off.  The render type is T1 authored data; the interpretation
that LoopAtWill defers to it is a strong reading of the enum + defaults rather
than a disassembled statement, and is flagged as such in the KB.

Plausibly bears on #32 (audio cutting in/out late in a match): a stuck looping
source holds its pool slot for the rest of the round.

Rigs: scratchpad/flushsnd.py (flush start/stop pairing), flushsnd2.py (stuck-
source hunt), loopaudit.py (the classification matrix), loopab.py (the A/B).
KB: context/wintesla-port.md audio section, context/decomp-reference.md env
table (BT_LOOP_AUDIT / BT_LOOP_LEGACY / BT_AUDIO_DUMP / BT_AUD_TAIL),
docs/AUDIO_FIDELITY.md F38.  checkctx.py CLEAN.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
arcattack
2026-07-25 13:20:19 -05:00
co-authored by Claude Opus 5
parent f3bdb3b85a
commit d39227ef39
10 changed files with 622 additions and 15 deletions
+112
View File
@@ -0,0 +1,112 @@
"""Gitea #51 A/B -- prove the stuck coolant sound before the fix, and gone after.
Same session both ways; the only difference is BT_LOOP_LEGACY, which selects the
OLD loop rule (`AL_LOOPING = sample != ForceStatic`) vs the new one (a LoopAtWill
sample defers to the source's authored AudioRenderType).
The measurement is the engine's own once-a-second playing-source dump
(BT_AUDIO_DUMP): after all the flush releases, sit idle, then ask what is STILL
playing with loop=1. A coolant source in that list is #51 reproduced.
Kills only the PIDs it spawns.
"""
import ctypes
import os
import re
import subprocess
import sys
import time
KEYUP = 0x0002
VK_H = 0x48
VK_C = 0x43
user32 = ctypes.windll.user32
REPO = r"C:\git\bt411"
def run(legacy):
tag = "legacy" if legacy else "fixed"
log = os.path.join(REPO, "scratchpad_loopab_%s.log" % tag)
if os.path.exists(log):
os.remove(log)
env = dict(os.environ)
env.update({
"BT_START_INSIDE": "1",
"BT_KEY_NOFOCUS": "1",
"BT_DEV_GAUGES": "1",
"BT_AUDIO_DUMP": "1",
"BT_FLUSH_LOG": "1",
"BT_LOG": log,
})
if legacy:
env["BT_LOOP_LEGACY"] = "1"
else:
env.pop("BT_LOOP_LEGACY", None)
p = subprocess.Popen(
[os.path.join(REPO, "build", "Release", "btl4.exe"), "-egg", "LAST.EGG"],
cwd=os.path.join(REPO, "content"), env=env,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("[%s] pid %d" % (tag, p.pid))
try:
deadline = time.time() + 240
while time.time() < deadline:
if os.path.exists(log) and "first frame" in open(log, errors="replace").read():
break
time.sleep(2)
else:
p.terminate()
return tag, None
time.sleep(6)
# exercise the coolant plumbing: flush, valve, flush -- the pressure
# inc/dec layers (LoopAtWill/Transient) are the suspect samples
for _ in range(4):
user32.keybd_event(VK_H, 0, 0, 0); time.sleep(0.5)
user32.keybd_event(VK_H, 0, KEYUP, 0); time.sleep(0.6)
user32.keybd_event(VK_C, 0, 0, 0); time.sleep(0.2)
user32.keybd_event(VK_C, 0, KEYUP, 0); time.sleep(1.2)
mark = len(open(log, errors="replace").read())
print("[%s] idling 25s" % tag)
time.sleep(25)
finally:
p.terminate()
time.sleep(1)
t = open(log, errors="replace").read()
idle = t[mark:]
playing = re.findall(r"^\[playing\].*$", idle, re.M)
stuck = {}
for l in playing:
m = re.search(r"src=(\d+) (\S+).*loop=(\d+)", l)
if m and m.group(3) == "1":
stuck[m.group(2)] = stuck.get(m.group(2), 0) + 1
return tag, stuck
results = {}
for legacy in (True, False):
tag, stuck = run(legacy)
results[tag] = stuck
print("\n=============== A/B RESULT ===============")
for tag in ("legacy", "fixed"):
s = results.get(tag)
if s is None:
print("\n[%s] never reached the mission" % tag)
continue
coolant = {k: v for k, v in s.items() if "oolant" in k}
print("\n[%s] still playing with loop=1 after all releases: %d distinct samples"
% (tag, len(s)))
for k, v in sorted(s.items(), key=lambda kv: -kv[1]):
mark = " <-- COOLANT" if "oolant" in k else ""
print(" %-30s seen %dx%s" % (k, v, mark))
print(" coolant sources stuck: %d" % len(coolant))
L = results.get("legacy") or {}
F = results.get("fixed") or {}
lc = len([k for k in L if "oolant" in k])
fc = len([k for k in F if "oolant" in k])
print("\nVERDICT: legacy coolant-stuck=%d fixed coolant-stuck=%d -> %s"
% (lc, fc, "FIX CONFIRMED" if lc > fc else
("no coolant stick reproduced in either arm" if lc == 0 else "NOT fixed")))
sys.exit(0)