Class: YandexSpeechApi::Speaker

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

Overview

Note:

Before usage you should get your 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 your key to third party.

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

Examples:

Usage sample.


require 'yandex_speech'

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

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

Hash syntax


require 'yandex_speech'

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

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.


128
129
130
# File 'lib/yandex_speech.rb', line 128

def emotion
  @emotion
end

#formatFormat (readonly)

Note:

do not use :wav format for large texts. Result audio file will be

How remote server should decode audio data for us.

soo big and Yandex truncates those texts.


159
160
161
# File 'lib/yandex_speech.rb', line 159

def format
  @format
end

#languageLanguage (readonly)

Note:

speaker with russian language can't translate, or even synthesize

Speaker language.

english text (actually it can, but official documentation strongly recommend to select right language for a text)


144
145
146
# File 'lib/yandex_speech.rb', line 144

def language
  @language
end

#speedSpeed (readonly)

Determines dictor speech speed.


101
102
103
# File 'lib/yandex_speech.rb', line 101

def speed
  @speed
end

#voiceVoice (readonly)

Preferred dictor voice.

female voices: 'jane',  'oksana', 'alyss', 'omazh'
male voice:    'zahar', 'ermil'

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

def voice
  @voice
end

Class Method Details

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

Creates Speaker instance.

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: :russian

    Speaker#language for details.

  • :format (Symbol) — default: :mp3

    Speaker#format for details.

  • :key (Symbol) — default: :unknown

    Speaker#key for details.


62
63
64
# File 'lib/yandex_speech.rb', line 62

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

Instance Method Details

#save_to_file(text, filename = 'temporary') ⇒ String

Saves synthesized voice to file.


208
209
210
211
212
213
214
215
# File 'lib/yandex_speech.rb', line 208

def save_to_file(text, filename = 'temporary')
  binary_data = request text

  full_path = "#{File.expand_path(filename)}.#{format.type}"
  File.open(full_path, 'w') { |f| f.write binary_data }

  full_path.to_s
end

#say(text) ⇒ Object

Speeches text.

@return: You hear the voice. o_0


186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/yandex_speech.rb', line 186

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

  file = Tempfile.new ['yandex_speech_temp_file', '.mp3']
  file.write binary_data

  player = MP3_Player.init
  player.play file.path
ensure
  file.close
  file.unlink
end