146 lines
6.7 KiB
JavaScript
146 lines
6.7 KiB
JavaScript
// Match a pilot plus changing marker in 10 ms windows of the selected RCT input.
|
|
import {ANCHOR_HZ, MARKERS_HZ, ORDER, SYMBOL_SECONDS, TRIAL_SECONDS} from './acoustic-pattern.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) {
|
|
this.rate = rate;
|
|
this.size = Math.round(0.01 * rate);
|
|
this.windowMs = 1000 * this.size / rate;
|
|
this.target = Math.ceil(TRIAL_SECONDS * 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.haveInput = true;
|
|
this.inputMissing = false;
|
|
}
|
|
interrupt() {
|
|
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);
|
|
if (power[0] < min || power[marker] < min || this.energy === 0) 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.observeLevel(valid);
|
|
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);
|
|
} 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,
|
|
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()};
|
|
}
|
|
}
|