Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/3483.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Throttle `UserSpeakingFrame` to broadcast at most every 200ms instead of on every audio chunk, reducing frame processing overhead during user speech.
15 changes: 14 additions & 1 deletion src/pipecat/transports/base_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import asyncio
import time
from typing import Optional

from loguru import logger
Expand Down Expand Up @@ -77,6 +78,11 @@ def __init__(self, params: TransportParams, **kwargs):

# Track user speaking state for interruption logic
self._user_speaking = False
# Last time a UserSpeakingFrame was pushed.
self._user_speaking_frame_time = 0
# How often a UserSpeakingFrame should be pushed (value should be
# greater than the audio chunks to have any effect).
self._user_speaking_frame_period = 0.2

# Task to process incoming audio (VAD) and push audio frames downstream
# if passthrough is enabled.
Expand Down Expand Up @@ -423,7 +429,7 @@ async def _audio_task_handler(self):
await self._deprecated_run_turn_analyzer(frame, vad_state, previous_vad_state)

if vad_state == VADState.SPEAKING:
await self.broadcast_frame(UserSpeakingFrame)
await self._user_currently_speaking()

# Push audio downstream if passthrough is set.
if self._params.audio_in_passthrough:
Expand All @@ -444,6 +450,13 @@ async def _audio_task_handler(self):
else:
await self.push_frame(VADUserStoppedSpeakingFrame())

async def _user_currently_speaking(self):
"""Handle user speaking frame."""
diff_time = time.time() - self._user_speaking_frame_time
if diff_time >= self._user_speaking_frame_period:
await self.broadcast_frame(UserSpeakingFrame)
self._user_speaking_frame_time = time.time()

#
# DEPRECATED.
#
Expand Down
5 changes: 2 additions & 3 deletions src/pipecat/transports/base_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def __init__(
# Last time a BotSpeakingFrame was pushed.
self._bot_speaking_frame_time = 0
# How often a BotSpeakingFrame should be pushed (value should be
# lower than the audio chunks).
# greater than the audio chunks to have any effect).
self._bot_speaking_frame_period = 0.2
# Last time the bot actually spoke.
self._bot_speech_last_time = 0
Expand Down Expand Up @@ -644,8 +644,7 @@ async def _bot_currently_speaking(self):

diff_time = time.time() - self._bot_speaking_frame_time
if diff_time >= self._bot_speaking_frame_period:
await self._transport.push_frame(BotSpeakingFrame())
await self._transport.push_frame(BotSpeakingFrame(), FrameDirection.UPSTREAM)
await self._transport.broadcast_frame(BotSpeakingFrame)
self._bot_speaking_frame_time = time.time()

self._bot_speech_last_time = time.time()
Expand Down