Files
Kai-Sa-Cam/mic-bridge/index.html
T
2026-10-22 02:27:01 +02:00

60 lines
3.1 KiB
HTML

<!doctype html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Cam</title>
<body style="margin:0;font-family:sans-serif;background:#111;color:#eee">
<iframe src="/stream.html?src=webcam&mode=mse" style="width:100%;height:62vh;border:0"></iframe>
<div style="padding:6px 10px;display:flex;gap:10px;align-items:center">
<span id="s" style="flex:1">micro éteint</span>
<label>Gain <input id="g" type="range" min="0.2" max="5" step="0.1" value="1"></label>
</div>
<button id="b" style="width:100%;height:18vh;font-size:2rem">🎤 Micro OFF</button>
<script>
const s = document.getElementById('s'), b = document.getElementById('b'), g = document.getElementById('g');
let ws, ctx, node, stream, wake, on = false, buf = [], n = 0;
const worklet = URL.createObjectURL(new Blob([
"class P extends AudioWorkletProcessor{process(i){const c=i[0][0];if(c)this.port.postMessage(c.slice(0));return true}}registerProcessor('p',P)"
], { type: 'application/javascript' }));
function connect() {
if (!on) return;
ws = new WebSocket((location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host + '/mic/ws');
ws.onopen = () => { ws.send(JSON.stringify({ rate: ctx.sampleRate })); s.textContent = 'micro actif (' + ctx.sampleRate + ' Hz)'; };
ws.onclose = () => { if (on) { s.textContent = 'reconnexion...'; setTimeout(connect, 1000); } };
}
async function lock() { try { wake = await navigator.wakeLock.request('screen'); } catch (e) {} }
async function start() {
on = true; s.textContent = 'demande micro...';
try { stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true } }); }
catch (e) { on = false; s.textContent = 'micro refusé: ' + e.message; return; }
ctx = new AudioContext(); await ctx.resume();
await ctx.audioWorklet.addModule(worklet);
const src = ctx.createMediaStreamSource(stream);
node = new AudioWorkletNode(ctx, 'p');
node.port.onmessage = e => {
buf.push(e.data); n += e.data.length;
if (n < 2048) return;
const o = new Int16Array(n), k0 = +g.value; let k = 0;
for (const c of buf) for (let i = 0; i < c.length; i++) o[k++] = Math.max(-1, Math.min(1, c[i] * k0)) * 32767;
buf = []; n = 0;
if (ws && ws.readyState === 1) ws.send(o.buffer);
};
const mute = ctx.createGain(); mute.gain.value = 0;
src.connect(node); node.connect(mute); mute.connect(ctx.destination);
connect(); lock(); try { document.querySelector('iframe').contentDocument.querySelector('video').muted = true; } catch (e) {}
b.textContent = '🎤 Micro ON (toucher pour couper)';
}
function stop() {
on = false; s.textContent = 'micro éteint'; b.textContent = '🎤 Micro OFF';
try { node && node.disconnect(); } catch (e) {}
stream && stream.getTracks().forEach(t => t.stop());
ctx && ctx.close(); ws && ws.close(); wake && wake.release().catch(() => {});
node = stream = ctx = ws = wake = null; buf = []; n = 0;
}
b.onclick = () => on ? stop() : start();
document.addEventListener('visibilitychange', () => {
if (on && document.visibilityState === 'visible') { ctx && ctx.resume(); lock(); }
});
</script>