-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext2speech.py
More file actions
165 lines (136 loc) · 4.85 KB
/
text2speech.py
File metadata and controls
165 lines (136 loc) · 4.85 KB
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
import os
import time
from tqdm import tqdm
def get_filepaths(directory):
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
if filename.endswith('.wav'):
filename = filename.replace(".wav","")
file_paths.append(filename)
return file_paths
def tts_gtts(input_path, save_info=True, convert_format=True):
from gtts import gTTS
if convert_format:
import librosa
from scipy.io import wavfile
sen_list = open(input_path).read().splitlines()
if save_info:
if os.path.exists('tts_info_gtts.txt'):
f_out = open('tts_info_gtts.txt','a')
else:
f_out = open('tts_info_gtts.txt','w')
outputdir = os.path.join(input_path.replace(".txt", '_gtts'))
if not os.path.exists(outputdir):
os.mkdir(outputdir)
exist_file_list = []
else:
exist_file_list = get_filepaths(outputdir)
for line in tqdm(sen_list):
index = line.split("#")[0]
sen = line.split("#")[1]
if index in exist_file_list:
continue
else:
outputpath = os.path.join(outputdir,index+'.wav')
tts = gTTS(sen, lang='zh-TW')
tts.save(outputpath)
#convert output waveform to other format
if convert_format:
y, sr = librosa.load(outputpath, sr=16000)
wavfile.write(outputpath, sr, y)
time.sleep(1.5)
else:
time.sleep(2)
if save_info:
f_out.write(index+"#"+sen+"\n")
def tts_paddle(input_path, save_info=True):
import paddle
from paddlespeech.cli import TTSExecutor
tts_executor = TTSExecutor()
sen_list = open(input_path).read().splitlines()
if save_info:
if os.path.exists('tts_info_paddle.txt'):
f_out = open('tts_info_paddle.txt','a')
else:
f_out = open('tts_info_paddle.txt','w')
outputdir = os.path.join(input_path.replace(".txt", '_paddle'))
if not os.path.exists(outputdir):
os.mkdir(outputdir)
exist_file_list = []
else:
exist_file_list = get_filepaths(outputdir)
for line in tqdm(sen_list):
index = line.split("#")[0]
sen = line.split("#")[1]
if index in exist_file_list:
continue
else:
outputpath = os.path.join(outputdir,index+'.wav')
wav_file = tts_executor(
text=sen,
output=outputpath,
am='fastspeech2_aishell3',
am_config=None,
am_ckpt=None,
am_stat=None,
phones_dict=None,
tones_dict=None,
speaker_dict=None,
voc='pwgan_aishell3',
voc_config=None,
voc_ckpt=None,
voc_stat=None,
lang='zh',
device=paddle.get_device())
if save_info:
f_out.write(index+"#"+sen+"\n")
def tts_ttskit(input_path, save_info=True):
from ttskit import sdk_api
sen_list = open(input_path).read().splitlines()
if save_info:
if os.path.exists('tts_info_ttskit.txt'):
f_out = open('tts_info_ttskit.txt','a')
else:
f_out = open('tts_info_ttskit.txt','w')
outputdir = os.path.join(input_path.replace(".txt", '_ttskit'))
if not os.path.exists(outputdir):
os.mkdir(outputdir)
exist_file_list = []
else:
exist_file_list = get_filepaths(outputdir)
for line in tqdm(sen_list):
index = line.split("#")[0]
sen = line.split("#")[1]
if index in exist_file_list:
continue
else:
outputpath = os.path.join(outputdir,index+'.wav')
sdk_api.tts_sdk(sen,output=outputpath)
if save_info:
f_out.write(index+"#"+sen+"\n")
def tts_zhtts(input_path, save_info=True):
import zhtts
tts = zhtts.TTS(text2mel_name="TACOTRON")
sen_list = open(input_path).read().splitlines()
if save_info:
if os.path.exists('tts_info_zhtts.txt'):
f_out = open('tts_info_zhtts.txt','a')
else:
f_out = open('tts_info_zhtts.txt','w')
outputdir = os.path.join(input_path.replace(".txt", '_zhtts'))
if not os.path.exists(outputdir):
os.mkdir(outputdir)
exist_file_list = []
else:
exist_file_list = get_filepaths(outputdir)
for line in tqdm(sen_list):
index = line.split("#")[0]
sen = line.split("#")[1]
if index in exist_file_list:
continue
else:
outputpath = os.path.join(outputdir,index+'.wav')
tts.text2wav(sen, outputpath)
if save_info:
f_out.write(index+"#"+sen+"\n")