Quellcode durchsuchen

[3.0.0] sending 16bits int PCM to whisper

Penta vor 3 Wochen
Ursprung
Commit
f832cab3e1
1 geänderte Dateien mit 11 neuen und 9 gelöschten Zeilen
  1. 11 9
      chatbot.py

+ 11 - 9
chatbot.py

@@ -122,7 +122,7 @@ class STTSink(Sink):
         if not pcm_bytes:
         if not pcm_bytes:
             return
             return
 
 
-        audio = discord_pcm_to_whisper_float32(pcm_bytes)
+        audio = discord_pcm_to_whisper_int16(pcm_bytes)
         if not audio:
         if not audio:
             return
             return
 
 
@@ -150,20 +150,22 @@ class STTSink(Sink):
 # Liste pour stocker l'historique des conversations
 # Liste pour stocker l'historique des conversations
 conversation_history = []
 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""
         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
     # resample 48kHz -> 16kHz
     audio_16k = resample_poly(audio_float32, up=1, down=3)
     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):
 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"""
     """Filtre le contenu d'un retour de modèle de language, comme pour enlever les pensées dans le cas par exemple de DeepSeek"""