summaryrefslogtreecommitdiff
path: root/client/speaker.py
blob: f2d4ab753a607fa910d2d0721bfe1155a94d2cc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
A Speaker handles audio 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")