82 lines
4.2 KiB
Python
82 lines
4.2 KiB
Python
"""Install word-verified continuity revisions, preserving all other voice takes."""
|
|
from pathlib import Path
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import runpy
|
|
import shutil
|
|
import sys
|
|
import argparse
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
GAME = ROOT / "do-otboy"
|
|
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 write(path, value):
|
|
path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--batch", default="continuity")
|
|
parser.add_argument("--count", type=int, default=18)
|
|
parser.add_argument("--version", default="0.2.11")
|
|
args = parser.parse_args()
|
|
assert args.batch and all(c.isalnum() or c in "_-" for c in args.batch)
|
|
work = ROOT / "builds" / args.batch
|
|
story = read(GAME / "data/phone_story.json")
|
|
metadata = read(GAME / "data/voice_manifest.json")
|
|
assembly = read(work / "assembly.json")
|
|
prepared = []
|
|
for character in ("katya", "mother"):
|
|
for segment in read(work / (character + "_split/alignment.json")):
|
|
key = segment["id"]
|
|
assert segment["text"] == story["lines"][key]["text"], key
|
|
assert segment["reference_words"] == segment["matched_words"], key
|
|
path = work / (character + "_split") / segment["file"]
|
|
info = sf.info(path)
|
|
assert info.channels == 1 and info.subtype == "PCM_16" and info.duration > .2
|
|
prepared.append((character, segment, path))
|
|
assert len(prepared) == args.count
|
|
for _, segment, path in prepared:
|
|
shutil.copyfile(path, GAME / "assets/voice" / segment["file"])
|
|
runpy.run_path(str(ROOT / "tools/write_phone_story.py"))
|
|
runpy.run_path(str(ROOT / "tools/write_voice_direction.py"))
|
|
score = {entry["id"]: entry for entry in read(ROOT / "voiceover/performance.json")["lines"]}
|
|
alignment = read(ROOT / "voiceover/alignment.json")
|
|
changed = {segment["id"] for _, segment, _ in prepared}
|
|
for batch in alignment:
|
|
alignment[batch] = [entry for entry in alignment[batch] if entry["id"] not in changed]
|
|
levels = read(GAME / "data/voice_levels.json")
|
|
for character, segment, path in prepared:
|
|
key = segment["id"]
|
|
original = metadata.get(key) or next(e for e in metadata.values() if e["speaker"] == score[key]["speaker"])
|
|
entry = {field: original[field] for field in ("provider", "voice", "voice_id", "voice_name", "speaker")}
|
|
entry.update(assembly[character], flow_id=assembly["flow_id"], id=key,
|
|
text=score[key]["text"], direction=score[key]["direction"],
|
|
tts_text=score[key]["tts_text"], pronunciation=score[key]["pronunciation"],
|
|
file="assets/voice/" + path.name, source_start=segment["start"],
|
|
source_end=segment["end"], wav_duration=segment["duration"],
|
|
sha256=hashlib.sha256(path.read_bytes()).hexdigest())
|
|
metadata[key] = entry
|
|
samples, _ = sf.read(path)
|
|
levels[key] = round(20 * math.log10(max(1e-8, float((samples ** 2).mean()) ** .5)), 3)
|
|
alignment.setdefault(args.batch + "_" + character, []).append(segment)
|
|
assert all(metadata[key]["text"] == line["text"] for key, line in story["lines"].items())
|
|
write(GAME / "data/voice_manifest.json", metadata)
|
|
write(GAME / "data/voice_levels.json", levels)
|
|
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=args.version, 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(f"Installed {len(prepared)} verified lines; preserved {len(metadata)-len(prepared)} other recordings")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|