Add WAKE_START state

This commit is contained in:
Michael Hansen
2023-12-08 12:41:15 -06:00
parent f481eca0e9
commit 3f5697f76a
+28 -23
View File
@@ -36,6 +36,7 @@ _DIR = Path(__file__).parent
class SatelliteState(str, Enum): class SatelliteState(str, Enum):
WAKE_START = "wake-start"
WAKE = "wake" WAKE = "wake"
ASR = "asr" ASR = "asr"
VAD_RESET = "vad-reset" VAD_RESET = "vad-reset"
@@ -354,11 +355,9 @@ class SatelliteEventHandler(AsyncEventHandler):
return True return True
if RunSatellite.is_type(event.type) and (self.satellite_task is None): if RunSatellite.is_type(event.type) and (self.satellite_task is None):
self.after_tts_state = None
if self.has_wake: if self.has_wake:
# Local wake word detection # Local wake word detection
self.state = SatelliteState.WAKE self.state = SatelliteState.WAKE_START
self.satellite_task = asyncio.create_task(self.run_satellite_wake()) self.satellite_task = asyncio.create_task(self.run_satellite_wake())
else: else:
# Remote wake word detection # Remote wake word detection
@@ -371,12 +370,11 @@ class SatelliteEventHandler(AsyncEventHandler):
# STT transcript # STT transcript
await self._forward_event(event) await self._forward_event(event)
_LOGGER.debug(event) _LOGGER.debug("-> %s", event)
if self.has_wake: if self.has_wake:
# Ready for next wake word detection # Ready for next wake word detection
self.state = SatelliteState.WAKE self.state = SatelliteState.WAKE_START
_LOGGER.info("Detecting wake word")
else: else:
self.state = SatelliteState.VAD_RESET self.state = SatelliteState.VAD_RESET
elif self.has_snd and ( elif self.has_snd and (
@@ -395,7 +393,7 @@ class SatelliteEventHandler(AsyncEventHandler):
self.state = SatelliteState.PLAYING_AUDIO self.state = SatelliteState.PLAYING_AUDIO
await self._forward_event(event) await self._forward_event(event)
_LOGGER.debug(event) _LOGGER.debug("-> %s", event)
elif AudioChunk.is_type(event.type) and (self.snd_client is not None): elif AudioChunk.is_type(event.type) and (self.snd_client is not None):
# Forward to sound service # Forward to sound service
event = self._process_snd_audio(event) event = self._process_snd_audio(event)
@@ -403,7 +401,7 @@ class SatelliteEventHandler(AsyncEventHandler):
elif AudioStop.is_type(event.type): elif AudioStop.is_type(event.type):
# TTS stop # TTS stop
await self._forward_event(event) await self._forward_event(event)
_LOGGER.debug(event) _LOGGER.debug("-> %s", event)
if self.snd_client is not None: if self.snd_client is not None:
await self.snd_client.disconnect() await self.snd_client.disconnect()
@@ -421,7 +419,7 @@ class SatelliteEventHandler(AsyncEventHandler):
self.state = last_state self.state = last_state
await self._forward_event(event) await self._forward_event(event)
_LOGGER.debug(event) _LOGGER.debug("-> %s", event)
elif ( elif (
Detect.is_type(event.type) Detect.is_type(event.type)
or Transcribe.is_type(event.type) or Transcribe.is_type(event.type)
@@ -435,7 +433,7 @@ class SatelliteEventHandler(AsyncEventHandler):
# - VoiceStarted/VoiceStopped for when user starts/stops speaking # - VoiceStarted/VoiceStopped for when user starts/stops speaking
# - Synthesize for TTS text # - Synthesize for TTS text
await self._forward_event(event) await self._forward_event(event)
_LOGGER.debug(event) _LOGGER.debug("-> %s", event)
elif Error.is_type(event.type): elif Error.is_type(event.type):
await self._forward_event(event) await self._forward_event(event)
_LOGGER.warning(event) _LOGGER.warning(event)
@@ -456,7 +454,7 @@ class SatelliteEventHandler(AsyncEventHandler):
try: try:
mic_client = self._make_mic_client() mic_client = self._make_mic_client()
async with mic_client: async with mic_client:
while True: while self.is_running:
mic_event = await mic_client.read_event() mic_event = await mic_client.read_event()
if mic_event is None: if mic_event is None:
_LOGGER.warning("Microphone service disconnected") _LOGGER.warning("Microphone service disconnected")
@@ -542,24 +540,31 @@ class SatelliteEventHandler(AsyncEventHandler):
wake_client = AsyncClient.from_uri(self.cli_args.wake_uri) wake_client = AsyncClient.from_uri(self.cli_args.wake_uri)
async with mic_client, wake_client: async with mic_client, wake_client:
_LOGGER.info("Detecting wake word")
if self.cli_args.wake_word_name:
# Request specific wake word(s)
detect_event = Detect(names=self.cli_args.wake_word_name).event()
else:
detect_event = Detect().event()
await wake_client.write_event(detect_event)
await self._forward_event(detect_event)
_LOGGER.debug(detect_event)
# Read events in parallel # Read events in parallel
mic_task = asyncio.create_task(mic_client.read_event()) mic_task = asyncio.create_task(mic_client.read_event())
wake_task = asyncio.create_task(wake_client.read_event()) wake_task = asyncio.create_task(wake_client.read_event())
pending = {mic_task, wake_task} pending = {mic_task, wake_task}
while self.is_running: while self.is_running:
if self.state == SatelliteState.WAKE_START:
# Send detect event
_LOGGER.info("Detecting wake word")
if self.cli_args.wake_word_name:
# Request specific wake word(s)
detect_event = Detect(
names=self.cli_args.wake_word_name
).event()
else:
detect_event = Detect().event()
_LOGGER.debug("<- %s", detect_event)
await wake_client.write_event(detect_event)
await self._forward_event(detect_event)
self.state = SatelliteState.WAKE
# Wait for mic/wake events
done, pending = await asyncio.wait( done, pending = await asyncio.wait(
pending, return_when=asyncio.FIRST_COMPLETED pending, return_when=asyncio.FIRST_COMPLETED
) )