forked from mozilla/TTS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
synthesize.py
309 lines (263 loc) · 11.3 KB
/
synthesize.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
import time
import torch
import json
from datetime import datetime, date
import string
from glob import glob
import numpy as np
from pathlib import Path
import sys
import random
import argparse
from TTS.utils.synthesis import synthesis
from TTS.utils.generic_utils import setup_model
from TTS.utils.io import load_config, load_checkpoint
from TTS.utils.text.symbols import make_symbols, symbols, phonemes
from TTS.utils.audio import AudioProcessor
from TTS.utils.text.text_cleaning import clean_sentence
from TTS.vocoder.utils.generic_utils import setup_generator
def tts(model,
vocoder_model,
C,
VC,
text,
ap,
ap_vocoder,
use_cuda,
batched_vocoder,
speaker_id=None,
style_input=None,
figures=False):
use_vocoder_model = vocoder_model is not None
waveform, alignment, _, postnet_output, stop_tokens, _ = synthesis(
model, text, C, use_cuda, ap, speaker_id, style_input=style_input,
truncated=False, enable_eos_bos_chars=C.enable_eos_bos_chars,
use_griffin_lim=(not use_vocoder_model), do_trim_silence=True)
if C.model == "Tacotron" and use_vocoder_model:
postnet_output = ap.out_linear_to_mel(postnet_output.T).T
# correct if there is a scale difference b/w two models
if use_vocoder_model:
vocoder_input = torch.FloatTensor(postnet_output.T).unsqueeze(0)
waveform = vocoder_model.inference(vocoder_input)
if use_cuda:
waveform = waveform.cpu()
#waveform = waveform.detach().numpy()
waveform = waveform.numpy()
waveform = waveform.flatten()
# if use_vocoder_model:
# postnet_output = ap._denormalize(postnet_output)
# postnet_output = ap_vocoder._normalize(postnet_output)
# vocoder_input = torch.FloatTensor(postnet_output.T).unsqueeze(0)
# waveform = vocoder_model.generate(
# vocoder_input.cuda() if use_cuda else vocoder_input,
# batched=batched_vocoder,
# target=8000,
# overlap=400)
return alignment, postnet_output, stop_tokens, waveform
def load_vocoder(lib_path, model_file, model_config, use_cuda):
sys.path.append(lib_path) # set this if ParallelWaveGAN is not installed globally
#pylint: disable=import-outside-toplevel
vocoder_config = load_config(model_config)
vocoder_model = setup_generator(vocoder_config)
checkpoint = torch.load(model_file, map_location='cpu')
print(' > Model step:', checkpoint['step'])
vocoder_model.load_state_dict(checkpoint['model'])
vocoder_model.remove_weight_norm()
vocoder_model.inference_padding = 0
vocoder_config = load_config(model_config)
ap_vocoder = AudioProcessor(**vocoder_config['audio'])
if use_cuda:
vocoder_model.cuda()
return vocoder_model.eval(), ap_vocoder
def split_into_sentences(text):
text = text.replace('.', '.<stop>')
text = text.replace('!', '!<stop>')
text = text.replace('?', '?<stop>')
sentences = text.split("<stop>")
sentences = list(filter(None, [s.strip() for s in sentences])) # remove empty sentences
return sentences
def main(**kwargs):
global symbols, phonemes # pylint: disable=global-statement
current_date = date.today()
current_date = current_date.strftime("%B %d %Y")
start_time = time.time()
# read passed variables from gui
text = kwargs['text'] # text to generate speech from
use_cuda = kwargs['use_cuda'] # if gpu exists default is true
project = kwargs['project'] # path to project folder
vocoder_type = kwargs['vocoder'] # vocoder type, default is GL
use_gst = kwargs['use_gst'] # use style_wave for prosody
style_dict = kwargs['style_input'] # use style_wave for prosody
speaker_id = kwargs['speaker_id'] # name of the selected speaker
sentence_file = kwargs['sentence_file'] # path to file if generate from file
out_path = kwargs['out_path'] # path to save the output wav
batched_vocoder = True
# load speakers
speakers_file_path = Path(project, "speakers.json")
if speakers_file_path.is_file():
speaker_data = json.load(open(speakers_file_path, 'r'))
num_speakers = len(speaker_data)
#get the speaker id for selected speaker
if speaker_id >= num_speakers:
print('Speaker ID outside of number of speakers range. Using default 0.')
speaker_id = 0
speaker_name = [speaker for speaker, id in speaker_data.items() if speaker_id == id][0]
else:
speaker_name = [speaker for speaker, id in speaker_data.items() if speaker_id == id][0]
else:
speaker_name = 'Default'
num_speakers = 0
speaker_id = None
# load the config
config_path = Path(project, "config.json")
C = load_config(config_path)
if use_gst:
if style_dict is not None:
style_input = style_dict
else:
style_input = None
# load the audio processor
ap = AudioProcessor(**C.audio)
# if the vocabulary was passed, replace the default
if 'characters' in C.keys():
symbols, phonemes = make_symbols(**C.characters)
# find the tts model file in project folder
try:
tts_model_file = glob(str(Path(project, '*.pth.tar')))
if not tts_model_file:
raise FileNotFoundError
model_path = tts_model_file[0]
except FileNotFoundError:
print('[!] TTS Model not found in path: "{}"'.format(project))
# load the model
num_chars = len(phonemes) if C.use_phonemes else len(symbols)
model = setup_model(num_chars, num_speakers, C)
# if gpu is not available use cpu
model, state = load_checkpoint(model, model_path, use_cuda=use_cuda)
model.decoder.max_decoder_steps = 2000
model.eval()
print(' > Model step:', state['step'])
print(' > Model r: ', state['r'])
# load vocoder
if vocoder_type is 'MelGAN':
try:
model_file = glob(str(Path(project, 'vocoder/*.pth.tar')))
vocoder, ap_vocoder = load_vocoder(str(Path('TTS')),
str(model_file[0]),
str(Path(project, 'vocoder/config.json')),
use_cuda)
except Exception:
print('[!] Error loading vocoder: "{}"'.format(project))
sys.exit(0)
elif vocoder_type is 'WaveRNN':
try:
model_file = glob(str(Path(project, 'vocoder/*.pkl')))
vocoder, ap_vocoder = load_vocoder(str(Path('TTS')), str(model_file[0]), str(Path(project, 'config.yml')), use_cuda)
except Exception:
print('[!] Error loading vocoder: "{}"'.format(project))
sys.exit(0)
else:
vocoder, ap_vocoder = None, None
print(" > Vocoder: {}".format(vocoder_type))
print(' > Using style input: {}\n'.format(style_input))
if sentence_file != '':
with open(sentence_file, "r", encoding='utf8') as f:
list_of_sentences = [s.strip() for s in f.readlines()]
else:
list_of_sentences = [text.strip()]
# iterate over every passed sentence and synthesize
for _, tts_sentence in enumerate(list_of_sentences):
wav_list = []
# remove character which are not alphanumerical or contain ',. '
tts_sentence = clean_sentence(tts_sentence)
print(" > Text: {}".format(tts_sentence))
# build filename
current_time = datetime.now().strftime("%H%M%S")
file_name = ' '.join(tts_sentence.split(" ")[:10])
# if multiple sentences in one line -> split them
tts_sentence = split_into_sentences(tts_sentence)
# if sentence was split in sub-sentences -> iterate over them
for sentence in tts_sentence:
# synthesize voice
_, _, _, wav = tts(model,
vocoder,
C,
None,
sentence,
ap,
ap_vocoder,
use_cuda,
batched_vocoder,
speaker_id=speaker_id,
style_input=style_input,
figures=False)
# join sub-sentences back together and add a filler between them
wav_list += list(wav)
wav_list += [0] * 10000
wav = np.array(wav_list)
# finalize filename
file_name = "_".join([str(current_time), file_name])
file_name = file_name.translate(
str.maketrans('', '', string.punctuation.replace('_', ''))) + '.wav'
if out_path == "":
out_dir = str(Path(project, 'output', current_date, speaker_name))
out_path = os.path.join(out_dir, file_name)
else:
out_dir = os.path.dirname(out_path)
# create output directory if it doesn't exist
if not os.path.isdir(out_dir):
os.makedirs(out_dir, exist_ok=True)
# save generated wav to disk
ap.save_wav(wav, out_path)
end_time = time.time()
print(" > Run-time: {}".format(end_time - start_time))
print(" > Saving output to {}\n".format(out_path))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--text',
type=str,
required=True,
help='Text to generate speech.')
parser.add_argument('--project_path',
type=str,
required=True,
help='Path to model')
parser.add_argument('--use_gst',
type=bool,
help='Use Global Style Tokens.',
default=False)
parser.add_argument('--use_cuda',
type=bool,
help='Run model on CUDA.',
default=False)
parser.add_argument('--style_input',
type=bool,
help='Use style input.',
default=False)
parser.add_argument('--vocoder_type',
type=str,
help='Vocoder Type -> Choices: [Griffin-Lim, WaveRNN, MelGAN].',
default="MelGAN")
parser.add_argument('--speaker_id',
type=int,
help='Select speaker by ID.',
default=0)
parser.add_argument('--sentence_file',
type=str,
help='Path to text file to generate speech from.',
default="")
parser.add_argument('--out_path',
type=str,
help='Path to save final wav file.',
default="")
args = parser.parse_args()
main(text=args.text,
use_cuda=args.use_cuda,
use_gst=args.use_gst,
style_input=args.style_input,
project=args.project_path,
speaker_id=args.speaker_id,
vocoder=args.vocoder_type,
sentence_file=args.sentence_file,
out_path=args.out_path)