Skip to content

Instantly share code, notes, and snippets.

@egibney
Created May 25, 2020 12:51
Show Gist options
  • Save egibney/179bcf44dd5814a1541b067337005166 to your computer and use it in GitHub Desktop.
Save egibney/179bcf44dd5814a1541b067337005166 to your computer and use it in GitHub Desktop.
def convert_to_audio(text, gender)
client = Google::Cloud::TextToSpeech.text_to_speech
input_text = { text: text }
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices
# https://cloud.google.com/text-to-speech/docs/voices
if gender == 'MALE'
name = 'en-US-Standard-D'
else
name = 'en-US-Standard-E'
end
voice = {
language_code: "en-US",
name: name,
ssml_gender: gender
}
audio_config = { audio_encoding: "MP3" }
response = client.synthesize_speech(
input: input_text,
voice: voice,
audio_config: audio_config
)
# Need something here to catch the errors that Google sometimes sends, and then re-run the synthesize_speech function, possible after a short pause
return response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment