Class: YandexSpeechApi::Speaker

Inherits:
Object
  • Object
show all
Defined in:
lib/yandex_speech.rb,
lib/yandex_speech/speaker.rb

Overview

Speaker a class that convert English, Ukrain, Russian or Turkey text to speech. Solution based on Yandex SpeechKit technology.

Before usage you need to get an api key. It is free for non-commercial purposes (at least for now).

You can get this key from official site: tech.yandex.ru/speechkit

Note: Yandex provide many services throw this site. Your need exactly Yandex SpeechKit Cloud key. Other keys will not work!

Do not share this key to third party.

Examples:

Basic usage

key = File.open('secret key/key').readline.strip

speaker = YandexSpeechApi::Speaker.init key: key, language: 'russian'
speaker.save_to_file "Не будите спящего кота."

Hash syntax

key = File.open('secret key/key').readline.strip
message = "Don't trouble trouble until trouble troubles you"

speaker = YandexSpeechApi::Speaker.init({ key: key, language: 'english', voice: :zahar, speed: 0.23 })
speaker.say message

Block syntax

key = File.open('secret key/key').readline.strip
message = "one two three. one two three. one two three four."

speaker = YandexSpeechApi::Speaker.init do |s|
  s.key      = key
  s.voice    = :jane
  s.language = :english
  s.speed    = :slow
  s.emotion  = :good
end
speaker.say message

Defined Under Namespace

Classes: TextTooBig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#emotionEmotion (readonly)

How emotional dictor should speak.

Returns:



104
105
106
# File 'lib/yandex_speech.rb', line 104

def emotion
  @emotion
end

#formatFormat (readonly)

How remote server should decode audio data for us.

Do not use +:wav+ format for large texts. Result audio file will be
too big, and service truncates resulted file.

Returns:



125
126
127
# File 'lib/yandex_speech.rb', line 125

def format
  @format
end

#languageLanguage (readonly)

Speaker language.

Speaker with +russian+ language can't translate, or even synthesize
+english+ text (actually it can, but official documentation strongly
recommend to select right language for incoming text)

Returns:



115
116
117
# File 'lib/yandex_speech.rb', line 115

def language
  @language
end

#speedSpeed (readonly)

Determines dictor speech speed.

Returns:



90
91
92
# File 'lib/yandex_speech.rb', line 90

def speed
  @speed
end

#voiceVoice (readonly)

Preferred dictor voice.

Returns:



97
98
99
# File 'lib/yandex_speech.rb', line 97

def voice
  @voice
end

Class Method Details

.init(settings = {}, &callback) ⇒ YandexSpeech

Creates Speaker instance.

Parameters:

  • callback (Proc)

    Used to set object state throw … end block.

  • settings (HASH) (defaults to: {})

    Overrides default settings.

Options Hash (settings):

  • :speed (Symbol) — default: :standard

    @see Speaker#speed for details.

  • :voice (Symbol) — default: :alyss

    @see Speaker#voice for details.

  • :emotion (Symbol) — default: :neutral

    @see Speaker#emotion for details.

  • :language (Symbol) — default: :english

    @see Speaker#language for details.

  • :format (Symbol) — default: :mp3

    @see Speaker#format for details.

  • :key (Symbol) — default: :unknown

    @see Speaker#key for details.

Returns:

  • (YandexSpeech)


78
79
80
# File 'lib/yandex_speech.rb', line 78

def init(settings = {}, &callback)
  new settings, &callback
end

Instance Method Details

#save_to_file(text, path_to_file = '') ⇒ String

Saves synthesized voice to audio-file.

If path_to_file is empty it saves audio-file to ‘~/downloads’

Examples:

#save_to_file usage

key = File.open('secret key/key').readline.strip

speaker = YandexSpeechApi::Speaker.init key: key, language: 'russian'
speaker.save_to_file "Не будите спящего кота.", 'let_cat_sleep'

Parameters:

  • text (String)

    Something that should been said.

  • path_to_file (String) (defaults to: '')

    (‘~/downloads’) Path to new file (without file extension).

Returns:

  • (String)

    Absolute path to created file.



231
232
233
234
235
236
237
238
239
# File 'lib/yandex_speech.rb', line 231

def save_to_file(text, path_to_file = '')
  path_to_file = generate_path if path_to_file.empty?

  binary_data = request text
  absolute_path = "#{File.expand_path(path_to_file)}.#{format.type}"
  File.open(absolute_path, 'w') { |f| f.write binary_data }

  return absolute_path
end

#say(text) ⇒ NilClass

Speeches text.

Examples:

#Say usage

key = File.open('secret key/key').readline.strip
message = "one two three. one two three. one two three four."

speaker = YandexSpeechApi::Speaker.init do |s|
  s.key      = key
  s.voice    = :jane
  s.language = :english
  s.speed    = :slow
  s.emotion  = :good
end
speaker.say message

Parameters:

  • text (String)

    Something that should been said.

Returns:

  • (NilClass)

    You hear the sound.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/yandex_speech.rb', line 196

def say(text)
  format = Format.new :mp3
  binary_data = request text, format: format

  file = Tempfile.new ['yandex_speech_temp_file', ".#{format.type}"]
  file.write binary_data

  player = MP3_Player.init
  player.play file.path

  file.close(true) if file

  return nil
end

#symbolObject

Defines setters for @voice, @language,+@format+, @emotion, @speed, @key attributes.



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/yandex_speech.rb', line 160

%i(voice language format emotion speed key).each do |symbol|
  define_method "#{symbol}=" do |other|
    method_name = __method__.to_s.chop
    klass = YandexSpeechApi.const_get method_name.capitalize
    variable =
      if other.is_a? klass
        other
      else
        klass.new(other)
      end
    instance_variable_set "@#{method_name}", variable
  end
end