-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspeech.py
42 lines (37 loc) · 1.3 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
from threading import Thread
from queue import Queue
import platform
import os
import subprocess
class Speech_thread(Thread):
def __init__(self, *args, **kwargs):
super(Speech_thread, self).__init__(*args, **kwargs)
self.queue = Queue()
self.index = None
self.is_broadcast = False
def run(self):
if self.is_broadcast:
return
if platform.system() == "Darwin":
result = subprocess.run(['say', '-v', '?'], stdout=subprocess.PIPE)
output = result.stdout.decode('utf-8')
voices = []
for line in output.splitlines():
if line:
voices.append(line.split()[0])
name = voices[self.index]
while True:
text = self.queue.get()
os.system(f'say -v {name} "{text}"')
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)