Files
ComfyGen/make_voice_refs.py

42 lines
1.4 KiB
Python
Raw Normal View History

"""
Reference voices for the narrator (generate_voice.py clones them with Chatterbox).
Kokoro has clearly female / male English voices; a ~12 s calm sample of each is
enough for Chatterbox to take over the timbre in every language.
.venv-tts\\Scripts\\python.exe make_voice_refs.py
writes voice_refs/female.wav and voice_refs/male.wav
"""
import _bootstrap # Projektordner (config.py, Listen) vor allen anderen Modulen
import config
import os
import numpy as np
import soundfile as sf
from kokoro import KPipeline
HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, "voice_refs")
TEXT = ("Once upon a time, in a quiet little town by the sea, a child was born. "
"The years went by like seasons, slowly and then all at once. "
"There were first steps, first friends, and a first love, "
"and every single day added another page to the story.")
VOICES = {"female": "af_heart", "male": "am_michael"}
def main():
os.makedirs(OUT, exist_ok=True)
pipe = KPipeline(lang_code="a")
for name, voice in VOICES.items():
chunks = [audio for _, _, audio in pipe(TEXT, voice=voice, speed=0.9)]
audio = np.concatenate([c.numpy() if hasattr(c, "numpy") else c for c in chunks])
path = os.path.join(OUT, f"{name}.wav")
sf.write(path, audio, 24000)
print(f"{name}: {voice}, {len(audio) / 24000:.1f} s -> {path}")
if __name__ == "__main__":
main()