-
Notifications
You must be signed in to change notification settings - Fork 7
/
speech.py
57 lines (50 loc) · 1.56 KB
/
speech.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# encoding: utf-8
""" Espeak Class for xfd
Author Aske Olsson [email protected]
"""
import subprocess
import threading
import time
class Speech(object):
"""This class provides an easy way to output
speech with the espeak module for the xfd"""
def __init__(self):
"""Initialize"""
self.lock = threading.Lock()
self.text = "I'm sorry Dave, I'm afraid I can't do that"
def run(self):
"""Start the thread"""
self.speech_thread = threading.Thread(target = self.speechloop, name = 'Speech')
self.speech_thread.setDaemon(1)
self.speech_thread.start()
def join(self):
"""join in its own method"""
self.speech_thread.join()
def speak(self, newtext):
"""Set new text to be read"""
self.lock.acquire()
try:
if newtext:
self.text = newtext
finally:
self.lock.release()
def do_speak(self):
"""Do the speak/reading"""
fhdevnull = open('/dev/null', 'w')
try:
subprocess.call(["espeak", "-p", "20", "-s", "130", "-a", "190", "\""+self.text+"\""], shell=False, stdout=fhdevnull, stderr=fhdevnull)
finally:
# Clear text
self.text = ""
fhdevnull.close()
def speechloop(self):
"""Speech handle"""
while(1):
self.lock.acquire()
try:
if self.text:
self.do_speak()
finally:
self.lock.release()
time.sleep(7)