refactor: extract typed spectrum math
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import vm from "node:vm";
|
||||
import { build } from "esbuild";
|
||||
|
||||
async function loadSpectrumMath() {
|
||||
const result = await build({
|
||||
entryPoints: [new URL("../src/features/spectrum/math.ts", import.meta.url).pathname],
|
||||
bundle: true,
|
||||
format: "cjs",
|
||||
platform: "browser",
|
||||
target: "es2022",
|
||||
write: false,
|
||||
});
|
||||
const module = { exports: {} };
|
||||
vm.runInNewContext(result.outputFiles[0].text, {
|
||||
module,
|
||||
exports: module.exports,
|
||||
Array,
|
||||
ArrayBuffer,
|
||||
DataView,
|
||||
Float32Array,
|
||||
Float64Array,
|
||||
Int8Array,
|
||||
Uint8Array,
|
||||
Number,
|
||||
Math,
|
||||
TypeError,
|
||||
});
|
||||
return module.exports;
|
||||
}
|
||||
|
||||
test("compact spectrum frames decode signed i8 bins", async () => {
|
||||
const { decodeBase64Int8 } = await loadSpectrumMath();
|
||||
const bins = decodeBase64Int8("/4AAfw==");
|
||||
assert.deepEqual(Array.from(bins), [-1, -128, 0, 127]);
|
||||
assert.throws(() => decodeBase64Int8("%%%"), /Invalid base64/);
|
||||
});
|
||||
|
||||
test("noise floor uses the lower spectrum percentile without sorting input", async () => {
|
||||
const { estimateNoiseFloorDb } = await loadSpectrumMath();
|
||||
const bins = new Float32Array([-70, -100, -95, -90, -80, -85, -75]);
|
||||
const original = Array.from(bins);
|
||||
assert.equal(estimateNoiseFloorDb(bins), -95);
|
||||
assert.deepEqual(Array.from(bins), original);
|
||||
assert.equal(estimateNoiseFloorDb(new Float32Array()), null);
|
||||
});
|
||||
Reference in New Issue
Block a user