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 --- client/speaker.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 client/speaker.py (limited to 'client/speaker.py') 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") -- cgit v1.3.1