|
@@ -5,16 +5,16 @@
|
5
|
5
|
|
6
|
6
|
import sys
|
7
|
7
|
import signal
|
8
|
|
-import gobject
|
|
8
|
+from gi.repository import GObject
|
9
|
9
|
import os.path
|
10
|
10
|
import subprocess
|
11
|
11
|
from optparse import OptionParser
|
12
|
12
|
try:
|
13
|
|
- import yaml
|
|
13
|
+ import yaml
|
14
|
14
|
except:
|
15
|
|
- print "YAML is not supported. ~/.config/blather/options.yaml will not function"
|
|
15
|
+ print "YAML is not supported. ~/.config/blather/options.yaml will not function"
|
16
|
16
|
|
17
|
|
-#where are the files?
|
|
17
|
+# Where are the files?
|
18
|
18
|
conf_dir = os.path.expanduser("~/.config/blather")
|
19
|
19
|
lang_dir = os.path.join(conf_dir, "language")
|
20
|
20
|
command_file = os.path.join(conf_dir, "commands.conf")
|
|
@@ -23,230 +23,230 @@ history_file = os.path.join(conf_dir, "blather.history")
|
23
|
23
|
opt_file = os.path.join(conf_dir, "options.yaml")
|
24
|
24
|
lang_file = os.path.join(lang_dir,'lm')
|
25
|
25
|
dic_file = os.path.join(lang_dir,'dic')
|
26
|
|
-#make the lang_dir if it doesn't exist
|
|
26
|
+# Make the lang_dir if it doesn't exist
|
27
|
27
|
if not os.path.exists(lang_dir):
|
28
|
|
- os.makedirs(lang_dir)
|
|
28
|
+ os.makedirs(lang_dir)
|
29
|
29
|
|
30
|
30
|
class Blather:
|
31
|
|
- def __init__(self, opts):
|
32
|
|
-
|
33
|
|
- #import the recognizer so Gst doesn't clobber our -h
|
34
|
|
- from Recognizer import Recognizer
|
35
|
|
- self.ui = None
|
36
|
|
- self.options = {}
|
37
|
|
- ui_continuous_listen = False
|
38
|
|
- self.continuous_listen = False
|
39
|
|
-
|
40
|
|
- self.commands = {}
|
41
|
|
-
|
42
|
|
- #read the commands
|
43
|
|
- self.read_commands()
|
44
|
|
-
|
45
|
|
- #load the options file
|
46
|
|
- self.load_options()
|
47
|
|
-
|
48
|
|
- #merge the opts
|
49
|
|
- for k,v in opts.__dict__.items():
|
50
|
|
- if (not k in self.options) or opts.override:
|
51
|
|
- self.options[k] = v
|
52
|
|
-
|
53
|
|
- if self.options['interface'] != None:
|
54
|
|
- if self.options['interface'] == "q":
|
55
|
|
- from QtUI import UI
|
56
|
|
- elif self.options['interface'] == "g":
|
57
|
|
- from GtkUI import UI
|
58
|
|
- elif self.options['interface'] == "gt":
|
59
|
|
- from GtkTrayUI import UI
|
60
|
|
- else:
|
61
|
|
- print "no GUI defined"
|
62
|
|
- sys.exit()
|
63
|
|
-
|
64
|
|
- self.ui = UI(args, self.options['continuous'])
|
65
|
|
- self.ui.connect("command", self.process_command)
|
66
|
|
- #can we load the icon resource?
|
67
|
|
- icon = self.load_resource("icon.png")
|
68
|
|
- if icon:
|
69
|
|
- self.ui.set_icon_active_asset(icon)
|
70
|
|
- #can we load the icon_inactive resource?
|
71
|
|
- icon_inactive = self.load_resource("icon_inactive.png")
|
72
|
|
- if icon_inactive:
|
73
|
|
- self.ui.set_icon_inactive_asset(icon_inactive)
|
74
|
|
-
|
75
|
|
- if self.options['history']:
|
76
|
|
- self.history = []
|
77
|
|
-
|
78
|
|
- #create the recognizer
|
79
|
|
- try:
|
80
|
|
- self.recognizer = Recognizer(lang_file, dic_file, self.options['microphone'] )
|
81
|
|
- except Exception, e:
|
82
|
|
- #no recognizer? bummer
|
83
|
|
- sys.exit()
|
84
|
|
-
|
85
|
|
- self.recognizer.connect('finished',self.recognizer_finished)
|
86
|
|
-
|
87
|
|
- print "Using Options: ", self.options
|
88
|
|
-
|
89
|
|
- def read_commands(self):
|
90
|
|
- #read the.commands file
|
91
|
|
- file_lines = open(command_file)
|
92
|
|
- strings = open(strings_file, "w")
|
93
|
|
- for line in file_lines:
|
94
|
|
- print line
|
95
|
|
- #trim the white spaces
|
96
|
|
- line = line.strip()
|
97
|
|
- #if the line has length and the first char isn't a hash
|
98
|
|
- if len(line) and line[0]!="#":
|
99
|
|
- #this is a parsible line
|
100
|
|
- (key,value) = line.split(":",1)
|
101
|
|
- print key, value
|
102
|
|
- self.commands[key.strip().lower()] = value.strip()
|
103
|
|
- strings.write( key.strip()+"\n")
|
104
|
|
- #close the strings file
|
105
|
|
- strings.close()
|
106
|
|
-
|
107
|
|
- def load_options(self):
|
108
|
|
- #is there an opt file?
|
109
|
|
- try:
|
110
|
|
- opt_fh = open(opt_file)
|
111
|
|
- text = opt_fh.read()
|
112
|
|
- self.options = yaml.load(text)
|
113
|
|
- except:
|
114
|
|
- pass
|
115
|
|
-
|
116
|
|
-
|
117
|
|
- def log_history(self,text):
|
118
|
|
- if self.options['history']:
|
119
|
|
- self.history.append(text)
|
120
|
|
- if len(self.history) > self.options['history']:
|
121
|
|
- #pop off the first item
|
122
|
|
- self.history.pop(0)
|
123
|
|
-
|
124
|
|
- #open and truncate the blather history file
|
125
|
|
- hfile = open(history_file, "w")
|
126
|
|
- for line in self.history:
|
127
|
|
- hfile.write( line+"\n")
|
128
|
|
- #close the file
|
129
|
|
- hfile.close()
|
130
|
|
-
|
131
|
|
- # Print the cmd and then run the command
|
132
|
|
- def run_command(self, cmd):
|
133
|
|
- print cmd
|
134
|
|
- subprocess.call(cmd, shell=True)
|
135
|
|
-
|
136
|
|
- def recognizer_finished(self, recognizer, text):
|
137
|
|
- t = text.lower()
|
138
|
|
- #is there a matching command?
|
139
|
|
- if self.commands.has_key( t ):
|
140
|
|
- #run the valid_sentence_command if there is a valid sentence command
|
141
|
|
- if self.options['valid_sentence_command']:
|
142
|
|
- subprocess.call(self.options['valid_sentence_command'], shell=True)
|
143
|
|
- cmd = self.commands[t]
|
144
|
|
- #should we be passing words?
|
145
|
|
- if self.options['pass_words']:
|
146
|
|
- cmd+=" "+t
|
147
|
|
- self.run_command(cmd)
|
148
|
|
- else:
|
149
|
|
- self.run_command(cmd)
|
150
|
|
- self.log_history(text)
|
151
|
|
- else:
|
152
|
|
- #run the invalid_sentence_command if there is a valid sentence command
|
153
|
|
- if self.options['invalid_sentence_command']:
|
154
|
|
- subprocess.call(self.options['invalid_sentence_command'], shell=True)
|
155
|
|
- print "no matching command %s" %(t)
|
156
|
|
- #if there is a UI and we are not continuous listen
|
157
|
|
- if self.ui:
|
158
|
|
- if not self.continuous_listen:
|
159
|
|
- #stop listening
|
160
|
|
- self.recognizer.pause()
|
161
|
|
- #let the UI know that there is a finish
|
162
|
|
- self.ui.finished(t)
|
163
|
|
-
|
164
|
|
- def run(self):
|
165
|
|
- if self.ui:
|
166
|
|
- self.ui.run()
|
167
|
|
- else:
|
168
|
|
- blather.recognizer.listen()
|
169
|
|
-
|
170
|
|
- def quit(self):
|
171
|
|
- sys.exit()
|
172
|
|
-
|
173
|
|
- def process_command(self, UI, command):
|
174
|
|
- print command
|
175
|
|
- if command == "listen":
|
176
|
|
- self.recognizer.listen()
|
177
|
|
- elif command == "stop":
|
178
|
|
- self.recognizer.pause()
|
179
|
|
- elif command == "continuous_listen":
|
180
|
|
- self.continuous_listen = True
|
181
|
|
- self.recognizer.listen()
|
182
|
|
- elif command == "continuous_stop":
|
183
|
|
- self.continuous_listen = False
|
184
|
|
- self.recognizer.pause()
|
185
|
|
- elif command == "quit":
|
186
|
|
- self.quit()
|
187
|
|
-
|
188
|
|
- def load_resource(self,string):
|
189
|
|
- local_data = os.path.join(os.path.dirname(__file__), 'data')
|
190
|
|
- paths = ["/usr/share/blather/","/usr/local/share/blather", local_data]
|
191
|
|
- for path in paths:
|
192
|
|
- resource = os.path.join(path, string)
|
193
|
|
- if os.path.exists( resource ):
|
194
|
|
- return resource
|
195
|
|
- #if we get this far, no resource was found
|
196
|
|
- return False
|
|
31
|
+
|
|
32
|
+ def __init__(self, opts):
|
|
33
|
+ # Import the recognizer so Gst doesn't clobber our -h
|
|
34
|
+ from Recognizer import Recognizer
|
|
35
|
+ self.ui = None
|
|
36
|
+ self.options = {}
|
|
37
|
+ ui_continuous_listen = False
|
|
38
|
+ self.continuous_listen = False
|
|
39
|
+
|
|
40
|
+ self.commands = {}
|
|
41
|
+
|
|
42
|
+ # Read the commands
|
|
43
|
+ self.read_commands()
|
|
44
|
+
|
|
45
|
+ # Load the options file
|
|
46
|
+ self.load_options()
|
|
47
|
+
|
|
48
|
+ # Merge the opts
|
|
49
|
+ for k,v in opts.__dict__.items():
|
|
50
|
+ if (not k in self.options) or opts.override:
|
|
51
|
+ self.options[k] = v
|
|
52
|
+
|
|
53
|
+ if self.options['interface'] != None:
|
|
54
|
+ if self.options['interface'] == "q":
|
|
55
|
+ from QtUI import UI
|
|
56
|
+ elif self.options['interface'] == "g":
|
|
57
|
+ from GtkUI import UI
|
|
58
|
+ elif self.options['interface'] == "gt":
|
|
59
|
+ from GtkTrayUI import UI
|
|
60
|
+ else:
|
|
61
|
+ print "no GUI defined"
|
|
62
|
+ sys.exit()
|
|
63
|
+
|
|
64
|
+ self.ui = UI(args, self.options['continuous'])
|
|
65
|
+ self.ui.connect("command", self.process_command)
|
|
66
|
+ #can we load the icon resource?
|
|
67
|
+ icon = self.load_resource("icon.png")
|
|
68
|
+ if icon:
|
|
69
|
+ self.ui.set_icon_active_asset(icon)
|
|
70
|
+ #can we load the icon_inactive resource?
|
|
71
|
+ icon_inactive = self.load_resource("icon_inactive.png")
|
|
72
|
+ if icon_inactive:
|
|
73
|
+ self.ui.set_icon_inactive_asset(icon_inactive)
|
|
74
|
+
|
|
75
|
+ if self.options['history']:
|
|
76
|
+ self.history = []
|
|
77
|
+
|
|
78
|
+ # Create the recognizer
|
|
79
|
+ try:
|
|
80
|
+ self.recognizer = Recognizer(lang_file, dic_file, self.options['microphone'])
|
|
81
|
+ except Exception, e:
|
|
82
|
+ #no recognizer? bummer
|
|
83
|
+ print 'error making recognizer'
|
|
84
|
+ sys.exit()
|
|
85
|
+
|
|
86
|
+ self.recognizer.connect('finished', self.recognizer_finished)
|
|
87
|
+
|
|
88
|
+ print "Using Options: ", self.options
|
|
89
|
+
|
|
90
|
+ def read_commands(self):
|
|
91
|
+ # Read the commands file
|
|
92
|
+ file_lines = open(command_file)
|
|
93
|
+ strings = open(strings_file, "w")
|
|
94
|
+ for line in file_lines:
|
|
95
|
+ print line
|
|
96
|
+ # Trim the white spaces
|
|
97
|
+ line = line.strip()
|
|
98
|
+ # If the line has length and the first char isn't a hash
|
|
99
|
+ if len(line) and line[0]!="#":
|
|
100
|
+ # This is a parsible line
|
|
101
|
+ (key,value) = line.split(":",1)
|
|
102
|
+ print key, value
|
|
103
|
+ self.commands[key.strip().lower()] = value.strip()
|
|
104
|
+ strings.write( key.strip()+"\n")
|
|
105
|
+ # Close the strings file
|
|
106
|
+ strings.close()
|
|
107
|
+
|
|
108
|
+ def load_options(self):
|
|
109
|
+ # Is there an opt file?
|
|
110
|
+ try:
|
|
111
|
+ opt_fh = open(opt_file)
|
|
112
|
+ text = opt_fh.read()
|
|
113
|
+ self.options = yaml.load(text)
|
|
114
|
+ except:
|
|
115
|
+ pass
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+ def log_history(self,text):
|
|
119
|
+ if self.options['history']:
|
|
120
|
+ self.history.append(text)
|
|
121
|
+ if len(self.history) > self.options['history']:
|
|
122
|
+ # Pop off the first item
|
|
123
|
+ self.history.pop(0)
|
|
124
|
+
|
|
125
|
+ # Open and truncate the blather history file
|
|
126
|
+ hfile = open(history_file, "w")
|
|
127
|
+ for line in self.history:
|
|
128
|
+ hfile.write( line+"\n")
|
|
129
|
+ # Close the file
|
|
130
|
+ hfile.close()
|
|
131
|
+
|
|
132
|
+ def run_command(self, cmd):
|
|
133
|
+ '''Print the command, then run it'''
|
|
134
|
+ print cmd
|
|
135
|
+ subprocess.call(cmd, shell=True)
|
|
136
|
+
|
|
137
|
+ def recognizer_finished(self, recognizer, text):
|
|
138
|
+ t = text.lower()
|
|
139
|
+ # Is there a matching command?
|
|
140
|
+ if self.commands.has_key( t ):
|
|
141
|
+ # Run the valid_sentence_command if there is a valid sentence command
|
|
142
|
+ if self.options['valid_sentence_command']:
|
|
143
|
+ subprocess.call(self.options['valid_sentence_command'], shell=True)
|
|
144
|
+ cmd = self.commands[t]
|
|
145
|
+ # Should we be passing words?
|
|
146
|
+ if self.options['pass_words']:
|
|
147
|
+ cmd += " " + t
|
|
148
|
+ self.run_command(cmd)
|
|
149
|
+ else:
|
|
150
|
+ self.run_command(cmd)
|
|
151
|
+ self.log_history(text)
|
|
152
|
+ else:
|
|
153
|
+ # Run the invalid_sentence_command if there is an invalid sentence command
|
|
154
|
+ if self.options['invalid_sentence_command']:
|
|
155
|
+ subprocess.call(self.options['invalid_sentence_command'], shell=True)
|
|
156
|
+ print "no matching command %s" % t
|
|
157
|
+ # If there is a UI and we are not continuous listen
|
|
158
|
+ if self.ui:
|
|
159
|
+ if not self.continuous_listen:
|
|
160
|
+ # Stop listening
|
|
161
|
+ self.recognizer.pause()
|
|
162
|
+ # Let the UI know that there is a finish
|
|
163
|
+ self.ui.finished(t)
|
|
164
|
+
|
|
165
|
+ def run(self):
|
|
166
|
+ if self.ui:
|
|
167
|
+ self.ui.run()
|
|
168
|
+ else:
|
|
169
|
+ blather.recognizer.listen()
|
|
170
|
+
|
|
171
|
+ def quit(self):
|
|
172
|
+ sys.exit()
|
|
173
|
+
|
|
174
|
+ def process_command(self, UI, command):
|
|
175
|
+ print command
|
|
176
|
+ if command == "listen":
|
|
177
|
+ self.recognizer.listen()
|
|
178
|
+ elif command == "stop":
|
|
179
|
+ self.recognizer.pause()
|
|
180
|
+ elif command == "continuous_listen":
|
|
181
|
+ self.continuous_listen = True
|
|
182
|
+ self.recognizer.listen()
|
|
183
|
+ elif command == "continuous_stop":
|
|
184
|
+ self.continuous_listen = False
|
|
185
|
+ self.recognizer.pause()
|
|
186
|
+ elif command == "quit":
|
|
187
|
+ self.quit()
|
|
188
|
+
|
|
189
|
+ def load_resource(self,string):
|
|
190
|
+ local_data = os.path.join(os.path.dirname(__file__), 'data')
|
|
191
|
+ paths = ["/usr/share/blather/","/usr/local/share/blather", local_data]
|
|
192
|
+ for path in paths:
|
|
193
|
+ resource = os.path.join(path, string)
|
|
194
|
+ if os.path.exists( resource ):
|
|
195
|
+ return resource
|
|
196
|
+ # If we get this far, no resource was found
|
|
197
|
+ return False
|
197
|
198
|
|
198
|
199
|
|
199
|
200
|
if __name__ == "__main__":
|
200
|
|
- parser = OptionParser()
|
201
|
|
- parser.add_option("-i", "--interface", type="string", dest="interface",
|
202
|
|
- action='store',
|
203
|
|
- help="Interface to use (if any). 'q' for Qt, 'g' for GTK, 'gt' for GTK system tray icon")
|
204
|
|
-
|
205
|
|
- parser.add_option("-c", "--continuous",
|
206
|
|
- action="store_true", dest="continuous", default=False,
|
207
|
|
- help="starts interface with 'continuous' listen enabled")
|
208
|
|
-
|
209
|
|
- parser.add_option("-p", "--pass-words",
|
210
|
|
- action="store_true", dest="pass_words", default=False,
|
211
|
|
- help="passes the recognized words as arguments to the shell command")
|
212
|
|
-
|
213
|
|
- parser.add_option("-o", "--override",
|
214
|
|
- action="store_true", dest="override", default=False,
|
215
|
|
- help="override config file with command line options")
|
216
|
|
-
|
217
|
|
- parser.add_option("-H", "--history", type="int",
|
218
|
|
- action="store", dest="history",
|
219
|
|
- help="number of commands to store in history file")
|
220
|
|
-
|
221
|
|
- parser.add_option("-m", "--microphone", type="int",
|
222
|
|
- action="store", dest="microphone", default=None,
|
223
|
|
- help="Audio input card to use (if other than system default)")
|
224
|
|
-
|
225
|
|
- parser.add_option("--valid-sentence-command", type="string", dest="valid_sentence_command",
|
226
|
|
- action='store',
|
227
|
|
- help="command to run when a valid sentence is detected")
|
228
|
|
-
|
229
|
|
- parser.add_option( "--invalid-sentence-command", type="string", dest="invalid_sentence_command",
|
230
|
|
- action='store',
|
231
|
|
- help="command to run when an invalid sentence is detected")
|
232
|
|
-
|
233
|
|
- (options, args) = parser.parse_args()
|
234
|
|
- #make our blather object
|
235
|
|
- blather = Blather(options)
|
236
|
|
- #init gobject threads
|
237
|
|
- gobject.threads_init()
|
238
|
|
- #we want a main loop
|
239
|
|
- main_loop = gobject.MainLoop()
|
240
|
|
- #handle sigint
|
241
|
|
- signal.signal(signal.SIGINT, signal.SIG_DFL)
|
242
|
|
- #run the blather
|
243
|
|
- blather.run()
|
244
|
|
- #start the main loop
|
245
|
|
-
|
246
|
|
- try:
|
247
|
|
- main_loop.run()
|
248
|
|
- except:
|
249
|
|
- print "time to quit"
|
250
|
|
- main_loop.quit()
|
251
|
|
- sys.exit()
|
|
201
|
+ parser = OptionParser()
|
|
202
|
+ parser.add_option("-i", "--interface", type="string", dest="interface",
|
|
203
|
+ action='store',
|
|
204
|
+ help="Interface to use (if any). 'q' for Qt, 'g' for GTK, 'gt' for GTK system tray icon")
|
|
205
|
+
|
|
206
|
+ parser.add_option("-c", "--continuous",
|
|
207
|
+ action="store_true", dest="continuous", default=False,
|
|
208
|
+ help="starts interface with 'continuous' listen enabled")
|
|
209
|
+
|
|
210
|
+ parser.add_option("-p", "--pass-words",
|
|
211
|
+ action="store_true", dest="pass_words", default=False,
|
|
212
|
+ help="passes the recognized words as arguments to the shell command")
|
|
213
|
+
|
|
214
|
+ parser.add_option("-o", "--override",
|
|
215
|
+ action="store_true", dest="override", default=False,
|
|
216
|
+ help="override config file with command line options")
|
|
217
|
+
|
|
218
|
+ parser.add_option("-H", "--history", type="int",
|
|
219
|
+ action="store", dest="history",
|
|
220
|
+ help="number of commands to store in history file")
|
|
221
|
+
|
|
222
|
+ parser.add_option("-m", "--microphone", type="int",
|
|
223
|
+ action="store", dest="microphone", default=None,
|
|
224
|
+ help="Audio input card to use (if other than system default)")
|
|
225
|
+
|
|
226
|
+ parser.add_option("--valid-sentence-command", type="string", dest="valid_sentence_command",
|
|
227
|
+ action='store',
|
|
228
|
+ help="command to run when a valid sentence is detected")
|
|
229
|
+
|
|
230
|
+ parser.add_option( "--invalid-sentence-command", type="string", dest="invalid_sentence_command",
|
|
231
|
+ action='store',
|
|
232
|
+ help="command to run when an invalid sentence is detected")
|
|
233
|
+
|
|
234
|
+ (options, args) = parser.parse_args()
|
|
235
|
+ # Make our blather object
|
|
236
|
+ blather = Blather(options)
|
|
237
|
+ # Init gobject threads
|
|
238
|
+ GObject.threads_init()
|
|
239
|
+ # We want a main loop
|
|
240
|
+ main_loop = GObject.MainLoop()
|
|
241
|
+ # Handle sigint
|
|
242
|
+ signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
243
|
+ # Run the blather
|
|
244
|
+ blather.run()
|
|
245
|
+ # Start the main loop
|
|
246
|
+ try:
|
|
247
|
+ main_loop.run()
|
|
248
|
+ except:
|
|
249
|
+ print "time to quit"
|
|
250
|
+ main_loop.quit()
|
|
251
|
+ sys.exit()
|
252
|
252
|
|