|
@@ -0,0 +1,85 @@
|
|
1
|
+#!/usr/bin/env python2
|
|
2
|
+import gst
|
|
3
|
+import subprocess
|
|
4
|
+import os.path
|
|
5
|
+import time
|
|
6
|
+import gobject
|
|
7
|
+
|
|
8
|
+#define some global variables
|
|
9
|
+this_dir = os.path.dirname( os.path.abspath(__file__) )
|
|
10
|
+lang_dir = os.path.join(this_dir, "language")
|
|
11
|
+command_file = os.path.join(this_dir, "commands")
|
|
12
|
+strings_file = os.path.join(this_dir, "sentences.corpus")
|
|
13
|
+
|
|
14
|
+class TTS(gobject.GObject):
|
|
15
|
+ __gsignals__ = {
|
|
16
|
+ 'finished' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_BOOLEAN,))
|
|
17
|
+ }
|
|
18
|
+ def __init__(self):
|
|
19
|
+ gobject.GObject.__init__(self)
|
|
20
|
+ self.commands = {}
|
|
21
|
+ #build the pipeline
|
|
22
|
+ cmd = 'autoaudiosrc ! audioconvert ! audioresample ! vader name=vad ! pocketsphinx name=asr ! appsink sync=false'
|
|
23
|
+ self.pipeline=gst.parse_launch( cmd )
|
|
24
|
+ #get the Auto Speech Recognition piece
|
|
25
|
+ asr=self.pipeline.get_by_name('asr')
|
|
26
|
+ asr.connect('result', self.result)
|
|
27
|
+ asr.set_property('lm', os.path.join(lang_dir, 'lm'))
|
|
28
|
+ asr.set_property('dict', os.path.join(lang_dir, 'dic'))
|
|
29
|
+ asr.set_property('configured', True)
|
|
30
|
+ #get the Voice Activity DEtectoR
|
|
31
|
+ self.vad = self.pipeline.get_by_name('vad')
|
|
32
|
+ self.vad.set_property('auto-threshold',True)
|
|
33
|
+ self.read_commands()
|
|
34
|
+ #init gobject threads
|
|
35
|
+ gobject.threads_init()
|
|
36
|
+
|
|
37
|
+ def listen(self):
|
|
38
|
+ self.pipeline.set_state(gst.STATE_PLAYING)
|
|
39
|
+
|
|
40
|
+ def pause(self):
|
|
41
|
+ self.vad.set_property('silent', True)
|
|
42
|
+ self.pipeline.set_state(gst.STATE_PAUSED)
|
|
43
|
+
|
|
44
|
+ def result(self, asr, text, uttid):
|
|
45
|
+ self.emit("finished", True)
|
|
46
|
+ print text
|
|
47
|
+ #is there a matching command?
|
|
48
|
+ if self.commands.has_key( text ):
|
|
49
|
+ cmd = self.commands[text]
|
|
50
|
+ print cmd
|
|
51
|
+ subprocess.call(cmd, shell=True)
|
|
52
|
+ else:
|
|
53
|
+ print "no matching command"
|
|
54
|
+ #emit finished
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+ def read_commands(self):
|
|
58
|
+ #read the.commands file
|
|
59
|
+ file_lines = open(command_file)
|
|
60
|
+ strings = open(strings_file, "w")
|
|
61
|
+ for line in file_lines:
|
|
62
|
+ #trim the white spaces
|
|
63
|
+ line = line.strip()
|
|
64
|
+ #if the line has length and the first char isn't a hash
|
|
65
|
+ if len(line) and line[0]!="#":
|
|
66
|
+ #this is a parsible line
|
|
67
|
+ (key,value) = line.split(":",1)
|
|
68
|
+ print key, value
|
|
69
|
+ self.commands[key.strip()] = value.strip()
|
|
70
|
+ strings.write( key.strip()+"\n")
|
|
71
|
+ #close the strings file
|
|
72
|
+ strings.close()
|
|
73
|
+
|
|
74
|
+if __name__ == "__main__":
|
|
75
|
+ b = Blather()
|
|
76
|
+ b.listen()
|
|
77
|
+ main_loop = gobject.MainLoop()
|
|
78
|
+ #start the main loop
|
|
79
|
+ try:
|
|
80
|
+ main_loop.run()
|
|
81
|
+ except:
|
|
82
|
+ main_loop.quit()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|