forked from NeonGeckoCom/skill-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
242 lines (221 loc) · 10 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2022 Neongecko.com Inc.
# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds,
# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo
# BSD-3 License
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from copy import deepcopy
from tempfile import mkstemp
from threading import Event
from time import sleep
from typing import Optional
from mycroft_bus_client import Message
from neon_utils import LOG
from neon_utils.message_utils import get_message_user, dig_for_message
from neon_utils.signal_utils import wait_for_signal_clear
from neon_utils.skills import NeonSkill
from neon_utils.user_utils import get_user_prefs
from neon_utils.file_utils import load_commented_file
from ovos_plugin_manager.templates import TTS
from ovos_utils.sound import play_wav
from mycroft.skills import intent_file_handler
class DemoSkill(NeonSkill):
def __init__(self):
super(DemoSkill, self).__init__(name="DemoSkill")
self._active_demos = dict()
self._audio_output_done = Event()
@property
def demo_tts_plugin(self) -> str:
"""
Get the TTS engine spec to use for the demo user
"""
return self.settings.get("demo_tts_engine") or \
self.config_core["tts"].get("fallback_module") or \
"ovos-tts-plugin-mimic"
@property
def speak_timeout(self):
return self.settings.get("speak_timeout") or self._speak_timeout
@property
def intent_timeout(self):
return self.settings.get("intent_timeout") or 10
@property
def demo_filename(self):
"""
Get the name of the demo text resource file (including extension)
"""
return self.settings.get("filename") or "demo.txt"
def initialize(self):
# When demo prompt enabled, wait for load and prompt user
if self.settings.get("prompt_on_start"):
self.bus.once('mycroft.ready', self._show_demo_prompt)
self.add_event("recognizer_loop:audio_output_start",
self._audio_started)
self.add_event("recognizer_loop:audio_output_end", self._audio_stopped)
def _audio_started(self, _):
self._audio_output_done.clear()
def _audio_stopped(self, _):
self._audio_output_done.set()
def _show_demo_prompt(self, message):
"""
Handles first run demo prompt
:param message: message object associated with loaded emit
"""
LOG.debug("Prompting Demo!")
self.make_active()
show_demo = self.ask_yesno("ask_demo")
if show_demo == "yes":
message.context['neon_should_respond'] = True
message.context['username'] = 'local'
self.handle_show_demo(message)
return
elif show_demo == "no":
ask_next_time = self.ask_yesno("ask_demo_next_time")
if ask_next_time == "yes":
self.speak_dialog("confirm_demo_enabled")
self.update_skill_settings({"prompt_on_start": True})
return
self.speak_dialog("confirm_demo_disabled")
else:
self.speak_dialog("confirm_demo_disabled")
self.update_skill_settings({"prompt_on_start": False})
@intent_file_handler("show_demo.intent")
def handle_show_demo(self, message):
"""
Starts a brief demo
:param message: message object associated with request
"""
if not self.neon_in_request(message):
return
# TODO: Parse demo language from request
lang = self.lang
# Track demo state for the user
user = get_message_user(message)
self._active_demos[user] = Event()
# Define a demo profile so user profile isn't modified
profile = deepcopy(get_user_prefs(message))
profile['user']['username'] = 'demo'
profile['units']['measure'] = 'imperial'
# Confirm demo is starting
self._audio_output_done.clear() # Clear signal to wait for intro speak
self.speak_dialog("starting_demo")
# Read the demo prompts
demo_prompts = load_commented_file(self.find_resource(
self.demo_filename)).split('\n')
# Define message context for the demo actions
message_context = deepcopy(message.context)
message_context['neon_should_respond'] = True
message_context['username'] = 'demo'
message_context['user_profiles'] = [profile]
message_context['source'] = ['demo']
# Define a message that will be updated with any profile changes
message = Message("recognizer_loop:utterance", context=message_context)
prompter = {"name": "Demo",
"language": lang,
"gender": "female" if profile['speech'].get('tts_gender')
== "male" else "female"}
# Initialize the demo TTS
tts = self._get_demo_tts()
# Iterate over demo prompts until done or user says 'stop'
for prompt in demo_prompts:
if self._active_demos[user].is_set():
# Check to stop before speaking the prompt
break
self._speak_prompt(prompt, prompter, tts)
if self._active_demos[user].is_set():
# Check to stop before executing the prompt
break
LOG.info(message.context['user_profiles'][0]['units']['measure'])
message.data = {"lang": lang,
"utterances": [prompt.lower()]}
self._send_prompt(message)
self.speak_dialog("finished_demo")
self._active_demos.pop(user)
def _send_prompt(self, message: Message):
"""
Send a message to skill processing and wait for it to be handled
:param message: Message to emit to skills
"""
self._audio_output_done.clear() # Clear to wait for this response
resp = self.bus.wait_for_response(message,
"mycroft.skill.handler.complete",
self.intent_timeout)
if not resp:
LOG.error(f"Handler not completed for: "
f"{message.data.get('utterances')}")
else:
message.context['user_profiles'] = resp.context['user_profiles']
if not self._audio_output_done.wait(self.speak_timeout):
LOG.error(f"Timed out waiting")
else:
sleep(0.5)
# Wait for anything not yet in the audio queue
wait_for_signal_clear("isSpeaking", self.speak_timeout)
def _speak_prompt(self, prompt: str, prompter: dict, tts: Optional[TTS]):
"""
Speak the prompt in a user's voice and wait for playback to end
:param prompt: User request to speak that will be emitted to skills
:param prompter: Speaker config to use for spoken prompts
"""
self._audio_output_done.wait(self.speak_timeout)
if tts:
# If available, use skill-managed TTS
_, output_file = mkstemp()
wav_file, _ = tts.get_tts(prompt, output_file)
# TODO: If server, self.send_with_audio
play_wav(wav_file,
self.config_core.get("play_wav_cmdline")).wait(
self.speak_timeout)
else:
# Else fallback to audio module (probably same voice
self.speak(prompt, speaker=prompter)
self._audio_output_done.wait(self.speak_timeout)
def _get_demo_tts(self, lang: str = None) -> Optional[TTS]:
"""
Create a TTS plugin instance to
"""
from ovos_plugin_manager.tts import OVOSTTSFactory
engine = self.demo_tts_plugin
lang = lang or self.lang
config = {"module": engine,
"lang": lang}
try:
return OVOSTTSFactory.create(config)
except Exception as e:
LOG.error(f"Failed to load TTS Plugin: {self.demo_tts_plugin}")
LOG.error(e)
try:
LOG.info("Trying with configured fallback_module")
config['module'] = self.config_core["tts"].get("fallback_module")
return OVOSTTSFactory.create(config)
except Exception as e:
LOG.error(f"Failed to load TTS Plugin: {self.demo_tts_plugin}")
LOG.error(e)
return None
def stop(self):
user = get_message_user(dig_for_message())
if user in self._active_demos:
LOG.info(f"{user} requested stop")
self._active_demos[user].set()
def create_skill():
return DemoSkill()