-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtts_utils.py
431 lines (375 loc) · 13.7 KB
/
tts_utils.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import logging
import io
import os.path
import sys
import time
from pathlib import Path
import pyttsx3
from gtts import gTTS
from tts_wrapper import (
AbstractTTS,
MicrosoftClient,
MicrosoftTTS,
GoogleClient,
GoogleTTS,
SAPIClient,
SAPITTS,
SherpaOnnxClient,
SherpaOnnxTTS,
GoogleTransTTS,
GoogleTransClient,
)
import warnings
from threading import Thread
from configure_enc_utils import load_config, load_credentials
warnings.filterwarnings("ignore", category=RuntimeWarning)
utils = None
voices = None
ready = True
# Global dictionary to store TTS clients
tts_voiceid = {}
VALID_STYLES = [
"advertisement_upbeat",
"affectionate",
"angry",
"assistant",
"calm",
"chat",
"cheerful",
"customerservice",
"depressed",
"disgruntled",
"documentary-narration",
"embarrassed",
"empathetic",
"envious",
"excited",
"fearful",
"friendly",
"gentle",
"hopeful",
"lyrical",
"narration-professional",
"narration-relaxed",
"newscast",
"newscast-casual",
"newscast-formal",
"poetry-reading",
"sad",
"serious",
"shouting",
"sports_commentary",
"sports_commentary_excited",
"whispering",
"terrified",
"unfriendly",
]
def init(module):
"""Initialize utils module making it in memory instead of one time instance.
Args:
module: Instance of utils module.
Returns: None
"""
global utils
utils = module
def init_azure_tts():
"""Initialize unique instance of MicrosoftTTS based on the changes in voiceid.
Returns: MicrosoftTTS
"""
key = utils.config.get("azureTTS", "key")
config = load_config()
if key == "":
key = config.get("azureTTS")["key"]
location = utils.config.get("azureTTS", "location")
if location == "":
location = config.get("azureTTS")["location"]
voiceid = utils.config.get("azureTTS", "voiceid")
parts = voiceid.split("-")
lang = parts[0] + "-" + parts[1]
client = MicrosoftClient((key, location))
return MicrosoftTTS(client=client, voice=voiceid, lang=lang)
def init_google_tts():
"""Initialize unique instance of GoogleTTS based on the changes in voiceid.
Returns: GoogleTTS
"""
gcreds = utils.config.get("googleTTS", "creds")
path = Path(gcreds)
if not path.exists():
path = Path("google_creds.enc")
file_format = path.suffix
if file_format == ".enc":
gcreds = load_credentials(path)
logging.info(f"Google TTS credentials file location: {path}")
logging.info(f"Google TTS credentials file location: {path}")
if not isinstance(gcreds, dict) and os.path.isfile(gcreds):
logging.info(f"Google TTS credentials file: {gcreds}")
voiceid = utils.config.get("googleTTS", "voiceid")
client = GoogleClient(credentials=gcreds)
tts = GoogleTTS(client=client, voice=voiceid)
return tts
def init_sapi_tts():
"""Initialize unique instance of SAPITTS based on the changes in voiceid.
Returns: SAPITTS
"""
voiceid = utils.config.get("sapi5TTS", "voiceid")
client = SAPIClient()
client._client.setProperty("voice", voiceid)
client._client.setProperty("rate", utils.config.get("TTS", "rate"))
client._client.setProperty("volume", utils.config.get("TTS", "volume"))
return SAPITTS(client=client)
def init_onnx_tts():
"""Initialize unique instance of SherpaOnnxTTS based on the changes in voiceid.
Returns: SherpaOnnxTTS
"""
voiceid = utils.config.get("SherpaOnnxTTS", "voiceid")
if getattr(sys, "frozen", False):
home_directory = os.path.expanduser("~")
onnx_cache_path = os.path.join(
home_directory,
"AppData",
"Roaming",
"Ace Centre",
"AACSpeakHelper",
"models",
)
elif __file__:
app_data_path = os.path.abspath(os.path.dirname(__file__))
onnx_cache_path = os.path.join(app_data_path, "models")
if not os.path.isdir(onnx_cache_path):
os.mkdir(onnx_cache_path)
client = SherpaOnnxClient(model_path=onnx_cache_path, tokens_path=None)
tts = SherpaOnnxTTS(client)
tts.set_voice(voice_id=voiceid)
return tts
def init_googleTrans_tts():
"""Initialize unique instance of GoogleTransTTS based on the changes in voiceid.
Returns: GoogleTransTTS
"""
voiceid = utils.config.get("googleTransTTS", "voiceid")
client = GoogleTransClient(voiceid)
return GoogleTransTTS(client)
def speak(text="", list_voices=False):
"""Speak function convert text parameter to speech. This function decides which TTS Engine will be used
base on the config file received.
Then, it will call the specific function that will create the TTS Engine Instance.
Args:
text (str): String to be spoken by specific TTS Engine.
list_voices (bool): Use to return all available voices only instead of speech function .
Returns: None
"""
global voices
global ready
ready = False
try:
ttsengine = utils.config.get("TTS", "engine")
voice_id = utils.config.get(ttsengine, "voiceid")
if not voice_id:
voice_id = utils.config.get("TTS", "voiceid")
except Exception as e:
logging.error(f"Error getting TTS engine or voice ID: {e}")
return
finally:
ready = True
try:
file = utils.check_history(text)
if file is not None and os.path.isfile(file):
if list_voices:
tts_client = tts_voiceid[ttsengine][voice_id]
voices = tts_client.get_voices()
return
else:
voices = None
utils.play_audio(file, file=True)
logging.info(f"Speech synthesized for text [{text}] from cache.")
ready = True # Make sure to update ready after cache playback
return
except Exception as e:
logging.error(f"Error checking history or playing audio: {e}")
ready = True # In case of error, ensure ready is set back
return
logging.info(f"Speech synthesized for text [{text}].")
try:
if ttsengine in tts_voiceid and voice_id in tts_voiceid[ttsengine]:
tts_client = tts_voiceid[ttsengine][voice_id]
else:
match ttsengine:
case "azureTTS":
tts_client = init_azure_tts()
case "googleTTS":
tts_client = init_google_tts()
case "sapi5":
tts_client = init_sapi_tts()
case "SherpaOnnxTTS":
tts_client = init_onnx_tts()
case "googleTransTTS":
tts_client = init_googleTrans_tts()
case _:
tts_client = pyttsx3.init(ttsengine)
except Exception as e:
logging.error(f"Error initializing TTS client: {e}")
return
try:
if ttsengine not in tts_voiceid:
tts_voiceid[ttsengine] = {voice_id: tts_client}
else:
if voice_id not in tts_voiceid[ttsengine]:
tts_voiceid[ttsengine][voice_id] = tts_client
except Exception as e:
logging.error(f"Error storing TTS client: {e}")
return
if list_voices:
try:
voices = tts_client.get_voices()
return
except Exception as e:
logging.error(f"Error getting voices: {e}")
return
else:
voices = None
try:
match ttsengine:
case "azureTTS":
if utils.args["style"]:
azureSpeak(
text,
ttsengine,
tts_client,
utils.args["style"],
utils.args["styledegree"],
)
else:
azureSpeak(text, ttsengine, tts_client)
case "googleTTS":
googleSpeak(text, ttsengine, tts_client)
case "sapi5":
sapiSpeak(text, ttsengine, tts_client)
case "SherpaOnnxTTS":
onnxSpeak(text, ttsengine, tts_client)
case "googleTransTTS":
googleTransSpeak(text, ttsengine, tts_client)
case _:
tts_client.setProperty("voice", utils.config.get("TTS", "voiceid"))
tts_client.setProperty("rate", utils.config.get("TTS", "rate"))
tts_client.setProperty("volume", utils.config.get("TTS", "volume"))
tts_client.say(text)
tts_client.runAndWait()
except Exception as e:
logging.error(f"Error during TTS processing: {e}")
def onnxSpeak(text: str, engine, tts_client):
"""This function received the input parameters and make necessary modification (if needed). Then, those parameter
will be pass to ttsWrapperSpeak.
Args:
text (str): String to be spoken by the TTS Engine.
engine (str): Name of the TTS Engine.
tts_client: Instance of TTS Engine.
Returns: None
"""
ttsWrapperSpeak(text, tts_client, engine)
def azureSpeak(
text: str, engine, tts_client, style: str = None, styledegree: float = None
):
"""This function received the input parameters and make necessary modification (if needed). Then, those parameter
will be pass to ttsWrapperSpeak.
Args:
text (str): String to be spoken by the TTS Engine.
engine (str): Name of the TTS Engine.
tts_client: Instance of TTS Engine.
style (str): Set the SSML style format and wrap the text string.
styledegree (float): Set the SSML style degree format and wrap the text string.
Returns: None
"""
if style:
# Check if the provided style is in the valid styles array
if style in VALID_STYLES:
# Construct SSML with the specified style
ssml = f'<mstts:express-as style="{style}"'
if styledegree:
ssml += f' styledegree="{styledegree}"'
ssml += f">{text}</mstts:express-as>"
else:
# Style is not valid, use default SSML without style
ssml = text
else:
# Use default SSML without style
ssml = text
ttsWrapperSpeak(ssml, tts_client, engine)
def googleSpeak(text: str, engine, tts_client):
"""This function received the input parameters and make necessary modification (if needed). Then, those parameter
will be pass to ttsWrapperSpeak.
Args:
text (str): String to be spoken by the TTS Engine.
engine (str): Name of the TTS Engine.
tts_client: Instance of TTS Engine.
Returns: None
"""
ttsWrapperSpeak(text, tts_client, engine)
def googleTransSpeak(text: str, engine, tts_client):
"""This function received the input parameters and make necessary modification (if needed). Then, those parameter
will be pass to ttsWrapperSpeak.
Args:
text (str): String to be spoken by the TTS Engine.
engine (str): Name of the TTS Engine.
tts_client: Instance of TTS Engine.
Returns: None
"""
ttsWrapperSpeak(text, tts_client, engine)
def sapiSpeak(text: str, engine, tts_client):
"""This function received the input parameters and make necessary modification (if needed). Then, those parameter
will be pass to ttsWrapperSpeak.
Args:
text (str): String to be spoken by the TTS Engine.
engine (str): Name of the TTS Engine.
tts_client: Instance of TTS Engine.
Returns: None
"""
ttsWrapperSpeak(text, tts_client, engine)
def ttsWrapperSpeak(text: str, tts, engine):
"""This function identifies the TTS Instance and set format of the text and audio format.
Then, create a Thread that synthesize text to audio.
Args:
text (str): String to be spoken by the TTS Engine.
tts: Instance of TTS Engine.
engine (str): Name of the TTS Engine.
Returns: None
"""
fmt = "wav"
match tts:
case SherpaOnnxTTS():
pass
case GoogleTransTTS():
fmt = "mp3"
case AbstractTTS():
tts.ssml.clear_ssml()
text = tts.ssml.add(text)
try:
playText = Thread(target=playSpeech, args=(text, engine, fmt, tts))
playText.start()
except Exception as e:
print(e)
def playSpeech(text, engine, file_format, tts):
"""This function is run by a Thread which synthesize text to audio.
While audio is streaming, the audio is also saving in parallel.
Args:
text (str): String to be spoken by the TTS Engine.
engine (str): Name of the TTS Engine.
file_format (str): Audio Format.
tts: Instance of TTS Engine.
Returns: None
"""
global ready
ready = False # Start in an unready state
start = time.perf_counter()
try:
save_audio_file = utils.config.getboolean("TTS", "save_audio_file")
if save_audio_file:
utils.save_audio(text=text, engine=engine, file_format=file_format, tts=tts)
logging.info(f"Speech synthesized for text [{text}] saved in cache.")
else:
tts.speak_streamed(text)
except Exception as e:
logging.error(f"Error during TTS processing: {e}")
return
finally:
stop = time.perf_counter() - start
logging.info(f"Speech synthesis runtime is {stop:0.5f} seconds.")
ready = True # Ensure ready is set to True even after an exception