forked from SayanoAI/RVC-Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts_cli.py
63 lines (49 loc) · 1.85 KB
/
tts_cli.py
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
import hashlib
import numpy as np
import os
from lib.infer_pack.text.cleaners import english_cleaners
from webui import get_cwd
from webui.audio import load_input_audio
from webui.downloader import BASE_CACHE_DIR
CWD = get_cwd()
os.makedirs(os.path.join(CWD,"models","TTS","embeddings"),exist_ok=True)
TTS_MODELS_DIR = os.path.join(CWD,"models","TTS")
DEFAULT_SPEAKER = os.path.join(TTS_MODELS_DIR,"embeddings","Sayano.npy")
def cast_to_device(tensor, device):
try:
return tensor.to(device)
except Exception as e:
print(e)
return tensor
def __edge__(text, speaker="en-US-JennyNeural"):
import edge_tts
import asyncio
from threading import Thread
temp_dir = os.path.join(BASE_CACHE_DIR,"tts","edge",speaker)
os.makedirs(temp_dir,exist_ok=True)
tempfile = os.path.join(temp_dir,f"{hashlib.md5(text.encode('utf-8')).hexdigest()}.wav")
async def fetch_audio():
communicate = edge_tts.Communicate(text, speaker)
try:
with open(tempfile, "wb") as data:
async for chunk in communicate.stream():
if chunk["type"] == "audio":
data.write(chunk["data"])
except Exception as e:
print(e)
thread = Thread(target=asyncio.run, args=(fetch_audio(),),name="edge-tts",daemon=True)
thread.start()
thread.join()
try:
audio, sr = load_input_audio(tempfile,sr=16000)
return audio, sr
except Exception as e:
print(e)
return None
def generate_speech(text, method="edge",speaker="en-US-JennyNeural",dialog_only=False):
text = english_cleaners(text.strip(),dialog_only=dialog_only) #clean text
if text and len(text) == 0:
return (np.zeros(0).astype(np.int16),16000)
if method=="edge":
return __edge__(text,speaker)
else: return None