Files
2026-11-14 08:55:48 +01:00

40 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""Check generated postcard narration and ensure its text stays in sync."""
import json
import hashlib
import os
from pathlib import Path
import subprocess
import sys
import wave
root = Path(__file__).resolve().parents[1]
source = json.loads((root / 'assets/postcards/recordings-source.json').read_text())
current = json.loads(subprocess.check_output([
os.environ.get('DART_BIN', 'dart'),
'tool/export_postcards.dart',
], cwd=root))
if source != current:
sys.exit('Postcard narration source differs from the current card text.')
if len(source) != 14 or len({p['id'] for p in source}) != 14:
sys.exit('Expected fourteen uniquely identified postcards.')
seconds = 0
illustrations = set()
for card in source:
illustration = root / 'assets/postcards/illustrations' / (card['id'] + '.png')
data = illustration.read_bytes()
if not data.startswith(b'\x89PNG\r\n\x1a\n'):
sys.exit(f'{illustration}: expected PNG illustration')
illustrations.add(hashlib.sha256(data).digest())
path = root / 'assets/postcards/audio' / (card['id'] + '.wav')
with wave.open(str(path), 'rb') as wav:
if wav.getnchannels() != 1 or wav.getframerate() != 24000 or wav.getsampwidth() != 2:
sys.exit(f'{path}: expected mono PCM16 at 24 kHz')
duration = wav.getnframes() / wav.getframerate()
if not 3 < duration < 180:
sys.exit(f'{path}: implausible duration {duration:.1f}s')
seconds += duration
if len(illustrations) != len(source):
sys.exit('Each postcard must have a distinct illustration.')
print(f'14 distinct illustrations and recordings, {seconds/60:.1f} minutes, source text synchronized.')