diff --git a/mycroft/configuration/mycroft.conf b/mycroft/configuration/mycroft.conf index 7c35091a48..d827bc1e65 100644 --- a/mycroft/configuration/mycroft.conf +++ b/mycroft/configuration/mycroft.conf @@ -44,7 +44,8 @@ // File locations of sounds to play for system events "sounds": { "start_listening": "snd/start_listening.wav", - "end_listening": "snd/end_listening.wav" + "end_listening": "snd/end_listening.wav", + "acknowledge": "snd/acknowledge.mp3" }, // Mechanism used to play WAV audio files diff --git a/mycroft/res/snd/acknowledge.mp3 b/mycroft/res/snd/acknowledge.mp3 new file mode 100644 index 0000000000..efb06b4975 Binary files /dev/null and b/mycroft/res/snd/acknowledge.mp3 differ diff --git a/mycroft/skills/core.py b/mycroft/skills/core.py index 06febb605a..7d630709b5 100644 --- a/mycroft/skills/core.py +++ b/mycroft/skills/core.py @@ -44,7 +44,9 @@ from mycroft.skills.settings import SkillSettings from mycroft.skills.skill_data import (load_vocabulary, load_regex, to_alnum, munge_regex, munge_intent_parser, read_vocab_file) -from mycroft.util import camel_case_split, resolve_resource_file +from mycroft.util import (camel_case_split, + resolve_resource_file, + play_audio_file) from mycroft.util.log import LOG MainModule = '__init__' @@ -1648,6 +1650,26 @@ class MycroftSkill: for e in list(self.scheduled_repeats): self.cancel_scheduled_event(e) + def acknowledge(self): + """ Acknowledge a successful request. + + This method plays a sound to acknowledge a request that does not + require a verbal response. This is intended to provide simple feedback + to the user that their request was handled successfully. + """ + file = resolve_resource_file( + self.config_core.get('sounds').get('acknowledge')) + + if not file: + LOG.warning("Could not find 'acknowledge' audio file!") + return + + process = play_audio_file(file) + if process: + process.wait() + else: + LOG.warning("Unable to play 'acknowledge' audio file!") + ####################################################################### # FallbackSkill base class diff --git a/mycroft/util/__init__.py b/mycroft/util/__init__.py index 4db7e00fc3..1b19dae6d1 100644 --- a/mycroft/util/__init__.py +++ b/mycroft/util/__init__.py @@ -18,7 +18,7 @@ import socket import subprocess import pyaudio -from os.path import join, expanduser +from os.path import join, expanduser, splitext from threading import Thread from time import sleep @@ -93,6 +93,36 @@ def resolve_resource_file(res_name): return None # Resource cannot be resolved +def play_audio_file(uri: str): + """ Play an audio file. + + This wraps the other play_* functions, choosing the correct one based on + the file extension. The function will return directly and play the file + in the background. + + Arguments: + uri: uri to play + + Returns: subprocess.Popen object. None if the format is not supported or + an error occurs playing the file. + + """ + extension_to_function = { + '.wav': play_wav, + '.mp3': play_mp3, + '.ogg': play_ogg + } + _, extension = splitext(uri) + play_function = extension_to_function.get(extension.lower()) + if play_function: + return play_function(uri) + else: + LOG.error("Could not find a function capable of playing {uri}." + " Supported formats are {keys}." + .format(uri=uri, keys=list(extension_to_function.keys()))) + return None + + def play_wav(uri): """ Play a wav-file.