mycroft_stt.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 import rospy
17 from mycroft_ros.msg import Speech
18 
19 from mycroft import dialog
20 from mycroft.enclosure.api import EnclosureAPI
21 from mycroft.client.speech.listener import RecognizerLoop
22 from mycroft.configuration import Configuration
23 from mycroft.identity import IdentityManager
24 from mycroft.lock import Lock as PIDLock # Create/Support PID locking file
25 from mycroft.messagebus.client.ws import WebsocketClient
26 from mycroft.messagebus.message import Message
27 from mycroft.util import create_daemon, wait_for_exit_signal, \
28  reset_sigint_handler
29 from mycroft.util.log import LOG
30 
31 bus = None # Mycroft messagebus connection
32 lock = Lock()
33 loop = None
34 config = None
35 speech_pub = rospy.Publisher("mycroft/speech", Speech, queue_size=10)
36 
37 
39  LOG.info("Begin Recording...")
40  bus.emit(Message('recognizer_loop:record_begin'))
41 
42 
44  LOG.info("End Recording...")
45  bus.emit(Message('recognizer_loop:record_end'))
46 
47 
49  LOG.debug("Notifying enclosure of no internet connection")
50  bus.emit(Message('enclosure.notify.no_internet'))
51 
52 
54  """ Forward mycroft.awoken to the messagebus. """
55  LOG.info("Listener is now Awake: ")
56  bus.emit(Message('mycroft.awoken'))
57 
58 
59 def handle_wakeword(event):
60  LOG.info("Wakeword Detected: " + event['utterance'])
61  bus.emit(Message('recognizer_loop:wakeword', event))
62 
63 
64 def handle_utterance(event):
65  LOG.info("Utterance: " + str(event['utterances']))
66  context = {'client_name': 'mycroft_listener'}
67  if 'ident' in event:
68  ident = event.pop('ident')
69  context['ident'] = ident
70  bus.emit(Message('recognizer_loop:utterance', event, context))
71  speech_pub.publish(Speech(event["utterances"]))
72 
73 
75  bus.emit(Message('mycroft.speech.recognition.unknown'))
76 
77 
78 def handle_speak(event):
79  """
80  Forward speak message to message bus.
81  """
82  bus.emit(Message('speak', event))
83 
84 
86  LOG.info("Failed to find intent.")
87  data = {'utterance': dialog.get('not.loaded')}
88  bus.emit(Message('speak', data))
89 
90 
91 def handle_sleep(event):
92  loop.sleep()
93 
94 
95 def handle_wake_up(event):
96  loop.awaken()
97 
98 
99 def handle_mic_mute(event):
100  loop.mute()
101 
102 
103 def handle_mic_unmute(event):
104  loop.unmute()
105 
106 
108  """
109  Query microphone mute status.
110  """
111  data = {'muted': loop.is_muted()}
112  bus.emit(event.response(data))
113 
114 
115 def handle_paired(event):
116  IdentityManager.update(event.data)
117 
118 
120  """
121  Mute recognizer loop
122  """
123  if config.get("listener").get("mute_during_output"):
124  loop.mute()
125 
126 
127 def handle_audio_end(event):
128  """
129  Request unmute, if more sources have requested the mic to be muted
130  it will remain muted.
131  """
132  if config.get("listener").get("mute_during_output"):
133  loop.unmute() # restore
134 
135 
136 def handle_stop(event):
137  """
138  Handler for mycroft.stop, i.e. button press
139  """
140  loop.force_unmute()
141 
142 
144  # TODO: Move this into the Enclosure (not speech client)
145  # Reset the UI to indicate ready for speech processing
146  EnclosureAPI(bus).reset()
147 
148 
149 def main():
150  rospy.init_node('mycroft_stt')
151  rospy.loginfo(rospy.get_caller_id() + " started")
152  global bus
153  global loop
154  global config
156  PIDLock("voice")
157  bus = WebsocketClient() # Mycroft messagebus, see mycroft.messagebus
158  Configuration.init(bus)
159  config = Configuration.get()
160 
161  # Register handlers on internal RecognizerLoop bus
162  loop = RecognizerLoop()
163  loop.on('recognizer_loop:utterance', handle_utterance)
164  loop.on('recognizer_loop:speech.recognition.unknown', handle_unknown)
165  loop.on('speak', handle_speak)
166  loop.on('recognizer_loop:record_begin', handle_record_begin)
167  loop.on('recognizer_loop:awoken', handle_awoken)
168  loop.on('recognizer_loop:wakeword', handle_wakeword)
169  loop.on('recognizer_loop:record_end', handle_record_end)
170  loop.on('recognizer_loop:no_internet', handle_no_internet)
171 
172  # Register handlers for events on main Mycroft messagebus
173  bus.on('open', handle_open)
174  bus.on('complete_intent_failure', handle_complete_intent_failure)
175  bus.on('recognizer_loop:sleep', handle_sleep)
176  bus.on('recognizer_loop:wake_up', handle_wake_up)
177  bus.on('mycroft.mic.mute', handle_mic_mute)
178  bus.on('mycroft.mic.unmute', handle_mic_unmute)
179  bus.on('mycroft.mic.get_status', handle_mic_get_status)
180  bus.on("mycroft.paired", handle_paired)
181  bus.on('recognizer_loop:audio_output_start', handle_audio_start)
182  bus.on('recognizer_loop:audio_output_end', handle_audio_end)
183  bus.on('mycroft.stop', handle_stop)
184 
185  create_daemon(bus.run_forever)
186  create_daemon(loop.run)
187 
189  rospy.spin()
190 
191 
192 if __name__ == "__main__":
193  main()
def create_daemon(target, args=(), kwargs=None)
def handle_utterance(event)
Definition: mycroft_stt.py:64
def handle_unknown()
Definition: mycroft_stt.py:74
def handle_awoken()
Definition: mycroft_stt.py:53
def handle_mic_unmute(event)
Definition: mycroft_stt.py:103
def handle_record_end()
Definition: mycroft_stt.py:43
def handle_mic_get_status(event)
Definition: mycroft_stt.py:107
def handle_speak(event)
Definition: mycroft_stt.py:78
def handle_paired(event)
Definition: mycroft_stt.py:115
def handle_stop(event)
Definition: mycroft_stt.py:136
def handle_audio_end(event)
Definition: mycroft_stt.py:127
def handle_sleep(event)
Definition: mycroft_stt.py:91
def handle_record_begin()
Definition: mycroft_stt.py:38
def handle_audio_start(event)
Definition: mycroft_stt.py:119
def handle_wake_up(event)
Definition: mycroft_stt.py:95
def handle_no_internet()
Definition: mycroft_stt.py:48
def handle_mic_mute(event)
Definition: mycroft_stt.py:99
def handle_complete_intent_failure(event)
Definition: mycroft_stt.py:85
def handle_open()
Definition: mycroft_stt.py:143
def get(phrase, lang=None, context=None)
def handle_wakeword(event)
Definition: mycroft_stt.py:59


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