fix: voice chat — fade in/out chunks, bigger buffer, volume boost to reduce bubbling

This commit is contained in:
Mk 2026-05-26 13:13:13 +00:00
parent a59c84535a
commit 7eef966f6e
2 changed files with 34 additions and 15 deletions

47
game.js
View File

@ -1355,8 +1355,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(2048, 1, 1); voiceProcessor = audioCtx.createScriptProcessor(4096, 1, 1);
console.log('[voice] ScriptProcessor created, bufferSize=2048'); console.log('[voice] ScriptProcessor created, bufferSize=4096');
voiceProcessor.onaudioprocess = (e) => { voiceProcessor.onaudioprocess = (e) => {
if (!voiceActive) return; if (!voiceActive) return;
@ -1394,25 +1394,44 @@ function customConfirm(msg, onYes) {
console.error('[voice] Socket connect error:', err.message); console.error('[voice] Socket connect error:', err.message);
}); });
voiceSocket.on('voice_in', (payload) => { // Очередь воспроизведения голоса — склеиваем чанки без щелчков
// Воспроизводим входящий голос — raw PCM int16 const voiceQueue = [];
const { data, meta, volume } = payload; let voicePlaying = false;
if (!audioCtx || audioCtx.state === 'closed') return; function playVoiceChunk(float32, volume) {
console.log('[voice] voice_in from', meta.name, 'volume:', volume, 'bytes:', data.byteLength); const FADE = 64; // сэмплов для плавного перехода
// Fade in начало
const int16 = new Int16Array(data); for (let i = 0; i < FADE && i < float32.length; i++) {
const float32 = new Float32Array(int16.length); float32[i] *= i / FADE;
for (let i = 0; i < int16.length; i++) { }
float32[i] = int16[i] / (int16[i] < 0 ? 0x8000 : 0x7FFF) * (volume || 1); // Fade out конец
for (let i = 0; i < FADE && i < float32.length; i++) {
float32[float32.length - 1 - i] *= i / FADE;
} }
const buf = audioCtx.createBuffer(1, float32.length, 24000); const buf = audioCtx.createBuffer(1, float32.length, 24000);
buf.getChannelData(0).set(float32); buf.getChannelData(0).set(float32);
const src = audioCtx.createBufferSource(); const src = audioCtx.createBufferSource();
src.buffer = buf; src.buffer = buf;
const gain = audioCtx.createGain(); const gain = audioCtx.createGain();
gain.gain.value = 1; gain.gain.value = Math.min(1, volume * 1.5); // усилить тихий голос
src.connect(gain).connect(audioCtx.destination); src.connect(gain).connect(audioCtx.destination);
src.start(); // Склеиваем: начинаем сразу после предыдущего чанка
const when = voicePlaying ? audioCtx.currentTime + 0.02 : audioCtx.currentTime;
voicePlaying = true;
src.start(when);
src.onended = () => { voicePlaying = false; };
}
voiceSocket.on('voice_in', (payload) => {
// Воспроизводим входящий голос — raw PCM int16
const { data, meta, volume } = payload;
if (!audioCtx || audioCtx.state === 'closed') return;
const int16 = new Int16Array(data);
const float32 = new Float32Array(int16.length);
for (let i = 0; i < int16.length; i++) {
float32[i] = int16[i] / (int16[i] < 0 ? 0x8000 : 0x7FFF);
}
playVoiceChunk(float32, volume || 1);
// Индикатор // Индикатор
speakingIndicator.style.display = 'block'; speakingIndicator.style.display = 'block';

View File

@ -92,6 +92,6 @@
</div> </div>
</div> </div>
<script src="game.js?v=16"></script> <script src="game.js?v=17"></script>
</body> </body>
</html> </html>