-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextToSpeech.py
41 lines (30 loc) · 1.15 KB
/
textToSpeech.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
import os
from google.cloud import texttospeech
import asyncio
credential_path = "creds.json"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credential_path
def tts(lang, rawText):
client = texttospeech.TextToSpeechClient()
text_input = texttospeech.SynthesisInput(text=rawText)
voice = texttospeech.VoiceSelectionParams(
language_code=lang,
name=f"{lang}-Wavenet-A",
ssml_gender=texttospeech.SsmlVoiceGender.MALE,
)
if voice is None:
voice = texttospeech.VoiceSelectionParams(
language_code=lang,
name=f"{lang}-Standard-A",
ssml_gender=texttospeech.SsmlVoiceGender.MALE,
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = client.synthesize_speech(
input=text_input, voice=voice, audio_config=audio_config
)
# The response's audio_content is binary.
with open("output.mp3", "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')