Skip to content

Instantly share code, notes, and snippets.

@ChicagoDev
Created May 18, 2023 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChicagoDev/1cc71d4a9121b9bdb3684e2831b8dfc2 to your computer and use it in GitHub Desktop.
Save ChicagoDev/1cc71d4a9121b9bdb3684e2831b8dfc2 to your computer and use it in GitHub Desktop.
import os
from google.cloud import texttospeech
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('ga-service-key.json')
def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
client = texttospeech.TextToSpeechClient(credentials=credentials)
input_text = texttospeech.SynthesisInput(text=text)
# # Note: the voice can also be specified by name.
# # Names of voices can be retrieved with client.list_voices().
voice = texttospeech.VoiceSelectionParams(
language_code="en-US",
name="en-US-Standard-C",
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE,
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = client.synthesize_speech(
request={"input": input_text,
"voice": voice,
"audio_config": audio_config}
)
# The response's audio_content is binary.
with open("output.mp3", "wb") as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
print(client.list_voices())
synthesize_text("Hello, my name is Will. I'm working on my computer science final with Flynn. Its worth 50% of my grade.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment