#!/usr/bin/env node // A stand-in for scripts/audio-tag.py (release 35 C1), with its exact command // line and JSON — so the laugh scan, its CLI and the app's job run in a test // with no python, no sherpa-onnx and no model file. // // node fakeAudioTag.mjs --model --labels [--threads N] [--classes 16,18,19,20,21] // // It "hears laughter" where a window's sound is mostly the test kit's laugh // tone (testing/clips/reactionStream.mjs LAUGH_TONE_HZ, by a Goertzel filter // against the window's total energy): 0.8 for the first class (Laughter), // 0.05 for the second, 0.01 for the rest; elsewhere 0.01. Deterministic. It // is not a sound model. Point AUDIO_TAG_PYTHON at `node` and AUDIO_TAG_SCRIPT // at this file (AUDIO_TAG_MODEL at any existing file). import { readFileSync } from "node:fs"; import { pathToFileURL } from "node:url"; import { LAUGH_TONE_HZ } from "../clips/reactionStream.mjs"; function readWav(file) { const buf = readFileSync(file); let at = 12; let rate = 16000; while (at + 8 <= buf.length) { const id = buf.toString("ascii", at, at + 4); const size = buf.readUInt32LE(at + 4); if (id === "fmt ") rate = buf.readUInt32LE(at + 12); if (id === "data") { const n = Math.floor(Math.min(size, buf.length - at - 8) / 2); const x = new Float32Array(n); for (let i = 0; i < n; i += 1) x[i] = buf.readInt16LE(at + 8 + i * 2) / 32768; return { x, rate }; } at += 8 + size + (size % 2); } return { x: new Float32Array(0), rate }; } /** The share of a stretch's energy at `hz` (0..1). */ export function toneShare(x, rate, hz = LAUGH_TONE_HZ) { if (!x.length) return 0; const w = (2 * Math.PI * hz) / rate; const c = 2 * Math.cos(w); let s1 = 0; let s2 = 0; let total = 0; for (let i = 0; i < x.length; i += 1) { const s0 = x[i] + c * s1 - s2; s2 = s1; s1 = s0; total += x[i] * x[i]; } const power = (s1 * s1 + s2 * s2 - c * s1 * s2) / (x.length / 2); return total > 1e-9 ? Math.min(1, power / total) : 0; } export function fakeProbs(x, rate, classes) { const laugh = toneShare(x, rate) > 0.35; return classes.map((_, i) => (laugh ? (i === 0 ? 0.8 : i === 1 ? 0.05 : 0.01) : 0.01)); } function main(argv) { let classes = [16, 18, 19, 20, 21]; let model = "fake.onnx"; const positional = []; for (let i = 0; i < argv.length; i += 1) { const a = argv[i]; if (a === "--classes") classes = argv[++i].split(",").filter(Boolean).map(Number); else if (a === "--model") model = argv[++i]; else if (a === "--labels" || a === "--threads") i += 1; else positional.push(a); } const req = JSON.parse(readFileSync(positional[0], "utf8")); const items = (req.items ?? []).map((item) => { const rec = { id: item.id, seconds: 0, window: null, hop: null, scores: null }; try { const { x, rate } = readWav(item.wav); rec.seconds = Math.round((x.length / rate) * 1000) / 1000; if (item.window == null) rec.scores = [fakeProbs(x, rate, classes)]; else { const win = Math.round(item.window * rate); const hop = Math.max(1, Math.round((item.hop ?? item.window) * rate)); rec.window = item.window; rec.hop = item.hop ?? item.window; rec.scores = []; for (let a = 0; a + win <= x.length; a += hop) rec.scores.push(fakeProbs(x.subarray(a, a + win), rate, classes)); } } catch (e) { rec.error = e instanceof Error ? e.message : String(e); } return rec; }); process.stdout.write(JSON.stringify({ model: model.split("/").pop(), version: "fake", classes, names: classes.map(String), items })); } if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) main(process.argv.slice(2));