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/play_sound_in_ovos_audio #60

Merged
merged 7 commits into from
Sep 8, 2023
Merged
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
71 changes: 35 additions & 36 deletions ovos_dinkum_listener/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
from ovos_plugin_manager.vad import OVOSVADFactory
from ovos_plugin_manager.vad import get_vad_configs
from ovos_plugin_manager.wakewords import get_ww_lang_configs, get_ww_supported_langs, get_ww_module_configs
from ovos_utils.file_utils import resolve_resource_file
from ovos_utils.log import LOG, log_deprecation
from ovos_utils.process_utils import ProcessStatus, StatusCallbackMap, ProcessState
from ovos_utils.sound import play_audio

from ovos_dinkum_listener.plugins import load_stt_module, load_fallback_stt
from ovos_dinkum_listener.transformers import AudioTransformersService
Expand Down Expand Up @@ -507,13 +505,13 @@ def _hotword_audio(self, audio_bytes: bytes, ww_context: dict):
event = ww_context.get("event")

if sound:
context = {'client_name': 'ovos_dinkum_listener',
'source': 'listener',
'destination': ["audio"] # default native-source
}
LOG.debug(f"Handling listen sound: {sound}")
try:
sound = resolve_resource_file(sound, config=self.config)
if sound:
play_audio(sound)
except Exception as e:
LOG.warning(e)
self.bus.emit(Message("mycroft.audio.play_sound",
{"uri": sound}, context))

if listen:
msg_type = "recognizer_loop:wakeword"
Expand Down Expand Up @@ -646,45 +644,43 @@ def _handle_unmute(self, _message: Message):
self.voice_loop.is_muted = False

def _handle_listen(self, message: Message):
instant_listen = self.config.get('listener', {}).get('instant_listen')
if self.config.get('confirm_listening'):
sound = self.config.get('sounds', {}).get('start_listening')
sound = resolve_resource_file(sound, config=self.config)
if sound:
play = play_audio(sound)
if not instant_listen:
play.wait(10)
else:
LOG.error(f"Requested sound not available: {sound}")
context = {'client_name': 'ovos_dinkum_listener',
'source': 'listener',
'destination': ["audio"] # default native-source
}
message = message or Message("", context=context) # might be None
self.bus.emit(message.forward("mycroft.audio.play_sound", {"uri": sound}))

self.voice_loop.skip_next_wake = True

def _handle_mic_get_status(self, event):
def _handle_mic_get_status(self, message: Message):
"""Query microphone mute status."""
data = {'muted': self.voice_loop.is_muted}
self.bus.emit(event.response(data))
self.bus.emit(message.response(data))

def _handle_audio_start(self, event):
"""Mute voice loop."""
def _handle_audio_start(self, message: Message):
"""audio output started"""
if self.config.get("listener").get("mute_during_output"):
self.voice_loop.is_muted = True

def _handle_audio_end(self, event):
"""Request unmute, if more sources have requested the mic to be muted
it will remain muted.
"""
def _handle_audio_end(self, message: Message):
"""audio output ended"""
if self.config.get("listener").get("mute_during_output"):
self.voice_loop.is_muted = False # restore

def _handle_stop(self, event):
def _handle_stop(self, message: Message):
"""Handler for mycroft.stop, i.e. button press."""
self.voice_loop.is_muted = False # restore

# state events
def _handle_change_state(self, event):
def _handle_change_state(self, message: Message):
"""Set listening state."""
# TODO - unify this api, should match ovos-listener exactly
state = event.data.get("state")
mode = event.data.get("mode")
state = message.data.get("state")
mode = message.data.get("mode")

# NOTE: the enums are also strings and will match
if state:
Expand All @@ -693,7 +689,7 @@ def _handle_change_state(self, event):
elif state == ListeningState.DETECT_WAKEWORD or state == ListeningState.WAITING_CMD: # "continuous"
self.voice_loop.reset_state()
elif state == ListeningState.RECORDING: # "recording"
self.voice_loop.start_recording(event.data.get("recording_name"))
self.voice_loop.start_recording(message.data.get("recording_name"))
else:
LOG.error(f"Invalid listening state: {state}")

Expand All @@ -709,33 +705,36 @@ def _handle_change_state(self, event):
else:
LOG.error(f"Invalid listen mode: {mode}")

self._handle_get_state(event)
self._handle_get_state(message)

def _handle_get_state(self, event):
def _handle_get_state(self, message: Message):
"""Query listening state"""
# TODO - unify this api, should match ovos-listener exactly
data = {'mode': self.voice_loop.listen_mode,
"state": self.voice_loop.state}
self.bus.emit(event.reply("recognizer_loop:state", data))
self.bus.emit(message.reply("recognizer_loop:state", data))

def _handle_stop_recording(self, event):
def _handle_stop_recording(self, message: Message):
"""Stop current recording session """
self.voice_loop.stop_recording()
sound = self.config.get('sounds', {}).get('end_listening')
if sound:
self.bus.emit(message.forward("mycroft.audio.play_sound", {"uri": sound}))

def _handle_extend_listening(self, event):
def _handle_extend_listening(self, message: Message):
""" when a skill is activated (converse) reset the timeout until wakeword is needed again
only used when in hybrid listening mode """
if self.voice_loop.listen_mode == ListeningMode.HYBRID:
self.voice_loop.last_ww = time.time()

def _handle_sleep(self, event):
def _handle_sleep(self, message: Message):
"""Put the voice loop to sleep."""
self.voice_loop.go_to_sleep()

def _handle_wake_up(self, event):
def _handle_wake_up(self, message: Message):
"""Wake up the voice loop."""
self.voice_loop.wakeup()
self.bus.emit(Message("mycroft.awoken"))
self.bus.emit(message.reply("mycroft.awoken"))

# OPM bus api
def _handle_get_languages_stt(self, message):
Expand Down
Loading