jenkins af45190bfb bstein: serve the phone sound source and 30-minute RCT microphone detector
Vendor the web assets from Lesavka a5758755. The RCT browser performs signal recognition and sends numeric observations through the existing receipt service.
2026-12-03 12:11:53 -06:00

75 lines
3.4 KiB
JavaScript

import {AudioReports} from './mic-reporting.js';
import {referenceLoop, SOURCE_SECONDS} from './acoustic-pattern.js';
const play = document.getElementById('play');
const stopButton = document.getElementById('stop');
const status = document.getElementById('state');
let active = null;
function clockSample(owner) {
const frames = Math.floor(Math.max(0,owner.context.currentTime - owner.started) * owner.context.sampleRate);
return {processedFrames:frames, sampleRate:owner.context.sampleRate};
}
function stop(message) {
const owner = active;
active = null;
if (owner) {
clearInterval(owner.timer);
const reason = message === 'Playback complete' ? 'reference complete' :
message === 'Playback stopped' ? 'operator' : 'source interrupted';
owner.reports?.finish(performance.now(), clockSample(owner), {sampleRate:owner.context.sampleRate}, reason);
owner.context.onstatechange = null;
if (owner.source) {
owner.source.onended = null;
try { owner.source.stop(); } catch {}
}
void owner.context.close().catch(() => {});
void owner.wake?.release().catch(() => {});
}
play.disabled = false; stopButton.disabled = true;
status.textContent = message;
}
play.onclick = async () => {
if (active) return;
try {
const context = new AudioContext();
const owner = {context};
active = owner; play.disabled = true; stopButton.disabled = false;
status.textContent = 'Starting speaker';
await context.resume();
if (active !== owner) return;
const data = referenceLoop(context.sampleRate);
const buffer = context.createBuffer(1, data.length, context.sampleRate);
buffer.copyToChannel(data, 0);
const source = context.createBufferSource();
owner.source = source; source.buffer = buffer; source.loop = true;
source.connect(context.destination);
const start = context.currentTime;
owner.started = start;
owner.reports = new AudioReports(text => {
if (active === owner) document.getElementById('report').textContent = text;
});
source.onended = () => { if (active === owner) stop('Playback complete'); };
context.onstatechange = () => {
if (active === owner && context.state !== 'running') stop('Speaker interrupted');
};
source.start(start); source.stop(start + SOURCE_SECONDS);
const show = () => {
owner.reports.observe(performance.now(), clockSample(owner), {sampleRate:context.sampleRate});
const remaining = Math.max(0, Math.ceil(SOURCE_SECONDS - (context.currentTime - start)));
status.textContent = 'Playing · ' + Math.floor(remaining / 60) + ':' +
String(remaining % 60).padStart(2, '0') + ' remaining';
};
show(); owner.timer = setInterval(show, 1000);
try {
const wake = await navigator.wakeLock?.request('screen');
if (active === owner) owner.wake = wake;
else await wake?.release();
} catch {}
} catch { stop('Speaker unavailable'); }
};
stopButton.onclick = () => stop('Playback stopped');
document.addEventListener('visibilitychange', () => {
if (document.hidden && active) stop('Playback interrupted · phone page hidden');
});
window.addEventListener('pagehide', () => stop('Playback stopped'));