-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts.py
More file actions
32 lines (27 loc) · 1.16 KB
/
Copy pathtts.py
File metadata and controls
32 lines (27 loc) · 1.16 KB
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
import os, numpy as np, sounddevice as sd
from TTS.api import TTS
MODEL_DIR = os.path.join(os.path.dirname(__file__), "models", "tts")
os.makedirs(MODEL_DIR, exist_ok=True)
model = TTS("tts_models/en/ljspeech/tacotron2-DDC", gpu=False).to("cpu")
vocoder = None
def load_vocoder():
global vocoder
# from TTS.utils.synthesizer import Synthesizer # low-level loader
# vocoder = Synthesizer(
# tts_config_path=None,
# vocoder_checkpoint="vocoder_models/en/ljspeech/hifigan_v2/model.pth",
# vocoder_config="vocoder_models/en/ljspeech/hifigan_v2/config.json",
# use_cuda=False
# )
pass
def tts(text, voice="emma", pitch=0, speed=1.0, tone=0.0, volume=1.0):
text = text.strip()
if not text:
return np.zeros(int(0.1 * 22050), dtype=np.int16)
wav = model.tts(text) # create float32 wav
wav = np.array(wav, dtype=np.float32)
if speed != 1.0:
wav = np.interp(np.arange(0, len(wav), speed), np.arange(len(wav)), wav)
wav = np.clip(wav, -1.0, 1.0)
wav *= volume # apply volume
return (wav * 32767).astype(np.int16)