Files
Diabolo/DoOtboy/tools/install_stairwell_voices.py
T

53 lines
3.2 KiB
Python

"""Replace three stairwell lines without changing any other recorded take."""
from pathlib import Path
import hashlib
import json
import runpy
import shutil
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "builds/python_audio"))
import soundfile as sf
WORK = ROOT / "builds/soundscape"
GAME = ROOT / "do-otboy"
def read(path): return json.loads(path.read_text(encoding="utf-8"))
def write(path, value): path.write_text(json.dumps(value, ensure_ascii=False, indent=2)+"\n", encoding="utf-8")
source = read(WORK / "stairs_assembly.json")
segments = read(WORK / "stairs_split/alignment.json")
for segment in segments:
assert segment["matched_words"] == segment["reference_words"]
shutil.copyfile(WORK / "stairs_split" / segment["file"], GAME / "assets/voice" / segment["file"])
samples, rate = sf.read(WORK / "stairs_katya.mp3")
sf.write(GAME / "assets/voice/question_06_k.wav", samples, rate, subtype="PCM_16")
runpy.run_path(str(ROOT / "tools/write_phone_story.py"))
runpy.run_path(str(ROOT / "tools/write_voice_direction.py"))
metadata = read(GAME / "data/voice_manifest.json")
score = {e["id"]: e for e in read(ROOT / "voiceover/performance.json")["lines"]}
segments.append(dict(id="question_06_k", text=score["question_06_k"]["text"], start=0,
end=len(samples)/rate, duration=len(samples)/rate, file="question_06_k.wav"))
for segment in segments:
key = segment["id"]
original = metadata[key]
take = source["katya" if key.endswith("_k") else "mother"]
path = GAME / "assets/voice" / segment["file"]
entry = {field: original[field] for field in ["voice_id", "voice_name", "voice", "provider", "model_id", "speaker"]}
entry.update(id=key, text=score[key]["text"], direction=score[key]["direction"],
tts_text=score[key]["tts_text"], pronunciation=score[key]["pronunciation"],
generation_id=take.get("generation_id", take.get("id")), node_id=take["node_id"],
file="assets/voice/"+path.name, source_start=segment["start"], source_end=segment["end"],
wav_duration=round(segment["duration"],3), sha256=hashlib.sha256(path.read_bytes()).hexdigest())
if "transcription_generation_id" in take: entry["transcription_generation_id"] = take["transcription_generation_id"]
metadata[key] = entry
alignment = read(ROOT / "voiceover/alignment.json")
replaced = {segment["id"] for segment in segments}
for batch in alignment:
alignment[batch] = [e for e in alignment[batch] if e["id"] not in replaced]
alignment["intact_stairwell"] = segments
write(GAME / "data/voice_manifest.json", metadata)
write(ROOT / "voiceover/alignment.json", alignment)
for character, speaker in [("carol", "Мама"), ("jessica", "Катя")]:
entries = [dict(e, file="../../do-otboy/"+e["file"]) for e in metadata.values() if e["speaker"] == speaker]
write(ROOT / "voiceover" / character / "manifest.json", dict(status="complete", version="0.2.3", voice_id=entries[0]["voice_id"], voice_name=entries[0]["voice_name"], ready=entries, pending=[]))
write(ROOT / "voiceover" / character / "alignment.json", [e for batch in alignment.values() for e in batch if metadata[e["id"]]["speaker"] == speaker])
print("Updated Carol/Jessica stairwell lines: intact stairs, no lighting")