Class: Acapela::Voice

Inherits:
Object
  • Object
show all
Defined in:
lib/acapela/voice.rb,
lib/acapela/constants.rb

Constant Summary collapse

QUALITY_LOW =
'8k'
QUALITY_HIGH =
'22k'
GENDER_FEMALE =
:female
GENDER_MALE =
:male

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(speaker, gender, *languages) ⇒ Voice

Returns a new instance of Voice.



7
8
9
10
11
12
13
14
# File 'lib/acapela/voice.rb', line 7

def initialize(speaker, gender, *languages)
  @speaker = speaker
  @gender = gender
  @quality = QUALITY_HIGH

  @languages = Set.new
  languages.each { |lang| @languages.add(lang) }
end

Instance Attribute Details

#genderObject (readonly)

Returns the value of attribute gender.



5
6
7
# File 'lib/acapela/voice.rb', line 5

def gender
  @gender
end

#languagesObject (readonly)

Returns the value of attribute languages.



5
6
7
# File 'lib/acapela/voice.rb', line 5

def languages
  @languages
end

#qualityObject (readonly)

Returns the value of attribute quality.



5
6
7
# File 'lib/acapela/voice.rb', line 5

def quality
  @quality
end

#speakerObject (readonly)

Returns the value of attribute speaker.



5
6
7
# File 'lib/acapela/voice.rb', line 5

def speaker
  @speaker
end

Class Method Details

.extract_from_options(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/acapela/voice.rb', line 70

def self.extract_from_options(options = {})
  if speaker = options[:speaker]
    named_voice(speaker)
  else
    language = options[:language] || :en
    gender = options[:gender]
    speakers_for_language = self.map[language]

    speakers = case gender
    when GENDER_FEMALE
      speakers_for_language[GENDER_FEMALE]
    when GENDER_MALE
      speakers_for_language[GENDER_MALE]
    else
      speakers_for_language[GENDER_FEMALE] +
        speakers_for_language[GENDER_MALE]
    end

    if speakers.empty?
      nil
    else
      named_voice(speakers.sample)
    end
  end
end

.mapObject



61
62
63
64
# File 'lib/acapela/voice.rb', line 61

def self.map
  reset_map unless defined?(@@map)
  @@map
end

.named_voice(speaker) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/acapela/voice.rb', line 96

def self.named_voice(speaker)
  unless defined?(@@voices)
    @@voices = Hash.new
    self.map.each do |language, voices|
      [GENDER_MALE, GENDER_FEMALE].each do |gender|
        voices[gender].each do |name|
          downcase_name = name.downcase.to_sym
          if @@voices[downcase_name].nil?
            @@voices[downcase_name] = Voice.new(name, gender, language.to_s)
          else
            @@voices[downcase_name].languages.add(language.to_s)
          end
        end
      end
    end
  end

  if info = @@voices[speaker.to_s.downcase.to_sym]
    info
  else
    raise Error.new("Voice does not exist.")
  end
end

.override_map(override_voice_map = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/acapela/voice.rb', line 32

def self.override_map(override_voice_map = {})
  current_map = self.map
  override_voice_map.each do |language, voices|
    language_sym = language.to_sym
    if voices.nil?
      # Remove voice directive.
      current_map.delete(language_sym)
    elsif voices.is_a?(String) || voices.is_a?(Symbol)
      # Map voice directive.
      current_map[language_sym] = self.map[voices]
    elsif voices.is_a?(Hash)
      # Replace/Override
      if entry = self.map[language_sym]
        new_entry = entry.dup
      else
        new_entry = Hash.new
      end

      voices.each do |gender, speakers|
        if gender.is_a?(Symbol) && speakers.is_a?(Array)
          new_entry[gender] = speakers
        end
      end
      current_map[language_sym] = new_entry
    end
  end
  @@map = current_map
end

.reset_mapObject



66
67
68
# File 'lib/acapela/voice.rb', line 66

def self.reset_map
  @@map = Voices::PER_LANGUAGE.dup
end

Instance Method Details

#high_quality!Object



28
29
30
# File 'lib/acapela/voice.rb', line 28

def high_quality!
  @quality = QUALITY_HIGH
end

#idObject



16
17
18
# File 'lib/acapela/voice.rb', line 16

def id
  "#{speaker.downcase}#{quality}"
end

#languageObject



20
21
22
# File 'lib/acapela/voice.rb', line 20

def language
  languages.first
end

#low_quality!Object



24
25
26
# File 'lib/acapela/voice.rb', line 24

def low_quality!
  @quality = QUALITY_LOW
end