Files
logic-puzzle/scripts/check_narration_assets.py
T

51 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Verify every bundled recording matches the current diary text and its WAV master."""
import array, hashlib, json, subprocess, wave
from pathlib import Path
root=Path(__file__).resolve().parents[1]
source=root/'assets/narration';raw=root/'assets/audio'
canonical=json.loads((source/'scripts.json').read_text())
campaign=json.loads((root/'assets/campaign.json').read_text())['levels']
pages={p['id']:p for p in json.loads((root/'assets/notebook.json').read_text())['pages']}
manifest=json.loads((source/'manifest.json').read_text());runtime=json.loads((raw/'narration_manifest.json').read_text())
assert runtime['tracks']==manifest['tracks']
assert len(canonical)==len(manifest['tracks'])
assert {r['pageId'] for r in canonical}==set(manifest['tracks'])
for record in canonical:
if record['pageId'].startswith('campaign.'):
_,index,phase=record['pageId'].split('.')
assert record['text']==campaign[int(index)][phase], 'Campaign narration is stale'
elif record.get('speaker')=='Narrator':
page=pages[record['pageId']]
expected=campaign[page['campaignIndex']][page['textSource']] if page.get('textSource') in ('before','after') else page['text']
assert record['text']==expected, 'Notebook narration is stale'
track=manifest['tracks'][record['pageId']]
assert track['text']==record['text']
assert track['textSha256']==hashlib.sha256(record['text'].encode()).hexdigest()
assert track['resource']==record['resource']
if record.get('speaker')=='Narrator':
assert track['speaker']=='Narrator' and track['voice']=='bm_george'
assert track['speed']==record['speed']
assert 'assets/audio/'+track['file'] in (root/'pubspec.yaml').read_text()
assert '\n\n'.join(cue['text'] for cue in track['cues'])==record['text']
previous=0
for cue in track['cues']:
assert previous<=cue['startMs']<cue['endMs']<=track['durationMs']
previous=cue['endMs']
wav=source/'wav'/(record['resource']+'.wav');flac=raw/track['flacFile']
assert (raw/track['file']).read_bytes()==wav.read_bytes(),'Runtime WAV differs from its reviewed master'
assert track['wavSha256']==hashlib.sha256(wav.read_bytes()).hexdigest()
with wave.open(str(wav),'rb') as reader:
assert reader.getnchannels()==1 and reader.getsampwidth()==2 and reader.getframerate()==24000
assert reader.getnframes()==track['frames']
master=reader.readframes(reader.getnframes())
samples=array.array('h',master);peak=max(abs(v) for v in samples)
assert 1000<peak<32767,'Silent or clipped audio'
decoded=subprocess.check_output(['ffmpeg','-v','error','-i',str(flac),'-f','s16le','-acodec','pcm_s16le','-'])
assert decoded==master,'Runtime FLAC must preserve the WAV master exactly'
assert track['flacSha256']==hashlib.sha256(flac.read_bytes()).hexdigest()
print(f"PASS {record['pageId']}: {track['durationMs']/1000:.1f}s, WAV/master match, FLAC lossless, text/cues match")
for p in (root/'lib').rglob('*.dart'):
assert 'flutter_tts' not in p.read_text(),f'Device reader reintroduced in {p}'
print(f'All {len(canonical)} recordings verified; bundled file playback only.')