forked from CjangCjengh/MoeGoe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts_engine.py
179 lines (151 loc) · 5.95 KB
/
tts_engine.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from scipy.io.wavfile import write
from mel_processing import spectrogram_torch
from text import text_to_sequence, _clean_text
from models import SynthesizerTrn
import utils
import commons
import sys
import io
import re
import yaml
from torch import no_grad, LongTensor
import logging
logging.getLogger('numba').setLevel(logging.WARNING)
class TTS_Engine():
"""
Read the voice model and settings from the config file and perform the tts function of moegoe.
...
Attributes
----------
vits_model_path : str
Path of the Model file
config_file_path : str
Path of the Config file
speaker_id : int
voice actor id
hparams_ms : HParams
...
net_g_ms : SynthesizerTrn
...
Methods
-------
text_to_audio(self, text:str)
Convert text to wav byte stream
"""
def __init__(self, yaml_path: str):
"""
Parameters
----------
yaml_path : str
Path of the yaml file
yaml file should have the following format:
```
VITSModelPath: 'path/to/model.pth'
configFilePath: 'path/to/config.yaml'
speakerID: 0 'speaker id'
```
Raises
------
Exception
If there is an error in the process
"""
try:
__data: dict = self.__open_yaml(yaml_path)
# replace existing input course to yaml file
self.vits_model_path = __data['VITSModelPath']
self.config_file_path = __data['configFilePath']
self.speaker_id = __data['speakerID']
self.hparams_ms = utils.get_hparams_from_file(self.config_file_path)
# init Moegoe sequence
self.__init_moegoe()
_ = self.net_g_ms.eval()
utils.load_checkpoint(self.vits_model_path, self.net_g_ms)
except Exception as e:
raise Exception(f'Error in TTS_Engine __init__: {e}')
def __open_yaml(self, yaml_path: str) -> dict:
with open(yaml_path, 'r', encoding='utf-8') as file:
data = yaml.safe_load(file)
return data
def __init_moegoe(self):
n_speakers = self.hparams_ms.data.n_speakers if 'n_speakers' in self.hparams_ms.data.keys() else 0
n_symbols = len(self.hparams_ms.symbols) if 'symbols' in self.hparams_ms.keys() else 0
speakers = self.hparams_ms.speakers if 'speakers' in self.hparams_ms.keys() else ['0']
use_f0 = self.hparams_ms.data.use_f0 if 'use_f0' in self.hparams_ms.data.keys() else False
emotion_embedding = self.hparams_ms.data.emotion_embedding if 'emotion_embedding' in self.hparams_ms.data.keys() else False
#init object
self.net_g_ms = SynthesizerTrn(
n_symbols,
self.hparams_ms.data.filter_length // 2 + 1,
self.hparams_ms.train.segment_size,
n_speakers=n_speakers,
emotion_embedding=emotion_embedding,
**self.hparams_ms.model)
def __get_label_value(self, text:str, label:str, default, warning_name:str='value'):
value = re.search(rf'\[{label}=(.+?)\]', text)
if value:
try:
text = re.sub(rf'\[{label}=(.+?)\]', '', text, 1)
value = float(value.group(1))
except:
print(f'Invalid {warning_name}!')
sys.exit(1)
else:
value = default
return value, text
def __get_label(self, text:str, label:str):
if f'[{label}]' in text:
return True, text.replace(f'[{label}]', '')
else:
return False, text
def __get_text(self, text:str, hps:utils.HParams, cleaned=False):
if cleaned:
text_normalized = text_to_sequence(text, hps.symbols, [])
else:
text_normalized = text_to_sequence(text, hps.symbols, hps.data.text_cleaners)
if hps.data.add_blank:
text_normalized = commons.intersperse(text_normalized, 0)
text_normalized = LongTensor(text_normalized)
return text_normalized
def text_to_audio(self, text: str):
"""
Convert text to wav byte stream.
Parameters
----------
text : str
Text to be converted to audio
Raises
------
Exception
If there is an error in the process
Returns
-------
bytes_io.read() : bytes
Wav byte stream
"""
try:
length_scale, text = self.__get_label_value(text, 'LENGTH', 1, 'length scale')
noise_scale, text = self.__get_label_value(text, 'NOISE', 0.667, 'noise scale')
noise_scale_w, text = self.__get_label_value(text, 'NOISEW', 0.8, 'deviation of noise')
cleaned, text = self.__get_label(text, 'CLEANED')
sequence_text_normalized = self.__get_text(text, self.hparams_ms, cleaned=cleaned)
with no_grad():
x_tst = sequence_text_normalized.unsqueeze(0)
x_tst_lengths = LongTensor([sequence_text_normalized.size(0)])
sid = LongTensor([self.speaker_id])
audio = self.net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=noise_scale,
noise_scale_w=noise_scale_w, length_scale=length_scale)[0][0, 0].data.cpu().float().numpy()
bytes_wav = bytes()
bytes_io = io.BytesIO(bytes_wav)
write(bytes_io, self.hparams_ms.data.sampling_rate, audio)
return bytes_io.read()
except Exception as e:
raise Exception(f'Error in text_to_audio: {e}')
if __name__ == '__main__':
try:
tts_engine = TTS_Engine('config.yaml')
stream = tts_engine.text_to_audio('私は今日の状況を見なければなりません。')
with open('output.wav', 'wb') as f:
f.write(stream)
except Exception as e:
print(e)
sys.exit(1)