Skip to content

Commit

Permalink
Send esphome tts_stream event after audio bytes are actually loaded i…
Browse files Browse the repository at this point in the history
…nto memory (home-assistant#104448)

Send tts_stream event after audio bytes are actually loaded into memory
  • Loading branch information
jesserockz authored Nov 27, 2023
1 parent 7fbf68f commit 95c771e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions homeassistant/components/esphome/voice_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,6 @@ async def _send_tts(self, media_id: str) -> None:
if self.transport is None:
return

self.handle_event(
VoiceAssistantEventType.VOICE_ASSISTANT_TTS_STREAM_START, {}
)

extension, data = await tts.async_get_media_source_audio(
self.hass,
media_id,
Expand All @@ -331,11 +327,17 @@ async def _send_tts(self, media_id: str) -> None:

audio_bytes = wav_file.readframes(wav_file.getnframes())

_LOGGER.debug("Sending %d bytes of audio", len(audio_bytes))
audio_bytes_size = len(audio_bytes)

_LOGGER.debug("Sending %d bytes of audio", audio_bytes_size)

self.handle_event(
VoiceAssistantEventType.VOICE_ASSISTANT_TTS_STREAM_START, {}
)

bytes_per_sample = stt.AudioBitRates.BITRATE_16 // 8
sample_offset = 0
samples_left = len(audio_bytes) // bytes_per_sample
samples_left = audio_bytes_size // bytes_per_sample

while samples_left > 0:
bytes_offset = sample_offset * bytes_per_sample
Expand Down

0 comments on commit 95c771e

Please sign in to comment.