Skip to content

Commit

Permalink
Clean up tts played
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam committed Jun 26, 2024
1 parent 5d3c243 commit 2e046c6
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions wyoming_satellite/satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import math
import time
import wave
from dataclasses import dataclass
from enum import Enum, auto
from pathlib import Path
from typing import Callable, Dict, Final, List, Optional, Set, Union
Expand Down Expand Up @@ -60,6 +61,12 @@ class State(Enum):
STOPPED = auto()


@dataclass
class SoundEvent:
event: Event
is_tts: bool


# -----------------------------------------------------------------------------


Expand All @@ -76,7 +83,7 @@ def __init__(self, settings: SatelliteSettings) -> 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._snd_queue: "Optional[asyncio.Queue[SoundEvent]]" = None
self._wake_task: Optional[asyncio.Task] = None
self._wake_queue: "Optional[asyncio.Queue[Event]]" = None
self._event_task: Optional[asyncio.Task] = None
Expand Down Expand Up @@ -543,10 +550,10 @@ def _process_mic_audio(self, audio_bytes: bytes) -> bytes:
# Sound
# -------------------------------------------------------------------------

async def event_to_snd(self, event: Event) -> None:
async def event_to_snd(self, event: Event, is_tts: bool = True) -> None:
"""Send an event to the sound service."""
if self._snd_queue is not None:
self._snd_queue.put_nowait(event)
self._snd_queue.put_nowait(SoundEvent(event, is_tts))

def _make_snd_client(self) -> Optional[AsyncClient]:
"""Create client for snd service."""
Expand Down Expand Up @@ -581,7 +588,8 @@ async def _disconnect() -> None:
if self._snd_queue is None:
self._snd_queue = asyncio.Queue()

event = await self._snd_queue.get()
snd_event = await self._snd_queue.get()
event = snd_event.event

if snd_client is None:
snd_client = self._make_snd_client()
Expand All @@ -608,7 +616,7 @@ async def _disconnect() -> None:
event.type
):
await _disconnect()
if not hasattr(event, 'wav'):
if snd_event.is_tts:
await self.trigger_played()
snd_client = None # reconnect on next event
except asyncio.CancelledError:
Expand Down Expand Up @@ -655,8 +663,7 @@ async def _play_wav(
samples_per_chunk=self.settings.snd.samples_per_chunk,
volume_multiplier=self.settings.snd.volume_multiplier,
):
event.wav = True
await self.event_to_snd(event)
await self.event_to_snd(event, is_tts=False)
except Exception:
# Unmute in case of an error
self.microphone_muted = False
Expand Down

0 comments on commit 2e046c6

Please sign in to comment.