Files
Diabolo/DoOtboy/tools/install_jessica_voice.py
T

80 lines
3.6 KiB
Python
Raw Normal View History

"""Install the verified Jessica recordings, preserving every mother's file.
Authoring helper; requires the local downloaded batch, timestamps and soundfile.
Does not call a paid API. The game ships with the resulting WAV files.
"""
from pathlib import Path
import hashlib
import json
import shutil
import sys
import wave
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "builds/python_audio"))
import soundfile as sf
def read(path):
return json.loads(path.read_text(encoding="utf-8"))
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def write(path, value):
path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def main():
work = ROOT / "builds/jessica"
voiceover = ROOT / "voiceover/jessica"
game = ROOT / "do-otboy"
story = read(game / "data/phone_story.json")
metadata = read(game / "data/voice_manifest.json")
manifest = read(voiceover / "manifest.json")
original = {entry["id"]: entry for entry in manifest["ready"]}
assembly = read(work / "assembly.json")
aligned = read(work / "split/alignment.json")
segments = {entry["id"]: entry for entry in aligned}
assert len(segments) == 44
assert all(entry["reference_words"] == entry["matched_words"] for entry in aligned)
samples, rate = sf.read(work / "intro_09.mp3")
sf.write(work / "intro_09.wav", samples, rate, subtype="PCM_16")
mothers = {key: digest(game / "assets/voice" / (key + ".wav"))
for key, line in story["lines"].items() if line["speaker"] == "Мама"}
ready = []
for key, line in story["lines"].items():
if line["speaker"] != "Катя":
continue
if key in segments:
source = work / "split" / (key + ".wav")
provenance = dict(assembly["batch"], source_start=segments[key]["start"],
source_end=segments[key]["end"], matched_words=segments[key]["matched_words"])
elif key == "intro_09":
source = work / "intro_09.wav"
provenance = assembly["intro09"]
else:
source = voiceover / (key + ".wav")
provenance = {field: original[key][field] for field in ["generation_id", "node_id", "session_ids", "model_id"]}
with wave.open(str(source)) as audio:
assert audio.getsampwidth() == 2 and audio.getnframes() > 0, key
duration = round(audio.getnframes() / audio.getframerate(), 3)
target = game / "assets/voice" / (key + ".wav")
shutil.copyfile(source, target)
entry = dict(id=key, text=line["text"], voice_name=manifest["voice_name"],
voice_id=manifest["voice_id"], provider="ElevenLabs", **provenance,
wav_duration=duration, sha256=digest(target),
file="../../do-otboy/assets/voice/" + target.name)
ready.append(entry)
metadata[key] = dict(entry, speaker="Катя", voice=manifest["voice_name"],
file="assets/voice/" + target.name)
assert len(ready) == 48
assert all(digest(game / "assets/voice" / (key + ".wav")) == value for key, value in mothers.items())
manifest.update(status="complete", ready=ready, pending=[],
note="All 48 Katya lines installed. 44 split by word timestamps; all 337 script words matched. Mother unchanged.")
write(voiceover / "manifest.json", manifest)
write(voiceover / "alignment.json", aligned)
write(game / "data/voice_manifest.json", metadata)
print(f"Installed {len(ready)} Jessica lines; {len(mothers)} mother's recordings unchanged")
if __name__ == "__main__":
main()