client/speech/__main__.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 from threading import Lock
16 
17 from mycroft import dialog
18 from mycroft.enclosure.api import EnclosureAPI
19 from mycroft.client.speech.listener import RecognizerLoop
20 from mycroft.configuration import Configuration
21 from mycroft.identity import IdentityManager
22 from mycroft.lock import Lock as PIDLock # Create/Support PID locking file
23 from mycroft.messagebus.client.ws import WebsocketClient
24 from mycroft.messagebus.message import Message
25 from mycroft.util import create_daemon, wait_for_exit_signal, \
26  reset_sigint_handler
27 from mycroft.util.log import LOG
28 
29 bus = None # Mycroft messagebus connection
30 lock = Lock()
31 loop = None
32 config = None
33 
34 
36  LOG.info("Begin Recording...")
37  bus.emit(Message('recognizer_loop:record_begin'))
38 
39 
41  LOG.info("End Recording...")
42  bus.emit(Message('recognizer_loop:record_end'))
43 
44 
46  LOG.debug("Notifying enclosure of no internet connection")
47  bus.emit(Message('enclosure.notify.no_internet'))
48 
49 
51  """ Forward mycroft.awoken to the messagebus. """
52  LOG.info("Listener is now Awake: ")
53  bus.emit(Message('mycroft.awoken'))
54 
55 
56 def handle_wakeword(event):
57  LOG.info("Wakeword Detected: " + event['utterance'])
58  bus.emit(Message('recognizer_loop:wakeword', event))
59 
60 
61 def handle_utterance(event):
62  LOG.info("Utterance: " + str(event['utterances']))
63  context = {'client_name': 'mycroft_listener'}
64  if 'ident' in event:
65  ident = event.pop('ident')
66  context['ident'] = ident
67  bus.emit(Message('recognizer_loop:utterance', event, context))
68 
69 
71  bus.emit(Message('mycroft.speech.recognition.unknown'))
72 
73 
74 def handle_speak(event):
75  """
76  Forward speak message to message bus.
77  """
78  bus.emit(Message('speak', event))
79 
80 
82  LOG.info("Failed to find intent.")
83  data = {'utterance': dialog.get('not.loaded')}
84  bus.emit(Message('speak', data))
85 
86 
87 def handle_sleep(event):
88  loop.sleep()
89 
90 
91 def handle_wake_up(event):
92  loop.awaken()
93 
94 
95 def handle_mic_mute(event):
96  loop.mute()
97 
98 
99 def handle_mic_unmute(event):
100  loop.unmute()
101 
102 
104  """
105  Query microphone mute status.
106  """
107  data = {'muted': loop.is_muted()}
108  bus.emit(event.response(data))
109 
110 
111 def handle_paired(event):
112  IdentityManager.update(event.data)
113 
114 
116  """
117  Mute recognizer loop
118  """
119  if config.get("listener").get("mute_during_output"):
120  loop.mute()
121 
122 
123 def handle_audio_end(event):
124  """
125  Request unmute, if more sources have requested the mic to be muted
126  it will remain muted.
127  """
128  if config.get("listener").get("mute_during_output"):
129  loop.unmute() # restore
130 
131 
132 def handle_stop(event):
133  """
134  Handler for mycroft.stop, i.e. button press
135  """
136  loop.force_unmute()
137 
138 
140  # TODO: Move this into the Enclosure (not speech client)
141  # Reset the UI to indicate ready for speech processing
142  EnclosureAPI(bus).reset()
143 
144 
145 def main():
146  global bus
147  global loop
148  global config
150  PIDLock("voice")
151  bus = WebsocketClient() # Mycroft messagebus, see mycroft.messagebus
152  Configuration.init(bus)
153  config = Configuration.get()
154 
155  # Register handlers on internal RecognizerLoop bus
156  loop = RecognizerLoop()
157  loop.on('recognizer_loop:utterance', handle_utterance)
158  loop.on('recognizer_loop:speech.recognition.unknown', handle_unknown)
159  loop.on('speak', handle_speak)
160  loop.on('recognizer_loop:record_begin', handle_record_begin)
161  loop.on('recognizer_loop:awoken', handle_awoken)
162  loop.on('recognizer_loop:wakeword', handle_wakeword)
163  loop.on('recognizer_loop:record_end', handle_record_end)
164  loop.on('recognizer_loop:no_internet', handle_no_internet)
165 
166  # Register handlers for events on main Mycroft messagebus
167  bus.on('open', handle_open)
168  bus.on('complete_intent_failure', handle_complete_intent_failure)
169  bus.on('recognizer_loop:sleep', handle_sleep)
170  bus.on('recognizer_loop:wake_up', handle_wake_up)
171  bus.on('mycroft.mic.mute', handle_mic_mute)
172  bus.on('mycroft.mic.unmute', handle_mic_unmute)
173  bus.on('mycroft.mic.get_status', handle_mic_get_status)
174  bus.on("mycroft.paired", handle_paired)
175  bus.on('recognizer_loop:audio_output_start', handle_audio_start)
176  bus.on('recognizer_loop:audio_output_end', handle_audio_end)
177  bus.on('mycroft.stop', handle_stop)
178 
179  create_daemon(bus.run_forever)
180  create_daemon(loop.run)
181 
183 
184 
185 if __name__ == "__main__":
186  main()
def create_daemon(target, args=(), kwargs=None)
def get(phrase, lang=None, context=None)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40