forked from nikivanov/watney
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tts.py
36 lines (31 loc) · 1.29 KB
/
tts.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
import asyncio
import shlex
from events import Events
class TTSSpeaker:
def __init__(self, config, alsa, audioManager):
self.alsa = alsa
self.audioManager = audioManager
self.audioToken = "08843f08-92aa-49b0-840f-74c6b38092ff"
audioConfig = config["AUDIO"]
self.ttsCommand = audioConfig["TTSCommand"]
self.workQueue = asyncio.Queue()
self.startLoop()
def startLoop(self):
loop = asyncio.get_event_loop()
loop.create_task(self.queueLoop())
async def queueLoop(self):
print("Starting TTS...")
try:
while True:
ttsText = await self.workQueue.get()
if ttsText:
print("Saying '{}'".format(ttsText))
self.audioManager.lowerVolume(self.audioToken)
fullTTSCommand = self.ttsCommand.format(shlex.quote(ttsText))
process = await asyncio.create_subprocess_shell(fullTTSCommand, stdout=asyncio.subprocess.DEVNULL, stderr=asyncio.subprocess.DEVNULL)
await process.communicate()
self.audioManager.restoreVolume(self.audioToken)
except asyncio.CancelledError:
print("TTS stopped")
def sayText(self, text):
self.workQueue.put_nowait(text)