Skip to content

Commit 4622e6d

Browse files
committed
fix(twilio): add configurable startup delay to avoid initial audio jitter (fixes #1906)
1 parent aa524e9 commit 4622e6d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

examples/realtime/twilio/twilio_handler.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ async def start(self) -> None:
8989
await self.twilio_websocket.accept()
9090
print("Twilio WebSocket connection accepted")
9191

92+
# ---------------------------
93+
# STARTUP DELAY (fix jitter)
94+
# ---------------------------
95+
# Small configurable delay to allow websockets/handshakes and buffers
96+
# to settle before audio starts. Default is 0.5 seconds.
97+
try:
98+
startup_delay = float(os.getenv("TWILIO_STARTUP_DELAY_S", "0.5"))
99+
except Exception:
100+
startup_delay = 0.5
101+
102+
# Only perform the sleep if a positive value is configured
103+
if startup_delay > 0:
104+
# allow other coroutines to run while waiting
105+
await asyncio.sleep(startup_delay)
106+
# ---------------------------
107+
108+
# create tasks after warmup so we avoid missing the first audio frames
92109
self._realtime_session_task = asyncio.create_task(self._realtime_session_loop())
93110
self._message_loop_task = asyncio.create_task(self._twilio_message_loop())
94111
self._buffer_flush_task = asyncio.create_task(self._buffer_flush_loop())

0 commit comments

Comments
 (0)