@@ -52,26 +52,22 @@ def __init__(self, twilio_websocket: WebSocket):
5252 # Audio chunking (matches CLI demo)
5353 self .CHUNK_LENGTH_S = 0.05 # 50ms chunks
5454 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
55+ self .BUFFER_SIZE_BYTES = int (self .SAMPLE_RATE * self .CHUNK_LENGTH_S ) # ~400 bytes per 50ms
5856
5957 self ._stream_sid : str | None = None
6058 self ._audio_buffer : bytearray = bytearray ()
6159 self ._last_buffer_send_time = time .time ()
6260
6361 # Playback tracking for outbound audio
6462 self ._mark_counter = 0
65- self ._mark_data : dict [str , tuple [ str , int , int ]] = (
66- {}
67- ) # mark_id -> (item_id, content_index, byte_count)
63+ self ._mark_data : dict [
64+ str , tuple [ str , int , int ]
65+ ] = {} # mark_id -> (item_id, content_index, byte_count)
6866
6967 # ---- Deterministic startup warm-up (preferred over sleep) ----
7068 # Buffer the first N chunks before sending to OpenAI; then mark warmed.
7169 try :
72- self .STARTUP_BUFFER_CHUNKS = max (
73- 0 , int (os .getenv ("TWILIO_STARTUP_BUFFER_CHUNKS" , "3" ))
74- )
70+ self .STARTUP_BUFFER_CHUNKS = max (0 , int (os .getenv ("TWILIO_STARTUP_BUFFER_CHUNKS" , "3" )))
7571 except Exception :
7672 self .STARTUP_BUFFER_CHUNKS = 3
7773
@@ -243,9 +239,7 @@ async def _handle_mark_event(self, message: dict[str, Any]) -> None:
243239 if mark_id in self ._mark_data :
244240 item_id , item_content_index , byte_count = self ._mark_data [mark_id ]
245241 audio_bytes = b"\x00 " * byte_count # Placeholder bytes for tracker
246- self .playback_tracker .on_play_bytes (
247- item_id , item_content_index , audio_bytes
248- )
242+ self .playback_tracker .on_play_bytes (item_id , item_content_index , audio_bytes )
249243 print (
250244 f"Playback tracker updated: { item_id } , index { item_content_index } , { byte_count } bytes"
251245 )
@@ -269,9 +263,7 @@ async def _flush_audio_buffer(self) -> None:
269263 self ._startup_buffer .extend (buffer_data )
270264
271265 # target bytes = N chunks * bytes-per-chunk
272- target_bytes = self .BUFFER_SIZE_BYTES * max (
273- 0 , self .STARTUP_BUFFER_CHUNKS
274- )
266+ target_bytes = self .BUFFER_SIZE_BYTES * max (0 , self .STARTUP_BUFFER_CHUNKS )
275267
276268 if len (self ._startup_buffer ) >= target_bytes :
277269 # Warm-up complete: flush all buffered data in order
@@ -298,8 +290,7 @@ async def _buffer_flush_loop(self) -> None:
298290 current_time = time .time ()
299291 if (
300292 self ._audio_buffer
301- and current_time - self ._last_buffer_send_time
302- > self .CHUNK_LENGTH_S * 2
293+ and current_time - self ._last_buffer_send_time > self .CHUNK_LENGTH_S * 2
303294 ):
304295 await self ._flush_audio_buffer ()
305296
0 commit comments