Class: YandexSpeechApi::Speaker

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

Overview

class << self

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Setters

#emotion=, #format=, #key=, #language=, #speed=, #voice=

Constructor Details

#initialize(settings = {}, &callback) {|_self| ... } ⇒ YandexSpeechApi::Speaker

Parameters:

  • callback (Proc)

    Used to set object attributes through do…end block.

Yields:

  • (_self)

Yield Parameters:



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/yandex_speech.rb', line 118

def initialize(settings = {}, &callback)
  yield self if block_given?

  self.key = settings[:key]

  self.voice    ||= settings[:voice]    || "jane"
  self.speed    ||= settings[:speed]    || 1.0
  self.emotion  ||= settings[:emotion]  || "good"
  self.language ||= settings[:language] || "english"
  self.format   ||= settings[:format]   || "mp3"
end

Instance Attribute Details

#emotionString (readonly)

How emotional dictor should speak.

Returns:

  • (String)

    Any from those: “good”, “evil”, “neutral”



88
89
90
# File 'lib/yandex_speech.rb', line 88

def emotion
  @emotion
end

#formatSymbol (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:

  • (Symbol)

    Any from those: “mp3”, “wav”, “opus”



111
112
113
# File 'lib/yandex_speech.rb', line 111

def format
  @format
end

#languageString (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 correct language for text)

Returns:

  • (String)

    Any from those: “russian”, “english”, “ukrain”, “turkey”



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

def language
  @language
end

#speedFloat (readonly)

Dictor speech speed.

Returns:

  • (Float)

    Any from those - (0.1)..3



72
73
74
# File 'lib/yandex_speech.rb', line 72

def speed
  @speed
end

#voiceSymbol (readonly)

Preferred dictor voice.

Returns:

  • (Symbol)

    Any from those: :jane, :oksana, :alyss, :omazh, :zahar, :ermil



80
81
82
# File 'lib/yandex_speech.rb', line 80

def voice
  @voice
end

Instance Method Details

#keyString, NilClass

Returns:

  • (String, NilClass)


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

def key
  YandexSpeechApi.key || @key
end

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

Saves synthesized voice to audio-file.

Parameters:

  • text (String)

    Something that should been said.

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

    (Dir.pwd)

Returns:

  • (String)

    Absolute path to created file.



158
159
160
161
162
163
164
165
166
# File 'lib/yandex_speech.rb', line 158

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

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

  return absolute_path
end

#say(text) ⇒ NilClass

Speaks the text

Parameters:

  • text (String)

    Something that should been said.

Returns:

  • (NilClass)

    You hear the sound.



139
140
141
142
143
144
145
# File 'lib/yandex_speech.rb', line 139

def say(text)
  self.format = :mp3
  row_data = request text
  Sounds.play row_data

  return nil
end