#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Compare the closed box's ADPCM-A output with candidate decoders. python3 -I validation/fit_adpcma.py Runs `$EMU_TOOLS_GPL2/tools/ym2608-lle/build/lle2610_probe adpcma 90` for ten constant ROM bytes (every nibble of the stream is the same pair) and tests four candidate decoders on the first 27 decoded samples (one every third FM sample, from the ninth word): the step-index adjustments of Dialogic's note (-1 -1 -1 -1 +2 +4 +6 +8, 00-1366-001) or of ymfm (-1 -1 -1 -1 +2 +5 +7 +9, ymfm_adpcm.cpp:210), each with the delta truncated once, (2L+1)*ss/8 (ymfm), or term by term, ss*B2 + ss/2*B1 + ss/4*B0 + ss/8 (Dialogic). Common to all: steps floor(16 * 1.1**n), index clamped to 0..48, a 12-bit wrapping accumulator, and the word (acc * 16 * 15 >> 5) & ~3 for ATL $3F and ACL $1F (ymfm_adpcm.cpp:226-239, which the closed box was seen to follow for 13 level settings). """ from __future__ import annotations import math import os import subprocess import sys from fractions import Fraction from pathlib import Path ROOT = Path(__file__).resolve().parent.parent #: The probe as the GPL tool builds it (validation/build_referees.sh calls the tool's #: build.sh): ym2608-lle 0.1.0 in emu-tools-gpl2. A missing program is a missing input. _TOOLS2 = os.environ.get("EMU_TOOLS_GPL2") if not _TOOLS2: sys.exit("set EMU_TOOLS_GPL2 (the probe is $EMU_TOOLS_GPL2/tools/ym2608-lle/build/lle2610_probe)") PROBE = Path(_TOOLS2) / "tools" / "ym2608-lle" / "build" / "lle2610_probe" if not PROBE.is_file(): print(f"{PROBE} not built: run sh validation/build_referees.sh", file=sys.stderr) sys.exit(3) STEPS = [math.floor(16 * Fraction(11, 10) ** n) for n in range(49)] INC = {"ymfm": [-1, -1, -1, -1, 2, 5, 7, 9], "dialogic": [-1, -1, -1, -1, 2, 4, 6, 8]} def decode(nibble: int, inc: list[int], single: bool, n: int) -> list[int]: acc = index = 0 out = [] for _ in range(n): m, ss = nibble & 7, STEPS[index] if single: d = (2 * m + 1) * ss // 8 else: d = ss * (m >> 2 & 1) + ss // 2 * (m >> 1 & 1) + ss // 4 * (m & 1) + ss // 8 acc = (acc + (-d if nibble & 8 else d)) & 0xFFF index = min(48, max(0, index + inc[m])) signed = acc - 4096 if acc & 0x800 else acc out.append((signed * 16 * 15 >> 5) & ~3) return out def main() -> int: agree = {} for byte in ("11", "22", "33", "44", "55", "66", "77", "99", "CC", "FF"): text = subprocess.run([str(PROBE), "adpcma", byte, "90"], capture_output=True, text=True, check=True).stdout words = [int(x) for x in text.split("sample:\n", 1)[1].split()] seen = words[8::3][:27] for name, inc in INC.items(): for single in (True, False): key = f"{name} increments, {'single' if single else 'term-by-term'} truncation" agree.setdefault(key, 0) agree[key] += decode(int(byte[0], 16), inc, single, 27) == seen for key, count in agree.items(): print(f"{key}: {count} of 10 streams agree") return 0 if max(agree.values()) == 10 else 1 if __name__ == "__main__": sys.exit(main())