v47: WS base64 chunks — только текстовые фреймы, DPI не режет
This commit is contained in:
+23
-34
@@ -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 = 'v46';
|
||||
const VERSION = 'v47';
|
||||
document.getElementById('ver').textContent = VERSION;
|
||||
|
||||
// ---------- Перевод на русский (Groq) ----------
|
||||
@@ -612,56 +612,45 @@ async function transcribe(blob) {
|
||||
if (!GROQ_API_KEY) return { text: '', error: 'no_key' };
|
||||
const tStart = performance.now();
|
||||
|
||||
// Small file: direct POST
|
||||
if (blob.size <= 14000) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', new File([blob], 'audio.webm', { type: 'audio/webm' }));
|
||||
formData.append('model', 'whisper-large-v3');
|
||||
formData.append('language', 'it');
|
||||
try {
|
||||
const ctl = new AbortController();
|
||||
const t = setTimeout(() => ctl.abort(), 60000);
|
||||
const res = await fetch('https://proxy.kube5s.ru/openai/v1/audio/transcriptions', {
|
||||
method: 'POST', headers: { 'Authorization': `Bearer ${GROQ_API_KEY}` },
|
||||
body: formData, signal: ctl.signal
|
||||
});
|
||||
clearTimeout(t);
|
||||
const data = await res.json();
|
||||
console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB direct ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
||||
if (data.error) return { text: '', error: data.error.message || 'forbidden' };
|
||||
return { text: data.text || '', error: null };
|
||||
} catch (err) {
|
||||
return { text: '', error: err.name === 'AbortError' ? 'timeout' : 'network' };
|
||||
}
|
||||
}
|
||||
// Кодируем в base64 — только текстовые WS-фреймы, DPI не режет
|
||||
const base64 = await new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result.split(',')[1]);
|
||||
reader.onerror = reject;
|
||||
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);
|
||||
|
||||
// Large file: WebSocket (entire blob, no chunks)
|
||||
console.log('[WS] opening, blob=' + (blob.size/1024).toFixed(1) + 'KB');
|
||||
return new Promise((resolve) => {
|
||||
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 timeout = setTimeout(() => {
|
||||
console.log('[WS] timeout'); ws.close(); resolve({ text: '', error: 'timeout' });
|
||||
}, 60000);
|
||||
|
||||
ws.onopen = () => {
|
||||
console.log('[WS] connected');
|
||||
ws.send(JSON.stringify({ token: GROQ_API_KEY }));
|
||||
ws.send(blob); // Blob directly — no async/await needed
|
||||
console.log('[WS] blob sent ' + (blob.size/1024).toFixed(1) + 'KB');
|
||||
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.onmessage = (e) => {
|
||||
clearTimeout(timeout);
|
||||
ws.close();
|
||||
const data = JSON.parse(e.data);
|
||||
console.log('[WHISPER] ' + (blob.size/1024).toFixed(1) + 'KB ws ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
|
||||
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 });
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
console.log('[WS] error');
|
||||
clearTimeout(timeout);
|
||||
resolve({ text: '', error: 'network' });
|
||||
console.log('[WS] error'); clearTimeout(timeout); resolve({ text: '', error: 'network' });
|
||||
};
|
||||
} catch (err) {
|
||||
resolve({ text: '', error: 'network' });
|
||||
|
||||
Reference in New Issue
Block a user