Skip to content

Commit abab19c

Browse files
committed
style: fix lint issues in twilio_handler.py
1 parent 339ce0b commit abab19c

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

examples/realtime/twilio/twilio_handler.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,34 @@ def __init__(self, twilio_websocket: WebSocket):
5151

5252
# Audio chunking (matches CLI demo)
5353
self.CHUNK_LENGTH_S = 0.05 # 50ms chunks
54-
self.SAMPLE_RATE = 8000 # Twilio g711_ulaw at 8kHz
55-
self.BUFFER_SIZE_BYTES = int(self.SAMPLE_RATE * self.CHUNK_LENGTH_S) # ~400 bytes per 50ms
54+
self.SAMPLE_RATE = 8000 # Twilio g711_ulaw at 8kHz
55+
self.BUFFER_SIZE_BYTES = int(
56+
self.SAMPLE_RATE * self.CHUNK_LENGTH_S
57+
) # ~400 bytes per 50ms
5658

5759
self._stream_sid: str | None = None
5860
self._audio_buffer: bytearray = bytearray()
5961
self._last_buffer_send_time = time.time()
6062

6163
# Playback tracking for outbound audio
6264
self._mark_counter = 0
63-
self._mark_data: dict[str, tuple[str, int, int]] = {} # mark_id -> (item_id, content_index, byte_count)
65+
self._mark_data: dict[str, tuple[str, int, int]] = (
66+
{}
67+
) # mark_id -> (item_id, content_index, byte_count)
6468

6569
# ---- Deterministic startup warm-up (preferred over sleep) ----
6670
# Buffer the first N chunks before sending to OpenAI; then mark warmed.
6771
try:
68-
self.STARTUP_BUFFER_CHUNKS = max(0, int(os.getenv("TWILIO_STARTUP_BUFFER_CHUNKS", "3")))
72+
self.STARTUP_BUFFER_CHUNKS = max(
73+
0, int(os.getenv("TWILIO_STARTUP_BUFFER_CHUNKS", "3"))
74+
)
6975
except Exception:
7076
self.STARTUP_BUFFER_CHUNKS = 3
7177

7278
self._startup_buffer = bytearray()
73-
self._startup_warmed = self.STARTUP_BUFFER_CHUNKS == 0 # if 0, considered warmed immediately
79+
self._startup_warmed = (
80+
self.STARTUP_BUFFER_CHUNKS == 0
81+
) # if 0, considered warmed immediately
7482

7583
# Optional delay (defaults 0.0 because buffering is preferred)
7684
try:
@@ -235,8 +243,12 @@ async def _handle_mark_event(self, message: dict[str, Any]) -> None:
235243
if mark_id in self._mark_data:
236244
item_id, item_content_index, byte_count = self._mark_data[mark_id]
237245
audio_bytes = b"\x00" * byte_count # Placeholder bytes for tracker
238-
self.playback_tracker.on_play_bytes(item_id, item_content_index, audio_bytes)
239-
print(f"Playback tracker updated: {item_id}, index {item_content_index}, {byte_count} bytes")
246+
self.playback_tracker.on_play_bytes(
247+
item_id, item_content_index, audio_bytes
248+
)
249+
print(
250+
f"Playback tracker updated: {item_id}, index {item_content_index}, {byte_count} bytes"
251+
)
240252
del self._mark_data[mark_id]
241253

242254
except Exception as e:
@@ -257,7 +269,9 @@ async def _flush_audio_buffer(self) -> None:
257269
self._startup_buffer.extend(buffer_data)
258270

259271
# target bytes = N chunks * bytes-per-chunk
260-
target_bytes = self.BUFFER_SIZE_BYTES * max(0, self.STARTUP_BUFFER_CHUNKS)
272+
target_bytes = self.BUFFER_SIZE_BYTES * max(
273+
0, self.STARTUP_BUFFER_CHUNKS
274+
)
261275

262276
if len(self._startup_buffer) >= target_bytes:
263277
# Warm-up complete: flush all buffered data in order
@@ -284,7 +298,8 @@ async def _buffer_flush_loop(self) -> None:
284298
current_time = time.time()
285299
if (
286300
self._audio_buffer
287-
and current_time - self._last_buffer_send_time > self.CHUNK_LENGTH_S * 2
301+
and current_time - self._last_buffer_send_time
302+
> self.CHUNK_LENGTH_S * 2
288303
):
289304
await self._flush_audio_buffer()
290305

0 commit comments

Comments
 (0)