#!/usr/bin/env node // A stand-in for scripts/speaker-embed.py (release 34 N2), with its exact // command line and JSON — so the voice-match pipeline, its CLI and the app's // job run in a test with no python, no sherpa-onnx and no model file. // // node fakeSpeakerEmbed.mjs --emb [--threads N] // // The "embedding" is the clip's long-term spectrum: the energy at 32 // log-spaced frequencies (60 Hz–4 kHz, Goertzel over 32 ms frames), in dB, // mean-removed and L2-normalised. Deterministic, and it tells two voices of // different pitch and colour apart (syntheticVoice.mjs's VOICE_A and VOICE_B) // — which is all a test of the plumbing needs. It is not a speaker model. // Point VOICE_MATCH_PYTHON at `node` and VOICE_MATCH_SCRIPT at this file. import { readFileSync } from "node:fs"; import { pathToFileURL } from "node:url"; const BANDS = 32; const FRAME = 512; 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 }; } export function fakeEmbedding(x, rate) { const freqs = Array.from({ length: BANDS }, (_, i) => 60 * (4000 / 60) ** (i / (BANDS - 1))); const acc = new Array(BANDS).fill(0); let frames = 0; for (let a = 0; a + FRAME <= x.length; a += FRAME / 2) { frames += 1; freqs.forEach((f, b) => { const w = (2 * Math.PI * f) / rate; const c = 2 * Math.cos(w); let s1 = 0; let s2 = 0; for (let i = 0; i < FRAME; i += 1) { const win = 0.5 - 0.5 * Math.cos((2 * Math.PI * i) / (FRAME - 1)); const s0 = x[a + i] * win + c * s1 - s2; s2 = s1; s1 = s0; } acc[b] += s1 * s1 + s2 * s2 - c * s1 * s2; }); } if (!frames) return null; const db = acc.map((e) => 10 * Math.log10(e / frames + 1e-9)); const mean = db.reduce((s, v) => s + v, 0) / BANDS; const v = db.map((d) => d - mean); const n = Math.sqrt(v.reduce((s, e) => s + e * e, 0)); return n > 0 ? v.map((e) => Math.round((e / n) * 1e6) / 1e6) : null; } const isMain = !!process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url; if (isMain) { const args = process.argv.slice(2); const req = args.filter((a, i) => !a.startsWith("--") && !(i > 0 && args[i - 1].startsWith("--"))).pop(); const { items = [] } = JSON.parse(readFileSync(req, "utf8")); const out = items.map((it) => { try { const { x, rate } = readWav(it.wav); const e = x.length >= rate * 0.5 ? fakeEmbedding(x, rate) : null; return { id: it.id, embedding: e, seconds: Math.round((x.length / rate) * 1000) / 1000, ...(e ? {} : { error: "too short" }) }; } catch (e) { return { id: it.id, embedding: null, seconds: 0, error: String(e) }; } }); process.stdout.write(JSON.stringify({ model: "fake-spectrum", version: null, dim: BANDS, items: out })); }