v48: WS — один JSON с base64, без чанков, без бинарных фреймов
This commit is contained in:
+9
-19
@@ -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 = 'v47';
|
const VERSION = 'v48';
|
||||||
document.getElementById('ver').textContent = VERSION;
|
document.getElementById('ver').textContent = VERSION;
|
||||||
|
|
||||||
// ---------- Перевод на русский (Groq) ----------
|
// ---------- Перевод на русский (Groq) ----------
|
||||||
@@ -612,7 +612,7 @@ 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();
|
||||||
|
|
||||||
// Кодируем в base64 — только текстовые WS-фреймы, DPI не режет
|
// Кодируем blob в base64 (только текстовые WS-фреймы — DPI не режет)
|
||||||
const base64 = await new Promise((resolve, reject) => {
|
const base64 = await new Promise((resolve, reject) => {
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = () => resolve(reader.result.split(',')[1]);
|
reader.onload = () => resolve(reader.result.split(',')[1]);
|
||||||
@@ -620,9 +620,7 @@ async function transcribe(blob) {
|
|||||||
reader.readAsDataURL(blob);
|
reader.readAsDataURL(blob);
|
||||||
});
|
});
|
||||||
|
|
||||||
const CHUNK = 4096; // 4KB на чанк
|
console.log('[WS] b64=' + (base64.length/1024).toFixed(1) + 'KB');
|
||||||
const total = Math.ceil(base64.length / CHUNK);
|
|
||||||
console.log('[WS] blob=' + (blob.size/1024).toFixed(1) + 'KB b64=' + base64.length + ' chunks=' + total);
|
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
try {
|
try {
|
||||||
@@ -632,26 +630,18 @@ async function transcribe(blob) {
|
|||||||
}, 60000);
|
}, 60000);
|
||||||
|
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
console.log('[WS] connected, chunks=' + total);
|
ws.send(JSON.stringify({ token: GROQ_API_KEY, audio_b64: base64, mime: blob.type || 'audio/webm' }));
|
||||||
ws.send(JSON.stringify({ token: GROQ_API_KEY, total, mime: blob.type || 'audio/webm' }));
|
console.log('[WS] sent');
|
||||||
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.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-b64 ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
console.log('[WS] ← ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
||||||
if (data.error) resolve({ text: '', error: data.error });
|
resolve(data.error ? { text: '', error: data.error } : { text: data.text || '', error: null });
|
||||||
else resolve({ text: data.text || '', error: null });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onerror = () => {
|
ws.onerror = () => { clearTimeout(timeout); resolve({ text: '', error: 'network' }); };
|
||||||
console.log('[WS] error'); clearTimeout(timeout); resolve({ text: '', error: 'network' });
|
|
||||||
};
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
resolve({ text: '', error: 'network' });
|
resolve({ text: '', error: 'network' });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user