forked from Executedone/Chinese-FastSpeech2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthesize_all.py
407 lines (340 loc) · 14.1 KB
/
synthesize_all.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import re, os, json
import torch
import yaml
import numpy as np
from pypinyin import pinyin, Style, load_phrases_dict
from transformers import BertTokenizer
from utils.model import get_model, get_vocoder
from utils.tools import to_device, synth_samples
from text import text_to_sequence
from text_normalization import text_normalization, PUNCTRUATION, POLY_PHRASE, NUM_DICT
import jieba
from pypinyin_dict.phrase_pinyin_data import large_pinyin
from transformer.ProsodyModel import CharEmbedding
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f'####### device: {device} #######')
def read_lexicon(lex_path):
lexicon = {}
with open(lex_path) as f:
for line in f:
temp = re.split(r"\s+", line.strip("\n"))
word = temp[0]
phones = temp[1:]
if word.lower() not in lexicon:
lexicon[word.lower()] = phones
return lexicon
def read_phrase_dict(phra_path):
with open(phra_path, 'r', encoding='utf8') as f:
phra_dict = json.load(f)
return phra_dict
def _get_pinyins(pinyin_list, lexicon):
new_pinyins = []
temp = ''
for p in pinyin_list:
if p in lexicon:
if temp:
new_pinyins.append(temp)
temp = ''
new_pinyins.append(p)
else:
temp += p
if temp:
new_pinyins.append(temp)
return new_pinyins
def add_userword(words):
for w in words:
jieba.add_word(w)
def is_seg(word_list, phrase_phone_dict):
for w in word_list:
if len(w) <= 1:
flag = False
return flag
flag = False
for w in word_list:
if w in phrase_phone_dict:
flag = True
break
return flag
def word_segment(text, phrase_phone_dict, min_len=3):
new_word_list = []
word_list = jieba.lcut(text, HMM=True)
for w in word_list:
if w in phrase_phone_dict:
new_word_list.append(w)
continue
if len(w) >= min_len:
jieba.del_word(w)
sub_w_list = jieba.lcut(w, HMM=True)
flag = is_seg(sub_w_list, phrase_phone_dict)
if flag:
new_word_list.extend(sub_w_list)
else:
new_word_list.append(w)
jieba.add_word(w)
else:
new_word_list.append(w)
return new_word_list
def correct_pinyin_special(text_list, phrase_phone_dict):
correct_pinyins = []
specail_p = []
for text in text_list:
if text in phrase_phone_dict:
tmp = phrase_phone_dict[text]
else:
tmp = [p[0] for p in pinyin(text, style=Style.TONE3, strict=False, neutral_tone_with_five=True)]
if len(specail_p) > 0:
if re.findall(r'\d', tmp[0])[0] == '4':
specail_p[-1] = re.sub(r'\d', '2', specail_p[-1])
else:
specail_p[-1] = re.sub(r'\d', '4', specail_p[-1])
correct_pinyins.extend(specail_p)
specail_p = []
if '一' in text:
index = text.find('一')
if 0 <= index < len(tmp) - 1 and text[index + 1] not in list(NUM_DICT.values()):
if re.findall(r'\d', tmp[index + 1])[0] == '4':
tmp[index] = re.sub(r'\d', '2', tmp[index])
else:
tmp[index] = re.sub(r'\d', '4', tmp[index])
else:
pass
if '不' in text:
index = text.find('不')
if 0 <= index < len(tmp) - 1:
if re.findall(r'\d', tmp[index + 1])[0] == '4':
tmp[index] = re.sub(r'\d', '2', tmp[index])
else:
tmp[index] = re.sub(r'\d', '4', tmp[index])
elif index == len(tmp) - 1:
specail_p.extend(tmp)
tmp = []
else:
pass
correct_pinyins.extend(tmp)
return correct_pinyins
def correct_pinyin_tone3(pinyin_list):
if len(pinyin_list) >= 2:
for i in range(1, len(pinyin_list)):
try:
if re.findall(r'\d', pinyin_list[i-1])[0] == '3' and re.findall(r'\d', pinyin_list[i])[0] == '3':
pinyin_list[i-1] = pinyin_list[i-1].replace('3', '2')
except IndexError:
pass
return pinyin_list
def get_char_embeds(text, length, char_model, tokenizer):
input_ids = tokenizer.convert_tokens_to_ids(tokenizer.tokenize(text))
input_masks = [1] * len(input_ids)
type_ids = [0] * len(input_ids)
input_ids = torch.LongTensor([input_ids]).to(device)
input_masks = torch.LongTensor([input_masks]).to(device)
type_ids = torch.LongTensor([type_ids]).to(device)
with torch.no_grad():
char_embeds = char_model(input_ids, input_masks, type_ids).squeeze(0).cpu()
assert char_embeds.size(0) == len(length)
expand_vecs = list()
for vec, leng in zip(char_embeds, length):
vec = vec.expand(leng, -1)
expand_vecs.append(vec)
expand_embeds = torch.cat(expand_vecs, 0)
assert expand_embeds.size(0) == sum(length)
return expand_embeds.numpy()
def preprocess_mandarin(text, preprocess_config, char_model, tokenizer):
large_pinyin.load()
load_phrases_dict(POLY_PHRASE)
add_userword(list(POLY_PHRASE.keys()))
phones = []
lexicon = read_lexicon(preprocess_config["path"]["lexicon_path"])
phrase_phone_dict = read_phrase_dict(preprocess_config["path"]["phrase_phone_path"])
add_userword(list(phrase_phone_dict.keys()))
text = text_normalization(text)
text_list = word_segment(text, phrase_phone_dict, min_len=3)
print(text_list)
# 改进处理
pinyins = correct_pinyin_special(text_list, phrase_phone_dict) # 从biaobei字典中获取拼音
pinyins = _get_pinyins(pinyins, lexicon) # 从pypinyin中获取拼音
pinyins = correct_pinyin_tone3(pinyins) # 声调校正
# print("pinyin: ", pinyins)
length = []
for p in pinyins:
if p in lexicon:
phones += lexicon[p]
length.append(len(lexicon[p]))
elif p in PUNCTRUATION:
if p == '+':
phones += lexicon['jia1']
length.append(1)
else:
phones.append("sp")
length.append(1)
else:
phones.append("sp")
length.append(1)
if phones[-1] != 'sp':
phones.append('sp')
length.append(1)
text += '。'
phones = "{" + " ".join(phones) + "}"
print("Raw Text Sequence: {}".format(text))
print("Pinyin Sequence: {}".format(pinyins))
print("Phoneme Sequence: {}".format(phones))
sequence = np.array(text_to_sequence(phones, preprocess_config["preprocessing"]["text"]["text_cleaners"]))
try:
assert sum(length) == len(sequence)
char_embeds = get_char_embeds(text, length, char_model, tokenizer)
except Exception as e:
print(f'--WARNING-- get char embedding error as `{e}` --WARNING--')
char_embeds = None
return np.array(sequence), char_embeds
def synthesize(model, configs, vocoder, batchs, chars_embeds, control_values, result_path):
preprocess_config, model_config, train_config = configs
pitch_control, energy_control, duration_control = control_values
for i, batch in enumerate(batchs):
batch = to_device(batch, device)
if chars_embeds[i] is not None:
char_embeds = torch.from_numpy(chars_embeds[i]).float().to(device)
print('**** shape of char_embeds **** ', char_embeds.shape)
else:
char_embeds = None
print('++++ char_embeds is none... ++++ ')
with torch.no_grad():
# Forward
output = model(
*(batch[2:]),
char_vecs=char_embeds,
p_control=pitch_control,
e_control=energy_control,
d_control=duration_control
)
synth_samples(
batch,
output,
vocoder,
model_config,
preprocess_config,
result_path,
)
def synthesize_all(text_file, result_path):
restore_step = 0
pitch_control = 1.0
energy_control = 1.0
duration_control = 1.0
# Read Config
preprocess_config_path = "./config/AISHELL3/preprocess.yaml"
model_config_path = "./config/AISHELL3/model.yaml"
train_config_path = "./config/AISHELL3/train.yaml"
preprocess_config = yaml.load(open(preprocess_config_path, "r"), Loader=yaml.FullLoader)
model_config = yaml.load(open(model_config_path, "r"), Loader=yaml.FullLoader)
train_config = yaml.load(open(train_config_path, "r"), Loader=yaml.FullLoader)
configs = (preprocess_config, model_config, train_config)
# Result Path
os.makedirs(result_path, exist_ok=True)
# Get model
model = get_model(restore_step, configs, device, train=False)
# Load vocoder
vocoder = get_vocoder(model_config, device)
# Get char model, char tokenizer
char_model = CharEmbedding(preprocess_config['path']['char_model_path'])
char_model.to(device)
char_model.load_state_dict(
torch.load(
os.path.join(preprocess_config['path']['char_model_path'], 'best_model.pt'),
map_location=device)
)
char_model.eval()
char_tokenizer = BertTokenizer.from_pretrained(preprocess_config['path']['char_model_path'])
# Read file and Preprocess texts
with open(text_file, 'r', encoding='utf8') as f:
for line in f:
sample = line.strip().split()
ids, raw_texts = sample[0], ''.join(sample[1:])
speakers = np.array([0])
print(ids)
# 整句合成
phones_seq, char_embeds = preprocess_mandarin(raw_texts, preprocess_config, char_model, char_tokenizer)
texts = np.array([phones_seq])
char_embeds = np.array([char_embeds]) if char_embeds is not None else char_embeds
text_lens = np.array([len(texts[0])])
batchs = [([ids], [raw_texts], speakers, texts, text_lens, max(text_lens))]
chars_embeds = [char_embeds]
control_values = pitch_control, energy_control, duration_control
synthesize(model, configs, vocoder, batchs, chars_embeds, control_values, result_path)
# 构建音频合成类,为服务做准备
class SpeechSynthesis(object):
def __init__(self, config_dir):
print("loading built-in configs...")
self.internal_conf, \
self.preprocess_config, \
self.model_config, \
self.train_config = self._read_internal_config(config_dir)
print("loading acoustic model...")
self.model = get_model(0, self.internal_conf, device, train=False)
print("loading vocoder...")
self.vocoder = get_vocoder(self.model_config, device)
print("loading prosody model...")
self.char_model = CharEmbedding(self.preprocess_config['path']['char_model_path'])
self.char_model.to(device)
self.char_model.load_state_dict(
torch.load(
os.path.join(self.preprocess_config['path']['char_model_path'], 'best_model.pt'),
map_location=device
),
strict=False
)
self.char_model.eval()
self.char_tokenizer = BertTokenizer.from_pretrained(self.preprocess_config['path']['char_model_path'])
self.result_path = './'
jieba.initialize()
def _read_internal_config(self, config_dir):
preprocess_config_path = os.path.join(config_dir, "preprocess.yaml")
model_config_path = os.path.join(config_dir, "model.yaml")
train_config_path = os.path.join(config_dir, "train.yaml")
preprocess_config = yaml.load(open(preprocess_config_path, "r"), Loader=yaml.FullLoader)
model_config = yaml.load(open(model_config_path, "r"), Loader=yaml.FullLoader)
train_config = yaml.load(open(train_config_path, "r"), Loader=yaml.FullLoader)
configs = (preprocess_config, model_config, train_config)
return configs, preprocess_config, model_config, train_config
def text2speech(self, text, pitch_control=1.0, energy_control=1.0, duration_control=1.0):
if len(text) < 1:
print('no texts!')
return None
else:
raw_texts = re.sub(r"\s+", "", text.strip())
speakers = np.array([0])
# 整句合成
print('starting text processing')
phones_seq, char_embeds = preprocess_mandarin(raw_texts,
self.preprocess_config,
self.char_model,
self.char_tokenizer)
texts = np.array([phones_seq])
char_embeds = np.array([char_embeds]) if char_embeds is not None else char_embeds
text_lens = np.array([len(texts[0])])
batchs = [(['tmp'], [raw_texts], speakers, texts, text_lens, max(text_lens))]
chars_embeds = [char_embeds]
control_values = pitch_control, energy_control, duration_control
print('starting speech synthesizing...')
synthesize(self.model,
self.internal_conf,
self.vocoder,
batchs,
chars_embeds,
control_values,
self.result_path)
return os.path.join(os.path.abspath(self.result_path), "tmp.wav")
if __name__ == "__main__":
# 从文件中批量合成语音
# parser = argparse.ArgumentParser()
#
# parser.add_argument("--text_file", type=str, required=False, default="",
# help="text input path")
# parser.add_argument("--output_dir", type=str, required=False, default="",
# help="wav output path")
# args = parser.parse_args()
#
# synthesize_all(args.text_file, args.output_dir)
# 单句语音合成
tts = SpeechSynthesis('./config/AISHELL3')
while True:
text = input("请输入文本:")
print(tts.text2speech(text))
pass