Skip to content

Instantly share code, notes, and snippets.

@machisuji
Last active October 24, 2017 23:06
Show Gist options
  • Save machisuji/e50bcef41b19c5433c24f1142e092d02 to your computer and use it in GitHub Desktop.
Save machisuji/e50bcef41b19c5433c24f1142e092d02 to your computer and use it in GitHub Desktop.

Revisions

  1. machisuji revised this gist Oct 24, 2017. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions polly.rb
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,6 @@
    # @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)
    raise aws_not_configured_message unless aws_configured?

    client.synthesize_speech(
    response_target: output,
    output_format: "ogg_vorbis",
    @@ -15,8 +13,6 @@ def say(text, language: nil, output: nil, ssml: false)
    text: text,
    text_type: ssml ? 'ssml' : 'text'
    )
    rescue Aws::Polly::Errors::InvalidSsmlException => e
    nil
    end

    ##
  2. machisuji created this gist Oct 24, 2017.
    40 changes: 40 additions & 0 deletions polly.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    ##
    # 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)
    raise aws_not_configured_message unless aws_configured?

    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'
    )
    rescue Aws::Polly::Errors::InvalidSsmlException => e
    nil
    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