/
speech.py
32 lines (27 loc) · 967 Bytes
/
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
from threading import Thread
from queue import Queue
import platform
class Speech_thread(Thread):
def __init__(self, *args, **kwargs):
super(Speech_thread, self).__init__(*args, **kwargs)
self.queue = Queue()
self.index = None
def run(self):
if platform.system() == "Darwin":
import mac_say
name = mac_say.voices()[self.index][0]
while True:
text = self.queue.get()
mac_say.say([text, "-v", name])
else:
import pyttsx3
while True:
engine = pyttsx3.init()
voices = engine.getProperty('voices')
voice = voices[self.index]
engine.setProperty('voice', voice.id)
text = self.queue.get()
engine.say(text)
engine.runAndWait()
def put_text(self, text):
self.queue.put(text)