Add VoIP error tone (#92260)

* Play error tone when pipeline error occurs

* Play listening tone at the start of each cycle
This commit is contained in:
Michael Hansen 2023-04-29 14:24:56 -05:00 committed by GitHub
parent b9f2b0ad8b
commit c35aabe497
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 7 deletions

Binary file not shown.

View File

@ -105,6 +105,7 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
buffered_chunks_before_speech: int = 100, buffered_chunks_before_speech: int = 100,
listening_tone_enabled: bool = True, listening_tone_enabled: bool = True,
processing_tone_enabled: bool = True, processing_tone_enabled: bool = True,
error_tone_enabled: bool = True,
tone_delay: float = 0.2, tone_delay: float = 0.2,
tts_extra_timeout: float = 1.0, tts_extra_timeout: float = 1.0,
) -> None: ) -> None:
@ -120,6 +121,7 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
self.buffered_chunks_before_speech = buffered_chunks_before_speech self.buffered_chunks_before_speech = buffered_chunks_before_speech
self.listening_tone_enabled = listening_tone_enabled self.listening_tone_enabled = listening_tone_enabled
self.processing_tone_enabled = processing_tone_enabled self.processing_tone_enabled = processing_tone_enabled
self.error_tone_enabled = error_tone_enabled
self.tone_delay = tone_delay self.tone_delay = tone_delay
self.tts_extra_timeout = tts_extra_timeout self.tts_extra_timeout = tts_extra_timeout
@ -131,6 +133,8 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
self._session_id: str | None = None self._session_id: str | None = None
self._tone_bytes: bytes | None = None self._tone_bytes: bytes | None = None
self._processing_bytes: bytes | None = None self._processing_bytes: bytes | None = None
self._error_bytes: bytes | None = None
self._pipeline_error: bool = False
def connection_made(self, transport): def connection_made(self, transport):
"""Server is ready.""" """Server is ready."""
@ -161,8 +165,10 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
"""Forward audio to pipeline STT and handle TTS.""" """Forward audio to pipeline STT and handle TTS."""
if self._session_id is None: if self._session_id is None:
self._session_id = ulid() self._session_id = ulid()
if self.listening_tone_enabled:
await self._play_listening_tone() # Play listening tone at the start of each cycle
if self.listening_tone_enabled:
await self._play_listening_tone()
try: try:
# Wait for speech before starting pipeline # Wait for speech before starting pipeline
@ -221,11 +227,16 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
tts_audio_output="raw", tts_audio_output="raw",
) )
# Block until TTS is done speaking. if self._pipeline_error:
# self._pipeline_error = False
# This is set in _send_tts and has a timeout that's based on the if self.error_tone_enabled:
# length of the TTS audio. await self._play_error_tone()
await self._tts_done.wait() else:
# Block until TTS is done speaking.
#
# This is set in _send_tts and has a timeout that's based on the
# length of the TTS audio.
await self._tts_done.wait()
_LOGGER.debug("Pipeline finished") _LOGGER.debug("Pipeline finished")
except asyncio.TimeoutError: except asyncio.TimeoutError:
@ -307,6 +318,9 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
self._send_tts(media_id), self._send_tts(media_id),
"voip_pipeline_tts", "voip_pipeline_tts",
) )
elif event.type == PipelineEventType.ERROR:
# Play error tone instead of wait for TTS
self._pipeline_error = True
async def _send_tts(self, media_id: str) -> None: async def _send_tts(self, media_id: str) -> None:
"""Send TTS audio to caller via RTP.""" """Send TTS audio to caller via RTP."""
@ -372,6 +386,23 @@ class PipelineRtpDatagramProtocol(RtpDatagramProtocol):
) )
) )
async def _play_error_tone(self) -> None:
"""Play a tone to indicate a pipeline error occurred."""
if self._error_bytes is None:
# Do I/O in executor
self._error_bytes = await self.hass.async_add_executor_job(
self._load_pcm,
"error.pcm",
)
await self.hass.async_add_executor_job(
partial(
self.send_audio,
self._error_bytes,
**RTP_AUDIO_SETTINGS,
)
)
def _load_pcm(self, file_name: str) -> bytes: def _load_pcm(self, file_name: str) -> bytes:
"""Load raw audio (16Khz, 16-bit mono).""" """Load raw audio (16Khz, 16-bit mono)."""
return (Path(__file__).parent / file_name).read_bytes() return (Path(__file__).parent / file_name).read_bytes()

View File

@ -90,6 +90,7 @@ async def test_pipeline(
Context(), Context(),
listening_tone_enabled=False, listening_tone_enabled=False,
processing_tone_enabled=False, processing_tone_enabled=False,
error_tone_enabled=False,
) )
rtp_protocol.transport = Mock() rtp_protocol.transport = Mock()
@ -140,6 +141,7 @@ async def test_pipeline_timeout(hass: HomeAssistant, voip_device: VoIPDevice) ->
pipeline_timeout=0.001, pipeline_timeout=0.001,
listening_tone_enabled=False, listening_tone_enabled=False,
processing_tone_enabled=False, processing_tone_enabled=False,
error_tone_enabled=False,
) )
transport = Mock(spec=["close"]) transport = Mock(spec=["close"])
rtp_protocol.connection_made(transport) rtp_protocol.connection_made(transport)
@ -179,6 +181,7 @@ async def test_stt_stream_timeout(hass: HomeAssistant, voip_device: VoIPDevice)
audio_timeout=0.001, audio_timeout=0.001,
listening_tone_enabled=False, listening_tone_enabled=False,
processing_tone_enabled=False, processing_tone_enabled=False,
error_tone_enabled=False,
) )
transport = Mock(spec=["close"]) transport = Mock(spec=["close"])
rtp_protocol.connection_made(transport) rtp_protocol.connection_made(transport)
@ -262,6 +265,7 @@ async def test_tts_timeout(
Context(), Context(),
listening_tone_enabled=False, listening_tone_enabled=False,
processing_tone_enabled=False, processing_tone_enabled=False,
error_tone_enabled=False,
) )
rtp_protocol.transport = Mock() rtp_protocol.transport = Mock()
rtp_protocol.send_audio = Mock(side_effect=send_audio) rtp_protocol.send_audio = Mock(side_effect=send_audio)