Skip to content

Commit

Permalink
Make _paused state an event
Browse files Browse the repository at this point in the history
This removes the need for a busy waiting  while / sleep check, instead
uses the Event.wait() to wait until playback is resumed.
  • Loading branch information
forslund committed Jul 3, 2021
1 parent 33a430e commit 8dc0fd6
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions mycroft/tts/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import random
import re
from abc import ABCMeta, abstractmethod
from threading import Thread
from threading import Event, Thread
from time import time, sleep
from warnings import warn

Expand Down Expand Up @@ -54,7 +54,10 @@ class PlaybackThread(Thread):
def __init__(self, queue):
super(PlaybackThread, self).__init__()
self.queue = queue
self._paused = False

self._free_to_play = Event()
self._free_to_play.set()

self._terminated = False
self._processing_queue = False
self.enclosure = None
Expand Down Expand Up @@ -94,8 +97,7 @@ def run(self):
listening.
"""
while not self._terminated:
while self._paused:
sleep(0.2)
self._free_to_play.wait()
try:
(snd_type, data,
visemes, ident, listen) = self.queue.get(timeout=2)
Expand All @@ -116,7 +118,7 @@ def run(self):

if self.p:
while self.p.poll() is None:
if self._paused:
if not self._free_to_play.is_set():
self.p.terminate()
break
sleep(0.1)
Expand Down Expand Up @@ -157,12 +159,12 @@ def blink(self, rate=1.0):
self.enclosure.eyes_blink("b")

def pause(self):
"""pause thread"""
self._paused = True
"""Signal that the thread is no longer free to play."""
self._free_to_play.clear()

def resume(self):
"""resume thread"""
self._paused = False
"""Signal that the thred is now free to play."""
self._free_to_play.set()

def stop(self):
"""Stop thread"""
Expand Down

0 comments on commit 8dc0fd6

Please sign in to comment.