-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglados_tts.py
112 lines (102 loc) · 4.18 KB
/
glados_tts.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import torch
from utils.tools import prepare_text
from scipy.io.wavfile import write
import time
from sys import modules as mod
try:
import winsound
except ImportError:
from subprocess import call
from os import listdir
from phonemizer.backend.espeak.wrapper import EspeakWrapper
EspeakWrapper.set_library('C:\Program Files\eSpeak NG\libespeak-ng.dll')
# Select the device
if torch.is_vulkan_available():
device = 'vulkan'
if torch.cuda.is_available():
device = 'cuda'
else:
device = 'cpu'
# Load models
glados = torch.jit.load('models/glados.pt')
vocoder = torch.jit.load('models/vocoder-gpu.pt', map_location=device)
# Prepare models in RAM
for i in range(4):
init = glados.generate_jit(prepare_text(str(i)))
init_mel = init['mel_post'].to(device)
init_vo = vocoder(init_mel)
def prepare_audio(input_list):
for item in input_list:
tts_glados(item, cache=True)
def tts_glados(input_text, cache=False):
text = input_text
if cache == True:
filename = text.replace(" ", "")
filename = filename.replace(",", "")
filename = filename.replace("?", "")
filename = filename.replace(".", "")
filename = filename.replace("'", "")
filename += ".wav"
cached_files = listdir("voice")
if filename in cached_files:
filename = "voice\\" + filename
if 'winsound' in mod:
winsound.PlaySound(filename, winsound.SND_FILENAME)
else:
call(["aplay", filename])
else:
x = prepare_text(text).to('cpu')
with torch.no_grad():
tts_output = glados.generate_jit(x)
mel = tts_output['mel_post'].to(device)
audio = vocoder(mel)
audio = audio.squeeze()
audio = audio * 32768.0
audio = audio.cpu().numpy().astype('int16')
output_file = ('voice\\' + filename)
write(output_file, 22050, audio)
if 'winsound' in mod:
winsound.PlaySound(output_file, winsound.SND_FILENAME)
else:
call(["aplay", output_file])
else:
x = prepare_text(text).to('cpu')
with torch.no_grad():
tts_output = glados.generate_jit(x)
mel = tts_output['mel_post'].to(device)
audio = vocoder(mel)
audio = audio.squeeze()
audio = audio * 32768.0
audio = audio.cpu().numpy().astype('int16')
output_file = ('voice\\output.wav')
write(output_file, 22050, audio)
if 'winsound' in mod:
winsound.PlaySound(output_file, winsound.SND_FILENAME)
else:
call(["aplay", output_file])
def tts_glados_prod(input_text, cache=False):
text = input_text
if cache == True:
filename = text.replace(" ", "")
filename = filename.replace(",", "")
filename = filename.replace("?", "")
filename = filename.replace(".", "")
filename = filename.replace("'", "")
filename += ".wav"
cached_files = listdir("voice")
if filename in cached_files:
filename = "voice\\" + filename
if 'winsound' in mod:
winsound.PlaySound(filename, winsound.SND_FILENAME)
else:
call(["aplay", filename])
else:
if 'winsound' in mod:
winsound.PlaySound("voice\\IdontunderstandwhatyousaidEithermyvoicerecognitioncoregotcorruptedoryouareamoronPropablythesecondone.wav", winsound.SND_FILENAME)
else:
call(["aplay", "voice\\IdontunderstandwhatyousaidEithermyvoicerecognitioncoregotcorruptedoryouareamoronPropablythesecondone.wav"])
else:
if 'winsound' in mod:
winsound.PlaySound("voice\\IdontunderstandwhatyousaidEithermyvoicerecognitioncoregotcorruptedoryouareamoronPropablythesecondone.wav", winsound.SND_FILENAME)
else:
call(["aplay", "voice\\IdontunderstandwhatyousaidEithermyvoicerecognitioncoregotcorruptedoryouareamoronPropablythesecondone.wav"])