From 3442e5d0310e96cfb17281fcbc3cff904a2de849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Fri, 22 May 2026 17:32:11 +0400 Subject: [PATCH] =?UTF-8?q?revert:=20=D0=BE=D1=82=D0=BA=D0=B0=D1=82=20?= =?UTF-8?q?=D0=B4=D0=BE=20v48=20=E2=80=94=20=D1=83=D0=B1=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D1=8B=20v49-v53=20(close/race/onopen=20=D0=BA=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=8B=D0=BB=D0=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 87 +++++++++++++++++++----------------------------------- 1 file changed, 30 insertions(+), 57 deletions(-) diff --git a/index.html b/index.html index 3e6ba33..9126097 100644 --- a/index.html +++ b/index.html @@ -53,7 +53,7 @@ let mediaRecorder, audioChunks = [], userAudioBlob = null, ttsText = ''; let isRecording = false, isAnalyzing = false, micAnalyser = null, micAnimId = null; let recordStartTime = 0, recordDuration = 0; // ---------- Версия ---------- -const VERSION = 'v53'; +const VERSION = 'v48'; document.getElementById('ver').textContent = VERSION; // ---------- Перевод на русский (Groq) ---------- @@ -221,28 +221,19 @@ async function doCompare(blob, originalText, duration) { const elapsed = ((Date.now() - startTime) / 1000).toFixed(1); setStatus('Анализ... ' + elapsed + 'с'); }, 150); - - // Шаг 1: транскрипция (быстро, ~0.5с) - let whisperResult = { text: '', error: null }; - try { whisperResult = await transcribe(blob); } - catch(e) { console.log('[COMPARE] transcribe failed:', e); } - - // Шаг 2: фонетика (может виснуть на decodeAudioData, таймаут 10с) - let phon = { pitchHz: 0, pitchStability: 0, voiceClarity: 0, phonScore: 0 }; + let whisperResult, phon; try { - phon = await Promise.race([ - phoneticAnalysis(blob), - new Promise(r => setTimeout(() => r({ pitchHz: 0, pitchStability: 0, voiceClarity: 0, phonScore: 0 }), 10000)) + [whisperResult, phon] = await Promise.all([ + transcribe(blob), + phoneticAnalysis(blob) ]); - } catch(e) { console.log('[COMPARE] phonetics failed:', e); } - - clearInterval(timerId); - isAnalyzing = false; - - if (whisperResult.error && !whisperResult.text) { - setStatus('Ошибка: ' + (whisperResult.error || 'сеть')); + } catch(e) { + clearInterval(timerId); + isAnalyzing = false; + setStatus('Ошибка анализа.'); return ''; } + clearInterval(timerId); const phonEmoji = phon.phonScore >= 85 ? '🟢' : phon.phonScore >= 60 ? '🟡' : '🔴'; let recHTML = '', diffHTML = ''; @@ -273,6 +264,7 @@ async function doCompare(blob, originalText, duration) { ''; setStatus('Готово.'); + isAnalyzing = false; return whisperResult.text || ''; } @@ -620,13 +612,10 @@ async function transcribe(blob) { if (!GROQ_API_KEY) return { text: '', error: 'no_key' }; const tStart = performance.now(); + // Кодируем blob в base64 (только текстовые WS-фреймы — DPI не режет) const base64 = await new Promise((resolve, reject) => { const reader = new FileReader(); - reader.onload = () => { - const b64 = reader.result; - if (typeof b64 !== 'string') reject(new Error('not string')); - else resolve(b64.split(',')[1] || b64); - }; + reader.onload = () => resolve(reader.result.split(',')[1]); reader.onerror = reject; reader.readAsDataURL(blob); }); @@ -634,44 +623,28 @@ async function transcribe(blob) { console.log('[WS] b64=' + (base64.length/1024).toFixed(1) + 'KB'); return new Promise((resolve) => { - let settled = false; - const done = (r) => { if (settled) return; settled = true; resolve(r); }; + try { + const ws = new WebSocket('wss://proxy.kube5s.ru/ws'); + const timeout = setTimeout(() => { + console.log('[WS] timeout'); ws.close(); resolve({ text: '', error: 'timeout' }); + }, 60000); - const payload = JSON.stringify({ token: GROQ_API_KEY, audio_b64: base64, mime: blob.type || 'audio/webm' }); - const ws = new WebSocket('wss://proxy.kube5s.ru/ws'); - const timeout = setTimeout(() => { console.log('[WS] timeout'); try { ws.close(); } catch(e){} done({ text: '', error: 'timeout' }); }, 15000); + ws.onopen = () => { + ws.send(JSON.stringify({ token: GROQ_API_KEY, audio_b64: base64, mime: blob.type || 'audio/webm' })); + console.log('[WS] sent'); + }; - const trySend = () => { - if (settled) return; - const st = ws.readyState; - if (st === WebSocket.OPEN) { - try { ws.send(payload); console.log('[WS] sent'); } catch(e) { console.log('[WS] send err:', e); clearTimeout(timeout); done({ text: '', error: 'network' }); } - return; - } - if (st === WebSocket.CONNECTING) { setTimeout(trySend, 20); return; } - console.log('[WS] aborted, state=' + st); - clearTimeout(timeout); - done({ text: '', error: 'network' }); - }; - - ws.onopen = trySend; - trySend(); - - ws.onmessage = (e) => { - clearTimeout(timeout); - try { ws.close(); } catch(ex) {} - try { + ws.onmessage = (e) => { + clearTimeout(timeout); ws.close(); const data = JSON.parse(e.data); console.log('[WS] ← ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)')); - done(data.error ? { text: '', error: data.error } : { text: data.text || '', error: null }); - } catch(ex) { - console.log('[WS] parse err:', ex); - done({ text: '', error: 'parse' }); - } - }; + resolve(data.error ? { text: '', error: data.error } : { text: data.text || '', error: null }); + }; - ws.onerror = () => { console.log('[WS] error'); clearTimeout(timeout); done({ text: '', error: 'network' }); }; - ws.onclose = (e) => { console.log('[WS] close code=' + e.code); /* не резолвим — клиент сам закрывает после onmessage */ }; + ws.onerror = () => { clearTimeout(timeout); resolve({ text: '', error: 'network' }); }; + } catch (err) { + resolve({ text: '', error: 'network' }); + } }); }