Skip to content

Commit

Permalink
refactor/code_cleanup (#59)
Browse files Browse the repository at this point in the history
* refactor/revert_ovos_additions_to_playback_thread

remove all references to self.tts within the playback thread to simplify code, this does not break mycroft module compat as it was not present there

needs OpenVoiceOS/ovos-plugin-manager#195 for the cache cleaning

* allow older OPM versions

* allow older OPM versions
  • Loading branch information
JarbasAl authored Apr 29, 2024
1 parent 63391b6 commit 62c954f
Showing 1 changed file with 7 additions and 63 deletions.
70 changes: 7 additions & 63 deletions ovos_audio/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,17 @@ def __init__(self, queue=TTS.queue, bus=None):
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 = Event()
self.tts_transform = TTSTransformersService(self.bus)

@property
def is_running(self):
return self._started.is_set() and not self._terminated

def activate_tts(self, tts_id):
self.active_tts = tts_id
tts = self.get_attached_tts()
if tts:
tts.begin_audio()

def deactivate_tts(self):
if self.active_tts:
tts = self.get_attached_tts()
if tts:
tts.end_audio()
self.active_tts = None

def init(self, tts):
"""DEPRECATED! Init the TTS Playback thread."""
self.attach_tts(tts)
self.set_bus(tts.bus)

def set_bus(self, bus):
Expand All @@ -59,47 +43,6 @@ def set_bus(self, bus):
self.bus = bus
self.tts_transform.set_bus(bus)

@property
def tts(self):
tts = self.get_attached_tts()
if not tts and self._tts:
return self._tts[0]
return tts

@tts.setter
def tts(self, val):
self.attach_tts(val)

@property
def attached_tts(self):
return self._tts

def attach_tts(self, tts):
"""Add TTS to be cache checked."""
if tts not in self.attached_tts:
self.attached_tts.append(tts)

def detach_tts(self, tts):
"""Remove TTS from cache check."""
if tts in self.attached_tts:
self.attached_tts.remove(tts)

def get_attached_tts(self, tts_id=None):
tts_id = tts_id or self.active_tts
if not tts_id:
return
for tts in self.attached_tts:
if hasattr(tts, "tts_id"):
# opm plugin
if tts.tts_id == tts_id:
return tts

for tts in self.attached_tts:
if not hasattr(tts, "tts_id"):
# non-opm plugin
if tts.tts_name == tts_id:
return tts

def clear_queue(self):
"""Remove all pending playbacks."""
while not self.queue.empty():
Expand Down Expand Up @@ -149,14 +92,17 @@ def on_end(self, listen=False, message=None):
self._processing_queue = False
# Clear cache for all attached tts objects
# This is basically the only safe time
for tts in self.attached_tts:
tts.cache.curate()
try:
from ovos_plugin_manager.templates.tts import TTSContext
TTSContext.curate_caches()
except ImportError:
LOG.warning("failed to curate TTS cache. please update ovos-plugin-manager")
self.blink(0.2)

def _play(self):
try:
data, visemes, listen, tts_id, message = self._now_playing
self.activate_tts(tts_id)

self.on_start(message)

data, message.context = self.tts_transform.transform(data, message.context)
Expand All @@ -167,7 +113,7 @@ def _play(self):
if self.p:
self.p.communicate()
self.p.wait()
self.deactivate_tts()

if self.queue.empty():
self.on_end(listen, message)
except Empty:
Expand Down Expand Up @@ -272,8 +218,6 @@ def stop(self):

def shutdown(self):
self.stop()
for tts in self.attached_tts:
self.detach_tts(tts)

def __del__(self):
self.shutdown()

0 comments on commit 62c954f

Please sign in to comment.