Class: Mac::Say
- Inherits:
-
Object
- Object
- Mac::Say
- Defined in:
- lib/mac/say.rb,
lib/mac/say/version.rb
Overview
A class wrapper around the MacOS say
command
Allows to use simple TTS on Mac right from Ruby scripts
Defined Under Namespace
Classes: CommandNotFound, FileNotFound, UnknownVoiceAttribute, VoiceNotFound
Constant Summary collapse
- VOICE_PATTERN =
A regex pattern to parse say voices list output
/(^[\w\s-]+)\s+([\w-]+)\s+#\s([\p{Graph}\p{Zs}]+$)/i
- VOICE_ATTRIBUTES =
The list of the voice attributes available
[ :name, :language, :country, :sample, :gender, :joke, :quality, :singing ]
- VERSION =
mac-say version
'0.2.2'
Instance Attribute Summary collapse
-
#config ⇒ Hash
readonly
Current config.
-
#voices ⇒ Array<Hash>
readonly
Current voices list.
Class Method Summary collapse
-
.say(string, voice = :alex) ⇒ Array<String, Integer>
Read the given string with the given voice.
-
.voice(attribute = nil, value = nil, &block) ⇒ Array<Hash>, ...
Look for voices by their attributes (e.g. :name, :language, :country, :gender, etc.).
-
.voices ⇒ Array<Hash>
Get all the voices supported by the say command on current machine.
Instance Method Summary collapse
-
#initialize(voice: :alex, rate: 175, file: nil, say_path: ENV['USE_FAKE_SAY'] || '/usr/bin/say') ⇒ Say
constructor
Say constructor: sets initial configuration for say command to use.
-
#say(string: nil, file: nil, voice: nil, rate: nil) ⇒ Array<String, Integer>
(also: #read)
Read the given string/file with the given voice and rate.
-
#voice(attribute = nil, value = nil, &block) ⇒ Array<Hash>, ...
Look for voices by their attributes (e.g. :name, :language, :country, :gender, etc.).
Constructor Details
#initialize(voice: :alex, rate: 175, file: nil, say_path: ENV['USE_FAKE_SAY'] || '/usr/bin/say') ⇒ Say
Say constructor: sets initial configuration for say command to use
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mac/say.rb', line 81 def initialize(voice: :alex, rate: 175, file: nil, say_path: ENV['USE_FAKE_SAY'] || '/usr/bin/say') @config = { say_path: say_path, voice: voice, rate: rate, file: file } @voices = nil load_voices raise VoiceNotFound, "Voice '#{voice}' isn't a valid voice" unless valid_voice? voice end |
Instance Attribute Details
#config ⇒ Hash (readonly)
Current config
71 72 73 |
# File 'lib/mac/say.rb', line 71 def config @config end |
#voices ⇒ Array<Hash> (readonly)
Current voices list
67 68 69 |
# File 'lib/mac/say.rb', line 67 def voices @voices end |
Class Method Details
.say(string, voice = :alex) ⇒ Array<String, Integer>
Read the given string with the given voice
105 106 107 108 |
# File 'lib/mac/say.rb', line 105 def self.say(string, voice = :alex) mac = new(voice: voice.downcase.to_sym) mac.say(string: string) end |
.voice(attribute, value) ⇒ Array<Hash>, ... .voice {|voice| ... } ⇒ Array<Hash>, ...
Look for voices by their attributes (e.g. :name, :language, :country, :gender, etc.)
167 168 169 170 171 172 173 174 175 |
# File 'lib/mac/say.rb', line 167 def self.voice(attribute = nil, value = nil, &block) mac = new if block_given? mac.voice(&block) else mac.voice(attribute, value) end end |
.voices ⇒ Array<Hash>
Get all the voices supported by the say command on current machine
239 240 241 242 |
# File 'lib/mac/say.rb', line 239 def self.voices mac = new mac.voices end |
Instance Method Details
#say(string: nil, file: nil, voice: nil, rate: nil) ⇒ Array<String, Integer> Also known as: read
Read the given string/file with the given voice and rate
Providing file, voice or rate arguments changes instance state and influence all the subsequent #say calls unless they have their own custom arguments
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mac/say.rb', line 131 def say(string: nil, file: nil, voice: nil, rate: nil) if voice raise VoiceNotFound, "Voice '#{voice}' isn't a valid voice" unless valid_voice?(voice) @config[:voice] = voice end if file raise FileNotFound, "File '#{file}' wasn't found or it's not readable by the current user" unless valid_file_path?(file) @config[:file] = file end @config[:rate] = rate if rate execute_command(string) end |
#voice(attribute, value) ⇒ Array<Hash>, ... #voice {|voice| ... } ⇒ Array<Hash>, ...
Look for voices by their attributes (e.g. :name, :language, :country, :gender, etc.)
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/mac/say.rb', line 197 def voice(attribute = nil, value = nil, &block) return unless (attribute && !value.nil?) || block_given? raise UnknownVoiceAttribute, "Voice has no '#{attribute}' attribute" if attribute && !VOICE_ATTRIBUTES.include?(attribute) if block_given? found_voices = @voices.find_all(&block) else found_voices = @voices.find_all {|voice| voice[attribute] === value } end return if found_voices.empty? found_voices.count == 1 ? found_voices.first : found_voices end |