diff --git a/dist/index.html b/dist/index.html index fd35d3d..0c7d14c 100644 --- a/dist/index.html +++ b/dist/index.html @@ -256,12 +256,15 @@ async function transcribe(blob) { if (!GROQ_API_KEY) return { text: '', error: 'no_key' }; const tStart = performance.now(); + // Обрезаем 0.2с в начале (щелчок) + const sendBlob = await trimStartBlob(blob); + const form = new FormData(); - form.append('file', blob, 'audio.webm'); + form.append('file', sendBlob, sendBlob.type === 'audio/wav' ? 'audio.wav' : 'audio.webm'); form.append('model', 'whisper-large-v3'); form.append('language', 'it'); - console.log('[HTTP] blob ' + (blob.size/1024).toFixed(1) + 'KB → multipart POST'); + console.log('[HTTP] blob ' + (sendBlob.size/1024).toFixed(1) + 'KB → multipart POST'); try { const r = await fetch('https://lang.kube5s.ru/openai/v1/audio/transcriptions', { @@ -623,15 +626,30 @@ function encodeWAV(samples, sampleRate) { // Обрезает первые cutSec секунд, возвращает WAV Blob (для Groq) async function trimStartBlob(blob, cutSec = 0.2) { - const ctx = new OfflineAudioContext(1, 1, 44100); - const ab = await blob.arrayBuffer(); - const buf = await ctx.decodeAudioData(ab); - const sr = buf.sampleRate; - const data = buf.getChannelData(0); - const cutSamples = Math.floor(sr * cutSec); - const trimmed = data.slice(cutSamples); - const wav = encodeWAV(trimmed, sr); - return new Blob([wav], { type: 'audio/wav' }); + try { + const ab = await blob.arrayBuffer(); + const actx = new AudioContext(); + const rawBuf = await actx.decodeAudioData(ab); + const sr = rawBuf.sampleRate; + const cutSamples = Math.floor(sr * cutSec); + const newLen = rawBuf.length - cutSamples; + if (newLen < sr * 0.05) { actx.close(); return blob; } + + // Render trimmed via OfflineAudioContext (надёжнее ручного slice) + const offline = new OfflineAudioContext(1, newLen, sr); + const src = offline.createBufferSource(); + src.buffer = rawBuf; + src.connect(offline.destination); + src.start(0, cutSec / sr); + const rendered = await offline.startRendering(); + actx.close(); + + const wav = encodeWAV(rendered.getChannelData(0), sr); + return new Blob([wav], { type: 'audio/wav' }); + } catch(e) { + console.log('[trimStartBlob] fallback to raw:', e); + return blob; + } } @@ -761,7 +779,7 @@ async function doCompare(blob, originalText, duration) {