forked from ChelseaKR/olive-bark-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.test.mjs
More file actions
56 lines (49 loc) · 1.89 KB
/
Copy pathdetector.test.mjs
File metadata and controls
56 lines (49 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Node test for the PWA detector/level ports. Run: node --test pwa/
import assert from "node:assert/strict";
import { test } from "node:test";
import { Detector } from "./detector.js";
import { dbfs, rms, SILENCE_FLOOR_DBFS } from "./level.js";
function run(detector, readings) {
const events = [];
for (const [t, level] of readings) {
const ev = detector.push(t, level);
if (ev) events.push(ev);
}
const final = detector.flush();
if (final) events.push(final);
return events;
}
test("single loud frame filtered by min duration", () => {
const d = new Detector(-30, 0.5, 0.2);
assert.deepEqual(run(d, [[0, -10], [0.3, -60], [0.6, -60]]), []);
});
test("sustained loud makes one event", () => {
const d = new Detector(-30, 0.5, 0.2);
const readings = [];
for (let i = 0; i <= 10; i++) readings.push([i / 10, -10]);
readings.push([2.0, -60]);
const events = run(d, readings);
assert.equal(events.length, 1);
assert.equal(events[0].start, 0);
assert.ok(Math.abs(events[0].duration - 1.0) < 1e-9);
});
test("debounce bridges a brief dip", () => {
const d = new Detector(-30, 0.3, 1.0);
const events = run(d, [[0, -10], [0.5, -10], [1.0, -60], [1.5, -10], [2.0, -10], [4.0, -60]]);
assert.equal(events.length, 1);
assert.equal(events[0].end, 2.0);
});
test("dip longer than debounce splits events", () => {
const d = new Detector(-30, 0.3, 0.5);
const events = run(d, [[0, -10], [0.5, -10], [2.0, -60], [3.0, -10], [3.5, -10], [6.0, -60]]);
assert.equal(events.length, 2);
});
test("negative params rejected", () => {
assert.throws(() => new Detector(-30, -1, 0));
});
test("level math: full scale is 0 dBFS, silence floors", () => {
assert.equal(rms([]), 0);
assert.ok(Math.abs(dbfs(new Float32Array([1, 1, 1]))) < 1e-9);
assert.equal(dbfs(new Float32Array([0, 0])), SILENCE_FLOOR_DBFS);
assert.ok(Math.abs(dbfs(new Float32Array([0.5, 0.5])) - -6.0206) < 1e-3);
});