105 lines
5.7 KiB
JavaScript
105 lines
5.7 KiB
JavaScript
import {STAGE_MINUTES, TRIAL_SECONDS} from './acoustic-pattern.js';
|
|
|
|
export const phoneTest = new URLSearchParams(location.search).get('test') === 'phone';
|
|
const status = document.getElementById('path-state');
|
|
const detail = document.getElementById('path-detail');
|
|
const advance = document.getElementById('stage-next');
|
|
const timing = document.getElementById('path-timing');
|
|
let stageIndex = 0, device = null, attempt = 0;
|
|
function renderStage() {
|
|
document.getElementById('start').textContent = 'Start ' + STAGE_MINUTES[stageIndex] + '-minute receiver';
|
|
advance.hidden = true;
|
|
}
|
|
export function selectStageInput(input) {
|
|
if (device === input) return;
|
|
device = input; stageIndex = 0; attempt++;
|
|
renderStage();
|
|
}
|
|
export function beginStage(input) {
|
|
selectStageInput(input);
|
|
renderStage();
|
|
return {minutes:STAGE_MINUTES[stageIndex], attempt:++attempt};
|
|
}
|
|
export async function reviewStage(run, reason, delivered) {
|
|
if (reason !== 'phone test complete') return;
|
|
const ok = await delivered;
|
|
if (attempt !== run.stage.attempt) return;
|
|
if (!ok) { status.textContent = 'Stage held · Atlas receipt unavailable'; return; }
|
|
if (stageIndex + 1 < STAGE_MINUTES.length) {
|
|
advance.textContent = 'Advance to ' + STAGE_MINUTES[stageIndex + 1] + ' minutes';
|
|
advance.hidden = false;
|
|
}
|
|
}
|
|
const clock = ms => {
|
|
const s = Math.min(TRIAL_SECONDS, Math.floor(ms / 1000));
|
|
return Math.floor(s / 60) + ':' + String(s % 60).padStart(2, '0');
|
|
};
|
|
|
|
export function setupPhoneMode() {
|
|
if (!phoneTest) return;
|
|
document.title = 'RCT microphone path test';
|
|
document.querySelector('h1').textContent = document.title;
|
|
renderStage();
|
|
advance.onclick = () => { if (!advance.hidden) { stageIndex++; renderStage(); } };
|
|
document.getElementById('start').title = 'Wait for the phone signal, then measure the selected stage.';
|
|
for (const id of ['tone','tone-stop','output-status','output-report','path-link']) {
|
|
document.getElementById(id).hidden = true;
|
|
}
|
|
document.getElementById('output').closest('label').hidden = true;
|
|
status.hidden = false; status.textContent = 'Choose the Lesavka USB microphone';
|
|
}
|
|
export async function attachDetector(run, isCurrent, finish) {
|
|
await run.context.audioWorklet.addModule('acoustic-worklet.js');
|
|
if (!isCurrent()) return;
|
|
const node = new AudioWorkletNode(run.context, 'acoustic-probe', {processorOptions:{minutes:run.stage.minutes}});
|
|
run.detector = node;
|
|
run.pathInterruptions = 0;
|
|
status.textContent = 'Waiting for phone signal';
|
|
detail.hidden = true; timing.hidden = true;
|
|
const listenStart = performance.now();
|
|
node.onprocessorerror = () => { if (isCurrent()) finish('detector failed'); };
|
|
node.port.onmessage = ({data}) => {
|
|
if (!isCurrent()) return;
|
|
if (data.expired) { finish('receiver deadline or lost observer'); return; }
|
|
node.port.postMessage('ack');
|
|
const now = performance.now();
|
|
const wall = Date.now();
|
|
if (data.locked && run.pathStart === undefined) {
|
|
run.pathStart = now - data.observedMs;
|
|
run.pathWall = wall - data.observedMs;
|
|
}
|
|
if (data.locked && (Math.abs(now - run.pathStart - data.observedMs) > 2000 ||
|
|
Math.abs(wall - run.pathWall - data.observedMs) > 2000)) run.clockGap = true;
|
|
const interruptions = data.discontinuities + run.pathInterruptions + Number(!!run.clockGap);
|
|
const age = data.scoredEndMs === null ? 0 : Math.max(0, run.context.currentTime * 1000 - data.scoredEndMs);
|
|
run.path = {...data, discontinuities:interruptions, observedAtMono:now - age};
|
|
if (!data.locked && now-listenStart>=30000) { finish('phone signal not acquired'); return; }
|
|
if (data.locked) {
|
|
timing.hidden = data.timing.baselinePpm===null;
|
|
timing.textContent = 'Largest pitch change · ' + (data.timing.maxRatePpm/10000).toFixed(2) +
|
|
'% · Confirmed marker timing step · ' + data.timing.maxShiftMs + ' ms';
|
|
const target = run.stage.minutes + ':00';
|
|
const label = clock(data.observedMs) + ' / ' + target;
|
|
detail.hidden = data.missingWindows === 0 && data.sequenceFaults === 0;
|
|
detail.textContent = 'Longest reference mismatch · ' + Math.round(data.gapMaxMs) + ' ms';
|
|
let hold;
|
|
if (interruptions) hold = 'Observation interrupted';
|
|
else if (data.levelDrops) hold = 'Received audio level drop · ' + Math.round(data.longestLevelDropMs) + ' ms';
|
|
else if (data.timing.firstRateMs !== null) hold = 'Received pitch changed · ' + (data.timing.maxRatePpm/10000).toFixed(2) + '%';
|
|
else if (data.timing.firstShiftMs !== null) hold = 'Relative timing shift · ' + data.timing.maxShiftMs + ' ms';
|
|
else if (data.firstUnclearMs !== null) hold = 'Reference unclear · ' + Math.round(data.gapMaxMs) + ' ms';
|
|
else if (data.complete && (data.timing.rateCoverageMs < 0.8*data.observedMs ||
|
|
data.timing.edgeCount < 0.8*data.observedMs/100)) hold = 'Insufficient reference timing coverage';
|
|
if (hold) {
|
|
status.textContent = 'Stage held · ' + hold + ' · ' + label;
|
|
run.findingAt ??= data.observedMs;
|
|
if (interruptions || data.complete || data.observedMs-run.findingAt>=2000) finish('stage finding');
|
|
} else if (data.complete) {
|
|
status.textContent = 'Stage complete · ' + label + ' · review before advancing';
|
|
finish('phone test complete');
|
|
} else status.textContent = 'Signal tracking · ' + label;
|
|
}
|
|
};
|
|
run.source.connect(node); node.connect(run.context.destination);
|
|
}
|