alysa commited on
Commit
5402140
1 Parent(s): dc8bed4

Upload vits_pinyin.py

Browse files
Files changed (1) hide show
  1. vits_pinyin.py +88 -0
vits_pinyin.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ from pypinyin import Style
4
+ from pypinyin.contrib.neutral_tone import NeutralToneWith5Mixin
5
+ from pypinyin.converter import DefaultConverter
6
+ from pypinyin.core import Pinyin
7
+
8
+ from text import pinyin_dict
9
+ from bert import TTSProsody
10
+
11
+
12
+ class MyConverter(NeutralToneWith5Mixin, DefaultConverter):
13
+ pass
14
+
15
+
16
+ def is_chinese(uchar):
17
+ if uchar >= u'\u4e00' and uchar <= u'\u9fa5':
18
+ return True
19
+ else:
20
+ return False
21
+
22
+
23
+ def clean_chinese(text: str):
24
+ text = text.strip()
25
+ text_clean = []
26
+ for char in text:
27
+ if (is_chinese(char)):
28
+ text_clean.append(char)
29
+ else:
30
+ if len(text_clean) > 1 and is_chinese(text_clean[-1]):
31
+ text_clean.append(',')
32
+ text_clean = ''.join(text_clean).strip(',')
33
+ return text_clean
34
+
35
+
36
+ class VITS_PinYin:
37
+ def __init__(self, bert_path, device):
38
+ self.pinyin_parser = Pinyin(MyConverter())
39
+ self.prosody = TTSProsody(bert_path, device)
40
+
41
+ def get_phoneme4pinyin(self, pinyins):
42
+ result = []
43
+ count_phone = []
44
+ for pinyin in pinyins:
45
+ if pinyin[:-1] in pinyin_dict:
46
+ tone = pinyin[-1]
47
+ a = pinyin[:-1]
48
+ a1, a2 = pinyin_dict[a]
49
+ result += [a1, a2 + tone]
50
+ count_phone.append(2)
51
+ return result, count_phone
52
+
53
+ def chinese_to_phonemes(self, text):
54
+ text = clean_chinese(text)
55
+ phonemes = ["sil"]
56
+ chars = ['[PAD]']
57
+ count_phone = []
58
+ count_phone.append(1)
59
+ for subtext in text.split(","):
60
+ if (len(subtext) == 0):
61
+ continue
62
+ pinyins = self.correct_pinyin_tone3(subtext)
63
+ sub_p, sub_c = self.get_phoneme4pinyin(pinyins)
64
+ phonemes.extend(sub_p)
65
+ phonemes.append("sp")
66
+ count_phone.extend(sub_c)
67
+ count_phone.append(1)
68
+ chars.append(subtext)
69
+ chars.append(',')
70
+ phonemes.append("sil")
71
+ count_phone.append(1)
72
+ chars.append('[PAD]')
73
+ chars = "".join(chars)
74
+ char_embeds = self.prosody.get_char_embeds(chars)
75
+ char_embeds = self.prosody.expand_for_phone(char_embeds, count_phone)
76
+ return " ".join(phonemes), char_embeds
77
+
78
+ def correct_pinyin_tone3(self, text):
79
+ pinyin_list = [p[0] for p in self.pinyin_parser.pinyin(
80
+ text, style=Style.TONE3, strict=False, neutral_tone_with_five=True)]
81
+ if len(pinyin_list) >= 2:
82
+ for i in range(1, len(pinyin_list)):
83
+ try:
84
+ if re.findall(r'\d', pinyin_list[i-1])[0] == '3' and re.findall(r'\d', pinyin_list[i])[0] == '3':
85
+ pinyin_list[i-1] = pinyin_list[i-1].replace('3', '2')
86
+ except IndexError:
87
+ pass
88
+ return pinyin_list