diff --git a/game.js b/game.js index 42618bb..f304d36 100644 --- a/game.js +++ b/game.js @@ -1364,8 +1364,8 @@ function customConfirm(msg, onYes) { console.log('[voice] AudioContext state:', audioCtx.state, 'sampleRate:', audioCtx.sampleRate); const source = audioCtx.createMediaStreamSource(voiceStream); - voiceProcessor = audioCtx.createScriptProcessor(4096, 1, 1); - console.log('[voice] ScriptProcessor created, bufferSize=4096'); + voiceProcessor = audioCtx.createScriptProcessor(2048, 1, 1); + console.log('[voice] ScriptProcessor created, bufferSize=2048'); voiceProcessor.onaudioprocess = (e) => { if (!voiceActive) return; @@ -1411,17 +1411,22 @@ function customConfirm(msg, onYes) { let ringRead = 0; // позиция чтения let ringReady = 0; // сколько сэмплов готово let voicePlayActive = false; - const JBUF_TARGET = 2400; // целевой jitter buffer: 100мс при 24kHz + const JBUF_TARGET = 4800; // целевой jitter buffer: 200мс при 24kHz let jbufFill = 0; // текущее заполнение let lastVoiceFrom = ''; // кто говорит (для индикатора) // Воспроизводящий ScriptProcessor — читает из ring buffer const playProcessor = audioCtx.createScriptProcessor(2048, 1, 1); + let lastSample = 0; // для плавного fade при underrun playProcessor.onaudioprocess = (e) => { const out = e.outputBuffer.getChannelData(0); if (ringReady < 1) { - // Тишина — нет голоса - out.fill(0); + // Плавный fade-out от последнего сэмпла к тишине + for (let i = 0; i < out.length; i++) { + lastSample *= 0.9; + out[i] = lastSample; + } + voicePlayActive = false; return; } // Ждём накопления jitter buffer перед стартом @@ -1429,17 +1434,24 @@ function customConfirm(msg, onYes) { voicePlayActive = true; } if (!voicePlayActive) { - out.fill(0); + // Плавно затихаем пока буфер копится + for (let i = 0; i < out.length; i++) { + lastSample *= 0.95; + out[i] = lastSample; + } return; } - // Читаем из ring buffer + // Читаем из ring buffer с плавным fade-in на старте for (let i = 0; i < out.length; i++) { if (ringReady > 0) { out[i] = ringBuf[ringRead]; + lastSample = out[i]; // запоминаем для fade-out ringRead = (ringRead + 1) % RING_SIZE; ringReady--; } else { - out[i] = 0; + // Underrun — плавно затихаем + lastSample *= 0.85; + out[i] = lastSample; } } }; @@ -1473,7 +1485,7 @@ function customConfirm(msg, onYes) { voicePlayActive = false; // сброс при паузе ringReady = 0; // очистить буфер ringRead = ringWrite; // синхронизировать - }, 600); + }, 1500); }); voiceActive = true; diff --git a/index.html b/index.html index a57c073..f249f4c 100644 --- a/index.html +++ b/index.html @@ -93,6 +93,6 @@ - +