Skip to content

Instantly share code, notes, and snippets.

@machisuji
Last active October 24, 2017 23:06
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 machisuji/e50bcef41b19c5433c24f1142e092d02 to your computer and use it in GitHub Desktop.
Save machisuji/e50bcef41b19c5433c24f1142e092d02 to your computer and use it in GitHub Desktop.
##
# Synthesizes audio for the given text.
#
# @param text [String] The text to synthesize audio for.
# @param output [String|IO] (optional) Path to file to write or IO object to stream to.
# @return [SynthesizeSpeechOutput] A struct containing `audio_stream` (IO) and `content_type` (String).
def say(text, language: nil, output: nil, ssml: false)
client.synthesize_speech(
response_target: output,
output_format: "ogg_vorbis",
sample_rate: "22050",
voice_id: language_voice_id(language),
text: text,
text_type: ssml ? 'ssml' : 'text'
)
end
##
# Polly client. Configured through the environment variables
# AWS_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
def client
@client ||= Aws::Polly::Client.new
end
def language_voice_id(lang)
case lang
when "de"
"Hans"
when "ru"
"Maxim"
when "en"
"Brian"
else
"Brian"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment