Files
ComfyGen/make_voice_refs.py

59 lines
2.3 KiB
Python
Raw Permalink 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, voice_refs/male.wav and voice_refs/warrior.wav
(warrior: very deep, rough battle voice – Kokoro bm_lewis, −3 semitones, soft saturation)
"""
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"}
WARRIOR_TEXT = ("Hear me, sons of Rome! I have bled in this sand for years. I have broken chains and broken men. "
"Tonight I fight, and tomorrow I will be free!")
def warrior():
import librosa
audio = np.concatenate([a.numpy() if hasattr(a, "numpy") else a
for _, _, a in KPipeline(lang_code="b")(WARRIOR_TEXT, voice="bm_lewis", speed=0.88)])
deep = librosa.effects.pitch_shift(audio.astype(np.float32), sr=24000, n_steps=-3)
deep = deep / (np.abs(deep).max() + 1e-6)
rough = np.tanh(deep * 2.6) / np.tanh(2.6) * 0.6 + deep * 0.4
path = os.path.join(OUT, "warrior.wav")
sf.write(path, rough * 0.9, 24000)
print(f"warrior: bm_lewis deep/rough, {len(audio) / 24000:.1f} s -> {path}")
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}")
warrior()
if __name__ == "__main__":
main()