Files

55 lines
2.3 KiB
Python
Raw Permalink Normal View History

"""Import the user's siren excerpt; the game applies the one-time fade-in."""
from pathlib import Path
import argparse
import hashlib
import json
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "builds/python_audio"))
import imageio_ffmpeg
import numpy as np
import soundfile as sf
parser = argparse.ArgumentParser()
parser.add_argument("source", type=Path)
parser.add_argument("--from-seconds", type=float, default=8.0)
args = parser.parse_args()
assert args.from_seconds >= 0
work = ROOT / "builds/user_alarm"
work.mkdir(parents=True, exist_ok=True)
decoded = work / "excerpt.wav"
subprocess.run([
imageio_ffmpeg.get_ffmpeg_exe(), "-v", "error", "-y", "-i", str(args.source),
"-ss", str(args.from_seconds), "-map", "0:a:0", "-ac", "1", "-ar", "22050",
"-c:a", "pcm_s16le", str(decoded),
], check=True)
samples, rate = sf.read(decoded)
assert len(samples) > rate * 2 and np.isfinite(samples).all()
source_duration = len(samples) / rate
samples -= samples.mean()
samples *= min(10 ** (-19 / 20) / np.sqrt(np.mean(samples**2)), .65 / np.abs(samples).max())
width = int(.35 * rate)
blend = np.linspace(0, 1, width)
samples[:width] = samples[-width:] * (1-blend) + samples[:width] * blend
samples = samples[:-width]
target = ROOT / "do-otboy/assets/audio/siren_user.wav"
sf.write(target, samples, rate, subtype="PCM_16")
manifest = {
"source": "videoplayback.m4a", "provider": "User-provided recording",
"source_sha256": hashlib.sha256(args.source.read_bytes()).hexdigest(),
"source_start_seconds": args.from_seconds,
"source_end_seconds": round(args.from_seconds+source_duration, 4),
"file": "assets/audio/siren_user.wav", "duration": round(len(samples)/rate, 4),
"sample_rate": rate, "channels": 1, "encoding": "PCM16",
"loop_crossfade_seconds": .35, "playback_fade_seconds": 6.0,
"call_start_seconds": 24.0,
"rms_db": round(float(20*np.log10(np.sqrt(np.mean(samples**2)))), 3),
"peak_db": round(float(20*np.log10(np.abs(samples).max())), 3),
"sha256": hashlib.sha256(target.read_bytes()).hexdigest(),
}
(ROOT / "do-otboy/data/alarm_manifest.json").write_text(
json.dumps(manifest, ensure_ascii=False, indent=2)+"\n", encoding="utf-8")
print(json.dumps(manifest, ensure_ascii=False, indent=2))