Эх сурвалжийг харах

[3.0.0] sending 16bits int PCM to whisper

Penta 3 долоо хоног өмнө
parent
commit
f832cab3e1
1 өөрчлөгдсөн 11 нэмэгдсэн , 9 устгасан
  1. 11 9
      chatbot.py

+ 11 - 9
chatbot.py

@@ -122,7 +122,7 @@ class STTSink(Sink):
         if not pcm_bytes:
             return
 
-        audio = discord_pcm_to_whisper_float32(pcm_bytes)
+        audio = discord_pcm_to_whisper_int16(pcm_bytes)
         if not audio:
             return
 
@@ -150,20 +150,22 @@ class STTSink(Sink):
 # Liste pour stocker l'historique des conversations
 conversation_history = []
 
-def discord_pcm_to_whisper_float32(pcm_bytes: bytes) -> bytes:
-    # int16 PCM -> numpy
-    audio_int16 = np.frombuffer(pcm_bytes, dtype=np.int16)
-
-    if audio_int16.size == 0:
+def discord_pcm_to_whisper_int16(pcm_bytes: bytes) -> bytes:
+    # PCM 48 kHz int16 -> numpy
+    audio_48k = np.frombuffer(pcm_bytes, dtype=np.int16)
+    if audio_48k.size == 0:
         return b""
 
-    # int16 -> float32 [-1.0, 1.0]
-    audio_float32 = audio_int16.astype(np.float32) / 32768.0
+    # int16 -> float32 pour resample
+    audio_float32 = audio_48k.astype(np.float32) / 32768.0
 
     # resample 48kHz -> 16kHz
     audio_16k = resample_poly(audio_float32, up=1, down=3)
 
-    return audio_16k.astype(np.float32).tobytes()
+    # float32 -> int16 (CE QUE WHISPER ATTEND)
+    audio_16k_int16 = np.clip(audio_16k * 32768.0, -32768, 32767).astype(np.int16)
+
+    return audio_16k_int16.tobytes()
 
 def filter_message(message):
     """Filtre le contenu d'un retour de modèle de language, comme pour enlever les pensées dans le cas par exemple de DeepSeek"""