"""Prepare quiet mono phone-call ambience and foley from local generated takes.""" from pathlib import Path import hashlib import json import sys ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "builds/python_audio")) import numpy as np import soundfile as sf work = ROOT / "builds/soundscape" target = ROOT / "do-otboy/assets/audio/call" target.mkdir(parents=True, exist_ok=True) manifest = json.loads((work / "assembly.json").read_text(encoding="utf-8")) def save(entry, samples, rate): samples = samples - samples.mean() rms = np.sqrt(np.mean(samples ** 2)) assert rms > 1e-5 and np.isfinite(samples).all(), entry["id"] level = -23 if entry["loop"] else -20 samples *= min(10 ** (level / 20) / rms, .45 / np.max(np.abs(samples))) if entry["loop"]: width = int(rate * .2) blend = np.linspace(0, 1, width) samples[:width] = samples[-width:] * (1 - blend) + samples[:width] * blend samples = samples[:-width] else: width = min(int(rate * .008), len(samples) // 4) samples[:width] *= np.linspace(0, 1, width) samples[-width:] *= np.linspace(1, 0, width) path = target / (entry["id"] + ".wav") sf.write(path, samples, rate, subtype="PCM_16") entry.update(file="assets/audio/call/" + path.name, duration=round(len(samples) / rate, 4), rms_db=round(float(20*np.log10(np.sqrt(np.mean(samples**2)))), 3), peak_db=round(float(20*np.log10(np.max(np.abs(samples)))), 3), sha256=hashlib.sha256(path.read_bytes()).hexdigest()) for entry in manifest: data, rate = sf.read(work / (entry["id"] + ".mp3"), always_2d=True) save(entry, data.mean(axis=1), rate) rate = 22050 rng = np.random.default_rng(47) t = np.arange(rate * 12) / rate noise = rng.normal(0, 1, len(t)) noise = np.convolve(noise, np.ones(8)/8, mode="same") line = dict(id="line", loop=True, provider="Original procedural synthesis", description="Very quiet telephone noise floor") save(line, noise * (.9 + .1*np.sin(2*np.pi*t/12)), rate) manifest.append(line) t = np.arange(int(rate * .18)) / rate click = rng.normal(0, .35, len(t)) * np.exp(-t * 95) + np.sin(2*np.pi*270*t) * np.exp(-t*65) switch = dict(id="switch", loop=False, provider="Original procedural synthesis", description="Soft power or flashlight switch") save(switch, click, rate) manifest.append(switch) (ROOT / "do-otboy/data/sound_manifest.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2)+"\n", encoding="utf-8") print("Prepared", len(manifest), "mono PCM16 sound layers; peaks <= -6.9 dBFS") voice_levels = {} for path in (ROOT / "do-otboy/assets/voice").glob("*.wav"): samples, _ = sf.read(path) voice_levels[path.stem] = round(float(20*np.log10(max(1e-8, np.sqrt(np.mean(samples**2))))), 3) (ROOT / "do-otboy/data/voice_levels.json").write_text(json.dumps(voice_levels, indent=2)+"\n", encoding="utf-8")