summaryrefslogtreecommitdiff
path: root/client/speaker.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-07-11 09:38:42 -0700
committerschneefux <schneefux+commit@schneefux.xyz>2014-07-11 09:38:42 -0700
commit43eebc44166da07388a2e932d1234090fda957ec (patch)
treed5cc1793a0b6269df71cce2b58ea099c60e4e455 /client/speaker.py
parentea082f844a6748141aa61143421e4e9d0cc19bda (diff)
parent8a5e5ed385a5c467260c2f1b20816a66cebfc1c6 (diff)
downloadjasper-client-43eebc44166da07388a2e932d1234090fda957ec.tar.gz
jasper-client-43eebc44166da07388a2e932d1234090fda957ec.zip
Merge pull request #100 from astahlman/master
Platform independent audio output. Stop hardcoding /home/jasper.
Diffstat (limited to 'client/speaker.py')
-rw-r--r--client/speaker.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/client/speaker.py b/client/speaker.py
new file mode 100644
index 0000000..1b1255a
--- /dev/null
+++ b/client/speaker.py
@@ -0,0 +1,57 @@
+"""
+A Speaker handles audio output from Jasper to the user
+
+Speaker methods:
+ say - output 'phrase' as speech
+ play - play the audio in 'filename'
+ isAvailable - returns True if the platform supports this implementation
+"""
+import os
+import json
+
+class eSpeakSpeaker:
+ """
+ Uses the eSpeak speech synthesizer included in the Jasper disk image
+ """
+ @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.play("say.wav")
+
+ def play(self, filename):
+ os.system("aplay -D hw:1,0 " + filename)
+
+class saySpeaker:
+ """
+ Uses the OS X built-in 'say' command
+ """
+
+ @classmethod
+ def isAvailable(cls):
+ return os.system("which say") == 0
+
+ def shellquote(self, s):
+ return "'" + s.replace("'", "'\\''") + "'"
+
+ def say(self, phrase):
+ os.system("say " + self.shellquote(phrase))
+
+ def play(self, filename):
+ os.system("afplay " + filename)
+
+def newSpeaker():
+ """
+ Returns:
+ A speaker implementation available on the current platform
+
+ Raises:
+ ValueError if no speaker implementation is supported on this platform
+ """
+
+ for cls in [eSpeakSpeaker, saySpeaker]:
+ if cls.isAvailable():
+ return cls()
+ raise ValueError("Platform is not supported")