Skip to content

Instantly share code, notes, and snippets.

@furkanacaryes
Created January 6, 2021 14:49
Show Gist options
  • Save furkanacaryes/255fe356caf4c7d69bb7e992f2cd2565 to your computer and use it in GitHub Desktop.
Save furkanacaryes/255fe356caf4c7d69bb7e992f2cd2565 to your computer and use it in GitHub Desktop.
Google Speech
"""Synthesizes speech from the input string of text or ssml.
Note: ssml must be well-formed according to:
https://www.w3.org/TR/speech-synthesis/
"""
from google.cloud import texttospeech
from playsound import playsound
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-account.json"
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
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.MP3
)
# 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("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"')
playsound("output.mp3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment