Resolve sound events exactly as vanilla SoundManager does

Add SoundRegistry (sounds.json with replace, subtitles, file validation and
event references weighing as much as their event) and a SoundEngine model,
with a differential harness resolving every sounds.json event against the
real SoundManager.prepare and SimpleSoundInstance.getOrResolve.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-10 21:16:28 +02:00
parent 1e03d531eb
commit ad4d836f6f
7 changed files with 1651 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
#!/usr/bin/env python3
"""Shared runner for the audio differential harnesses.
Each harness writes an input file, runs a Java oracle against the pinned
client (the real SoundManager / SoundEngine / MusicManager / ambient handlers;
OpenAL runs on OpenAL Soft's "No Output" device so channel parameters can be
read back from real AL sources) and a Rust example replaying the same inputs
through Horizon's audio code, then requires byte-identical output.
"""
import json
import os
from pathlib import Path
import platform
import subprocess
try:
from . import lab, test_rust_parity as base, test_model_baking_parity as baking
except ImportError:
import lab, test_rust_parity as base, test_model_baking_parity as baking
RESOURCES = lab.LOCAL / 'horizon-client/26.3'
COMMON_SOURCES = [
lab.ROOT / 'tools/audio_harness.py',
lab.ROOT / 'tools/test_rust_parity.py',
lab.ROOT / 'tools/lab.py',
lab.ROOT / 'Cargo.toml',
lab.ROOT / 'Cargo.lock',
lab.ROOT / 'crates/horizon-client/Cargo.toml',
lab.ROOT / 'crates/horizon-client/src/sound_events.rs',
lab.ROOT / 'crates/horizon-client/src/sound_engine.rs',
]
def require_sounds():
if not (RESOURCES / 'assets/minecraft/sounds.json').is_file():
raise SystemExit('Sound assets missing: run tools/fetch_sounds.py first')
def run_harness(name, oracle, example, inputs_text, extra_sources=(), java_args=(), rust_args=(), scope='', timeout=1800):
"""Run oracle and example on inputs; returns the number of matched lines."""
require_sounds()
lock, jars, _ = base.verify_artifacts()
libraries = baking.client_libraries()
build = lab.LOCAL / f'rust-{name}'
build.mkdir(parents=True, exist_ok=True)
report = build / 'report.json'
report.unlink(missing_ok=True)
oracle_path = lab.ROOT / f'mods/horizon/src/test/java/dev/horizonlab/parity/{oracle}.java'
example_path = lab.ROOT / f'crates/horizon-client/examples/{example}.rs'
sources = [*COMMON_SOURCES, oracle_path, example_path, *[Path(s) for s in extra_sources]]
tested = base.source_hashes(sources)
inputs = build / 'inputs.txt'
inputs.write_text(inputs_text)
input_hash = lab.digest(inputs, 'sha256')
def run(command, log, env=None):
with (build / (log + '.log')).open('w') as out:
subprocess.run(list(map(str, command)), cwd=lab.ROOT, check=True, timeout=timeout, stdout=out, stderr=subprocess.STDOUT, env=env)
classes = build / 'classes'
classes.mkdir(exist_ok=True)
dependencies = os.pathsep.join(map(str, [jars['client'], *libraries]))
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-sourcepath', lab.ROOT / 'mods/horizon/src/test/java',
'-cp', dependencies, '-d', classes, oracle_path], 'compile-java')
run(['cargo', 'build', '--locked', '-p', 'horizon-client', '--example', example], 'compile-rust')
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--locked', '--no-deps', '--format-version', '1'], cwd=lab.ROOT))
binary = Path(metadata['target_directory']) / 'debug/examples' / (example + ('.exe' if os.name == 'nt' else ''))
run([binary, *rust_args, inputs, build / 'rust.txt'], 'rust')
env = dict(os.environ, ALSOFT_DRIVERS='null')
run(['java', '-Xmx1G', '-cp', str(classes) + os.pathsep + dependencies, f'dev.horizonlab.parity.{oracle}', *java_args, inputs, build / 'client.txt'],
'client', env=env)
matched = base.compare(build / 'client.txt', build / 'rust.txt')
base.ensure_sources_unchanged(sources, tested)
if input_hash != lab.digest(inputs, 'sha256'):
raise AssertionError('Input fixtures changed')
lab.write_json(report, dict(
schema=1, minecraft=lock['minecraft']['version'], manifest_sha1=lock['minecraft']['manifest_sha1'], platform=platform.platform(),
rustc=subprocess.check_output(['rustc', '--version'], text=True).strip(),
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
sources_sha256=tested, input_sha256=input_hash, client_jar_sha256=lab.digest(jars['client'], 'sha256'),
sounds_json_sha256=lab.digest(RESOURCES / 'assets/minecraft/sounds.json', 'sha256'),
matched_output_lines=matched, output_sha256=lab.digest(build / 'rust.txt', 'sha256'), scope=scope))
return matched, report
def bits(value):
import struct
return struct.pack('>f', value).hex()
+51
View File
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
"""Compare sound event resolution with the vanilla 26.3 client.
The oracle builds the real `SoundManager` registry from the client asset root
(`SoundManager.prepare` + `Preparations.apply`: sounds.json, `replace`,
subtitles, file validation against the listed .ogg resources, event references
weighing as much as their event) and resolves seeded `SimpleSoundInstance`s:
`getOrResolve`, `getVolume`, `getPitch`, `Sound.getAttenuationDistance`, the
stream/preload/type fields and the instance random's next draw. Horizon's
`SoundRegistry` + `SoundEngine::resolve` replay the same inputs.
"""
import json
import random
try:
from . import audio_harness as h
except ImportError:
import audio_harness as h
def fixtures():
sounds = json.loads((h.RESOURCES / 'assets/minecraft/sounds.json').read_text())
rng = random.Random(0x534f554e44)
volumes = [1.0, 0.5, 0.25, 2.0, 4.0, 0.0, 1e-3, 16.0]
pitches = [1.0, 0.5, 2.0, 0.8, 1.2, 3.0, 0.1]
lines = []
for name in sorted(sounds):
event = 'minecraft:' + name
count = len(sounds[name].get('sounds', []))
seeds = [0, 1, -1, 42] + [rng.getrandbits(64) - (1 << 63) for _ in range(max(4, 2 * count))]
for seed in seeds:
lines.append(f'{event} {seed} {h.bits(rng.choice(volumes))} {h.bits(rng.choice(pitches))}')
for event in ['minecraft:intentionally_empty', 'minecraft:empty', 'minecraft:not.a.sound', 'horizon:missing', 'block.stone.break', 'music.game']:
for seed in [0, 7, -99]:
lines.append(f'{event} {seed} {h.bits(1.0)} {h.bits(1.0)}')
return lines
def main():
lines = fixtures()
matched, report = h.run_harness(
'sound-events', 'VanillaSoundEventOracle', 'sound_event_fixture', ''.join(l + '\n' for l in lines),
java_args=[h.RESOURCES], rust_args=[h.RESOURCES],
scope='Real SoundManager.prepare/Preparations.apply over the client asset root and SimpleSoundInstance.getOrResolve/getVolume/getPitch '
'for every sounds.json event with seeded randoms (weighted choice, nested event references, MultipliedFloats volume/pitch, '
'stream/preload/attenuation fields, EMPTY and INTENTIONALLY_EMPTY sentinels, draw counts) and the full registered event list.')
print(f'{matched:,} sound event lines match the pinned client ({len(lines):,} resolutions): {report}', flush=True)
if __name__ == '__main__':
main()