From d9ddd711158e69a6eeb4900f68c80946705d1305 Mon Sep 17 00:00:00 2001 From: Jezra Date: Fri, 12 Apr 2013 12:31:39 -0700 Subject: That shit wasn't a TTS it was a Recognizer! --- Recognizer.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TTS.py | 86 ----------------------------------------------------------- blather.py | 14 +++++----- 3 files changed, 93 insertions(+), 93 deletions(-) create mode 100755 Recognizer.py delete mode 100755 TTS.py diff --git a/Recognizer.py b/Recognizer.py new file mode 100755 index 0000000..685e41d --- /dev/null +++ b/Recognizer.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python2 +import pygst +pygst.require('0.10') +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 Recognizer(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): + #emit finished + 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" + + 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__": + recognizer = Recognizer() + recognizer.listen() + main_loop = gobject.MainLoop() + #start the main loop + try: + main_loop.run() + except: + main_loop.quit() + + + diff --git a/TTS.py b/TTS.py deleted file mode 100755 index de4b61a..0000000 --- a/TTS.py +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env python2 -import pygst -pygst.require('0.10') -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): - #emit finished - 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" - - 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__": - tts = TTS() - tts.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 index 230f055..ddc2ce7 100755 --- a/blather.py +++ b/blather.py @@ -6,12 +6,12 @@ import gobject 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 +from Recognizer import Recognizer class Blather: def __init__(self): - self.tts = TTS(); - self.tts.connect('finished',self.tts_finished) + self.recognizer = Recognizer(); + self.recognizer.connect('finished',self.recognizer_finished) #make a window self.window = QMainWindow() center = QWidget() @@ -30,7 +30,7 @@ class Blather: self.lsbutton.clicked.connect(self.lsbutton_clicked) self.ccheckbox.clicked.connect(self.ccheckbox_clicked) - def tts_finished(self, x, y): + def recognizer_finished(self, x, y): if self.ccheckbox.isChecked(): pass else: @@ -42,19 +42,19 @@ class Blather: if checked: #disable lsbutton self.lsbutton.setEnabled(False) - self.tts.listen() + self.recognizer.listen() else: self.lsbutton.setEnabled(True) def lsbutton_stopped(self): - self.tts.pause() + self.recognizer.pause() self.lsbutton.setText("Listen") def lsbutton_clicked(self): val = self.lsbutton.text() print val if val == "Listen": - self.tts.listen() + self.recognizer.listen() self.lsbutton.setText("Stop") else: self.lsbutton_stopped() -- cgit 1.4.1