import {TRIAL_SECONDS} from './acoustic-pattern.js'; export const phoneTest = new URLSearchParams(location.search).get('test') === 'phone'; const status = document.getElementById('path-state'); 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; document.getElementById('start').textContent = 'Start receiver'; document.getElementById('start').title = 'Wait for the phone signal, then measure 30 minutes.'; 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'); run.detector = node; run.pathInterruptions = 0; status.textContent = 'Waiting for phone signal'; 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); run.path = {...data, discontinuities:interruptions}; if (data.locked) { const gaps = data.missingWindows > 0 || data.sequenceFaults > 0; const label = interruptions ? 'Observation interrupted' : gaps ? 'Signal breaks detected' : 'Signal tracking'; status.textContent = label + ' · ' + clock(data.observedMs) + ' / 30:00'; if (data.complete) { status.textContent = interruptions ? '30 minutes · incomplete observation' : gaps ? '30 minutes · signal breaks detected' : '30 minutes · continuous reference received'; finish('phone test complete'); } } }; run.source.connect(node); node.connect(run.context.destination); }