fix: voice v3 — smaller send chunks, 200ms jbuf, smooth fade on underrun, 1.5s timeout

This commit is contained in:
Mk 2026-05-26 14:08:55 +00:00
parent 2ee82a45b0
commit 714e4cf162
2 changed files with 22 additions and 10 deletions

30
game.js
View File

@ -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;

View File

@ -93,6 +93,6 @@
</div>
</div>
<script src="game.js?v=22"></script>
<script src="game.js?v=23"></script>
</body>
</html>