-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathvits_pinyin.py
102 lines (88 loc) · 3.05 KB
/
vits_pinyin.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
import re
from tn.chinese.normalizer import Normalizer
from pypinyin import lazy_pinyin, Style
from pypinyin.core import load_phrases_dict
from text import pinyin_dict
from bert import TTSProsody
def is_chinese(uchar):
if uchar >= u'\u4e00' and uchar <= u'\u9fa5':
return True
else:
return False
def clean_chinese(text: str):
text = text.strip()
text_clean = []
for char in text:
if (is_chinese(char)):
text_clean.append(char)
else:
if len(text_clean) > 1 and is_chinese(text_clean[-1]):
text_clean.append(',')
text_clean = ''.join(text_clean).strip(',')
return text_clean
def load_pinyin_dict():
my_dict={}
with open("./text/pinyin-local.txt", "r", encoding='utf-8') as f:
content = f.readlines()
for line in content:
cuts = line.strip().split()
hanzi = cuts[0]
phone = cuts[1:]
tmp = []
for p in phone:
tmp.append([p])
my_dict[hanzi] = tmp
load_phrases_dict(my_dict)
class VITS_PinYin:
def __init__(self, bert_path, device, hasBert=True):
load_pinyin_dict()
self.hasBert = hasBert
if self.hasBert:
self.prosody = TTSProsody(bert_path, device)
self.normalizer = Normalizer()
def get_phoneme4pinyin(self, pinyins):
result = []
count_phone = []
for pinyin in pinyins:
if pinyin[:-1] in pinyin_dict:
tone = pinyin[-1]
a = pinyin[:-1]
a1, a2 = pinyin_dict[a]
result += [a1, a2 + tone]
count_phone.append(2)
return result, count_phone
def chinese_to_phonemes(self, text):
text = self.normalizer.normalize(text)
text = clean_chinese(text)
phonemes = ["sil"]
chars = ['[PAD]']
count_phone = []
count_phone.append(1)
for subtext in text.split(","):
if (len(subtext) == 0):
continue
pinyins = self.correct_pinyin_tone3(subtext)
sub_p, sub_c = self.get_phoneme4pinyin(pinyins)
phonemes.extend(sub_p)
phonemes.append("sp")
count_phone.extend(sub_c)
count_phone.append(1)
chars.append(subtext)
chars.append(',')
phonemes.append("sil")
count_phone.append(1)
chars.append('[PAD]')
chars = "".join(chars)
char_embeds = None
if self.hasBert:
char_embeds = self.prosody.get_char_embeds(chars)
char_embeds = self.prosody.expand_for_phone(char_embeds, count_phone)
return " ".join(phonemes), char_embeds
def correct_pinyin_tone3(self, text):
pinyin_list = lazy_pinyin(text,
style=Style.TONE3,
strict=False,
neutral_tone_with_five=True,
tone_sandhi=True)
# , tone_sandhi=True -> 33变调
return pinyin_list