Prechádzať zdrojové kódy

[3.0.0] Audio buffering before treatment

Penta 3 týždňov pred
rodič
commit
3ed3c69686
1 zmenil súbory, kde vykonal 19 pridanie a 8 odobranie
  1. 19 8
      chatbot.py

+ 19 - 8
chatbot.py

@@ -93,6 +93,8 @@ class STTSink(Sink):
     def __init__(self):
         super().__init__()
         self.user_ws = {}
+        self.buffers = {}          # audio accumulé par user
+        self.last_send = {}        # timestamp dernier envoi
 
     async def _get_ws(self, user_id):
         if user_id not in self.user_ws:
@@ -119,17 +121,26 @@ class STTSink(Sink):
         if not pcm_bytes:
             return
 
-        audio_16k_float32 = discord_pcm_to_whisper_float32(pcm_bytes)
-
-        if not audio_16k_float32:
+        audio = discord_pcm_to_whisper_float32(pcm_bytes)
+        if not audio:
             return
 
-        asyncio.run_coroutine_threadsafe(self._send_audio(user_id, audio_16k_float32), MAIN_LOOP)
+        if user_id not in self.buffers:
+            self.buffers[user_id] = []
+            self.last_send[user_id] = time.time()
 
-        logger.debug(
-            f"[STT] audio reçu user={user_id} "
-            f"bytes_in={len(pcm_bytes)} bytes_out={len(audio_16k_float32)}"
-        )
+        self.buffers[user_id].append(audio)
+        now = time.time()
+
+        # Envoi toutes les ~600 ms
+        if now - self.last_send[user_id] >= 0.6:
+            chunk = b"".join(self.buffers[user_id])
+            self.buffers[user_id].clear()
+            self.last_send[user_id] = now
+
+            asyncio.run_coroutine_threadsafe(self._send_audio(user_id, chunk), MAIN_LOOP)
+
+            logger.debug(f"[STT] audio envoyé user={user_id} bytes={len(chunk)}")
 
     async def _send_audio(self, user_id, pcm_bytes):
         ws = await self._get_ws(user_id)