Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat/wait_while_speaking

extract into helper method + add wait flag to play_audio
  • Loading branch information
JarbasAl committed May 1, 2024
1 parent 18f4e11 commit e84f78b
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def dialog_renderer(self) -> Optional[MustacheDialogRenderer]:
session or else from Configuration.
"""
return self.resources.dialog_renderer

@property
def system_unit(self) -> str:
"""
Expand All @@ -538,7 +538,7 @@ def system_unit(self) -> str:
"""
sess = SessionManager.get()
return sess.system_unit

@property
def date_format(self) -> str:
"""
Expand All @@ -556,7 +556,7 @@ def time_format(self) -> str:
"""
sess = SessionManager.get()
return sess.time_format

@property
def location(self) -> dict:
"""
Expand Down Expand Up @@ -1633,18 +1633,9 @@ def speak(self, utterance: str, expect_response: bool = False,

if wait:
timeout = wait if isinstance(wait, int) else 15
sessid = SessionManager.get(m).session_id
event = Event()

def handle_output_end(msg):
sess = SessionManager.get(msg)
if sessid == sess.session_id:
event.set()

self.bus.on("recognizer_loop:audio_output_end", handle_output_end)
event.wait(timeout=timeout)
self.bus.remove("recognizer_loop:audio_output_end",
handle_output_end)
sess = SessionManager.get(m)
sess.is_speaking = True
SessionManager.wait_while_speaking(timeout, sess)

def speak_dialog(self, key: str, data: Optional[dict] = None,
expect_response: bool = False, wait: Union[bool, int] = False):
Expand Down Expand Up @@ -1674,7 +1665,8 @@ def speak_dialog(self, key: str, data: Optional[dict] = None,
)
self.speak(key, expect_response, wait, {})

def _play_audio_old(self, filename: str, instant: bool = False):
def _play_audio_old(self, filename: str, instant: bool = False,
wait: Union[bool, int] = False):
""" compat for ovos-core <= 0.0.7 """
if instant:
LOG.warning("self.play_audio instant flag requires ovos-core >= 0.0.8, "
Expand All @@ -1686,19 +1678,29 @@ def _play_audio_old(self, filename: str, instant: bool = False):
{"filename": filename, # TODO - deprecate filename in ovos-audio
"uri": filename # new namespace
}))

def _play_audio_classic(self, filename: str, instant: bool = False):
if wait:
timeout = wait if isinstance(wait, int) else 30
sess = SessionManager.get(message)
sess.is_speaking = True
SessionManager.wait_while_speaking(timeout, sess)

def _play_audio_classic(self, filename: str, instant: bool = False,
wait: Union[bool, int] = False):
""" compat for classic mycroft-core """
LOG.warning("self.play_audio requires ovos-core >= 0.0.4, "
"falling back to local skill playback")
play_audio(filename).wait()

@backwards_compat(pre_008=_play_audio_old, classic_core=_play_audio_classic)
def play_audio(self, filename: str, instant: bool = False):
def play_audio(self, filename: str, instant: bool = False,
wait: Union[bool, int] = False):
"""
Queue and audio file for playback
@param filename: File to play
@param instant: if True audio will be played instantly instead of queued with TTS
@param wait: set to True to block while the audio
is being played for 15 seconds. Alternatively, set
to an integer to specify a timeout in seconds.
"""
message = dig_for_message() or Message("")
# if running in docker we need to send binary data to the ovos-audio container
Expand All @@ -1721,6 +1723,11 @@ def play_audio(self, filename: str, instant: bool = False):
"binary_data": bindata}

self.bus.emit(message.forward(mtype, data))
if wait:
timeout = wait if isinstance(wait, int) else 30
sess = SessionManager.get(message)
sess.is_speaking = True
SessionManager.wait_while_speaking(timeout, sess)

def __get_response_v1(self, session=None):
"""Helper to get a response from the user
Expand Down

0 comments on commit e84f78b

Please sign in to comment.