Class: YandexSpeechApi::Speaker
- Inherits:
-
Object
- Object
- YandexSpeechApi::Speaker
- 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.
Defined Under Namespace
Classes: TextTooBig
Instance Attribute Summary collapse
-
#emotion ⇒ Emotion
readonly
How emotional dictor should speak.
-
#format ⇒ Format
readonly
How remote server should decode audio data for us.
-
#language ⇒ Language
readonly
Speaker language.
-
#speed ⇒ Speed
readonly
Determines dictor speech speed.
-
#voice ⇒ Voice
readonly
Preferred dictor voice.
Class Method Summary collapse
-
.init(settings = {}, &callback) ⇒ YandexSpeech
Creates
Speaker
instance.
Instance Method Summary collapse
-
#save_to_file(text, path_to_file = '') ⇒ String
Saves synthesized voice to audio-file.
-
#say(text) ⇒ NilClass
Speeches text.
-
#symbol ⇒ Object
Defines setters for @voice, @language,+@format+, @emotion, @speed, @key attributes.
Instance Attribute Details
#emotion ⇒ Emotion (readonly)
How emotional dictor should speak.
104 105 106 |
# File 'lib/yandex_speech.rb', line 104 def emotion @emotion end |
#format ⇒ Format (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.
125 126 127 |
# File 'lib/yandex_speech.rb', line 125 def format @format end |
#language ⇒ Language (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)
115 116 117 |
# File 'lib/yandex_speech.rb', line 115 def language @language end |
#speed ⇒ Speed (readonly)
Determines dictor speech speed.
90 91 92 |
# File 'lib/yandex_speech.rb', line 90 def speed @speed end |
#voice ⇒ Voice (readonly)
Preferred dictor voice.
97 98 99 |
# File 'lib/yandex_speech.rb', line 97 def voice @voice end |
Class Method Details
.init(settings = {}, &callback) ⇒ YandexSpeech
Creates Speaker
instance.
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’
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.(path_to_file)}.#{format.type}" File.open(absolute_path, 'w') { |f| f.write binary_data } return absolute_path end |
#say(text) ⇒ NilClass
Speeches text.
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 |
#symbol ⇒ Object
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 |