aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/test_portable_audio_decode.py (2978B)


      1 """Compare the imported MPEG decompression path with Windows Miles."""
      2 import array
      3 import json
      4 import math
      5 import pathlib
      6 import struct
      7 import subprocess
      8 import wave
      9 
     10 root = pathlib.Path(__file__).resolve().parents[2]
     11 work = root / 'local/client/portable-audio-decode'
     12 work.mkdir(parents=True, exist_ok=True)
     13 reference = work / 'reference.exe'
     14 subprocess.run([
     15     str(root / 'toolchains/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe'),
     16     '-O2', 'client/tests/audio_decode.c', '-o', str(reference),
     17 ], cwd=root, check=True)
     18 wsl_root = '/mnt/' + root.drive[0].lower() + root.as_posix()[2:]
     19 wsl = ['wsl', '-d', 'Ubuntu-26.04', '--cd', wsl_root, '--exec']
     20 subprocess.run(wsl + ['make', '-s', '-C', 'client', '-f', 'Makefile.linux', 'build/linux/audio-decode'], check=True)
     21 
     22 # Exercise odd/even RIFF metadata boundaries and a raw MPEG stream.
     23 fixtures = [root / 'anarchy-assets-14.9.1/cd_image/sound/music' / name for name in [
     24     'env/battle/ClanDeath/ClanDeath01.WAV',
     25     'env/omni/day/OCD_01.wav',
     26     'env/mountain/night/mn03.wav',
     27 ]]
     28 data = fixtures[0].read_bytes()
     29 offset = 12
     30 while data[offset:offset + 4] != b'data':
     31     size = struct.unpack_from('<I', data, offset + 4)[0]
     32     offset += 8 + size + (size & 1)
     33 size = struct.unpack_from('<I', data, offset + 4)[0]
     34 raw = work / 'raw.mp3'
     35 raw.write_bytes(data[offset + 8:offset + 8 + size])
     36 fixtures.append(raw)
     37 
     38 results = []
     39 for index, fixture in enumerate(fixtures):
     40     expected = work / ('windows-%d.wav' % index)
     41     actual = work / ('linux-%d.wav' % index)
     42     windows = subprocess.check_output([
     43         str(reference), str(fixture), str(expected),
     44         str(root / 'anarchy-runtime-14.9.1/Mss32.dll'),
     45         str(root / 'anarchy-runtime-14.9.1/cd_image/sound/redist/win32'),
     46     ], text=True, timeout=30)
     47     linux = subprocess.check_output(wsl + [
     48         'client/build/linux/audio-decode', fixture.relative_to(root).as_posix(),
     49         actual.relative_to(root).as_posix(),
     50     ], text=True, timeout=30)
     51     assert windows == linux, (windows, linux)
     52     with wave.open(str(expected)) as first, wave.open(str(actual)) as second:
     53         assert first.getparams() == second.getparams(), (first.getparams(), second.getparams())
     54         left = array.array('h', first.readframes(first.getnframes()))
     55         right = array.array('h', second.readframes(second.getnframes()))
     56     errors = [abs(a - b) for a, b in zip(left, right)]
     57     rms = math.sqrt(sum(error * error for error in errors) / len(errors))
     58     # Different MPEG decoders round differently; one PCM step is 1/32768 full scale.
     59     assert rms < 2 and max(errors) < 128, (fixture, rms, max(errors))
     60     results.append({'file': fixture.relative_to(root).as_posix(), 'samples': len(left),
     61                     'rms_pcm_error': rms, 'maximum_pcm_error': max(errors)})
     62 
     63 (work / 'report.json').write_text(json.dumps(results, indent=2) + '\n')
     64 print(json.dumps(results, indent=2))
     65 print('Windows/Linux decode contracts and PCM comparison passed.')