forked from floere/james
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogue_frontend.rb
executable file
·108 lines (85 loc) · 2.3 KB
/
dialogue_frontend.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
60
61
62
63
64
65
66
67
68
69
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require 'osx/cocoa'
require 'main_dialogue'
# TODO move some stuff in the dialogue
# TODO extract cocoa connection
# TODO implement callback
# debug
USE_TEXTUAL_INTERFACE = false
class DialogueFrontend
attr_reader :dialogue
def initialize
# load voices
load_voices
# get a dialogue
@dialogue = MainDialogue.new(self)
# get and configure a recognizer interface
start_recognizer
# get a synthesizer interface
start_synthesizer
# default voice
male
end
# callback method from the speech interface
def speechRecognizer_didRecognizeCommand( sender, command )
command = command.to_s
# call the dialogue system
@dialogue.hear(command)
# set actual commands
self.commands = @dialogue.expects
end
# callback method from dialogue
def say(text)
@synthesizer.startSpeakingString(text)
end
# callback from dialogue
def male
self.voice = @male_voice
end
# callback from dialogue
def female
self.voice = @female_voice
end
# wrapper for the cocoa setCommands
def commands=(commands)
@recognizer.setCommands(commands)
puts "expects: #{commands.join(', ')}"
end
private
# specialized setter for voice
def voice=(voice)
@synthesizer.setVoice(voice)
end
# start recognizing words
def start_recognizer
@recognizer = OSX::NSSpeechRecognizer.alloc.init
@recognizer.setBlocksOtherRecognizers(true)
@recognizer.setListensInForegroundOnly(false)
@recognizer.setDelegate(self)
self.commands = @dialogue.expects
@recognizer.startListening
end
# start speaking
def start_synthesizer
@synthesizer = OSX::NSSpeechSynthesizer.alloc.init
end
# load voices from yaml
def load_voices
yaml_voices = ''
File.open('config/voices.yml') do |f| yaml_voices << f.read end
voices = YAML.load(yaml_voices)
@male_voice = voices['male']
@female_voice = voices['female']
end
end
controller = DialogueFrontend.new
# code to use a textual interface
while USE_TEXTUAL_INTERFACE
exit_words = ['quit','exit']
puts "'#{exit_words.join("' or '")}' to quit. Expects: #{controller.dialogue.expects.join(', ')}"
input = gets.chomp
if exit_words.include?(input)
break
end
controller.dialogue.hear(input)
end
OSX::NSApplication.sharedApplication.run