forked from westparkcom/Python-Bing-TTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBingTTSGen.py
344 lines (324 loc) · 10 KB
/
BingTTSGen.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import hashlib
import os
import sys
import argparse
import shutil
# Define a few maps for reference
namemap = {
"ar-EG" : ["Hoda"],
"ar-SA" : ["Naayf"],
"bg-BG" : ["Ivan"],
"ca-ES" : ["HerenaRUS"],
"cs-CZ" : ["Jakub"],
"da-DK" : ["HelleRUS"],
"de-AT" : ["Michael"],
"de-CH" : ["Karsten"],
"de-DE" : ["Hedda", "HeddaRUS", "Stefan, Apollo"],
"el-GR" : ["Stefanos"],
"en-AU" : ["Catherine", "HayleyRUS"],
"en-CA" : ["Linda", "HeatherRUS"],
"en-GB" : ["Susan, Apollo", "HazelRUS", "George, Apollo"],
"en-IE" : ["Sean"],
"en-IN" : ["Heera, Apollo", "PriyaRUS", "Ravi, Apollo"],
"en-US" : ["ZiraRUS", "JessaRUS", "BenjaminRUS"],
"es-ES" : ["Laura, Apollo", "HelenaRUS", "Pablo, Apollo"],
"es-MX" : ["HildaRUS", "Raul, Apollo"],
"fi-FI" : ["HeidiRUS"],
"fr-CA" : ["Caroline", "HarmonieRUS"],
"fr-CH" : ["Guillaume"],
"fr-FR" : ["Julie, Apollo", "HortenseRUS", "Paul, Apollo"],
"he-IL" : ["Asaf"],
"hi-IN" : ["Kalpana, Apollo", "Kalpana", "Hemant"],
"hr-HR" : ["Matej"],
"hu-HU" : ["Szabolcs"],
"id-ID" : ["Andika"],
"it-IT" : ["Cosimo, Apollo","LuciaRUS"],
"ja-JP" : ["Ayumi, Apollo", "Ichiro, Apollo", "HarukaRUS"],
"ko-KR" : ["HeamiRUS"],
"ms-MY" : ["Rizwan"],
"nb-NO" : ["HuldaRUS"],
"nl-NL" : ["HannaRUS"],
"pl-PL" : ["PaulinaRUS"],
"pt-BR" : ["HeloisaRUS", "Daniel, Apollo"],
"pt-PT" : ["HeliaRUS"],
"ro-RO" : ["Andrei"],
"ru-RU" : ["Irina, Apollo", "Pavel, Apollo","EkaterinaRUS"],
"sk-SK" : ["Filip"],
"sl-SI" : ["Lado"],
"sv-SE" : ["HedvigRUS"],
"ta-IN" : ["Valluvar"],
"th-TH" : ["Pattara"],
"tr-TR" : ["SedaRUS"],
"vi-VN" : ["An"],
"zh-CN" : ["HuihuiRUS", "Yaoyao, Apollo", "Kangkang, Apollo"],
"zh-HK" : ["Tracy, Apollo", "TracyRUS", "Danny, Apollo"],
"zh-TW" : ["Yating, Apollo", "HanHanRUS", "Zhiwei, Apollo"]
}
formatmap = {
"raw-8khz-8bit-mono-mulaw" : None,
"raw-16khz-16bit-mono-pcm" : None,
"riff-8khz-8bit-mono-mulaw" : None,
"riff-16khz-16bit-mono-pcm" : None
}
# Get config from CLI args
i = 0
argsDict = {}
for item in sys.argv:
if i == 0:
i = i + 1
pass
else:
i = i + 1
paramname, paramval = item.partition(
"="
)[::2]
argsDict[paramname] = paramval
try:
helpReq = argsDict['--help']
print ("")
print (
"Usage: {} --cache=/path/to/cache --dest=/path/to/destination --lang=en-US --voice=\"ZiraUS\" --fileformat=riff-8khz-8bit-mono-mulaw --apikey=YOUR-API-KEY --text=\"Hello World\"".format(
sys.argv[0]
)
)
print ("")
print ("--cache : Location of file cache. Useful if you repeatedly request")
print (" the same text to speech and wish to conserve bandwidth")
print (" and reduce Azure costs. Must be writeable.")
print ("--dest : Location for the output file. Must be writeable.")
print ("--lang : Language to be spoken. Case sensitive.")
print ("--voice : Voice to be used. Case sensitive.")
print ("--fileformat : Format of the output file.")
print ("--apikey : API key generated by Azure.")
print ("--text : Text to convert to speech. MUST be enclosed in double quotes.")
print ("")
print ("Available language combinations (--lang --voice):")
for item, value in namemap.items():
for voiceval in value:
print (
"Language: {} Voice: {}".format(
item,
voiceval
)
)
print ("")
print ("Available file formats:")
for item, value in formatmap.items():
print (item)
sys.exit(1)
except:
pass
try:
destLoc = argsDict['--dest']
except:
print ("")
print ("Error: destination location not specified.")
print ("")
print (
"Usage: {} --cache=/path/to/cache --dest=/path/to/destination --lang=en-US --voice=\"ZiraUS\" --fileformat=riff-8khz-8bit-mono-mulaw --apikey=YOUR-API-KEY --text=\"Hello World\"".format(
sys.argv[0]
)
)
print ("")
sys.exit(1)
try:
ttsLang = argsDict['--lang']
except:
print ("")
print ("Error: language not specified.")
print ("")
print (
"Usage: {} --cache=/path/to/cache --dest=/path/to/destination --lang=en-US --voice=\"ZiraUS\" --fileformat=riff-8khz-8bit-mono-mulaw --apikey=YOUR-API-KEY --text=\"Hello World\"".format(
sys.argv[0]
)
)
print ("")
sys.exit(1)
try:
ttsVoice = argsDict['--voice']
except:
print ("")
print ("Error: voice not specified.")
print ("")
print (
"Usage: {} --cache=/path/to/cache --dest=/path/to/destination --lang=en-US --voice=\"ZiraUS\" --fileformat=riff-8khz-8bit-mono-mulaw --apikey=YOUR-API-KEY --text=\"Hello World\"".format(
sys.argv[0]
)
)
print ("")
sys.exit(1)
try:
ttsFormat = argsDict['--fileformat']
except:
print ("")
print ("Error: file format not specified.")
print ("")
print (
"Usage: {} --cache=/path/to/cache --dest=/path/to/destination --lang=en-US --voice=\"ZiraUS\" --fileformat=riff-8khz-8bit-mono-mulaw --apikey=YOUR-API-KEY --text=\"Hello World\"".format(
sys.argv[0]
)
)
print ("")
sys.exit(1)
try:
ttsText = argsDict['--text']
except:
print ("")
print ("Error: text not specified.")
print ("")
print (
"Usage: {} --cache=/path/to/cache --dest=/path/to/destination --lang=en-US --voice=\"ZiraUS\" --fileformat=riff-8khz-8bit-mono-mulaw --apikey=YOUR-API-KEY --text=\"Hello World\"".format(
sys.argv[0]
)
)
print ("")
sys.exit(1)
try:
ttsApiKey = argsDict['--apikey']
except:
print ("")
print ("Error: API key not specified.")
print ("")
print (
"Usage: {} --cache=/path/to/cache --dest=/path/to/destination --lang=en-US --voice=\"ZiraUS\" --fileformat=riff-8khz-8bit-mono-mulaw --apikey=YOUR-API-KEY --text=\"Hello World\"".format(
sys.argv[0]
)
)
print ("")
sys.exit(1)
try:
ttsCache = argsDict['--cache']
except:
ttsCache = None
if ttsVoice not in namemap[ttsLang]:
print ("")
print ("Error: invalid language or Voice specified, or invalid combination: The following language/Voice combinations are available:")
print ("")
for item, value in namemap.items():
for voiceval in value:
print (
"Language: {}".format(
item
)
)
print (
"Language: {} Voice: {}".format(
item, voiceval
)
)
sys.exit(1)
try:
formatname = formatmap[ttsFormat]
except:
print ("")
print ("Error: invalid format specified: The following formats are available:")
print ("")
for item, value in formatmap.items():
print (item)
sys.exit(1)
if ttsCache:
cacheaccess = os.access(ttsCache, os.W_OK)
if not cacheaccess:
print ("Cannot write to cache location, ignoring --cache setting...")
ttsCache = None
m = hashlib.md5()
# Hash lang+Voice+text
m.update(
(
"{}-{}-{}".format(
ttsLang,
ttsVoice,
ttsText
)
).encode(
'utf-8'
)
)
# create filename base on MD5 hash
filename = "{}.wav".format(
m.hexdigest()
)
if ttsCache:
# If our file already exists, just return it so we don't have to do an API call...
if os.path.isfile(os.path.join(ttsCache, filename)):
try:
shutil.copy2(
os.path.join(
ttsCache,
filename
),
destLoc
)
sys.exit(0)
except (Exception) as e:
print (
"Could not copy cached file: {}".format(
e
)
)
print ("Exiting...")
sys.exit(1)
# Wait to import this until after we check cache to keep things as speedy as possible...
from bingtts import Translator
translator = Translator(ttsApiKey)
try:
data = translator.speak(
ttsText.encode(
'utf-8'
).decode(
'latin-1'
),
ttsLang,
ttsVoice,
ttsFormat
)
except (Exception) as e:
print (
"Error retrieving speech file: {}".format(
e
)
)
sys.exit(1)
if ttsCache:
try:
with open(destLoc, 'wb') as f:
f.write(data)
except (Exception) as e:
print (
"Couldn't write to file {}: {}".format(
destLoc,
e
)
)
sys.exit(1)
try:
with open(os.path.join(ttsCache, filename), 'w') as f:
f.write(data)
except (Exception) as e:
cacheFileLoc = os.path.join(
ttsCache,
filename
)
print (
"Couldn't write to file {}: {}".format(
cacheFileLoc,
e
)
)
sys.exit(1)
else:
try:
with open(destLoc, 'wb') as f:
f.write(data)
except (Exception) as e:
print (
"Couldn't write to file {}: {}".format(
destLoc,
e
)
)
sys.exit(1)
sys.exit(0)