Class: Motion::Speech::Speaker

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/speech/speaker.rb

Constant Summary collapse

MultipleCallsToSpeakError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(speakable, options = {}, &block) ⇒ Speaker

Returns a new instance of Speaker.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/motion/speech/speaker.rb', line 12

def initialize(speakable, options={}, &block)
  @events = EventBlock.new
  @utterance = Utterance.new(speakable, options)
  @spoken = false

  if block_given?
    if block.arity == 0
      events.finish &block
    elsif block.arity == 1
      block.call events
    else
      raise ArgumentError, 'block must accept either 0 or 1 arguments'
    end
  end
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



4
5
6
# File 'lib/motion/speech/speaker.rb', line 4

def events
  @events
end

#utteranceObject (readonly)

Returns the value of attribute utterance.



4
5
6
# File 'lib/motion/speech/speaker.rb', line 4

def utterance
  @utterance
end

Class Method Details

.speak(*args, &block) ⇒ Object



8
9
10
# File 'lib/motion/speech/speaker.rb', line 8

def self.speak(*args, &block)
  new(*args, &block).speak
end

Instance Method Details

#messageObject



56
57
58
# File 'lib/motion/speech/speaker.rb', line 56

def message
  utterance.message
end

#pause(boundary) ⇒ Object



36
37
38
# File 'lib/motion/speech/speaker.rb', line 36

def pause(boundary)
  synthesizer.pauseSpeakingAtBoundary boundary_from_symbol(boundary)
end

#paused?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/motion/speech/speaker.rb', line 48

def paused?
  synthesizer.paused?
end

#resumeObject



44
45
46
# File 'lib/motion/speech/speaker.rb', line 44

def resume
  synthesizer.continueSpeaking
end

#speakObject



28
29
30
31
32
33
34
# File 'lib/motion/speech/speaker.rb', line 28

def speak
  raise MultipleCallsToSpeakError if @spoken

  synthesizer.speakUtterance utterance
  @spoken = true
  self
end

#speaking?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/motion/speech/speaker.rb', line 52

def speaking?
  synthesizer.speaking?
end

#stop(boundary) ⇒ Object



40
41
42
# File 'lib/motion/speech/speaker.rb', line 40

def stop(boundary)
  synthesizer.stopSpeakingAtBoundary boundary_from_symbol(boundary)
end

#synthesizerObject



60
61
62
# File 'lib/motion/speech/speaker.rb', line 60

def synthesizer
  @synthesizer ||= AVSpeechSynthesizer.new.tap { |s| s.delegate = self }
end