Module: Voice

Defined in:
lib/voice.rb

Constant Summary collapse

PLATFORM_IS_OSX =
(
  Object::RUBY_PLATFORM =~ /darwin/i ||
  Object::RUBY_PLATFORM =~ /java/i    # thanks jruby
)
DEFAULT_VOICE =
'Kathy'
NOVELTY_VOICES =
["Albert", "Bad News", "Bahh", "Bells",
  "Boing", "Bubbles", "Cellos", "Deranged", "Good News",
  "Hysterical", "Pipe Organ", "Trinoids", "Whisper", "Zarvox"
]
@@all =
nil
@@novelty =
nil

Class Method Summary collapse

Class Method Details

.all(opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/voice.rb', line 42

def self.all(opts={})
  return nil if @@all.nil?
  opts = {} unless opts.is_a?(Hash)

  if opts[:novelty] == 'only'
    @@novelty
  elsif opts[:novelty]
    @@all + @@novelty
  else
    @@all
  end
end

.defaultObject



55
56
57
58
59
# File 'lib/voice.rb', line 55

def self.default
  return nil if @@all.nil?
  return DEFAULT_VOICE if @@all.include?(DEFAULT_VOICE)
  return @@all.first
end

.detect_voicesObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/voice.rb', line 19

def detect_voices
  voices = []
  `say -v ?`.each_line do |l|
    # Each line resembles: Agnes               en_US    # Isn't it nice to have a computer that will talk to you?
    voice = l.split('#')[0]. # Grab everything before the comment marker
      strip.         # Drop the extra spaces at the end
      split(' ')     # Split into words
    voices << voice[0..-2].join(' ') # Drop the last word (locale), and join the rest with a single space
  end
  return voices
end

.get_rand(opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/voice.rb', line 61

def self.get_rand(opts={})
  return nil if @@all.nil?
  opts = {} unless opts.is_a?(Hash)

  voices = self.all(opts)

  voices[rand(voices.size)]
end

.say(text, opts = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/voice.rb', line 70

def self.say(text, opts={})
  opts = {} unless opts.is_a?(Hash)
  opts[:voice] = self.get_rand(opts) if opts[:random]
  opts[:voice] ||= self.default

  Say.say(text, opts)
  return nil
end