Spaces:
Running
Running
Turbo file mode: VAD phrases straight to the full-context pass
Browse files86 unmodified lines87888990919230 unmodified lines1231241251261271281291301311323 unmodified lines13613713813914014114222 unmodified lines1651661671681691703 unmodified lines17417517617717817918018118218318418586 unmodified linesstate.corrBusy = false; state.corrMs.push(m.ms); if (state.corrMs.length > 20) state.corrMs.shift();const s = state.segs.find(x => x.id === m.id);if (s && m.gen >= (s.corrGen || 0)) { s.corrGen = m.gen; s.corr = { toks: m.toks, upto: m.upto, final: m.final }; }pumpCorr(); render(); stats();}30 unmodified linesconst avg = a => a.length ? a.reduce((x, y) => x + y, 0) / a.length : 0;const c = avg(state.chunkMs);$('#st-chunk').textContent = c ? c.toFixed(0) + ' ms' : '–';$('#st-rtf').textContent = c ? (320 / c).toFixed(1) + '×' : '–';$('#st-corr').textContent = state.corrMs.length ? avg(state.corrMs).toFixed(0) + ' ms' : '–';$('#st-audio').textContent = state.audioSec.toFixed(1) + ' s';if (state.mode === 'file' && state.t0) $('#st-wall').textContent = ((performance.now() - state.t0) / 1000).toFixed(1) + ' s';}// ---------- audio ----------3 unmodified linesstream.postMessage({ type: 'audio', samples: x });}function resetAll() {state.audio = new Float32Array(SR * 120); state.n = 0; state.segs = []; state.audioSec = 0; state.corrQueue = []; state.chunkMs = []; state.corrMs = [];$('#transcript').replaceChildren(); state.accept = false; stream.postMessage({ type: 'reset' }); render(); stats();}let ac = null, mic = null, node = null;22 unmodified linesstate.mode = null; document.body.classList.remove('recording'); $('#rec').setAttribute('aria-pressed', 'false'); level(0);}function level(r) { const v = Math.min(1, r * 9); document.documentElement.style.setProperty('--lvl', v.toFixed(3)); }async function runFile(file) {if (state.mode === 'mic') await stopMic();resetAll(); state.mode = 'file'; $('#rec').classList.add('busy');3 unmodified linesconst off = new OfflineAudioContext(1, Math.ceil(buf.duration * SR), SR);const s = off.createBufferSource(); s.buffer = buf; s.connect(off.destination); s.start();const x = (await off.startRendering()).getChannelData(0);state.fileSec = x.length / SR; state.t0 = performance.now();const y = new Float32Array(x.length + Math.round(0.8 * SR)); y.set(x);for (let i = 0; i < y.length; i += SR * 4) pushAudio(y.slice(i, Math.min(y.length, i + SR * 4)));stream.postMessage({ type: 'end' });}// ---------- UI wiring ----------$('#rec').onclick = () => { if (!document.body.classList.contains('ready')) return; state.mode === 'mic' ? stopMic() : startMic().catch(e => { state.mode = null; setStatus('⚠ ' + e.message, 'err'); }); };$('#file').onchange = e => { const f = e.target.files[0]; if (f) runFile(f).catch(err => setStatus('⚠ ' + err.message, 'err')); e.target.value = ''; };86 unmodified lines8788899091929330 unmodified lines1241251261271281291301311321331343 unmodified lines13813914014114214314422 unmodified lines1671681691701711721731741751761771781791801811821831841851861871883 unmodified lines19219319419519619719819920020120220320420520620786 unmodified linesstate.corrBusy = false; state.corrMs.push(m.ms); if (state.corrMs.length > 20) state.corrMs.shift();const s = state.segs.find(x => x.id === m.id);if (s && m.gen >= (s.corrGen || 0)) { s.corrGen = m.gen; s.corr = { toks: m.toks, upto: m.upto, final: m.final }; }if (s && s.file) { state.audioSec = s.end / SR; if (--state.filePending <= 0) { state.mode = null; $('#rec').classList.remove('busy'); } }pumpCorr(); render(); stats();}30 unmodified linesconst avg = a => a.length ? a.reduce((x, y) => x + y, 0) / a.length : 0;const c = avg(state.chunkMs);$('#st-chunk').textContent = c ? c.toFixed(0) + ' ms' : '–';const wall = state.t0 ? (performance.now() - state.t0) / 1000 : 0;$('#st-rtf').textContent = state.segs.some(s => s.file) && wall ? (state.audioSec / wall).toFixed(0) + '×' : c ? (320 / c).toFixed(1) + '×' : '–';$('#st-corr').textContent = state.corrMs.length ? avg(state.corrMs).toFixed(0) + ' ms' : '–';$('#st-audio').textContent = state.audioSec.toFixed(1) + ' s';if (state.t0 && state.segs.some(s => s.file)) $('#st-wall').textContent = ((performance.now() - state.t0) / 1000).toFixed(1) + ' s';}// ---------- audio ----------3 unmodified linesstream.postMessage({ type: 'audio', samples: x });}function resetAll() {state.audio = new Float32Array(SR * 120); state.n = 0; state.segs = []; state.t0 = 0; state.audioSec = 0; state.corrQueue = []; state.chunkMs = []; state.corrMs = [];$('#transcript').replaceChildren(); state.accept = false; stream.postMessage({ type: 'reset' }); render(); stats();}let ac = null, mic = null, node = null;22 unmodified linesstate.mode = null; document.body.classList.remove('recording'); $('#rec').setAttribute('aria-pressed', 'false'); level(0);}function level(r) { const v = Math.min(1, r * 9); document.documentElement.style.setProperty('--lvl', v.toFixed(3)); }// Files go "turbo": energy-VAD cut into phrases (<=20 s), each decoded straight away by the full-context// encoder (WebGPU when available) + beam + LM. The live streaming pass is for the microphone.function vadCuts(x) {const F = 320, n = Math.floor(x.length / F), e = new Float32Array(n);for (let i = 0; i < n; i++) { let s = 0; for (let j = i * F; j < (i + 1) * F; j++) s += x[j] * x[j]; e[i] = Math.sqrt(s / F); }const sorted = Float32Array.from(e).sort(), floor = sorted[Math.floor(n * 0.1)] || 0, thr = Math.max(floor * 3, 0.003);const cuts = [0]; let segStart = 0, sil = 0;for (let i = 0; i < n; i++) {sil = e[i] < thr ? sil + 1 : 0;const len = (i - segStart) * F / SR;if ((sil >= 15 && len > 4) || (sil >= 8 && len > 12)) { const c = i - Math.floor(sil / 2); cuts.push(c * F); segStart = c; sil = 0; }else if (len > 20) { let best = i, bv = Infinity; for (let k = i - 250; k < i; k++) if (e[k] < bv) { bv = e[k]; best = k; } cuts.push(best * F); segStart = best; }}cuts.push(x.length);return cuts;}async function runFile(file) {if (state.mode === 'mic') await stopMic();resetAll(); state.mode = 'file'; $('#rec').classList.add('busy');3 unmodified linesconst off = new OfflineAudioContext(1, Math.ceil(buf.duration * SR), SR);const s = off.createBufferSource(); s.buffer = buf; s.connect(off.destination); s.start();const x = (await off.startRendering()).getChannelData(0);state.audio = x; state.n = x.length; state.fileSec = x.length / SR; state.t0 = performance.now();const cuts = vadCuts(x); state.filePending = cuts.length - 1;for (let k = 0; k + 1 < cuts.length; k++) {const seg = { id: state.seq++, start: cuts[k], end: cuts[k + 1], toks: [], frames: [], corr: null, final: true, gen: 1, shown: [], file: true };state.segs.push(seg);const lo = Math.max(0, cuts[k] - 1600), hi = Math.min(x.length, cuts[k + 1] + 1600);state.corrQueue.push({ type: 'correct', id: seg.id, gen: 1, samples: x.slice(lo, hi), upto: hi / SR, final: true, useLM: state.useLM, alpha: 0.2, beta: 1.5, beam: 4 });}pumpCorr();}// ---------- UI wiring ----------$('#rec').onclick = () => { if (!document.body.classList.contains('ready')) return; state.mode === 'mic' ? stopMic() : startMic().catch(e => { state.mode = null; setStatus('⚠ ' + e.message, 'err'); }); };$('#file').onchange = e => { const f = e.target.files[0]; if (f) runFile(f).catch(err => setStatus('⚠ ' + err.message, 'err')); e.target.value = ''; };
app.js+30-8