import {SpeechAlignment, phaseDelta} from './speech-alignment.js'; // Event codes on the numeric receipt: 1 attenuated speech, 2 timing step, // 3 uncertain alignment/level, 4 sustained cadence change. Time units are ms. export class SpeechDetector { constructor(reference,minutes) { this.align=new SpeechAlignment(reference); this.minutes=minutes; this.frames=[]; this.end=this.scored=this.trusted=this.count=0; this.events=[]; this.maxDrop=this.maxUnknown=this.shift=this.maxRate=0; this.firstDrop=this.firstUnknown=this.firstShift=this.firstRate=null; this.discontinuities=0; this.known=false; this.passage=[]; } event(kind, start, duration, value=0, phase=null) { const position=phase===null?-1:(start+phase)%this.align.length; const row=[++this.count,kind,10*start,10*duration,value,position]; this.events.push(row); if (this.events.length>16) this.events.shift(); if (kind===1) { this.firstDrop??=row[2]; this.maxDrop=Math.max(this.maxDrop,row[3]); } if (kind===3) { this.firstUnknown??=row[2]; this.maxUnknown=Math.max(this.maxUnknown,row[3]); } } flushLoss() { const loss=this.loss; if (loss && loss.active>=10) this.event(1,loss.start,loss.last-loss.start+1,0,loss.phase); this.loss=null; } score(to, anchor) { const base=this.end-this.frames.length; for (;this.scored=10) this.event(3,this.unknownStart,i-this.unknownStart); this.unknownStart=null; } this.trusted++; this.passage.push(frame); if (this.passage.length===20) { if (this.align.unclearPassage(this.passage,i-19,anchor)) this.event(3,i-19,20,0,anchor.phase); this.passage=[]; } const expected=this.align.expected(i,anchor.phase); const quietest=Math.min(expected,this.align.expected(i-1,anchor.phase),this.align.expected(i+1,anchor.phase)); const attenuated=expected>this.align.peak-20 && frame[0]3 || frame[0]>expected+anchor.gain-8)) this.flushLoss(); } } cadence(anchor) { if (!this.rateBase) { this.rateBase={...anchor,end:this.end}; return; } const ticks=this.end-this.rateBase.end; if (ticks<500) return; const ppm=Math.round(1e6*phaseDelta(anchor.phase,this.rateBase.phase,this.align.length)/ticks); if (Math.abs(ppm)>=4000 && Math.sign(ppm)===Math.sign(this.previousRate) && Math.abs(this.previousRate)>=4000) { this.firstRate??=10*this.rateBase.end; this.event(4,this.rateBase.end,ticks,ppm,this.rateBase.phase); } if (Math.abs(ppm)>Math.abs(this.maxRate)) this.maxRate=ppm; this.previousRate=ppm; this.rateBase={...anchor,end:this.end}; } observe(batch, endMs, complete) { this.frames.push(...batch); this.end+=batch.length; const anchor=this.align.match(this.frames,this.end); if (batch.some(row=>!row[2])) this.discontinuities++; const previous=this.candidate; const stable=anchor && previous && this.end-previous.end<=50 && Math.abs(phaseDelta(anchor.phase,previous.phase,this.align.length))<=2; if (stable) { const shift=this.anchor ? phaseDelta(anchor.phase,this.anchor.phase,this.align.length) : 0; const fresh=this.anchor && this.end-this.anchor.end<=275; if (!this.anchor && this.scored=4 && fresh) { this.flushLoss(); this.firstShift??=10*this.scored; if (Math.abs(10*shift)>Math.abs(this.shift)) this.shift=-10*shift; this.event(2,this.scored,this.end-this.scored,-10*shift,this.anchor.phase); this.rateBase=null; this.previousRate=0; this.score(this.end,null); } else { this.score(this.end,anchor); this.cadence(anchor); } this.anchor={...anchor,end:this.end}; this.known=true; } else if (this.end-this.scored>275) this.score(this.end-150,null); if (anchor) this.candidate={...anchor,end:this.end}; if (complete) { this.score(this.end,null); this.flushLoss(); if (this.unknownStart!=null && this.end-this.unknownStart>=10) { this.event(3,this.unknownStart,this.end-this.unknownStart); this.unknownStart=null; } } if (this.frames.length>300) this.frames.splice(0,this.frames.length-300); const drift=this.anchor ? (this.anchor.phase+this.end)%this.align.length : -1; return {locked:this.known, complete, observedMs:10*this.end, windows:this.end, scoredEndMs:endMs, stageMinutes:this.minutes,firstLevelDropMs:this.firstDrop,longestLevelDropMs:this.maxDrop, firstUnclearMs:this.firstUnknown,gapMaxMs:this.maxUnknown,discontinuities:this.discontinuities, timing:{rateCoverageMs:0,edgeCount:0,maxRatePpm:0,firstRateMs:null,maxShiftMs:this.shift, firstShiftMs:this.firstShift,baselinePpm:null}, speech:{coverageMs:10*this.trusted,phase:drift,phaseAgeMs:this.anchor?10*(this.end-this.anchor.end):-1, total:this.count,events:this.events.slice(),firstRateMs:this.firstRate,maxCadencePpm:this.maxRate}}; } }