Jezra 11 years ago
commit
6ea7c3c65b
4 changed files with 172 additions and 0 deletions
  1. 8
    0
      README
  2. 85
    0
      TTS.py
  3. 73
    0
      blather.py
  4. 6
    0
      commands.tmp

+ 8
- 0
README View File

@@ -0,0 +1,8 @@
1
+1. Run blather.py, this will generate a 'sentences.corpus' file based on sentences in the 'commands' file
2
+2. quit blather (there is a good chance it will just segfault)
3
+3. go to http://www.speech.cs.cmu.edu/tools/lmtool-new.html and upload the sentences.corpus file
4
+4. download the resulting XXXX.lm file to the 'language' directory and rename to file to 'lm'
5
+5. download the resulting XXXX.dic file to the 'language' directory and rename to file to 'dic'
6
+6. run blather.py
7
+7. start talking
8
+

+ 85
- 0
TTS.py View File

@@ -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
+

+ 73
- 0
blather.py View File

@@ -0,0 +1,73 @@
1
+#!/usr/bin/env python2
2
+import sys
3
+import signal
4
+import gobject
5
+# Qt stuff
6
+from PySide.QtCore import Signal, Qt
7
+from PySide.QtGui import QApplication, QWidget, QMainWindow, QVBoxLayout
8
+from PySide.QtGui import QLabel, QPushButton, QCheckBox
9
+from TTS import TTS
10
+
11
+class Blather:
12
+	def __init__(self):
13
+		self.tts = TTS();
14
+		self.tts.connect('finished',self.tts_finished)
15
+		#make a window
16
+		self.window = QMainWindow()
17
+		center = QWidget()
18
+		self.window.setCentralWidget(center)
19
+		
20
+		layout = QVBoxLayout()
21
+		center.setLayout(layout)
22
+		#make a listen/stop button
23
+		self.lsbutton = QPushButton("Listen")
24
+		layout.addWidget(self.lsbutton)
25
+		#make a continuous button
26
+		self.ccheckbox = QCheckBox("Continuous Listen")
27
+		layout.addWidget(self.ccheckbox)
28
+		
29
+		#connect the buttonsc
30
+		self.lsbutton.clicked.connect(self.lsbutton_clicked)
31
+		self.ccheckbox.clicked.connect(self.ccheckbox_clicked)
32
+	
33
+	def tts_finished(self, x, y):
34
+		if self.ccheckbox.isChecked():
35
+			pass
36
+		else:
37
+			self.lsbutton_stopped()
38
+			
39
+			
40
+	def ccheckbox_clicked(self):
41
+		checked = self.ccheckbox.isChecked()
42
+		if checked:
43
+			#disable lsbutton
44
+			self.lsbutton.setEnabled(False)
45
+			self.tts.listen()
46
+		else:
47
+			self.lsbutton.setEnabled(True)
48
+	
49
+	def lsbutton_stopped(self):
50
+		self.tts.pause()
51
+		self.lsbutton.setText("Listen")
52
+		
53
+	def lsbutton_clicked(self):
54
+		val = self.lsbutton.text()
55
+		print val
56
+		if val == "Listen":
57
+			self.tts.listen()
58
+			self.lsbutton.setText("Stop")
59
+		else:
60
+			self.lsbutton_stopped()
61
+			
62
+	def run(self):
63
+		self.window.show()
64
+		
65
+if __name__ == "__main__":
66
+	app = QApplication(sys.argv)
67
+	b = Blather()
68
+	b.run()
69
+	
70
+	signal.signal(signal.SIGINT, signal.SIG_DFL)
71
+	#start the app running
72
+	sys.exit(app.exec_())
73
+	

+ 6
- 0
commands.tmp View File

@@ -0,0 +1,6 @@
1
+# commands are key:value pairs 
2
+# key is the sentence to listen for
3
+# key must be in ALL CAPS 
4
+# value is the command to run when the key is spoken
5
+
6
+HELLO WORLD:echo "hello world"

Loading…
Cancel
Save