|
|
|
|
@ -1,70 +1,94 @@
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html><head><title>Voice Test</title></head><body>
|
|
|
|
|
<h1>Voice Capture Test</h1>
|
|
|
|
|
<button id="btn" style="padding:20px;font-size:24px;background:#2ecc71;color:#fff;border:none;border-radius:12px;cursor:pointer;">Start Mic</button>
|
|
|
|
|
<div id="log" style="font-family:monospace;white-space:pre;margin-top:20px;"></div>
|
|
|
|
|
<html>
|
|
|
|
|
<head><title>Voice Test</title></head>
|
|
|
|
|
<body style="background:#1a1a2e;color:#eee;font-family:monospace;padding:20px">
|
|
|
|
|
<h2>Voice Chat Debug</h2>
|
|
|
|
|
<div id="log" style="white-space:pre;overflow:auto;max-height:80vh;border:1px solid #444;padding:10px;font-size:12px"></div>
|
|
|
|
|
<script src="https://cdn.socket.io/4.7.4/socket.io.min.js"></script>
|
|
|
|
|
<script>
|
|
|
|
|
const log = document.getElementById('log');
|
|
|
|
|
function addLog(msg) { log.textContent += msg + '\n'; console.log(msg); }
|
|
|
|
|
const VOICE_SERVER = 'https://voicegrech.mkn8n.ru';
|
|
|
|
|
const logEl = document.getElementById('log');
|
|
|
|
|
function log(msg) { logEl.textContent += new Date().toISOString().substr(11,8) + ' ' + msg + '\n'; logEl.scrollTop = logEl.scrollHeight; }
|
|
|
|
|
|
|
|
|
|
let voiceStream, audioCtx, voiceProcessor, voiceSocket;
|
|
|
|
|
let debugCount = 0;
|
|
|
|
|
log('Starting voice test...');
|
|
|
|
|
|
|
|
|
|
document.getElementById('btn').onclick = async () => {
|
|
|
|
|
(async () => {
|
|
|
|
|
try {
|
|
|
|
|
addLog('1. Requesting mic...');
|
|
|
|
|
voiceStream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true } });
|
|
|
|
|
addLog('2. Got stream: ' + voiceStream.getTracks().map(t => t.label + ' ' + t.readyState).join(', '));
|
|
|
|
|
log('Requesting microphone...');
|
|
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({audio: {echoCancellation:true,noiseSuppression:true,autoGainControl:true}});
|
|
|
|
|
log('Got mic stream: ' + stream.getTracks().map(t => t.label + ' ' + t.readyState).join(', '));
|
|
|
|
|
|
|
|
|
|
audioCtx = new AudioContext({ sampleRate: 24000 });
|
|
|
|
|
if (audioCtx.state === 'suspended') await audioCtx.resume();
|
|
|
|
|
addLog('3. AudioContext: state=' + audioCtx.state + ' sampleRate=' + audioCtx.sampleRate);
|
|
|
|
|
const audioCtx = new AudioContext({sampleRate: 16000});
|
|
|
|
|
log('AudioContext: state=' + audioCtx.state + ' sampleRate=' + audioCtx.sampleRate);
|
|
|
|
|
if (audioCtx.state === 'suspended') { await audioCtx.resume(); log('AudioContext resumed: ' + audioCtx.state); }
|
|
|
|
|
|
|
|
|
|
const source = audioCtx.createMediaStreamSource(voiceStream);
|
|
|
|
|
voiceProcessor = audioCtx.createScriptProcessor(2048, 1, 1);
|
|
|
|
|
addLog('4. ScriptProcessor created');
|
|
|
|
|
const source = audioCtx.createMediaStreamSource(stream);
|
|
|
|
|
const analyser = audioCtx.createAnalyser();
|
|
|
|
|
analyser.fftSize = 2048;
|
|
|
|
|
source.connect(analyser);
|
|
|
|
|
|
|
|
|
|
voiceProcessor.onaudioprocess = (e) => {
|
|
|
|
|
debugCount++;
|
|
|
|
|
const pcm = e.inputBuffer.getChannelData(0);
|
|
|
|
|
const maxVal = Math.max(...Array.from(pcm).map(Math.abs));
|
|
|
|
|
if (debugCount <= 10) addLog('5. onaudioprocess #' + debugCount + ' samples=' + pcm.length + ' max=' + maxVal.toFixed(4));
|
|
|
|
|
// Check mic levels
|
|
|
|
|
const dataArr = new Float32Array(analyser.fftSize);
|
|
|
|
|
let checkCount = 0;
|
|
|
|
|
const checkInterval = setInterval(() => {
|
|
|
|
|
analyser.getFloatTimeDomainData(dataArr);
|
|
|
|
|
let sum = 0;
|
|
|
|
|
for (let i = 0; i < dataArr.length; i++) sum += dataArr[i] * dataArr[i];
|
|
|
|
|
const rms = Math.sqrt(sum / dataArr.length);
|
|
|
|
|
checkCount++;
|
|
|
|
|
if (checkCount <= 20 || checkCount % 50 === 0) log('Mic RMS: ' + rms.toFixed(6) + (rms > 0.008 ? ' SPEAKING' : ''));
|
|
|
|
|
}, 200);
|
|
|
|
|
|
|
|
|
|
if (!voiceSocket || !voiceSocket.connected) return;
|
|
|
|
|
const int16 = new Int16Array(pcm.length);
|
|
|
|
|
for (let i = 0; i < pcm.length; i++) {
|
|
|
|
|
const s = Math.max(-1, Math.min(1, pcm[i]));
|
|
|
|
|
log('Connecting to voice server...');
|
|
|
|
|
const socket = io(VOICE_SERVER, {transports: ['websocket']});
|
|
|
|
|
|
|
|
|
|
socket.on('connect', () => {
|
|
|
|
|
log('SOCKET CONNECTED: ' + socket.id);
|
|
|
|
|
socket.emit('voice_join', {world_id: 'test', x: 0, y: 0, name: 'Tester', mode: 'world', codec: 'pcm'});
|
|
|
|
|
log('voice_join sent');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('connect_error', (e) => {
|
|
|
|
|
log('SOCKET ERROR: ' + e.message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('voice_in', (payload) => {
|
|
|
|
|
log('RX voice_in from ' + (payload.meta?.from||'?').substring(0,8) + ' codec:' + payload.codec + ' size:' + (payload.data?.byteLength || payload.data?.length || '?'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Capture and send
|
|
|
|
|
const processor = audioCtx.createScriptProcessor(320, 1, 1);
|
|
|
|
|
source.connect(processor);
|
|
|
|
|
processor.connect(audioCtx.destination);
|
|
|
|
|
|
|
|
|
|
let seq = 0;
|
|
|
|
|
let sendCount = 0;
|
|
|
|
|
processor.onaudioprocess = (e) => {
|
|
|
|
|
if (!socket.connected) return;
|
|
|
|
|
const float32 = e.inputBuffer.getChannelData(0);
|
|
|
|
|
let rms = 0;
|
|
|
|
|
for (let i = 0; i < float32.length; i++) rms += float32[i] * float32[i];
|
|
|
|
|
rms = Math.sqrt(rms / float32.length);
|
|
|
|
|
|
|
|
|
|
if (rms < 0.005) return; // Skip silence
|
|
|
|
|
|
|
|
|
|
const int16 = new Int16Array(float32.length);
|
|
|
|
|
for (let i = 0; i < float32.length; i++) {
|
|
|
|
|
const s = Math.max(-1, Math.min(1, float32[i]));
|
|
|
|
|
int16[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
|
|
|
|
|
}
|
|
|
|
|
voiceSocket.emit('voice_data', int16.buffer);
|
|
|
|
|
|
|
|
|
|
socket.emit('voice_data', {codec: 'pcm', data: int16.buffer, seq: seq++, speaking: true});
|
|
|
|
|
sendCount++;
|
|
|
|
|
if (sendCount <= 5 || sendCount % 50 === 0) log('TX frame #' + sendCount + ' rms:' + rms.toFixed(4) + ' size:' + int16.buffer.byteLength);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const silentGain = audioCtx.createGain();
|
|
|
|
|
silentGain.gain.value = 0;
|
|
|
|
|
source.connect(voiceProcessor);
|
|
|
|
|
voiceProcessor.connect(silentGain);
|
|
|
|
|
silentGain.connect(audioCtx.destination);
|
|
|
|
|
addLog('6. Audio chain connected: source→processor→gain(0)→destination');
|
|
|
|
|
log('Voice capture active. Speak into mic!');
|
|
|
|
|
|
|
|
|
|
addLog('7. Connecting to voice server...');
|
|
|
|
|
voiceSocket = io('https://voicegrech.mkn8n.ru', { transports: ['websocket'] });
|
|
|
|
|
voiceSocket.on('connect', () => {
|
|
|
|
|
addLog('8. Socket connected: ' + voiceSocket.id);
|
|
|
|
|
voiceSocket.emit('voice_join', { world_id: 'test', x: 0, y: 0, name: 'Tester' });
|
|
|
|
|
addLog('9. Sent voice_join. Speak into mic — watch onaudioprocess logs above!');
|
|
|
|
|
});
|
|
|
|
|
voiceSocket.on('connect_error', (err) => addLog('ERROR: ' + err.message));
|
|
|
|
|
|
|
|
|
|
voiceSocket.on('voice_in', (payload) => {
|
|
|
|
|
addLog('VOICE_IN from ' + payload.meta.name + ' vol=' + payload.volume + ' bytes=' + payload.data.byteLength);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
document.getElementById('btn').textContent = 'Listening...';
|
|
|
|
|
document.getElementById('btn').style.background = '#e74c3c';
|
|
|
|
|
} catch(e) {
|
|
|
|
|
addLog('ERROR: ' + e.message + '\n' + e.stack);
|
|
|
|
|
log('ERROR: ' + e.message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
})();
|
|
|
|
|
</script>
|
|
|
|
|
</body></html>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
|