AudioBufferProcessor: fix user/bot audio buffers silence padding

This commit is contained in:
Aleix Conchillo Flaqué
2025-01-31 11:06:27 -08:00
parent 13611fd8e1
commit cda34a1320
2 changed files with 11 additions and 2 deletions

View File

@@ -72,6 +72,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed an issue in `AudioBufferProcessor` where user callback would not be
called on task cancellation.
- Fixed an issue in `AudioBufferProcessor` that would cause wrong silence
padding in some cases.
- Fixed an issue where `ElevenLabsTTSService` messages would return a 1009
websocket error by increasing the max message size limit to 16MB.

View File

@@ -79,14 +79,20 @@ class AudioBufferProcessor(FrameProcessor):
if isinstance(frame, InputAudioRawFrame):
resampled = self._resample_audio(frame)
self._user_audio_buffer.extend(resampled)
# Sync the bot's buffer to the user's buffer by adding silence if needed
# Sync the bot's buffer to the user's buffer by adding silence if needed.
if len(self._user_audio_buffer) > len(self._bot_audio_buffer):
silence = b"\x00" * len(resampled)
missing = len(self._user_audio_buffer) - len(self._bot_audio_buffer)
silence = b"\x00" * missing
self._bot_audio_buffer.extend(silence)
# If the bot is speaking, include all audio from the bot.
elif isinstance(frame, OutputAudioRawFrame):
resampled = self._resample_audio(frame)
self._bot_audio_buffer.extend(resampled)
# Sync the user's buffer to the bot's buffer by adding silence if needed.
if len(self._bot_audio_buffer) > len(self._user_audio_buffer):
missing = len(self._bot_audio_buffer) - len(self._user_audio_buffer)
silence = b"\x00" * missing
self._user_audio_buffer.extend(silence)
if self._buffer_size > 0 and len(self._user_audio_buffer) > self._buffer_size:
await self._call_on_audio_data_handler()