apptor-tiny-tts / speak.py
apptor's picture
apptor-tiny-tts German v1 (step 82044, Vocos 60k)
b5c6ae8 verified
Raw History Blame Contribute Delete
2.59 kB
"""apptor-tiny-tts: German text -> WAV on the CPU.
python speak.py "Hallo, wie geht es dir?" --voice F2 --out hallo.wav [--vocos] [--speed 1.0]
Needs espeak-ng, onnxruntime, numpy, soundfile, num2words; --vocos also torch and vocos.
"""
import argparse
import json
import re
from pathlib import Path
import numpy as np
import onnxruntime as ort
import soundfile as sf
from tinyvoca.text import phonemize
HERE = Path(__file__).resolve().parent
def main():
ap = argparse.ArgumentParser()
ap.add_argument("text")
ap.add_argument("--voice", default="F2", choices=["F2", "M2"])
ap.add_argument("--out", default="out.wav")
ap.add_argument("--speed", type=float, default=1.0)
ap.add_argument("--vocos", action="store_true", help="second stage: fine-tuned Vocos (cleaner sound)")
ap.add_argument("--threads", type=int, default=4)
a = ap.parse_args()
meta = json.loads((HERE / "apptor-tiny-tts-de.json").read_text(encoding="utf-8"))
opts = ort.SessionOptions()
opts.intra_op_num_threads = a.threads
session = ort.InferenceSession(str(HERE / "apptor-tiny-tts-de.onnx"), opts, providers=["CPUExecutionProvider"])
voice = meta["voices"].index(a.voice)
pieces = []
for sentence in re.split(r"(?<=[.!?…])\s+", a.text.strip()): # one sentence per call (<= 512 tokens)
if not sentence:
continue
p = phonemize(sentence)
audio, _ = session.run(None, {
"tokens": np.array([p.ids], np.int64), "stress": np.array([p.stress], np.int64),
"voice": np.array([voice], np.int64), "speed": np.array([a.speed], np.float32),
"pitch_shift": np.array([0.0], np.float32), "pitch_range": np.array([1.0], np.float32)})
pieces += [audio[0].astype(np.float32), np.zeros(int(0.12 * meta["sample_rate"]), np.float32)]
audio = np.concatenate(pieces)
if a.vocos:
import torch
from vocos import Vocos
torch.set_num_threads(a.threads)
voc = Vocos.from_pretrained("charactr/vocos-mel-24khz").eval()
voc.load_state_dict(torch.load(HERE / "apptor-tiny-tts-vocos-de.pt", map_location="cpu", weights_only=True))
with torch.inference_mode():
x = torch.from_numpy(audio)[None]
audio = voc.decode(voc.feature_extractor(x))[0, :len(audio)].numpy()
peak = float(np.abs(audio).max())
if peak > 0.95:
audio = audio * (0.95 / peak)
sf.write(a.out, audio, meta["sample_rate"])
print(f"wrote {a.out} ({len(audio) / meta['sample_rate']:.1f} s)")
if __name__ == "__main__":
main()