import {speechBuffer, referenceFeatures} from './speech-envelope.js'; import {SpeechDetector} from './speech-detector.js'; import {LocalRecording} from './local-recording.js'; const el=id=>document.getElementById(id); const digital=new URLSearchParams(location.search).get('test')==='digital'; let replayUrl=null, replayGeneration=0; function resetReplay() { el('hear-finding').hidden=true; el('hear-finding').onclick=null; el('speech-replay').hidden=true; el('speech-replay').removeAttribute('src'); el('speech-replay').load(); if (replayUrl) URL.revokeObjectURL(replayUrl); replayUrl=null; } const time=ms=>(ms/1000).toFixed(2)+' s'; export async function attachSpeech(run, isCurrent, finish) { const serial=++replayGeneration; resetReplay(); el('record-status').textContent=''; await run.context.audioWorklet.addModule('speech-worklet.js'); const buffer=await speechBuffer(run.context); if (!isCurrent()) return; const detector=new SpeechDetector(referenceFeatures(buffer.getChannelData(0),buffer.sampleRate),run.stage.minutes); const node=new AudioWorkletNode(run.context,'speech-probe',{processorOptions:{minutes:run.stage.minutes}}); run.detector=node; run.pathInterruptions=0; el('path-state').textContent='Listening for the spoken phrase'; el('path-detail').hidden=false; el('path-timing').hidden=true; if (el('keep-speech').checked) { run.recording=new LocalRecording({audioOnly:true,maxMs:(run.stage.minutes+1)*60000,maxBytes:32*1024*1024, onStatus:text=>{if(serial===replayGeneration) el('record-status').textContent=text;}, onReady:blob=>{ if(serial!==replayGeneration) return; replayUrl=URL.createObjectURL(blob); const player=el('speech-replay'); player.src=replayUrl; player.hidden=false; if (Number.isFinite(run.firstFindingMs)) { el('hear-finding').hidden=false; el('hear-finding').onclick=()=>{ player.currentTime=Math.max(0,(run.pathStart-run.clipStart+run.firstFindingMs)/1000-1.5); void player.play().catch(()=>{}); }; } }}); run.clipStart=performance.now(); run.recording.start(run.stream); } node.onprocessorerror=()=>{if(isCurrent()) finish('speech detector failed');}; node.port.onmessage=({data})=>{ if (!isCurrent()) return; if (data.expired) {finish('lost speech observer');return;} node.port.postMessage('ack'); const path=detector.observe(data.batch,data.endMs,data.complete); const now=performance.now(); const observedAtMono=now-Math.max(0,1000*run.context.currentTime-data.endMs); run.pathStart??=observedAtMono-path.observedMs; if(Math.abs(observedAtMono-run.pathStart-path.observedMs)>2000) run.clockGap=true; path.discontinuities+=run.pathInterruptions+Number(!!run.clockGap); run.path={...path,observedAtMono}; // Receipt kind 2 marks a digitally injected source; 1 is the phone/acoustic source. if (digital) run.path.speech={...path.speech,kind:2}; const s=path.speech; const first=s.events[0]?.[2]; if (Number.isFinite(first)) run.firstFindingMs=Math.min(run.firstFindingMs??Infinity,first); const clock=Math.floor(path.observedMs/60000)+':'+String(Math.floor(path.observedMs/1000)%60).padStart(2,'0'); let finding=''; if (path.firstLevelDropMs!==null) finding='Speech attenuation at '+time(path.firstLevelDropMs)+' · longest '+path.longestLevelDropMs+' ms'; else if (path.timing.firstShiftMs!==null) finding='Phrase timing shifted near '+time(path.timing.firstShiftMs); else if (s.firstRateMs!==null) finding='Speech cadence changed near '+time(s.firstRateMs); else if (path.discontinuities) finding='Capture interrupted'; else if (path.firstUnclearMs!==null || (data.complete && s.coverageMs