-
Notifications
You must be signed in to change notification settings - Fork 105
/
satellite.py
1239 lines (1008 loc) · 44.5 KB
/
satellite.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Satellite code."""
import asyncio
import logging
import math
import time
import wave
from enum import Enum, auto
from pathlib import Path
from typing import Callable, Dict, Final, Optional, Set, Union
from pyring_buffer import RingBuffer
from wyoming.asr import Transcript
from wyoming.audio import AudioChunk, AudioStart, AudioStop
from wyoming.client import AsyncClient
from wyoming.error import Error
from wyoming.event import Event, async_write_event
from wyoming.mic import MicProcessAsyncClient
from wyoming.ping import Ping, Pong
from wyoming.pipeline import PipelineStage, RunPipeline
from wyoming.satellite import (
PauseSatellite,
RunSatellite,
SatelliteConnected,
SatelliteDisconnected,
StreamingStarted,
StreamingStopped,
)
from wyoming.snd import SndProcessAsyncClient, Played
from wyoming.tts import Synthesize
from wyoming.vad import VoiceStarted, VoiceStopped
from wyoming.wake import Detect, Detection, WakeProcessAsyncClient
from .settings import SatelliteSettings
from .utils import DebugAudioWriter, multiply_volume, run_event_command, wav_to_events
from .vad import SileroVad
from .webrtc import WebRtcAudio
_LOGGER = logging.getLogger()
_PONG_TIMEOUT: Final = 5
_PING_SEND_DELAY: Final = 2
class State(Enum):
NOT_STARTED = auto()
STARTING = auto()
STARTED = auto()
RESTARTING = auto()
STOPPING = auto()
STOPPED = auto()
# -----------------------------------------------------------------------------
class SatelliteBase:
"""Base class for satellites."""
def __init__(self, settings: SatelliteSettings) -> None:
self.settings = settings
self.server_id: Optional[str] = None
self._state = State.NOT_STARTED
self._state_changed = asyncio.Event()
self._writer: Optional[asyncio.StreamWriter] = None
self._mic_task: Optional[asyncio.Task] = None
self._mic_webrtc: Optional[Callable[[bytes], bytes]] = None
self._snd_task: Optional[asyncio.Task] = None
self._snd_queue: "Optional[asyncio.Queue[Event]]" = None
self._wake_task: Optional[asyncio.Task] = None
self._wake_queue: "Optional[asyncio.Queue[Event]]" = None
self._event_task: Optional[asyncio.Task] = None
self._event_queue: "Optional[asyncio.Queue[Event]]" = None
self._ping_server_enabled: bool = False
self._pong_received_event = asyncio.Event()
self._ping_server_task: Optional[asyncio.Task] = None
self.microphone_muted = False
self._unmute_microphone_task: Optional[asyncio.Task] = None
# Debug audio recording
self.wake_audio_writer: Optional[DebugAudioWriter] = None
self.stt_audio_writer: Optional[DebugAudioWriter] = None
if settings.debug_recording_dir:
self.wake_audio_writer = DebugAudioWriter(
settings.debug_recording_dir,
"wake",
ring_buffer_size=(2 * 16000 * 2 * 1), # last 2 sec
)
self.stt_audio_writer = DebugAudioWriter(
settings.debug_recording_dir, "stt"
)
@property
def is_running(self) -> bool:
"""True if not stopping/stopped."""
return self._state not in (State.STOPPED,)
@property
def state(self) -> State:
"""Get current state."""
return self._state
@state.setter
def state(self, value: State) -> None:
"""Set state."""
self._state = value
self._state_changed.set()
self._state_changed.clear()
async def run(self) -> None:
"""Run main satellite loop."""
while self.is_running:
try:
if self.state == State.NOT_STARTED:
await self._start()
elif self.state == State.RESTARTING:
await self._restart()
elif self.state == State.STOPPING:
await self._stop()
elif self.state == State.STOPPED:
await self.stopped()
break
else:
await self._state_changed.wait()
except Exception:
if self.is_running:
# Automatically restart
_LOGGER.exception("Unexpected error running satellite")
self.state = State.RESTARTING
async def stop(self) -> None:
"""Stop the satellite"""
self.state = State.STOPPING
# Wait for STOPPED
while self.state != State.STOPPED:
await self._state_changed.wait()
async def set_server(self, server_id: str, writer: asyncio.StreamWriter) -> None:
"""Set event writer."""
self.server_id = server_id
self._writer = writer
_LOGGER.debug("Server set: %s", server_id)
await self.trigger_server_connected()
async def clear_server(self) -> None:
"""Remove writer."""
self.server_id = None
self._writer = None
self._disable_ping()
_LOGGER.debug("Server disconnected")
await self.trigger_server_disonnected()
async def event_to_server(self, event: Event) -> None:
"""Send an event to the server."""
if self._writer is None:
return
try:
await async_write_event(event, self._writer)
except Exception as err:
await self.clear_server()
if isinstance(err, ConnectionResetError):
_LOGGER.warning("Server disconnected unexpectedly")
else:
_LOGGER.exception("Unexpected error sending event to server")
def _enable_ping(self) -> None:
self._ping_server_enabled = True
self._ping_server_task = asyncio.create_task(self._ping_server(), name="ping")
def _disable_ping(self) -> None:
self._ping_server_enabled = False
if self._ping_server_task is not None:
self._ping_server_task.cancel()
self._ping_server_task = None
async def _ping_server(self) -> None:
try:
while self.is_running:
await asyncio.sleep(_PING_SEND_DELAY)
if (self.server_id is None) or (not self._ping_server_enabled):
# No server connected
continue
# Send ping and wait for pong
self._pong_received_event.clear()
await self.event_to_server(Ping().event())
try:
await asyncio.wait_for(
self._pong_received_event.wait(), timeout=_PONG_TIMEOUT
)
except asyncio.TimeoutError:
if self.server_id is None:
# No server connected
continue
_LOGGER.warning("Did not receive ping response within timeout")
await self.clear_server()
except asyncio.CancelledError:
pass
except Exception:
_LOGGER.exception("Unexpected error in ping server task")
# -------------------------------------------------------------------------
async def _start(self) -> None:
"""Connect to services."""
self.state = State.STARTING
await self._connect_to_services()
self.state = State.STARTED
await self.started()
async def started(self) -> None:
"""Called when satellite has started."""
async def _stop(self) -> None:
"""Disconnect from services."""
self.server_id = None
self._writer = None
await self._disconnect_from_services()
self._disable_ping()
self.state = State.STOPPED
async def stopped(self) -> None:
"""Called when satellite has stopped."""
async def event_from_server(self, event: Event) -> None:
"""Called when an event is received from the server."""
if Ping.is_type(event.type):
# Respond with pong
ping = Ping.from_event(event)
await self.event_to_server(Pong(text=ping.text).event())
if not self._ping_server_enabled:
# Enable pinging
self._enable_ping()
_LOGGER.debug("Ping enabled")
elif Pong.is_type(event.type):
# Response from our ping
self._pong_received_event.set()
elif AudioChunk.is_type(event.type):
# TTS audio
await self.event_to_snd(event)
elif AudioStart.is_type(event.type):
# TTS started
await self.event_to_snd(event)
await self.trigger_tts_start()
elif AudioStop.is_type(event.type):
# TTS stopped
await self.event_to_snd(event)
await self.trigger_tts_stop()
elif Detect.is_type(event.type):
# Wake word detection started
await self.trigger_detect()
elif Detection.is_type(event.type):
# Wake word detected
_LOGGER.debug("Wake word detected")
await self.trigger_detection(Detection.from_event(event))
elif VoiceStarted.is_type(event.type):
# STT start
await self.trigger_stt_start()
elif VoiceStopped.is_type(event.type):
# STT stop
await self.trigger_stt_stop()
elif Transcript.is_type(event.type):
# STT text
_LOGGER.debug(event)
await self.trigger_transcript(Transcript.from_event(event))
elif Synthesize.is_type(event.type):
# TTS request
_LOGGER.debug(event)
await self.trigger_synthesize(Synthesize.from_event(event))
elif Error.is_type(event.type):
_LOGGER.warning(event)
await self.trigger_error(Error.from_event(event))
# Forward everything except audio to event service
if not AudioChunk.is_type(event.type):
await self.forward_event(event)
async def _send_run_pipeline(self) -> None:
"""Sends a RunPipeline event with the correct stages."""
if self.settings.wake.enabled:
# Local wake word detection
start_stage = PipelineStage.ASR
restart_on_end = False
else:
# Remote wake word detection
start_stage = PipelineStage.WAKE
restart_on_end = not self.settings.vad.enabled
if self.settings.snd.enabled:
# Play TTS response
end_stage = PipelineStage.TTS
else:
# No audio output
end_stage = PipelineStage.HANDLE
run_pipeline = RunPipeline(
start_stage=start_stage, end_stage=end_stage, restart_on_end=restart_on_end
).event()
await self.event_to_server(run_pipeline)
await self.forward_event(run_pipeline)
async def _restart(self) -> None:
"""Disconnects from services and restarts loop."""
self.state = State.RESTARTING
await self._disconnect_from_services()
_LOGGER.debug("Restarting in %s second(s)", self.settings.restart_timeout)
await asyncio.sleep(self.settings.restart_timeout)
self.state = State.NOT_STARTED
async def _connect_to_services(self) -> None:
"""Connects to configured services."""
if self.settings.mic.enabled:
_LOGGER.debug(
"Connecting to mic service: %s",
self.settings.mic.uri or self.settings.mic.command,
)
self._mic_task = asyncio.create_task(self._mic_task_proc(), name="mic")
if self.settings.snd.enabled:
_LOGGER.debug(
"Connecting to snd service: %s",
self.settings.snd.uri or self.settings.snd.command,
)
self._snd_task = asyncio.create_task(self._snd_task_proc(), name="snd")
if self.settings.wake.enabled:
_LOGGER.debug(
"Connecting to wake service: %s",
self.settings.wake.uri or self.settings.wake.command,
)
self._wake_task = asyncio.create_task(self._wake_task_proc(), name="wake")
if self.settings.event.enabled:
_LOGGER.debug(
"Connecting to event service: %s",
self.settings.event.uri or self.settings.event.command,
)
self._event_task = asyncio.create_task(
self._event_task_proc(), name="event"
)
_LOGGER.info("Connected to services")
async def _disconnect_from_services(self) -> None:
"""Disconnects from running services."""
if self._mic_task is not None:
_LOGGER.debug("Stopping microphone service")
self._mic_task.cancel()
self._mic_task = None
if self._snd_task is not None:
_LOGGER.debug("Stopping sound service")
self._snd_task.cancel()
self._snd_task = None
if self._wake_task is not None:
_LOGGER.debug("Stopping wake service")
self._wake_task.cancel()
self._wake_task = None
if self._event_task is not None:
_LOGGER.debug("Stopping event service")
self._event_task.cancel()
self._event_task = None
_LOGGER.debug("Disconnected from services")
# -------------------------------------------------------------------------
# Microphone
# -------------------------------------------------------------------------
async def event_from_mic(
self, event: Event, audio_bytes: Optional[bytes] = None
) -> None:
"""Called when an event is received from the mic service.
For AudioChunk events, the audio_bytes may be set if the audio was
already unpacked for preprocessing. Use this to avoid unpacking the
event again.
"""
def _make_mic_client(self) -> Optional[AsyncClient]:
"""Create client for mic service."""
if self.settings.mic.command:
program, *program_args = self.settings.mic.command
return MicProcessAsyncClient(
rate=self.settings.mic.rate,
width=self.settings.mic.width,
channels=self.settings.mic.channels,
samples_per_chunk=self.settings.mic.samples_per_chunk,
program=program,
program_args=program_args,
)
if self.settings.mic.uri:
return AsyncClient.from_uri(self.settings.mic.uri)
return None
async def _mic_task_proc(self) -> None:
"""Mic service loop."""
mic_client: Optional[AsyncClient] = None
audio_bytes: Optional[bytes] = None
if self.settings.mic.needs_webrtc and (self._mic_webrtc is None):
_LOGGER.debug("Using webrtc audio enhancements")
self._mic_webrtc = WebRtcAudio(
self.settings.mic.auto_gain, self.settings.mic.noise_suppression
)
async def _disconnect() -> None:
try:
if mic_client is not None:
await mic_client.disconnect()
except Exception:
pass # ignore disconnect errors
while self.is_running:
try:
if mic_client is None:
mic_client = self._make_mic_client()
assert mic_client is not None
await mic_client.connect()
_LOGGER.debug("Connected to mic service")
event = await mic_client.read_event()
if event is None:
_LOGGER.warning("Mic service disconnected")
await _disconnect()
mic_client = None # reconnect
await asyncio.sleep(self.settings.mic.reconnect_seconds)
continue
# Audio processing
if self.settings.mic.needs_processing and AudioChunk.is_type(
event.type
):
chunk = AudioChunk.from_event(event)
audio_bytes = self._process_mic_audio(chunk.audio)
event = AudioChunk(
rate=chunk.rate,
width=chunk.width,
channels=chunk.channels,
audio=audio_bytes,
).event()
else:
audio_bytes = None
await self.event_from_mic(event, audio_bytes)
except asyncio.CancelledError:
break
except Exception:
_LOGGER.exception("Unexpected error in mic read task")
await _disconnect()
mic_client = None # reconnect
await asyncio.sleep(self.settings.mic.reconnect_seconds)
await _disconnect()
def _process_mic_audio(self, audio_bytes: bytes) -> bytes:
"""Perform audio pre-processing on mic input."""
if self.settings.mic.volume_multiplier != 1.0:
audio_bytes = multiply_volume(
audio_bytes, self.settings.mic.volume_multiplier
)
if self._mic_webrtc is not None:
# Noise suppression and auto gain
audio_bytes = self._mic_webrtc(audio_bytes)
return audio_bytes
# -------------------------------------------------------------------------
# Sound
# -------------------------------------------------------------------------
async def event_to_snd(self, event: Event) -> None:
"""Send an event to the sound service."""
if self._snd_queue is not None:
self._snd_queue.put_nowait(event)
def _make_snd_client(self) -> Optional[AsyncClient]:
"""Create client for snd service."""
if self.settings.snd.command:
program, *program_args = self.settings.snd.command
return SndProcessAsyncClient(
rate=self.settings.snd.rate,
width=self.settings.snd.width,
channels=self.settings.snd.channels,
program=program,
program_args=program_args,
)
if self.settings.snd.uri:
return AsyncClient.from_uri(self.settings.snd.uri)
return None
async def _snd_task_proc(self) -> None:
"""Snd service loop."""
snd_client: Optional[AsyncClient] = None
async def _disconnect() -> None:
try:
if snd_client is not None:
await snd_client.disconnect()
except Exception:
pass # ignore disconnect errors
while self.is_running:
try:
if self._snd_queue is None:
self._snd_queue = asyncio.Queue()
event = await self._snd_queue.get()
if snd_client is None:
snd_client = self._make_snd_client()
assert snd_client is not None
await snd_client.connect()
_LOGGER.debug("Connected to snd service")
# Audio processing
if self.settings.snd.needs_processing and AudioChunk.is_type(
event.type
):
chunk = AudioChunk.from_event(event)
audio_bytes = self._process_snd_audio(chunk.audio)
event = AudioChunk(
rate=chunk.rate,
width=chunk.width,
channels=chunk.channels,
audio=audio_bytes,
).event()
await snd_client.write_event(event)
if self.settings.snd.disconnect_after_stop and AudioStop.is_type(
event.type
):
await _disconnect()
await self.forward_event(Played().event())
snd_client = None # reconnect on next event
except asyncio.CancelledError:
break
except Exception:
_LOGGER.exception("Unexpected error in snd read task")
await _disconnect()
snd_client = None # reconnect
self._snd_queue = None
await asyncio.sleep(self.settings.snd.reconnect_seconds)
await _disconnect()
def _process_snd_audio(self, audio_bytes: bytes) -> bytes:
"""Perform audio pre-processing on snd output."""
if self.settings.snd.volume_multiplier != 1.0:
audio_bytes = multiply_volume(
audio_bytes, self.settings.snd.volume_multiplier
)
return audio_bytes
async def _play_wav(
self, wav_path: Optional[Union[str, Path]], mute_microphone: bool = False
) -> None:
"""Send WAV as events to sound service."""
if (not wav_path) or (not self.settings.snd.enabled):
return
try:
if mute_microphone:
with wave.open(str(wav_path), "rb") as wav_file:
seconds_to_mute = wav_file.getnframes() / wav_file.getframerate()
seconds_to_mute += self.settings.mic.seconds_to_mute_after_awake_wav
_LOGGER.debug("Muting microphone for %s second(s)", seconds_to_mute)
self.microphone_muted = True
self._unmute_microphone_task = asyncio.create_task(
self._unmute_microphone_after(seconds_to_mute)
)
for event in wav_to_events(
wav_path,
samples_per_chunk=self.settings.snd.samples_per_chunk,
volume_multiplier=self.settings.snd.volume_multiplier,
):
await self.event_to_snd(event)
except Exception:
# Unmute in case of an error
self.microphone_muted = False
raise
async def _unmute_microphone_after(self, seconds: float) -> None:
await asyncio.sleep(seconds)
self.microphone_muted = False
_LOGGER.debug("Unmuted microphone")
# -------------------------------------------------------------------------
# Wake
# -------------------------------------------------------------------------
async def event_from_wake(self, event: Event) -> None:
"""Called when an event is received from the wake service."""
async def event_to_wake(self, event: Event) -> None:
"""Send event to the wake service."""
if self._wake_queue is not None:
self._wake_queue.put_nowait(event)
def _make_wake_client(self) -> Optional[AsyncClient]:
"""Create client for wake service."""
if self.settings.wake.command:
program, *program_args = self.settings.wake.command
return WakeProcessAsyncClient(
rate=self.settings.wake.rate,
width=self.settings.wake.width,
channels=self.settings.wake.channels,
program=program,
program_args=program_args,
)
if self.settings.wake.uri:
return AsyncClient.from_uri(self.settings.wake.uri)
return None
async def _wake_task_proc(self) -> None:
"""Wake service loop."""
wake_client: Optional[AsyncClient] = None
to_client_task: Optional[asyncio.Task] = None
from_client_task: Optional[asyncio.Task] = None
pending: Set[asyncio.Task] = set()
async def _disconnect() -> None:
nonlocal to_client_task, from_client_task
try:
if wake_client is not None:
await wake_client.disconnect()
# Clean up tasks
if to_client_task is not None:
to_client_task.cancel()
to_client_task = None
if from_client_task is not None:
from_client_task.cancel()
from_client_task = None
except Exception:
pass # ignore disconnect errors
while self.is_running:
try:
if self._wake_queue is None:
self._wake_queue = asyncio.Queue()
if wake_client is None:
wake_client = self._make_wake_client()
assert wake_client is not None
await wake_client.connect()
_LOGGER.debug("Connected to wake service")
# Reset
from_client_task = None
to_client_task = None
pending = set()
self._wake_queue = asyncio.Queue()
# Inform wake service of which wake word(s) to detect
await self._send_wake_detect()
# Read/write in "parallel"
if to_client_task is None:
# From satellite to wake service
to_client_task = asyncio.create_task(
self._wake_queue.get(), name="wake_to_client"
)
pending.add(to_client_task)
if from_client_task is None:
# From wake service to satellite
from_client_task = asyncio.create_task(
wake_client.read_event(), name="wake_from_client"
)
pending.add(from_client_task)
done, pending = await asyncio.wait(
pending, return_when=asyncio.FIRST_COMPLETED
)
if to_client_task in done:
# Event to go to wake service (audio)
assert to_client_task is not None
event = to_client_task.result()
to_client_task = None
await wake_client.write_event(event)
if from_client_task in done:
# Event from wake service (detection)
assert from_client_task is not None
event = from_client_task.result()
from_client_task = None
if event is None:
_LOGGER.warning("Wake service disconnected")
await _disconnect()
wake_client = None # reconnect
await asyncio.sleep(self.settings.wake.reconnect_seconds)
continue
await self.event_from_wake(event)
except asyncio.CancelledError:
break
except Exception:
_LOGGER.exception("Unexpected error in wake read task")
await _disconnect()
wake_client = None # reconnect
await asyncio.sleep(self.settings.wake.reconnect_seconds)
await _disconnect()
async def _send_wake_detect(self) -> None:
"""Inform wake word service of which wake words to detect."""
await self.event_to_wake(Detect(names=self.settings.wake.names).event())
await self.trigger_detect()
# -------------------------------------------------------------------------
# Events
# -------------------------------------------------------------------------
async def trigger_server_connected(self) -> None:
"""Called when connected to server."""
_LOGGER.info("Connected to server")
await run_event_command(self.settings.event.connected)
await self.forward_event(SatelliteConnected().event())
async def trigger_server_disonnected(self) -> None:
"""Called when disconnected from server."""
_LOGGER.info("Disconnected from server")
await run_event_command(self.settings.event.disconnected)
await self.forward_event(SatelliteDisconnected().event())
async def trigger_streaming_start(self) -> None:
"""Called when audio streaming starts."""
await run_event_command(self.settings.event.streaming_start)
await self.forward_event(StreamingStarted().event())
async def trigger_streaming_stop(self) -> None:
"""Called when audio streaming stops."""
await run_event_command(self.settings.event.streaming_stop)
await self.forward_event(StreamingStopped().event())
async def trigger_detect(self) -> None:
"""Called when wake word detection starts."""
await run_event_command(self.settings.event.detect)
async def trigger_detection(self, detection: Detection) -> None:
"""Called when wake word is detected."""
await run_event_command(self.settings.event.detection, detection.name)
await self._play_wav(
self.settings.snd.awake_wav,
mute_microphone=self.settings.mic.mute_during_awake_wav,
)
async def trigger_transcript(self, transcript: Transcript) -> None:
"""Called when speech-to-text text is received."""
await run_event_command(self.settings.event.transcript, transcript.text)
await self._play_wav(self.settings.snd.done_wav)
async def trigger_stt_start(self) -> None:
"""Called when user starts speaking."""
await run_event_command(self.settings.event.stt_start)
async def trigger_stt_stop(self) -> None:
"""Called when user stops speaking."""
await run_event_command(self.settings.event.stt_stop)
async def trigger_synthesize(self, synthesize: Synthesize) -> None:
"""Called when text-to-speech text is received."""
await run_event_command(self.settings.event.synthesize, synthesize.text)
async def trigger_tts_start(self) -> None:
"""Called when text-to-speech audio starts."""
await run_event_command(self.settings.event.tts_start)
async def trigger_tts_stop(self) -> None:
"""Called when text-to-speech audio stops."""
await run_event_command(self.settings.event.tts_stop)
async def trigger_error(self, error: Error) -> None:
"""Called when an error occurs on the server."""
await run_event_command(self.settings.event.error, error.text)
async def forward_event(self, event: Event) -> None:
"""Forward an event to the event service."""
if self._event_queue is not None:
self._event_queue.put_nowait(event)
def _make_event_client(self) -> Optional[AsyncClient]:
"""Create client for event service."""
if self.settings.event.uri:
return AsyncClient.from_uri(self.settings.event.uri)
return None
async def _event_task_proc(self) -> None:
"""Event service loop."""
event_client: Optional[AsyncClient] = None
async def _disconnect() -> None:
try:
if event_client is not None:
await event_client.disconnect()
except Exception:
pass # ignore disconnect errors
while self.is_running:
try:
if self._event_queue is None:
self._event_queue = asyncio.Queue()
event = await self._event_queue.get()
if event_client is None:
event_client = self._make_event_client()
assert event_client is not None
await event_client.connect()
_LOGGER.debug("Connected to event service")
await event_client.write_event(event)
except asyncio.CancelledError:
break
except Exception:
_LOGGER.exception("Unexpected error in event read task")
await _disconnect()
event_client = None # reconnect
self._event_queue = None
await asyncio.sleep(self.settings.event.reconnect_seconds)
await _disconnect()
# -----------------------------------------------------------------------------
class AlwaysStreamingSatellite(SatelliteBase):
"""Satellite that always streams audio."""
def __init__(self, settings: SatelliteSettings) -> None:
super().__init__(settings)
self.is_streaming = False
if settings.vad.enabled:
_LOGGER.warning("VAD is enabled but will not be used")
if settings.wake.enabled:
_LOGGER.warning("Local wake word detection is enabled but will not be used")
async def event_from_server(self, event: Event) -> None:
await super().event_from_server(event)
if RunSatellite.is_type(event.type):
self.is_streaming = True
_LOGGER.info("Streaming audio")
await self._send_run_pipeline()
await self.trigger_streaming_start()
elif PauseSatellite.is_type(event.type):
self.is_streaming = False
_LOGGER.info("Satellite paused")
elif Detection.is_type(event.type):
# Start debug recording
if self.stt_audio_writer is not None:
self.stt_audio_writer.start()
elif Transcript.is_type(event.type) or Error.is_type(event.type):
# Stop debug recording
if self.stt_audio_writer is not None:
self.stt_audio_writer.stop()
if Transcript.is_type(event.type):
# We're always streaming
_LOGGER.info("Streaming audio")
# Re-trigger streaming start even though we technically don't stop
# so the event service can reset LEDs, etc.
await self.trigger_streaming_start()
async def event_from_mic(
self, event: Event, audio_bytes: Optional[bytes] = None
) -> None:
if (not self.is_streaming) or self.microphone_muted:
return
if AudioChunk.is_type(event.type):
# Forward to server
await self.event_to_server(event)
# Debug audio recording
if self.stt_audio_writer is not None:
if audio_bytes is None:
chunk = AudioChunk.from_event(event)
audio_bytes = chunk.audio
self.stt_audio_writer.write(audio_bytes)
# -----------------------------------------------------------------------------
class VadStreamingSatellite(SatelliteBase):
"""Satellite that only streams after speech is detected."""
def __init__(self, settings: SatelliteSettings) -> None:
if not settings.vad.enabled:
raise ValueError("VAD is not enabled")
super().__init__(settings)
self.is_streaming = False
self.vad = SileroVad(
threshold=settings.vad.threshold, trigger_level=settings.vad.trigger_level
)
# Timestamp in the future when we will have timed out (set with
# time.monotonic())
self.timeout_seconds: Optional[float] = None
# Audio from right before speech starts (circular buffer)
self.vad_buffer: Optional[RingBuffer] = None
if settings.vad.buffer_seconds > 0:
# Assume 16Khz, 16-bit mono samples
vad_buffer_bytes = int(math.ceil(settings.vad.buffer_seconds * 16000 * 2))
self.vad_buffer = RingBuffer(maxlen=vad_buffer_bytes)
if settings.wake.enabled:
_LOGGER.warning("Local wake word detection is enabled but will not be used")
self._is_paused = False
async def event_from_server(self, event: Event) -> None:
await super().event_from_server(event)
if RunSatellite.is_type(event.type):
self._is_paused = False
_LOGGER.info("Waiting for speech")
elif Detection.is_type(event.type):
# Start debug recording
if self.stt_audio_writer is not None:
self.stt_audio_writer.start()
elif (
Transcript.is_type(event.type)
or Error.is_type(event.type)
or PauseSatellite.is_type(event.type)
):
if PauseSatellite.is_type(event.type):
self._is_paused = True
_LOGGER.debug("Satellite paused")
self.is_streaming = False
# Stop debug recording
if self.stt_audio_writer is not None:
self.stt_audio_writer.stop()
async def event_from_mic(
self, event: Event, audio_bytes: Optional[bytes] = None
) -> None:
if (
(not AudioChunk.is_type(event.type))
or self.microphone_muted
or self._is_paused
):
return
# Only unpack chunk once
chunk: Optional[AudioChunk] = None
# Debug audio recording
if self.stt_audio_writer is not None:
if audio_bytes is None:
# Need to unpack
chunk = AudioChunk.from_event(event)
audio_bytes = chunk.audio
self.stt_audio_writer.write(audio_bytes)
if (
self.is_streaming