Files
rpi-random-projects/voice-server/tts.py
T
2023-04-29 09:13:08 -07:00

26 lines
817 B
Python

import google.cloud.texttospeech as tts
OUTPUT_FILE = "output.wav"
# todo: make sure to make the file name unique and delete it after sending
def text_to_wav(voice_name: str, text: str):
language_code = "-".join(voice_name.split("-")[:2])
text_input = tts.SynthesisInput(text=text)
voice_params = tts.VoiceSelectionParams(
language_code=language_code, name=voice_name
)
audio_config = tts.AudioConfig(audio_encoding=tts.AudioEncoding.LINEAR16)
client = tts.TextToSpeechClient()
response = client.synthesize_speech(
input=text_input,
voice=voice_params,
audio_config=audio_config,
)
with open(OUTPUT_FILE, "wb") as out:
out.write(response.audio_content)
print(f'Generated speech saved to "{OUTPUT_FILE}"')
return OUTPUT_FILE