Files
Cinderwild/tools/trailer/make_trailer_voice.py

52 lines
2.1 KiB
Python

"""Old Tam's lines for the trailer (tools/trailer/make_trailer.sh mixes them
in): short ones, in his voice (the same clone as tools/voice/make_voice.py),
each checked by Whisper and the best of three takes kept. Output:
tools/trailer/voice/<name>.ogg. Run with the voice environment:
tools/voice/.venv-qwen/bin/python tools/trailer/make_trailer_voice.py
"""
import json, os, subprocess, sys, tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "voice"))
import make_voice as mv # (its voice, its checks)
OUT = os.path.join(HERE, "voice")
## name -> what he says (make_trailer.sh times each against the shots).
LINES = {
"dawn": "Ha! Awake at last! Washed up on my island, eh?",
"wolves": "Hush now. See it, there, at the edge of the light?",
"cove": "Well, you made it down!",
}
def main():
import numpy as np
import soundfile as sf
import mlx_whisper
from mlx_audio.tts.utils import load_model
os.makedirs(OUT, exist_ok=True)
model = load_model(mv.MODEL)
sr = getattr(model, "sample_rate", 24000)
tmp = tempfile.mkdtemp()
for name, text in LINES.items():
best = None
for k in range(3):
res = list(model.generate(text=text, ref_audio=mv.REF_AUDIO, ref_text=mv.REF_TEXT))
audio = np.concatenate([np.array(r.audio) for r in res])
wav = os.path.join(tmp, "%s_%d.wav" % (name, k))
sf.write(wav, audio, sr)
heard = mlx_whisper.transcribe(wav, path_or_hf_repo=mv.WHISPER, language="en")["text"].strip()
score = mv.match(text, heard)
if best is None or score > best[0]:
best = (score, wav, len(audio) / sr)
if score >= 0.98:
break
subprocess.run(["ffmpeg", "-v", "error", "-y", "-i", best[1], "-ac", "2", "-c:a", "vorbis", "-strict", "-2",
"-q:a", "5", os.path.join(OUT, name + ".ogg")], check=True)
print(" %s (%.1f s, words %.0f%%)" % (name, best[2], best[0] * 100), flush=True)
json.dump(LINES, open(os.path.join(OUT, "lines.json"), "w"), indent=2)
if __name__ == "__main__":
main()