// Match a pilot plus changing marker in 10 ms windows of the selected RCT input. import {ANCHOR_HZ, MARKERS_HZ, ORDER, SYMBOL_SECONDS, STAGE_MINUTES} from './acoustic-pattern.js'; import {AcousticTiming} from './acoustic-timing.js'; import {PilotWindow} from './pilot-window.js'; // A review threshold for continuous test sound. It does not model intelligibility. export const LEVEL_DROP_MS = 100; const DROP_POWER_RATIO = 0.01; // 20 dB below the recently recognized reference. export class AcousticDetector { constructor(rate, minutes = STAGE_MINUTES.at(-1)) { if (!STAGE_MINUTES.includes(minutes)) throw new RangeError('unsupported stage'); this.minutes = minutes; this.rate = rate; this.size = Math.round(0.01 * rate); this.windowMs = 1000 * this.size / rate; this.timing = new AcousticTiming(this.windowMs); this.anchorSin = Math.sin(2*Math.PI*ANCHOR_HZ/rate); this.target = Math.ceil(minutes * 60 * rate / this.size); this.coeff = [ANCHOR_HZ, ...MARKERS_HZ].map(f => 2 * Math.cos(2 * Math.PI * f / rate)); this.q1 = new Float64Array(this.coeff.length); this.q2 = new Float64Array(this.coeff.length); this.count = this.energy = this.tick = this.edges = this.lastChange = 0; this.lastMarker = this.slot = -1; this.locked = false; this.windows = this.bad = this.gap = this.longest = this.sequenceFaults = this.interruptions = 0; this.faults = []; this.recent = new PilotWindow(this.windowMs); this.nextFrame = null; this.referencePower = null; this.quietWindows = this.dropCount = this.longestDropMs = 0; this.firstDropMs = null; this.firstUnclearMs = null; this.haveInput = true; this.inputMissing = false; } interrupt() { this.timing.reset(); if (this.locked && this.windows < this.target) { this.interruptions++; this.record('observation interrupted'); this.recent.interrupt(); } this.edges = 0; this.slot = this.lastMarker = -1; this.count = this.energy = 0; this.quietWindows = 0; this.referencePower = null; this.haveInput = true; this.q1.fill(0); this.q2.fill(0); } record(kind) { if (this.faults.length < 64) this.faults.push({atMs: Math.round(this.windows * this.windowMs), kind}); } detect() { const power = this.coeff.map((c, i) => Math.max(0, this.q1[i] ** 2 + this.q2[i] ** 2 - c * this.q1[i] * this.q2[i])); const marker = power.indexOf(Math.max(...power.slice(1)), 1); // Both tones must be distinguishable at room level, including against // the optional RCT's separate 1 kHz output pulses. const min = Math.max((0.0008 * this.size / 2) ** 2, 0.035 * this.size * this.energy); this.carrierPresent = power[0]>=min && this.energy>0; const runnerUp = Math.max(...power.slice(1).filter((_,i)=>i+1!==marker)); this.timingMarker = power[marker]>=min && power[marker]>=2*runnerUp ? marker-1 : -1; if (!this.carrierPresent || power[marker] < min) return -1; return marker - 1; } observeLevel(valid) { const power = this.energy / this.size; const quiet = this.haveInput && this.referencePower !== null && power <= DROP_POWER_RATIO * this.referencePower; if (valid && this.haveInput && !quiet) { this.referencePower = this.referencePower === null ? power : this.referencePower + 0.05 * (power - this.referencePower); } this.quietWindows = quiet ? this.quietWindows + 1 : 0; const threshold = Math.ceil(LEVEL_DROP_MS / this.windowMs); if (this.quietWindows < threshold) return; if (this.quietWindows === threshold) { this.dropCount++; this.firstDropMs ??= Math.round((this.windows - this.quietWindows) * this.windowMs); } this.longestDropMs = Math.max(this.longestDropMs, this.quietWindows * this.windowMs); } window(marker, endMs) { this.tick++; let valid = marker >= 0; const dwellMs = (this.tick - this.lastChange) * this.windowMs; if (valid && marker !== this.lastMarker) { const pair = ORDER.findIndex((v, i) => v === marker && ORDER[(i + ORDER.length - 1) % ORDER.length] === this.lastMarker); const expected = this.slot < 0 || pair === (this.slot + 1) % ORDER.length; const onTime = Math.abs(dwellMs - 1000 * SYMBOL_SECONDS) <= 2.1 * this.windowMs; const matched = pair >= 0 && expected && onTime; this.edges = matched ? this.edges + 1 : 0; if (this.locked && !matched) { this.sequenceFaults++; this.record('marker order or timing'); valid = false; } this.slot = pair; this.lastMarker = marker; this.lastChange = this.tick; } else if (valid && dwellMs > 1000 * SYMBOL_SECONDS + 2.1 * this.windowMs) { valid = false; // A frozen or replayed single symbol is audible energy too. } if (!this.locked) { if (!valid) this.edges = 0; if (this.edges < 4) return; this.locked = true; } this.windows++; this.scoredEndMs = endMs; this.observeLevel(valid); this.timing.marker(this.haveInput && !this.quietWindows ? this.timingMarker : -1, this.windows*this.windowMs); const phase = this.carrierPresent && this.haveInput && !this.quietWindows ? Math.atan2(this.anchorSin*this.q2[0], this.q1[0]-0.5*this.coeff[0]*this.q2[0])/(2*Math.PI) : null; this.timing.carrier(phase, this.windows*this.windowMs); if (!valid) { if (!this.gap) this.record('signal missing or out of sequence'); this.bad++; this.gap++; this.longest = Math.max(this.longest, this.gap); if (this.gap * this.windowMs >= LEVEL_DROP_MS) { this.firstUnclearMs ??= Math.round((this.windows - this.gap) * this.windowMs); } } else this.gap = 0; this.recent.observe(valid, endMs); } process(samples, frame, length) { if (this.windows >= this.target) return; const missing = !samples || samples.length < length; if ((this.nextFrame !== null && this.nextFrame !== frame) || (missing && !this.inputMissing)) this.interrupt(); this.inputMissing = missing; this.nextFrame = frame + length; for (let i = 0; i < length && this.windows < this.target; i++) { if (missing && i >= (samples?.length ?? 0)) this.haveInput = false; const x = samples?.[i] ?? 0; this.energy += x * x; for (let j = 0; j < this.coeff.length; j++) { const q = x + this.coeff[j] * this.q1[j] - this.q2[j]; this.q2[j] = this.q1[j]; this.q1[j] = q; } if (++this.count === this.size) { this.window(this.detect(), (frame + i + 1) * 1000 / this.rate); this.q1.fill(0); this.q2.fill(0); this.count = this.energy = 0; this.haveInput = true; } } } summary(nowMs) { return {locked: this.locked, complete: this.windows >= this.target, observedMs: this.windows * this.windowMs, windows: this.windows, levelDrops: this.dropCount, longestLevelDropMs: this.longestDropMs, firstLevelDropMs: this.firstDropMs, firstUnclearMs: this.firstUnclearMs, timing:this.timing.summary(), stageMinutes: this.minutes, scoredEndMs: this.scoredEndMs ?? null, missingWindows: this.bad, sequenceFaults: this.sequenceFaults, missingFraction: this.windows ? this.bad / this.windows : null, gapMaxMs: this.longest * this.windowMs, trailingGapMs: this.gap * this.windowMs, discontinuities: this.interruptions, recent: this.recent.summary(nowMs), firstFaults: this.faults.slice()}; } }