From 84a92e5327997815488276021e5498f7681ab2d7 Mon Sep 17 00:00:00 2001 From: schneefux Date: Sun, 8 Jun 2014 20:39:35 -0700 Subject: Stop hardcoding /home/jasper and abstract out mic.say for multiplatform support --- boot/boot.py | 20 +++++++++++--------- boot/boot.sh | 14 ++++++++++++-- client/g2p.py | 11 ++++++----- client/main.py | 3 ++- client/mic.py | 13 ++++++------- client/speaker.py | 35 +++++++++++++++++++++++++++++++++++ client/start.sh | 2 +- client/test.py | 8 +++++++- 8 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 client/speaker.py diff --git a/boot/boot.py b/boot/boot.py index 59217ec..fcaeedd 100755 --- a/boot/boot.py +++ b/boot/boot.py @@ -1,14 +1,16 @@ #!/usr/bin/env python -import os, json +import os, sys import urllib2 import yaml - import vocabcompiler +import traceback + +lib_path = os.path.abspath('../client') +sys.path.append(lib_path) -def say(phrase, OPTIONS = " -vdefault+m3 -p 40 -s 160 --stdout > ../static/audio/say.wav"): - os.system("espeak " + json.dumps(phrase) + OPTIONS) - os.system("aplay -D hw:1,0 ../static/audio/say.wav") +import speaker as speak +speaker = speak.newSpeaker() def configure(): try: @@ -19,17 +21,17 @@ def configure(): vocabcompiler.compile("../client/sentences.txt", "../client/dictionary.dic", "../client/languagemodel.lm") print "STARTING CLIENT PROGRAM" - os.system("/home/pi/jasper/client/start.sh &") + os.system("$JASPER_HOME/jasper/client/start.sh &") except: - print "COULD NOT CONNECT TO NETWORK" - say("Hello, I could not connect to a network. Please read the documentation to configure your Raspberry Pi.") + traceback.print_exc() + speaker.say("Hello, I could not connect to a network. Please read the documentation to configure your Raspberry Pi.") if __name__ == "__main__": print "==========STARTING JASPER CLIENT==========" print "==========================================" print "COPYRIGHT 2013 SHUBHRO SAHA, CHARLIE MARSH" print "==========================================" - say("Hello.... I am Jasper... Please wait one moment.") + speaker.say("Hello.... I am Jasper... Please wait one moment.") configure() diff --git a/boot/boot.sh b/boot/boot.sh index 5ea1b3d..927b4b2 100755 --- a/boot/boot.sh +++ b/boot/boot.sh @@ -1,6 +1,16 @@ -cd /home/pi/jasper/boot/ +if [[ -z "$JASPER_HOME" ]]; then + if [[ -d "/home/pi" ]]; then + JASPER_HOME="/home/pi" + export JASPER_HOME; + else + echo "Error: \$JASPER_HOME is not set." + exit 0; + fi +fi + +cd $JASPER_HOME/jasper/boot LD_LIBRARY_PATH="/usr/local/lib" export LD_LIBRARY_PATH PATH=$PATH:/usr/local/lib/ export PATH -python boot.py & \ No newline at end of file +python boot.py & diff --git a/client/g2p.py b/client/g2p.py index 06fef14..6d72bfe 100644 --- a/client/g2p.py +++ b/client/g2p.py @@ -4,6 +4,7 @@ import re TEMP_FILENAME = "g2ptemp" PHONE_MATCH = re.compile(r' (.*) ') +PHONETISAURUS_PATH = os.environ['JASPER_HOME'] + "/phonetisaurus" def parseLine(line): @@ -16,7 +17,7 @@ def parseOutput(output): def translateWord(word): out = subprocess.check_output(['phonetisaurus-g2p', '--model=%s' % - os.path.expanduser("~/phonetisaurus/g014b2b.fst"), '--input=%s' % word]) + PHONETISAURUS_PATH + "/g014b2b.fst", '--input=%s' % word]) return parseLine(out) @@ -34,8 +35,8 @@ def translateWords(words): def translateFile(input_filename, output_filename=None): - out = subprocess.check_output(['phonetisaurus-g2p', '--model=%s' % os.path.expanduser( - "~/phonetisaurus/g014b2b.fst"), '--input=%s' % input_filename, '--words', '--isfile']) + out = subprocess.check_output(['phonetisaurus-g2p', '--model=%s' % + PHONETISAURUS_PATH + "/g014b2b.fst", '--input=%s' % input_filename, '--words', '--isfile']) out = parseOutput(out) if output_filename: @@ -51,5 +52,5 @@ def translateFile(input_filename, output_filename=None): if __name__ == "__main__": - translateFile(os.path.expanduser("~/phonetisaurus/sentences.txt"), - os.path.expanduser("~/phonetisaurus/dictionary.dic")) + translateFile(PHONETISAURUS_PATH + "/phonetisaurus/sentences.txt", + PHONETISAURUS_PATH + "/phonetisaurus/dictionary.dic") diff --git a/client/main.py b/client/main.py index c5805c4..35ef715 100644 --- a/client/main.py +++ b/client/main.py @@ -1,5 +1,6 @@ import yaml import sys +import speaker from conversation import Conversation @@ -20,7 +21,7 @@ if __name__ == "__main__": profile = yaml.safe_load(open("profile.yml", "r")) - mic = Mic("languagemodel.lm", "dictionary.dic", + mic = Mic(speaker.newSpeaker(), "languagemodel.lm", "dictionary.dic", "languagemodel_persona.lm", "dictionary_persona.dic") mic.say("How can I be of service?") diff --git a/client/mic.py b/client/mic.py index d746ba1..0c41df9 100644 --- a/client/mic.py +++ b/client/mic.py @@ -22,17 +22,18 @@ class Mic: speechRec = None speechRec_persona = None - def __init__(self, lmd, dictd, lmd_persona, dictd_persona, lmd_music=None, dictd_music=None): + def __init__(self, speaker, lmd, dictd, lmd_persona, dictd_persona, lmd_music=None, dictd_music=None): """ Initiates the pocketsphinx instance. Arguments: + speaker -- used to output audio lmd -- filename of the full language model dictd -- filename of the full dictionary (.dic) lmd_persona -- filename of the 'Persona' language model (containing, e.g., 'Jasper') dictd_persona -- filename of the 'Persona' dictionary (.dic) """ - + self.speaker = speaker hmdir = "/usr/local/share/pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k" if lmd_music and dictd_music: @@ -237,7 +238,7 @@ class Mic: if THRESHOLD == None: THRESHOLD = self.fetchThreshold() - os.system("aplay -D hw:1,0 ../static/audio/beep_hi.wav") + self.speaker.playSound("../static/audio/beep_hi.wav") # prepare recording stream audio = pyaudio.PyAudio() @@ -267,7 +268,7 @@ class Mic: if average < THRESHOLD * 0.8: break - os.system("aplay -D hw:1,0 ../static/audio/beep_lo.wav") + self.speaker.playSound("beep_lo.wav") # save the audio data stream.stop_stream() @@ -291,6 +292,4 @@ class Mic: def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav"): # alter phrase before speaking phrase = alteration.clean(phrase) - - os.system("espeak " + json.dumps(phrase) + OPTIONS) - os.system("aplay -D hw:1,0 say.wav") + self.speaker.say(phrase) diff --git a/client/speaker.py b/client/speaker.py new file mode 100644 index 0000000..304b426 --- /dev/null +++ b/client/speaker.py @@ -0,0 +1,35 @@ +""" +A Speaker handles output from Jasper to the user. +""" +import os, json + +class PiSpeaker: + + @classmethod + def isAvailable(cls): + return os.system("which espeak") == 0 + + def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav"): + os.system("espeak " + json.dumps(phrase) + OPTIONS) + self.playSound("say.wav") + + def playSound(self, filename): + os.system("aplay -D hw:1,0 " + filename) + +class MacSpeaker: + + @classmethod + def isAvailable(cls): + return os.system("which say") == 0 + + def say(self, phrase): + os.system("say " + phrase) + + def playSound(self, filename): + os.system("afplay " + filename) + +def newSpeaker(): + for cls in [PiSpeaker, MacSpeaker]: + if cls.isAvailable(): + return cls() + raise ValueError("Platform is not supported") diff --git a/client/start.sh b/client/start.sh index f3eea8b..cc33f9c 100755 --- a/client/start.sh +++ b/client/start.sh @@ -1,3 +1,3 @@ -cd /home/pi/jasper/client/ +cd $JASPER_HOME/jasper/client rm -rf ../old_client python main.py & diff --git a/client/test.py b/client/test.py index 80191ca..fb7d147 100644 --- a/client/test.py +++ b/client/test.py @@ -1,3 +1,8 @@ +import os + +if os.environ.get('JASPER_HOME') is None: + os.environ['JASPER_HOME'] = '/home/pi' + import unittest import argparse from mock import patch @@ -6,6 +11,7 @@ import yaml import test_mic import g2p import brain +import speaker def activeInternet(): @@ -23,7 +29,7 @@ class TestMic(unittest.TestCase): self.time_clip = "../static/audio/time.wav" from mic import Mic - self.m = Mic("languagemodel.lm", "dictionary.dic", + self.m = Mic(speaker.newSpeaker(), "languagemodel.lm", "dictionary.dic", "languagemodel_persona.lm", "dictionary_persona.dic") def testTranscribeJasper(self): -- cgit v1.3.1