60 lines
3.7 KiB
JavaScript
60 lines
3.7 KiB
JavaScript
// HEL-910 offline feasibility probe (workstation-local, zero network beyond loopback):
|
|
// does Chromium --use-file-for-fake-audio-capture feed our schedule into getUserMedia,
|
|
// and does the AudioContext analyser probe see it at 50ms resolution?
|
|
// Usage: node probe_fake_capture.mjs <chrome> <wav> <outdir>
|
|
import { spawn } from 'node:child_process';
|
|
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
|
|
const [chrome, wav, out] = process.argv.slice(2);
|
|
mkdirSync(out, { recursive: true });
|
|
const cdpPort = 9711, httpPort = 9712;
|
|
const page = `<!doctype html><meta charset=utf-8><body><script>
|
|
window.__log = [];
|
|
(async () => {
|
|
try {
|
|
const s = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } });
|
|
const ctx = new AudioContext(); await ctx.resume();
|
|
const an = ctx.createAnalyser(); an.fftSize = 512;
|
|
ctx.createMediaStreamSource(s).connect(an);
|
|
const buf = new Uint8Array(an.fftSize);
|
|
const t0 = performance.now();
|
|
window.__timer = setInterval(() => {
|
|
an.getByteTimeDomainData(buf);
|
|
let acc = 0; for (const v of buf) { const d = (v - 128) / 128; acc += d * d; }
|
|
window.__log.push([Math.round(performance.now() - t0), Math.round(Math.sqrt(acc / buf.length) * 100000) / 100000]);
|
|
}, 50);
|
|
window.__ok = true;
|
|
} catch (e) { window.__ok = false; window.__err = String(e); }
|
|
})();
|
|
</script></body>`;
|
|
writeFileSync(join(out, 'probe.html'), page);
|
|
const server = spawn('python3', ['-m', 'http.server', String(httpPort), '--bind', '127.0.0.1', '--directory', out], { stdio: 'ignore' });
|
|
const profile = join(tmpdir(), `hel910-probe-${Date.now()}`);
|
|
const browser = spawn(chrome, [
|
|
'--headless=new', `--remote-debugging-port=${cdpPort}`, `--user-data-dir=${profile}`, '--no-first-run',
|
|
'--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream',
|
|
`--use-file-for-fake-audio-capture=${wav}`,
|
|
'--autoplay-policy=no-user-gesture-required', 'about:blank',
|
|
], { stdio: 'ignore', env: { ...process.env, LD_LIBRARY_PATH: `${process.env.HOME}/chromium-libs/root/usr/lib/x86_64-linux-gnu:${process.env.HOME}/chromium-libs/root/usr/lib` } });
|
|
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
let targets;
|
|
for (let i = 0; i < 60 && !targets; i++) { try { targets = await (await fetch(`http://127.0.0.1:${cdpPort}/json`)).json(); } catch { await sleep(250); } }
|
|
const ws = new WebSocket(targets.find(t => t.type === 'page').webSocketDebuggerUrl);
|
|
await new Promise(r => ws.addEventListener('open', r, { once: true }));
|
|
let id = 0; const waiting = new Map();
|
|
ws.addEventListener('message', ({ data }) => { const m = JSON.parse(data); if (m.id && waiting.has(m.id)) { const { ok } = waiting.get(m.id); waiting.delete(m.id); ok(m.result); } });
|
|
const cdp = (method, params = {}) => new Promise(ok => { const n = ++id; waiting.set(n, { ok }); ws.send(JSON.stringify({ id: n, method, params })); });
|
|
await cdp('Page.navigate', { url: `http://127.0.0.1:${httpPort}/probe.html` });
|
|
await sleep(12000);
|
|
const res = await cdp('Runtime.evaluate', { expression: 'JSON.stringify({ok: window.__ok, err: window.__err, samples: window.__log.length, log: window.__log})', returnByValue: true });
|
|
writeFileSync(join(out, 'probe-result.json'), res.result.value);
|
|
const parsed = JSON.parse(res.result.value);
|
|
const speaking = parsed.log.filter(([, e]) => e > 0.02);
|
|
console.log('gum ok:', parsed.ok, parsed.err || '', '| samples:', parsed.samples);
|
|
console.log('speaking windows (>0.02 rms):', speaking.length);
|
|
console.log('first/last speaking ms:', speaking[0]?.[0], speaking[speaking.length - 1]?.[0]);
|
|
console.log('max rms:', Math.max(...parsed.log.map(([, e]) => e)));
|
|
browser.kill(); server.kill();
|