80 lines
4.4 KiB
JavaScript
80 lines
4.4 KiB
JavaScript
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);
|
|
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};
|
|
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<path.observedMs))
|
|
finding='Part of the phrase could not be verified';
|
|
el('path-detail').textContent='Reference matched '+time(s.coverageMs)+' of '+time(path.observedMs)+
|
|
(s.total?' · '+s.total+' recorded observations':'');
|
|
el('path-state').textContent=(data.complete?(finding?'Stage held':'Stage complete'):
|
|
(finding?'Still measuring':path.locked?'Tracking speech':'Acquiring speech'))+' · '+clock+' / '+run.stage.minutes+':00'+
|
|
(finding?' · '+finding:'');
|
|
if (data.complete) finish(finding?'stage finding':'phone test complete');
|
|
};
|
|
run.source.connect(node); node.connect(run.context.destination);
|
|
}
|