Class: Vocalware::Voice

Inherits:
Object
  • Object
show all
Defined in:
lib/vocalware/voice.rb

Overview

Voice encapsulates voice parameters like engine_id, voice_id, lang_id etc. And provides class method to look up voice by parameters.

Examples:

# Find a voice with name Juan. Exception will be raise if the voice with
# such name is not unique. As well as if the voice not found.
voice = Voice.find(:name => 'Juan')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Voice

Returns a new instance of Voice.

Parameters:

  • attributes (Hash)

    voice attributes



74
75
76
77
78
79
80
# File 'lib/vocalware/voice.rb', line 74

def initialize(attributes)
  attributes.each do |attr, value|
    self.instance_variable_set("@#{attr}", value)
  end
  @lang_id = LANGUAGES[lang.to_sym] ||
             raise(Error, "Unknown lang #{lang.inspect}")
end

Instance Attribute Details

#descriptionObject (readonly)



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

def description
  @description
end

#engine_idObject (readonly)



12
13
14
# File 'lib/vocalware/voice.rb', line 12

def engine_id
  @engine_id
end

#genderObject (readonly)



25
26
27
# File 'lib/vocalware/voice.rb', line 25

def gender
  @gender
end

#langObject (readonly)



15
16
17
# File 'lib/vocalware/voice.rb', line 15

def lang
  @lang
end

#lang_idObject (readonly)



31
32
33
# File 'lib/vocalware/voice.rb', line 31

def lang_id
  @lang_id
end

#nameObject (readonly)



18
19
20
# File 'lib/vocalware/voice.rb', line 18

def name
  @name
end

#voice_idObject (readonly)



22
23
24
# File 'lib/vocalware/voice.rb', line 22

def voice_id
  @voice_id
end

Class Method Details

.allArray<Vocalware::Voice>

Get all voices.

Returns:



36
37
38
# File 'lib/vocalware/voice.rb', line 36

def self.all
  @all ||= send(:load_all)
end

.find(attrs) ⇒ Object

Find a voice by attributes.

Parameters:

  • attrs (Hash)

    attributes to find a voice

Returns:

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vocalware/voice.rb', line 45

def self.find(attrs)
  voices = all.select {|voice| voice.match?(attrs) }

  raise(FindVoiceError,
        "No voice found using #{attrs.inspect}"
       ) if voices.empty?
  raise(FindVoiceError,
        "More than 1 voice found using #{attrs.inspect}"
       ) if voices.size > 1

  voices.first
end

Instance Method Details

#match?(attributes) ⇒ Boolean

Verify that the voice matches the passed attributes.

Parameters:

  • attributes (Hash)

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/vocalware/voice.rb', line 87

def match?(attributes)
  attributes.each do |attr, val|
    return false if send(attr).to_s != val.to_s
  end
  true
end