jenkins af45190bfb bstein: serve the phone sound source and 30-minute RCT microphone detector
Vendor the web assets from Lesavka a5758755. The RCT browser performs signal recognition and sends numeric observations through the existing receipt service.
2026-12-03 12:11:53 -06:00

109 lines
4.9 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';
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;
}
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.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;
}
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++;
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;
if (this.nextFrame !== null && this.nextFrame !== frame) this.interrupt();
this.nextFrame = frame + length;
for (let i = 0; i < length && this.windows < this.target; i++) {
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;
}
}
}
summary(nowMs) {
return {locked: this.locked, complete: this.windows >= this.target,
observedMs: this.windows * this.windowMs, windows: this.windows,
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()};
}
}