Stt On discord Vc For My ai vtuber (how do i fix?) A Discord bot developer is seeking help to fix speech-to-text functionality for their AI VTuber in a voice channel, encountering issues with audio processing and integration. python import time import asyncio import threading from socket import socket, AF INET, SOCK STREAM from json import dumps, loads from collections import deque from typing import Optional import discord from discord.ext import voice recv from discord.ext import commands import numpy as np import logging import pyaudio logging.getLogger "discord.ext.voice recv.reader" .setLevel logging.ERROR Local audio capture optional local audio module = None try: Add current directory to Python path for local modules import sys import os sys.path.insert 0, os.path.dirname os.path.abspath file import local audio capture local audio module = local audio capture print " OK Local audio capture module loaded" except ImportError as e: print f" WARN Local audio capture module not available: {e}" Additional debugging try: import pyaudio print " OK PyAudio is available" except Exception as pyaudio error: print f" ERROR PyAudio error: {pyaudio error}" voice recv available = True voice recv imported = voice recv connecting = False ============================= CONFIG ============================= CORE HOST = os.getenv "CORE HOST", "127.0.0.1" CORE PORT = int os.getenv "CORE PORT", "8765" DISCORD TOKEN = os.getenv "DISCORD TOKEN" if not DISCORD TOKEN: raise RuntimeError "DISCORD TOKEN environment variable not set" Optional: set MONITORED USER IDS="123,456" to only react to specific users MONITORED USER IDS = { int x.strip for x in os.getenv "MONITORED USER IDS", "" .split "," if x.strip .isdigit } Local audio capture settings LOCAL AUDIO ENABLED = os.getenv "LOCAL AUDIO ENABLED", "false" .lower == "true" Disabled by default to avoid conflicts LOCAL AUDIO DEVICE INDEX = os.getenv "LOCAL AUDIO DEVICE INDEX" if LOCAL AUDIO DEVICE INDEX is not None: LOCAL AUDIO DEVICE INDEX = int LOCAL AUDIO DEVICE INDEX else: Default to Yeti microphone if not specified LOCAL AUDIO DEVICE INDEX = 1 ============================= AUDIO / STATE ============================= librosa = None def get librosa : global librosa if librosa is None: import librosa as librosa librosa = librosa return librosa vc buffer = deque buffer lock = threading.Lock last audio time = 10 SILENCE TIMEOUT = 1.5 tts playing flag = False tts cooldown time = 10 Time when TTS cooldown ends TTS COOLDOWN DURATION = 1.5 Seconds to ignore audio after TTS finishes echo protection current voice client: Optional discord.VoiceClient = None current sink = None bot loop: Optional asyncio.AbstractEventLoop = None local audio pa = None PyAudio instance for local audio local audio stream = None Stream for local audio local audio thread = None Thread for local audio processing voice connect lock = asyncio.Lock voice action lock = asyncio.Lock ============================= PATCH OPUS DECODER ============================= import discord.opus real decode = discord.opus.Decoder.decode def safe decode self, args, kwargs : try: return real decode self, args, kwargs except discord.opus.OpusError: try: return real decode self, None, fec=False except Exception: return b"" discord.opus.Decoder.decode = safe decode ============================= HELPERS ============================= def is allowed member member: discord.Member - bool: Handle None member if member is None: return False if member.bot: return False if MONITORED USER IDS: return member.id in MONITORED USER IDS return True raw pcm counter = 0 debug counter = 0 vc debug wav dir = "debug audio" os.makedirs vc debug wav dir, exist ok=True def save debug wav audio, sample rate: int, filename: str : """Save audio to debug audio/ as a WAV file for STT debugging.""" global debug counter debug counter += 1 audio = np.asarray audio, dtype=np.float32 audio = np.clip audio, -1.0, 1.0 path = os.path.join vc debug wav dir, filename try: import soundfile as sf sf.write path, audio, sample rate except Exception as e: print f" DEBUG Save failed {filename} : {e}" def discord pcm to whisper pcm bytes: bytes - np.ndarray: """Convert Discord PCM 48kHz, 2ch, s16le, interleaved → 16kHz mono float32.""" global raw pcm counter raw pcm counter += 1 raw = np.frombuffer pcm bytes, dtype=np.int16 n = raw.size Warn about clipping in source audio int16 at extremes if raw pcm counter <= 5 or raw pcm counter % 50 == 0: clipped = np.sum raw == 32767 | raw == -32768 if clipped n 0.05: print f" CLIP { raw pcm counter}: {clipped}/{n} samples at int16 extremes — source audio too loud " Always log first 5, then periodically if raw pcm counter < 5 or raw pcm counter % 50 == 0: print f" PCM { raw pcm counter}: {len pcm bytes }B, {n} int16, first 10: {raw :10 .tolist }" Stage 1 debug: save raw PCM first 5 frames only — special format with subtype if raw pcm counter <= 5: try: import soundfile as sf sf.write f"{ vc debug wav dir}/raw{ raw pcm counter} stereo.wav", raw.reshape -1, 2 , 48000, subtype='PCM 16', except Exception as e: print f" PCM Save err: {e}" --- Downmix stereo to mono --- Discord Opus decodes to 48kHz stereo s16le interleaved. Each Opus frame has N int16 values = N/2 stereo pairs. if n = 2 and n % 2 == 0: mono 48k = raw.astype np.float32 .reshape -1, 2 .mean axis=1 else: mono 48k = raw.astype np.float32 Normalize int16 → float32 -1, 1 mono 48k /= 32768.0 mono 48k = np.clip mono 48k, -1.0, 1.0 Stage 2 debug: after mono downmix at 48kHz if raw pcm counter <= 5: save debug wav mono 48k, 48000, f"stage2 { raw pcm counter} 48k mono.wav" --- Resample 48kHz → 16kHz with anti-aliasing --- Attenuate by 0.9 before resampling to give the anti-alias filter headroom and prevent clipping from filter overshoot on loud/transient audio. try: from scipy.signal import resample poly mono 16k = resample poly mono 48k 0.9, up=1, down=3 except ImportError: librosa local = get librosa mono 16k = librosa local.resample mono 48k 0.9, orig sr=48000, target sr=16000 mono 16k = np.clip mono 16k, -1.0, 1.0 Stage 3 debug: final 16kHz output first 5 frames if raw pcm counter <= 5: save debug wav mono 16k, 16000, f"stage3 { raw pcm counter} 16k mono.wav" return mono 16k.astype np.float32 def send audio to core audio np: np.ndarray - Optional str : MAX SECONDS = 8 max samples = 16000 MAX SECONDS if len audio np max samples: audio np = audio np -max samples: for attempt in range 3 : try: print f"🔌 Connecting to vtuber core at {CORE HOST}:{CORE PORT} attempt {attempt+1}/3 ..." sock = socket AF INET, SOCK STREAM sock.settimeout 30.0 sock.connect CORE HOST, CORE PORT print "✅ Connected to vtuber core" payload = {"samples": audio np.tolist } data = dumps payload .encode "utf-8" sock.sendall len data .to bytes 4, "big" + data response = b"" while True: chunk = sock.recv 4096 if not chunk: break response += chunk sock.close if not response: print "⚠️ No response from vtuber core" return None result = loads response.decode "utf-8" wav path = result.get "wav path" if wav path and os.path.exists wav path : return wav path print f"⚠️ TTS file not found: {wav path}" return None except ConnectionRefusedError, TimeoutError, OSError as e: print f"❌ Core not ready attempt {attempt+1}/3 : {e}" time.sleep 5 except Exception as e: print f"❌ send audio to core error: {e}" import traceback traceback.print exc return None return None def play wav in vc vc: discord.VoiceClient, wav path: str : """Play WAV file in Discord VC.""" global tts playing flag if not vc or vc.channel is None or not os.path.exists wav path : return try: if hasattr vc, "is playing" and vc.is playing : vc.stop except Exception: pass tts playing flag = True def after play err : global tts playing flag, tts cooldown time tts playing flag = False tts cooldown time = time.time + TTS COOLDOWN DURATION Set cooldown try: if os.path.exists wav path : os.unlink wav path print f"🗑️ Cleaned up: {wav path}" except Exception as e: print f"⚠️ Failed to clean up {wav path}: {e}" if err: print f"❌ Playback error: {err}" try: vc.play discord.FFmpegPCMAudio wav path, executable="ffmpeg" , after=after play print f"▶️ Playing: {wav path}" except Exception as e: print f"❌ Error playing audio: {e}" tts playing flag = False print f"❌ Error playing audio: {e}" tts playing flag = False def local audio player wav path: str : """Player function for local audio capture responses.""" global current voice client if current voice client and os.path.exists wav path : if bot loop and bot loop.is running : bot loop.call soon threadsafe lambda: play wav in vc current voice client, wav path else: play wav in vc current voice client, wav path def stop voice listener vc: Optional "discord.VoiceProtocol" = None : """Stop any existing voice receive sink safely.""" if vc is None or not isinstance vc, discord.VoiceClient : return for name in "stop listening", "stop" : fn = getattr vc, name, None if callable fn : try: fn except Exception: pass break def stop local audio capture : """Stop local audio capture if running.""" global local audio pa, local audio stream, local audio thread if local audio module and local audio pa: try: local audio module.stop local audio capture local audio pa, local audio stream local audio pa = None local audio stream = None local audio thread = None print "🛑 Local audio capture stopped" except Exception as e: print f"❌ Error stopping local audio capture: {e}" async def connect or move to channel channel: discord.VoiceChannel : """Single safe path for connecting/moving the voice client.""" global current voice client, current sink, local audio pa, local audio stream, local audio thread async with voice connect lock: guild = channel.guild vc = guild.voice client if vc: if vc.channel is None: try: await vc.disconnect force=True except Exception: pass vc = None elif isinstance vc, discord.VoiceClient and vc.channel = channel: await vc.move to channel print f"🔊 Moved to {channel.name}" current voice client = vc return vc Fresh connect print f"🔌 Connecting to {channel.name}..." if not voice recv available: raise RuntimeError "voice recv not available" vc = await channel.connect cls=voice recv imported.VoiceRecvClient, timeout=30.0, self deaf=False, self mute=False print "✅ Connected with VoiceRecvClient" stop voice listener vc Attach proper AudioSink for voice recv sink = DiscordAudioSink vc.listen sink current sink = sink current voice client = vc print f"🎧 VC connected: {channel.name} voice recv sink attached " Start local audio capture if enabled and available if LOCAL AUDIO ENABLED and local audio module: try: print "🎤 Starting local audio capture..." local audio pa, local audio stream, local audio thread = local audio module.start local audio capture device index=LOCAL AUDIO DEVICE INDEX, discord vc player=local audio player print "✅ Local audio capture started" except Exception as e: print f"❌ Failed to start local audio capture: {e}" Try without specific device index try: print "🔄 Retrying with default device..." local audio pa, local audio stream, local audio thread = local audio module.start local audio capture discord vc player=local audio player print "✅ Local audio capture started with default device" except Exception as e2: print f"❌ Failed to start local audio capture with default device: {e2}" elif LOCAL AUDIO ENABLED: print "⚠️ Local audio capture enabled but module not available" return vc ============================= AUDIO SINK - Proper voice recv subclass ============================= class DiscordAudioSink voice recv imported.AudioSink : """Proper voice recv.AudioSink for PCM capture - Whisper""" def init self : super . init self.rolling = self.rolling max = 50 ~1 second 50 chunks × ~320 samples @16kHz self.speech active = False self.post speech seen = 0 self.hold chunks = int 12000 / 320 ~0.75s end-of-turn silence hold def wants opus self - bool: return False def write self, user, data : global last audio time, vc buffer, buffer lock, tts cooldown time if user is None or not data.pcm: return if not is allowed member user : return if tts playing flag or time.time < tts cooldown time: return try: audio np = discord pcm to whisper data.pcm if audio np.size < 80: return rms = np.sqrt np.mean audio np 2 amp = np.max np.abs audio np is speech = amp = 0.003 and rms = 0.0006 Always maintain rolling buffer all audio, no filtering self.rolling.append audio np if len self.rolling self.rolling max: self.rolling.pop 0 if not is speech and not self.speech active: return with buffer lock: if not self.speech active and is speech: print f"🔊 Speech onset {amp:.5f} rms={rms:.5f} prepending {len self.rolling } rolling chunks" for chunk in self.rolling :-1 : vc buffer.append chunk self.rolling.clear self.speech active = True self.post speech seen = 0 if is speech: self.post speech seen = 0 else: self.post speech seen += 1 vc buffer.append audio np last audio time = time.time if self.post speech seen = self.hold chunks: self.speech active = False except Exception as e: print f"⚠️ Audio sink write error: {e}" def cleanup self : print "🧹 Audio sink cleanup" ============================= SPEECH PROCESSOR THREAD ============================= def vc speech processor : global last audio time, tts playing flag, current voice client print " REFRESH Speech processor thread started" print "💡 Make sure vtuber core.py is running" while True: time.sleep 0.1 try: with buffer lock: if not vc buffer: continue time since audio = time.time - last audio time total samples = sum len chunk for chunk in vc buffer audio np is already 16kHz mono after discord pcm to whisper buffer full = total samples = 16000 2 ~2s utterance silence long = time since audio = 1.0 and total samples = 14000 ~0.9s minimum length max wait = time since audio = 5.0 and total samples = 14000 if not buffer full and not silence long and not max wait: continue chunks = list vc buffer vc buffer.clear if not chunks: continue audio np = np.concatenate chunks if audio np.size < 8000: duration short = audio np.size / 16000.0 print f"⚠️ Audio too short {audio np.size} samples = {duration short:.2f}s , skipping" continue current time = time.time if current time < tts cooldown time: continue if tts playing flag: continue audio np is already converted to 16kHz mono in discord pcm to whisper duration = audio np.size / 16000.0 print f"📊 Audio buffer: {len chunks } chunks, {len audio np } total samples {duration:.2f}s " print f"📤 Sending {duration:.2f}s audio to core..." Save exact STT input for debugging save debug wav audio np, 16000, f"stt input { debug counter}.wav" wav path = send audio to core audio np if not wav path: continue if current voice client and isinstance current voice client, discord.VoiceClient and wav path: print f"🔊 AI reply: {wav path}" if bot loop and bot loop.is running : bot loop.call soon threadsafe lambda: play wav in vc current voice client, wav path else: play wav in vc current voice client, wav path with buffer lock: last audio time = 0.0 except Exception as e: print f"❌ Speech processor error: {e}" threading.Thread target=vc speech processor, daemon=True .start ============================= DISCORD BOT ============================= intents = discord.Intents.default intents.voice states = True intents.message content = True intents.members = True bot = commands.Bot command prefix=" ", intents=intents @bot.event async def on ready : global bot loop bot loop = asyncio.get running loop print f" OK Bot logged in as {bot.user}" print " BOT Voice bot ready " @bot.event async def on voice state update member, before, after : """Single clean voice handler""" global connecting if connecting or member.bot or not is allowed member member or after.channel is None or before.channel == after.channel: return connecting = True try: await asyncio.sleep 2 await connect or move to channel after.channel except Exception as e: print f"❌ Voice error: {e}" finally: connecting = False @bot.command async def join ctx : """Join voice channel""" if not ctx.author.voice: await ctx.send "❌ Join voice first " return await connect or move to channel ctx.author.voice.channel await ctx.send "✅ Joined " @bot.command async def leave ctx : """Leave voice""" vc = ctx.guild.voice client if vc: stop voice listener vc stop local audio capture Stop local audio capture await vc.disconnect await ctx.send "👋 Left" bot.run DISCORD TOKEN other file: python import socket import json import numpy as np import tempfile import soundfile as sf import os import sys import argparse import threading from rich.console import Console import nltk import re nltk.download 'punkt', quiet=True EXCLUDED PHRASES = set import numpy as np from scipy.signal import resample poly def pcm s16le 48k stereo to 16k mono float32 pcm bytes: bytes - np.ndarray: audio i16 = np.frombuffer pcm bytes, dtype=np.int16 if audio i16.size == 0: return np.zeros 0, dtype=np.float32 Drop incomplete stereo frame if needed. usable = audio i16.size // 2 2 audio i16 = audio i16 :usable frames x channels stereo = audio i16.reshape -1, 2 stereo → mono mean, not sum mono = stereo.astype np.float32 .mean axis=1 int16 → float32 mono = mono / 32768.0 safety clamp — this is what keeps your amplitude in -1, 1 mono = np.clip mono, -1.0, 1.0 48 kHz → 16 kHz mono 16k = resample poly mono, up=1, down=3 return mono 16k.astype np.float32 Ensure HF HOME doesn't point to stale cache overrides external tool env vars if "HF HOME" in os.environ and "TEST OMNIVOICE" in os.environ.get "HF HOME", "" : del os.environ "HF HOME" if "HF HUB CACHE" not in os.environ: os.environ "HF HUB CACHE" = os.path.join os.path.expanduser "~" , ".cache", "huggingface", "hub" Parse arguments parser = argparse.ArgumentParser description="Standalone VTuber Core Server" parser.add argument "--voice", type=str, default="meuro-enhanced-v2.wav" parser.add argument "--cfg-weight", type=float, default=0.5 args = parser.parse args console = Console from improved local stt import process audio chunk def is valid text text : text = text.strip if not text: return False Check for repetitive patterns that indicate feedback words = text.lower .split if len words 3: unique words = set words repetition ratio = len words / len unique words if len unique words 0 else 0 if repetition ratio 3.0: High repetition likely means feedback print f"🚫 Likely feedback detected repetition ratio: {repetition ratio:.2f} : '{text}'" return False repeated spam like "the the the" - increased threshold to allow more variety if len words 3: Only check repetition if we have more than 3 words if len set words <= 1: Only reject if all words are identical print f"🚫 All words identical: '{text}'" return False return True HALLUCINATED PHRASES = { "thanks for watching", "subscribe", "thank you", "you", "thank you for watching", "please subscribe", "like and subscribe", "thanks", "bye", "goodbye", "see you", "thank", "thanks for listening", "thank you for listening", "the", "a", "and", "i", "we", "you", "it", } def is valid transcription text : """Check if transcription is valid not garbage/hallucination """ if not text or not text.strip : return False cleaned = re.sub r' . ?, ', '', text.lower .strip if cleaned in HALLUCINATED PHRASES: print f"🚫 Hallucination filtered: '{text}'" return False if cleaned in EXCLUDED PHRASES: return False words = text.split if len words < 2: print f"🚫 Too short: '{text}'" return False unique words = len set words if len words 3 and unique words / len words < 0.3: print f"🚫 Repetitive: '{text}'" return False return True AUDIO DEBUG DIR = "debug audio" os.makedirs AUDIO DEBUG DIR, exist ok=True audio counter = 0 def save debug wav audio, sample rate: int, filename: str : """Save audio to debug audio/ as a WAV file for STT debugging.""" audio = np.asarray audio, dtype=np.float32 audio = np.clip audio, -1.0, 1.0 path = os.path.join AUDIO DEBUG DIR, filename try: sf.write path, audio, sample rate except Exception as e: print f" DEBUG Save failed {filename} : {e}" def safe transcribe audio np or pcm bytes, , input sr: int = 48000, input channels: int = 2 : """Accept either: - float32 numpy audio at 16kHz mono current behavior - raw PCM bytes: S16LE stereo at 48kHz will convert to 16k mono float32 """ global audio counter Convert bytes - float32 mono @16kHz16kHz if isinstance audio np or pcm bytes, bytes, bytearray : if input sr = 48000 or input channels = 2: print f" WARN PCM conversion assumes 48kHz stereo S16LE; got sr={input sr}, ch={input channels}" audio np = pcm s16le 48k stereo to 16k mono float32 bytes audio np or pcm bytes debug in sr = 16000 print f" PCM Converted PCM bytes - float32 mono {audio np.size} samples @16k" else: audio np = audio np or pcm bytes debug in sr = 16000 bot-side discord pcm to whisper already produced 16kHz mono if audio np is None or getattr audio np, "size", 0 == 0: return "" print f" SEARCH Audio analysis - size: {audio np.size}, max amplitude: {np.max np.abs audio np :.6f}" Limit audio length max samples = 48000 if len audio np max samples: audio np = audio np -max samples: print f" TRIM Trimmed to last {max samples} samples" max amp = np.max np.abs audio np print f" MIC Incoming audio | amp={max amp:.6f} | samples={len audio np }" print f" PROCESS Processing audio: {audio np.size} samples, {max amp:.6f} max amplitude" Save debug WAV exact STT input audio counter += 1 save debug wav audio np, debug in sr, f"debug { audio counter}.wav" print f" DEBUG Saved: debug { audio counter}.wav" duration s = len audio np / 16000.0 if duration s < 0.5: print f"🚫 Rejected audio: {duration s:.2f}s too short" return "" if max amp < 0.001: print f"🚫 Rejected audio: amp={max amp:.6f}" return "" if max amp 1.05: print f"🛑 Clipped audio: amp={max amp:.6f} should be ≤1.0 " try: print " STT Transcribing with improved local STT fixed ..." text = process audio chunk "discord user", audio np or "" print f" RAW RAW STT: '{text}'" print f" TEXT Transcribed text: '{text}'" return text Always str except Exception as stt error: print f" STT-ERROR STT failed: {stt error}" return "" import requests SYSTEM PROMPT = "You are a helpful AI." def simple llm text : try: prompt = f"{SYSTEM PROMPT}\n\nUser: {text}\nAI:" print f"📤 Sending to Ollama: '{text :50 }...'" response = requests.post "http://127.0.0.1:11434/api/generate", headers={"Content-Type": "application/json"}, json={ "model": "drivedenpadev/deepseek-v3.2", "prompt": prompt, "stream": False }, timeout=90 print f"📥 Ollama response status: {response.status code}" if response.status code = 200: print f"⚠️ Ollama returned status {response.status code}: {response.text}" return "Sorry, something went wrong." safer JSON handling try: data = response.json except Exception: print "❌ Failed to parse JSON:", response.text return "Invalid response from LLM." print f"📄 Ollama response data: {data}" result = data.get "response", "" .strip if not result: print "⚠️ Empty response from model" return "No response from AI." print f"💬 Extracted response: '{result}'" return result except requests.exceptions.ConnectionError: print "❌ Cannot connect to Ollama - is it running?" return "Sorry, LLM service unavailable." except requests.exceptions.Timeout: print "❌ Ollama request timed out" return "Sorry, taking too long to think." except Exception as e: print f"❌ Ollama error: {e}" import traceback traceback.print exc return "Sorry, something went wrong." SYSTEM PROMPT = """ You are a funny VTuber. Keep replies under 14 words. No emojis. Act human """ TTS using tts.py standalone sys.path.insert 0, os.path.dirname file from tts import TextToSpeechService, sanitize tts text try: print " ROCKET Initializing TTS service..." tts = TextToSpeechService print f" OK TTS service initialized device: {tts.device} " Check if voice file exists if args.voice and os.path.exists args.voice : print f" OK Voice model found: {args.voice}" elif args.voice: print f" WARN Voice model not found: {args.voice}" else: print " WARN No voice model specified" except Exception as e: print f" ERROR Failed to initialize TTS service: {e}" import traceback traceback.print exc tts = None HOST = os.getenv "CORE HOST", "127.0.0.1" PORT = int os.getenv "CORE PORT", "8765" def handle audio audio np or pcm bytes : try: print "📝 Transcribing..." text = safe transcribe audio np or pcm bytes print f"📝 Transcribed text: '{text}'" print f"👤 You: {text}" if not is valid transcription text : print f"⏭️ Skipping Ollama invalid transcript " return None, text response = simple llm text print f"🤖 AI: {response}" Trim response to reasonable length if len response 500: response = response :500 + "..." print f"✂️ Trimmed long response: {len response } chars" safe text = sanitize tts text response print f"🔤 Sanitized text: '{safe text}'" try: if tts is None: print "❌ TTS service is not initialized" return None, text print "🎵 Generating audio..." sr, audio = tts.synthesize safe text, audio prompt path=args.voice, cfg weight=args.cfg weight print f"🎵 TTS result: sr={sr}, audio shape={getattr audio, 'shape', 'N/A' }" if audio is None or len audio == 0: print "🔇 TTS generated empty audio, skipping" return None, text tmp = tempfile.NamedTemporaryFile suffix=".wav", delete=False tmp.close sf.write tmp.name, audio, sr print f"✅ TTS generated successfully: {tmp.name}" return tmp.name, text except Exception as tts error: print f"❌ TTS generation failed: {tts error}" import traceback traceback.print exc return None, text except Exception as e: print f"❌ Error: {e}" import traceback traceback.print exc return None def server loop : sock = socket.socket socket.AF INET, socket.SOCK STREAM sock.setsockopt socket.SOL SOCKET, socket.SO REUSEADDR, 1 sock.bind HOST, PORT sock.listen 1 print f" SERVER Standalone Core on {HOST}:{PORT}" while True: conn, addr = sock.accept print f" CONNECT {addr}" Read length + data size bytes = conn.recv 4 size = int.from bytes size bytes, "big" data = b"" while len data < size: data += conn.recv 4096 payload = json.loads data Incoming payload audio formats choose one : 1 float32 mono/whatever samples: payload "samples" as list float 2 raw PCM bytes S16LE stereo 48k : payload "pcm s16le" as base64 str or list int - If using base64: payload "pcm s16le" must be a base64-encoded S16LE byte stream. if "pcm s16le" in payload: pcm val = payload "pcm s16le" if isinstance pcm val, str : import base64 audio input = base64.b64decode pcm val else: assume list int of int16 bytes audio input = bytes pcm val else: audio list = payload.get "samples", audio input = np.array audio list, dtype=np.float32 wav path, transcription = handle audio audio input response = json.dumps {"wav path": wav path or "", "transcription": transcription} .encode conn.sendall response conn.close if name == " main ": server loop what am i doing wrong help i’ve tried everything and nothing works to fix stt on discord i’m trying to do it locally because i am losing my mind after months of trying to make this work