-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogletts.py
73 lines (61 loc) · 2.68 KB
/
googletts.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
import config
import logging
import AudioCache
import os
from pathlib import Path
from google.cloud import texttospeech
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = config.GOOGLE_KEYFILE_PATH
def energy_enum_to_speaking_rate(Energy):
energy_based_speaking_rate = 0.95
if Energy == Energy.EXHAUSTED:
energy_based_speaking_rate = 0.75
elif Energy == Energy.TIRED:
energy_based_speaking_rate = 0.85
elif Energy == Energy.NORMAL:
energy_based_speaking_rate = 0.95
elif Energy == Energy.ENERGIZED:
energy_based_speaking_rate = 1.10
elif Energy == Energy.HYPER:
energy_based_speaking_rate = 1.20
return energy_based_speaking_rate
def download_audio(Prompt, LangCode="de-DE", VoiceName="de-DE-Wavenet-E", Pitch=5.2, SprakingRate=1.0):
"""Download audio file from Google TTS API and cache it."""
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text=Prompt)
voice = texttospeech.VoiceSelectionParams(
language_code=LangCode, name=VoiceName
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.LINEAR16, pitch=Pitch, speaking_rate=SprakingRate
)
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
file_name = AudioCache.text_to_hash(Prompt) + ".wav"
file_path = Path(config.AUDIO_CACHE_FOLDER, file_name).resolve()
try:
with open(file_path, "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
logging.debug(f"FS - Saved audio file: {file_path}.")
except:
logging.error(f"FS - Error saving audio file: {file_path}.")
def download_audio_czech(Prompt, LangCode="cs-CZ", VoiceName="cs-CZ-Wavenet-A", Pitch=-18, SpeakingRate=0.85):
"""Download audio file from Google TTS API and cache it."""
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text=Prompt)
voice = texttospeech.VoiceSelectionParams(
language_code=LangCode, name=VoiceName
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.LINEAR16, pitch=Pitch, speaking_rate=SpeakingRate
)
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
file_name = AudioCache.text_to_hash(Prompt) + ".wav"
file_path = Path(config.AUDIO_CACHE_FOLDER, file_name).resolve()
with open(file_path, "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
logging.debug(f"FS - Saved audio file: {file_path}.")