v45: WebSocket — весь блоб одним сообщением, без чанков

This commit is contained in:
“Naeel”
2026-05-22 16:31:35 +04:00
parent 05afcc7bd2
commit d4aebf4b2c
+11 -15
View File
@@ -53,7 +53,7 @@ let mediaRecorder, audioChunks = [], userAudioBlob = null, ttsText = '';
let isRecording = false, isAnalyzing = false, micAnalyser = null, micAnimId = null; let isRecording = false, isAnalyzing = false, micAnalyser = null, micAnimId = null;
let recordStartTime = 0, recordDuration = 0; let recordStartTime = 0, recordDuration = 0;
// ---------- Версия ---------- // ---------- Версия ----------
const VERSION = 'v44'; const VERSION = 'v45';
document.getElementById('ver').textContent = VERSION; document.getElementById('ver').textContent = VERSION;
// ---------- Перевод на русский (Groq) ---------- // ---------- Перевод на русский (Groq) ----------
@@ -608,8 +608,6 @@ document.getElementById('btnPlayUser').onclick = async () => {
} }
}; };
const CHUNK_SIZE = 4096;
async function transcribe(blob) { async function transcribe(blob) {
if (!GROQ_API_KEY) return { text: '', error: 'no_key' }; if (!GROQ_API_KEY) return { text: '', error: 'no_key' };
const tStart = performance.now(); const tStart = performance.now();
@@ -637,33 +635,31 @@ async function transcribe(blob) {
} }
} }
// Large file: WebSocket // Large file: WebSocket (entire blob, no chunks)
const total = Math.ceil(blob.size / CHUNK_SIZE); console.log('[WS] opening, blob=' + (blob.size/1024).toFixed(1) + 'KB');
return new Promise((resolve) => { return new Promise((resolve) => {
try { try {
const ws = new WebSocket('wss://proxy.kube5s.ru/ws'); const ws = new WebSocket('wss://proxy.kube5s.ru/ws');
const timeout = setTimeout(() => { ws.close(); resolve({ text: '', error: 'timeout' }); }, 60000); const timeout = setTimeout(() => { console.log('[WS] timeout'); ws.close(); resolve({ text: '', error: 'timeout' }); }, 60000);
ws.onopen = () => { ws.onopen = async () => {
ws.send(JSON.stringify({ token: GROQ_API_KEY, chunks: total })); console.log('[WS] connected');
(async () => { ws.send(JSON.stringify({ token: GROQ_API_KEY }));
for (let i = 0; i < total; i++) { ws.send(await blob.arrayBuffer());
const chunk = blob.slice(i * CHUNK_SIZE, Math.min((i + 1) * CHUNK_SIZE, blob.size)); console.log('[WS] blob sent');
ws.send(await chunk.arrayBuffer());
}
})();
}; };
ws.onmessage = (e) => { ws.onmessage = (e) => {
clearTimeout(timeout); clearTimeout(timeout);
ws.close(); ws.close();
const data = JSON.parse(e.data); const data = JSON.parse(e.data);
console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB ws/' + total + 'chunks ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)')); console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB ws ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
if (data.error) resolve({ text: '', error: data.error }); if (data.error) resolve({ text: '', error: data.error });
else resolve({ text: data.text || '', error: null }); else resolve({ text: data.text || '', error: null });
}; };
ws.onerror = () => { ws.onerror = () => {
console.log('[WS] error');
clearTimeout(timeout); clearTimeout(timeout);
resolve({ text: '', error: 'network' }); resolve({ text: '', error: 'network' });
}; };