fix: voice v3 — smaller send chunks, 200ms jbuf, smooth fade on underrun, 1.5s timeout
This commit is contained in:
parent
2ee82a45b0
commit
714e4cf162
30
game.js
30
game.js
|
|
@ -1364,8 +1364,8 @@ function customConfirm(msg, onYes) {
|
||||||
console.log('[voice] AudioContext state:', audioCtx.state, 'sampleRate:', audioCtx.sampleRate);
|
console.log('[voice] AudioContext state:', audioCtx.state, 'sampleRate:', audioCtx.sampleRate);
|
||||||
|
|
||||||
const source = audioCtx.createMediaStreamSource(voiceStream);
|
const source = audioCtx.createMediaStreamSource(voiceStream);
|
||||||
voiceProcessor = audioCtx.createScriptProcessor(4096, 1, 1);
|
voiceProcessor = audioCtx.createScriptProcessor(2048, 1, 1);
|
||||||
console.log('[voice] ScriptProcessor created, bufferSize=4096');
|
console.log('[voice] ScriptProcessor created, bufferSize=2048');
|
||||||
|
|
||||||
voiceProcessor.onaudioprocess = (e) => {
|
voiceProcessor.onaudioprocess = (e) => {
|
||||||
if (!voiceActive) return;
|
if (!voiceActive) return;
|
||||||
|
|
@ -1411,17 +1411,22 @@ function customConfirm(msg, onYes) {
|
||||||
let ringRead = 0; // позиция чтения
|
let ringRead = 0; // позиция чтения
|
||||||
let ringReady = 0; // сколько сэмплов готово
|
let ringReady = 0; // сколько сэмплов готово
|
||||||
let voicePlayActive = false;
|
let voicePlayActive = false;
|
||||||
const JBUF_TARGET = 2400; // целевой jitter buffer: 100мс при 24kHz
|
const JBUF_TARGET = 4800; // целевой jitter buffer: 200мс при 24kHz
|
||||||
let jbufFill = 0; // текущее заполнение
|
let jbufFill = 0; // текущее заполнение
|
||||||
let lastVoiceFrom = ''; // кто говорит (для индикатора)
|
let lastVoiceFrom = ''; // кто говорит (для индикатора)
|
||||||
|
|
||||||
// Воспроизводящий ScriptProcessor — читает из ring buffer
|
// Воспроизводящий ScriptProcessor — читает из ring buffer
|
||||||
const playProcessor = audioCtx.createScriptProcessor(2048, 1, 1);
|
const playProcessor = audioCtx.createScriptProcessor(2048, 1, 1);
|
||||||
|
let lastSample = 0; // для плавного fade при underrun
|
||||||
playProcessor.onaudioprocess = (e) => {
|
playProcessor.onaudioprocess = (e) => {
|
||||||
const out = e.outputBuffer.getChannelData(0);
|
const out = e.outputBuffer.getChannelData(0);
|
||||||
if (ringReady < 1) {
|
if (ringReady < 1) {
|
||||||
// Тишина — нет голоса
|
// Плавный fade-out от последнего сэмпла к тишине
|
||||||
out.fill(0);
|
for (let i = 0; i < out.length; i++) {
|
||||||
|
lastSample *= 0.9;
|
||||||
|
out[i] = lastSample;
|
||||||
|
}
|
||||||
|
voicePlayActive = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Ждём накопления jitter buffer перед стартом
|
// Ждём накопления jitter buffer перед стартом
|
||||||
|
|
@ -1429,17 +1434,24 @@ function customConfirm(msg, onYes) {
|
||||||
voicePlayActive = true;
|
voicePlayActive = true;
|
||||||
}
|
}
|
||||||
if (!voicePlayActive) {
|
if (!voicePlayActive) {
|
||||||
out.fill(0);
|
// Плавно затихаем пока буфер копится
|
||||||
|
for (let i = 0; i < out.length; i++) {
|
||||||
|
lastSample *= 0.95;
|
||||||
|
out[i] = lastSample;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Читаем из ring buffer
|
// Читаем из ring buffer с плавным fade-in на старте
|
||||||
for (let i = 0; i < out.length; i++) {
|
for (let i = 0; i < out.length; i++) {
|
||||||
if (ringReady > 0) {
|
if (ringReady > 0) {
|
||||||
out[i] = ringBuf[ringRead];
|
out[i] = ringBuf[ringRead];
|
||||||
|
lastSample = out[i]; // запоминаем для fade-out
|
||||||
ringRead = (ringRead + 1) % RING_SIZE;
|
ringRead = (ringRead + 1) % RING_SIZE;
|
||||||
ringReady--;
|
ringReady--;
|
||||||
} else {
|
} else {
|
||||||
out[i] = 0;
|
// Underrun — плавно затихаем
|
||||||
|
lastSample *= 0.85;
|
||||||
|
out[i] = lastSample;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -1473,7 +1485,7 @@ function customConfirm(msg, onYes) {
|
||||||
voicePlayActive = false; // сброс при паузе
|
voicePlayActive = false; // сброс при паузе
|
||||||
ringReady = 0; // очистить буфер
|
ringReady = 0; // очистить буфер
|
||||||
ringRead = ringWrite; // синхронизировать
|
ringRead = ringWrite; // синхронизировать
|
||||||
}, 600);
|
}, 1500);
|
||||||
});
|
});
|
||||||
|
|
||||||
voiceActive = true;
|
voiceActive = true;
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="game.js?v=22"></script>
|
<script src="game.js?v=23"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue