Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor looped time.sleep to use Event.wait #31

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Was this hard-coded delay necessary?
except Exception as e:
LOG.exception(e)

Expand Down
20 changes: 9 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, log_deprecation
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,14 @@ 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()
self.tts_transform = TTSTransformersService(self.bus)

@property
Expand Down Expand Up @@ -192,11 +191,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 Down Expand Up @@ -225,7 +223,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 @@ -241,15 +239,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
Loading