From 6ea7c3c65bb14fded486acb4b8e9e2bb70efe40e Mon Sep 17 00:00:00 2001 From: Jezra Date: Tue, 9 Apr 2013 22:49:54 -0700 Subject: Init --- README | 8 ++++++ TTS.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ blather.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ commands.tmp | 6 +++++ 4 files changed, 172 insertions(+) create mode 100644 README create mode 100755 TTS.py create mode 100755 blather.py create mode 100644 commands.tmp diff --git a/README b/README new file mode 100644 index 0000000..ab8248b --- /dev/null +++ b/README @@ -0,0 +1,8 @@ +1. Run blather.py, this will generate a 'sentences.corpus' file based on sentences in the 'commands' file +2. quit blather (there is a good chance it will just segfault) +3. go to http://www.speech.cs.cmu.edu/tools/lmtool-new.html and upload the sentences.corpus file +4. download the resulting XXXX.lm file to the 'language' directory and rename to file to 'lm' +5. download the resulting XXXX.dic file to the 'language' directory and rename to file to 'dic' +6. run blather.py +7. start talking + diff --git a/TTS.py b/TTS.py new file mode 100755 index 0000000..96a6f64 --- /dev/null +++ b/TTS.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python2 +import gst +import subprocess +import os.path +import time +import gobject + +#define some global variables +this_dir = os.path.dirname( os.path.abspath(__file__) ) +lang_dir = os.path.join(this_dir, "language") +command_file = os.path.join(this_dir, "commands") +strings_file = os.path.join(this_dir, "sentences.corpus") + +class TTS(gobject.GObject): + __gsignals__ = { + 'finished' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_BOOLEAN,)) + } + def __init__(self): + gobject.GObject.__init__(self) + self.commands = {} + #build the pipeline + cmd = 'autoaudiosrc ! audioconvert ! audioresample ! vader name=vad ! pocketsphinx name=asr ! appsink sync=false' + self.pipeline=gst.parse_launch( cmd ) + #get the Auto Speech Recognition piece + asr=self.pipeline.get_by_name('asr') + asr.connect('result', self.result) + asr.set_property('lm', os.path.join(lang_dir, 'lm')) + asr.set_property('dict', os.path.join(lang_dir, 'dic')) + asr.set_property('configured', True) + #get the Voice Activity DEtectoR + self.vad = self.pipeline.get_by_name('vad') + self.vad.set_property('auto-threshold',True) + self.read_commands() + #init gobject threads + gobject.threads_init() + + def listen(self): + self.pipeline.set_state(gst.STATE_PLAYING) + + def pause(self): + self.vad.set_property('silent', True) + self.pipeline.set_state(gst.STATE_PAUSED) + + def result(self, asr, text, uttid): + self.emit("finished", True) + print text + #is there a matching command? + if self.commands.has_key( text ): + cmd = self.commands[text] + print cmd + subprocess.call(cmd, shell=True) + else: + print "no matching command" + #emit finished + + + def read_commands(self): + #read the.commands file + file_lines = open(command_file) + strings = open(strings_file, "w") + for line in file_lines: + #trim the white spaces + line = line.strip() + #if the line has length and the first char isn't a hash + if len(line) and line[0]!="#": + #this is a parsible line + (key,value) = line.split(":",1) + print key, value + self.commands[key.strip()] = value.strip() + strings.write( key.strip()+"\n") + #close the strings file + strings.close() + +if __name__ == "__main__": + b = Blather() + b.listen() + main_loop = gobject.MainLoop() + #start the main loop + try: + main_loop.run() + except: + main_loop.quit() + + + diff --git a/blather.py b/blather.py new file mode 100755 index 0000000..230f055 --- /dev/null +++ b/blather.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python2 +import sys +import signal +import gobject +# Qt stuff +from PySide.QtCore import Signal, Qt +from PySide.QtGui import QApplication, QWidget, QMainWindow, QVBoxLayout +from PySide.QtGui import QLabel, QPushButton, QCheckBox +from TTS import TTS + +class Blather: + def __init__(self): + self.tts = TTS(); + self.tts.connect('finished',self.tts_finished) + #make a window + self.window = QMainWindow() + center = QWidget() + self.window.setCentralWidget(center) + + layout = QVBoxLayout() + center.setLayout(layout) + #make a listen/stop button + self.lsbutton = QPushButton("Listen") + layout.addWidget(self.lsbutton) + #make a continuous button + self.ccheckbox = QCheckBox("Continuous Listen") + layout.addWidget(self.ccheckbox) + + #connect the buttonsc + self.lsbutton.clicked.connect(self.lsbutton_clicked) + self.ccheckbox.clicked.connect(self.ccheckbox_clicked) + + def tts_finished(self, x, y): + if self.ccheckbox.isChecked(): + pass + else: + self.lsbutton_stopped() + + + def ccheckbox_clicked(self): + checked = self.ccheckbox.isChecked() + if checked: + #disable lsbutton + self.lsbutton.setEnabled(False) + self.tts.listen() + else: + self.lsbutton.setEnabled(True) + + def lsbutton_stopped(self): + self.tts.pause() + self.lsbutton.setText("Listen") + + def lsbutton_clicked(self): + val = self.lsbutton.text() + print val + if val == "Listen": + self.tts.listen() + self.lsbutton.setText("Stop") + else: + self.lsbutton_stopped() + + def run(self): + self.window.show() + +if __name__ == "__main__": + app = QApplication(sys.argv) + b = Blather() + b.run() + + signal.signal(signal.SIGINT, signal.SIG_DFL) + #start the app running + sys.exit(app.exec_()) + diff --git a/commands.tmp b/commands.tmp new file mode 100644 index 0000000..3835915 --- /dev/null +++ b/commands.tmp @@ -0,0 +1,6 @@ +# commands are key:value pairs +# key is the sentence to listen for +# key must be in ALL CAPS +# value is the command to run when the key is spoken + +HELLO WORLD:echo "hello world" -- cgit 1.4.1