119 lines
6.5 KiB
JavaScript
119 lines
6.5 KiB
JavaScript
import {attachSpeech} from './speech-watch.js';
|
|
import {STAGE_MINUTES, TRIAL_SECONDS} from './acoustic-pattern.js';
|
|
|
|
export const phoneTest = new URLSearchParams(location.search).get('test') === 'phone';
|
|
export const speechMode = new URLSearchParams(location.search).get('signal') !== 'tones';
|
|
const status = document.getElementById('path-state');
|
|
const detail = document.getElementById('path-detail');
|
|
const duration = document.getElementById('stage-duration');
|
|
const timing = document.getElementById('path-timing');
|
|
let stageIndex = 0, unlockedIndex = 0, device = null, attempt = 0;
|
|
function renderStage() {
|
|
for (const [index, option] of Array.from(duration.options).entries()) {
|
|
option.disabled = index > unlockedIndex;
|
|
}
|
|
duration.selectedIndex = stageIndex;
|
|
document.getElementById('start').textContent = 'Start ' + STAGE_MINUTES[stageIndex] + '-minute receiver';
|
|
}
|
|
export function selectStageInput(input) {
|
|
if (device === input) return;
|
|
device = input; stageIndex = 0; unlockedIndex = 0; attempt++;
|
|
renderStage();
|
|
}
|
|
export function beginStage(input) {
|
|
selectStageInput(input);
|
|
unlockedIndex = stageIndex;
|
|
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; }
|
|
unlockedIndex = Math.min(STAGE_MINUTES.length - 1, STAGE_MINUTES.indexOf(run.stage.minutes) + 1);
|
|
renderStage();
|
|
}
|
|
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;
|
|
for (const minutes of STAGE_MINUTES) duration.add(new Option(minutes+' min',String(minutes)));
|
|
document.getElementById('stage-label').hidden = false;
|
|
renderStage();
|
|
document.getElementById('local-review').hidden = !speechMode;
|
|
if (speechMode) status.title='Compare received speech to the phone phrase';
|
|
duration.onchange = () => {
|
|
if (!duration.disabled && duration.selectedIndex>=0 && duration.selectedIndex<=unlockedIndex) {
|
|
stageIndex = duration.selectedIndex;
|
|
}
|
|
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) {
|
|
if (speechMode) return attachSpeech(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 timing coverage · pitch ' + Math.round(100*data.timing.rateCoverageMs/data.observedMs) +
|
|
'% · markers ' + Math.round(10000*data.timing.edgeCount/data.observedMs) + '%';
|
|
if (hold) {
|
|
status.textContent = (data.complete ? 'Stage held · ' : 'Still measuring · ') + hold + ' · ' + label;
|
|
if (data.complete) 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);
|
|
}
|