File size: 2,585 Bytes
b5c6ae8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | """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()
|