v48: WS — один JSON с base64, без чанков, без бинарных фреймов

This commit is contained in:
“Naeel”
2026-05-22 17:01:30 +04:00
parent 26c5adfc79
commit 8f8a21bbcc
+9 -19
View File
@@ -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 = 'v47';
const VERSION = 'v48';
document.getElementById('ver').textContent = VERSION;
// ---------- Перевод на русский (Groq) ----------
@@ -612,7 +612,7 @@ async function transcribe(blob) {
if (!GROQ_API_KEY) return { text: '', error: 'no_key' };
const tStart = performance.now();
// Кодируем в base64 только текстовые WS-фреймы, DPI не режет
// Кодируем blob в base64 (только текстовые WS-фреймы DPI не режет)
const base64 = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result.split(',')[1]);
@@ -620,9 +620,7 @@ async function transcribe(blob) {
reader.readAsDataURL(blob);
});
const CHUNK = 4096; // 4KB на чанк
const total = Math.ceil(base64.length / CHUNK);
console.log('[WS] blob=' + (blob.size/1024).toFixed(1) + 'KB b64=' + base64.length + ' chunks=' + total);
console.log('[WS] b64=' + (base64.length/1024).toFixed(1) + 'KB');
return new Promise((resolve) => {
try {
@@ -632,26 +630,18 @@ async function transcribe(blob) {
}, 60000);
ws.onopen = () => {
console.log('[WS] connected, chunks=' + total);
ws.send(JSON.stringify({ token: GROQ_API_KEY, total, mime: blob.type || 'audio/webm' }));
for (let i = 0; i < base64.length; i += CHUNK) {
ws.send(JSON.stringify({ chunk: base64.slice(i, i + CHUNK) }));
}
console.log('[WS] all sent');
ws.send(JSON.stringify({ token: GROQ_API_KEY, audio_b64: base64, mime: blob.type || 'audio/webm' }));
console.log('[WS] sent');
};
ws.onmessage = (e) => {
clearTimeout(timeout);
ws.close();
clearTimeout(timeout); ws.close();
const data = JSON.parse(e.data);
console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB ws-b64 ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
if (data.error) resolve({ text: '', error: data.error });
else resolve({ text: data.text || '', error: null });
console.log('[WS] ← ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
resolve(data.error ? { text: '', error: data.error } : { text: data.text || '', error: null });
};
ws.onerror = () => {
console.log('[WS] error'); clearTimeout(timeout); resolve({ text: '', error: 'network' });
};
ws.onerror = () => { clearTimeout(timeout); resolve({ text: '', error: 'network' }); };
} catch (err) {
resolve({ text: '', error: 'network' });
}