81 lines
3.8 KiB
JavaScript
81 lines
3.8 KiB
JavaScript
import {AudioReports} from './mic-reporting.js';
|
|
import {referenceLoop, STAGE_MINUTES, SOURCE_LEAD_SECONDS} from './acoustic-pattern.js';
|
|
|
|
const play = document.getElementById('play');
|
|
const stopButton = document.getElementById('stop');
|
|
const status = document.getElementById('state');
|
|
const duration = document.getElementById('duration');
|
|
for (const minutes of STAGE_MINUTES) duration.add(new Option(minutes + ' min', String(minutes)));
|
|
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; duration.disabled = false;
|
|
status.textContent = message;
|
|
}
|
|
play.onclick = async () => {
|
|
if (active) return;
|
|
try {
|
|
const minutes = Number(duration.value);
|
|
if (!STAGE_MINUTES.includes(minutes)) return;
|
|
const sourceSeconds = 60 * minutes + SOURCE_LEAD_SECONDS;
|
|
duration.disabled = true;
|
|
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 + sourceSeconds);
|
|
const show = () => {
|
|
owner.reports.observe(performance.now(), clockSample(owner), {sampleRate:context.sampleRate});
|
|
const remaining = Math.max(0, Math.ceil(sourceSeconds - (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'));
|