Skip to content

Commit

Permalink
Refactor looped time.sleep to use Event.wait
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Oct 11, 2023
1 parent b69c879 commit 6def906
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ovos_audio/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def _play(self, message):
prefered_service = None
try:
self.play(tracks, prefered_service, repeat)
time.sleep(0.5)
time.sleep(0.5) # TODO: Is this hard-coded delay necessary?
except Exception as e:
LOG.exception(e)

Expand Down
21 changes: 10 additions & 11 deletions ovos_audio/playback.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import random
import threading
from ovos_audio.transformers import TTSTransformersService
from ovos_bus_client.message import Message
from ovos_plugin_manager.templates.tts import TTS
from ovos_utils.log import LOG
from ovos_utils.sound import play_audio
from queue import Empty
from threading import Thread
from time import time, sleep
from threading import Thread, Event
from time import time


class PlaybackThread(Thread):
Expand All @@ -20,14 +19,15 @@ def __init__(self, queue=TTS.queue, bus=None):
self.queue = queue or TTS.queue
self._terminated = False
self._processing_queue = False
self._paused = False
self._do_playback = Event()
self.enclosure = None
self.p = None
self._tts = []
self.bus = bus or None
self._now_playing = None
self.active_tts = None
self._started = threading.Event()
self._started = Event()
# TODO: Config to disable
self.tts_transform = TTSTransformersService(self.bus)

@property
Expand Down Expand Up @@ -192,11 +192,10 @@ def run(self, cb=None):
listening.
"""
LOG.info("PlaybackThread started")
self._paused = False
self._do_playback.set()
self._started.set()
while not self._terminated:
while self._paused:
sleep(0.2)
self._do_playback.wait()
try:
# HACK: we do these check to account for direct usages of TTS.queue singletons
speech_data = self.queue.get(timeout=2)
Expand All @@ -223,7 +222,7 @@ def run(self, cb=None):
self._now_playing = (data, visemes, listen, tts_id, message)
self._play()
except Exception as e:
pass
LOG.debug(e)

def show_visemes(self, pairs):
"""Send viseme data to enclosure
Expand All @@ -239,15 +238,15 @@ def show_visemes(self, pairs):

def pause(self):
"""pause thread"""
self._paused = True
self._do_playback.clear()
if self.p:
self.p.terminate()

def resume(self):
"""resume thread"""
if self._now_playing:
self._play()
self._paused = False
self._do_playback.set()

def clear(self):
"""Clear all pending actions for the TTS playback thread."""
Expand Down

0 comments on commit 6def906

Please sign in to comment.