forked from hamin-shim/ai-speaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
30 lines (24 loc) · 1.17 KB
/
api.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
from google.cloud import texttospeech
def main(text, save_path):
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text=text)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="ko-KR", ssml_gender=texttospeech.SsmlVoiceGender.FEMALE
# language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.LINEAR16)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# The response's audio_content is binary.
with open(save_path, "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print(f'Audio content written to file "{save_path}"')