|  | 
| 23 | 23 | FORMAT = np.int16 | 
| 24 | 24 | CHANNELS = 1 | 
| 25 | 25 | ENERGY_THRESHOLD = 0.015  # RMS threshold for barge‑in while assistant is speaking | 
| 26 |  | -PREBUFFER_CHUNKS = 3      # initial jitter buffer (~120ms with 40ms chunks) | 
| 27 |  | -FADE_OUT_MS = 12          # short fade to avoid clicks when interrupting | 
|  | 26 | +PREBUFFER_CHUNKS = 3  # initial jitter buffer (~120ms with 40ms chunks) | 
|  | 27 | +FADE_OUT_MS = 12  # short fade to avoid clicks when interrupting | 
| 28 | 28 | 
 | 
| 29 | 29 | # Set up logging for OpenAI agents SDK | 
| 30 | 30 | # logging.basicConfig( | 
| @@ -108,14 +108,18 @@ def _output_callback(self, outdata, frames: int, time, status) -> None: | 
| 108 | 108 | 
 | 
| 109 | 109 |             samples, item_id, content_index = self.current_audio_chunk | 
| 110 | 110 |             samples_filled = 0 | 
| 111 |  | -            while samples_filled < len(outdata) and self.fade_done_samples < self.fade_total_samples: | 
|  | 111 | +            while ( | 
|  | 112 | +                samples_filled < len(outdata) and self.fade_done_samples < self.fade_total_samples | 
|  | 113 | +            ): | 
| 112 | 114 |                 remaining_output = len(outdata) - samples_filled | 
| 113 | 115 |                 remaining_fade = self.fade_total_samples - self.fade_done_samples | 
| 114 | 116 |                 n = min(remaining_output, remaining_fade) | 
| 115 | 117 | 
 | 
| 116 | 118 |                 src = samples[self.chunk_position : self.chunk_position + n].astype(np.float32) | 
| 117 | 119 |                 # Linear ramp from current level down to 0 across remaining fade samples | 
| 118 |  | -                idx = np.arange(self.fade_done_samples, self.fade_done_samples + n, dtype=np.float32) | 
|  | 120 | +                idx = np.arange( | 
|  | 121 | +                    self.fade_done_samples, self.fade_done_samples + n, dtype=np.float32 | 
|  | 122 | +                ) | 
| 119 | 123 |                 gain = 1.0 - (idx / float(self.fade_total_samples)) | 
| 120 | 124 |                 ramped = np.clip(src * gain, -32768.0, 32767.0).astype(np.int16) | 
| 121 | 125 |                 outdata[samples_filled : samples_filled + n, 0] = ramped | 
| @@ -155,7 +159,10 @@ def _output_callback(self, outdata, frames: int, time, status) -> None: | 
| 155 | 159 |             if self.current_audio_chunk is None: | 
| 156 | 160 |                 try: | 
| 157 | 161 |                     # Respect a small jitter buffer before starting playback | 
| 158 |  | -                    if self.prebuffering and self.output_queue.qsize() < self.prebuffer_target_chunks: | 
|  | 162 | +                    if ( | 
|  | 163 | +                        self.prebuffering | 
|  | 164 | +                        and self.output_queue.qsize() < self.prebuffer_target_chunks | 
|  | 165 | +                    ): | 
| 159 | 166 |                         break | 
| 160 | 167 |                     self.prebuffering = False | 
| 161 | 168 |                     self.current_audio_chunk = self.output_queue.get_nowait() | 
|  | 
0 commit comments