// scripts/voice.mjs, the voice engines' wrapper: its argv refusals, and the // zero-install engine (rnnoise through ffmpeg's arnndn) end to end — the // output's shape (48 kHz mono 16-bit, a bare header, exactly the input's sample // count), its alignment (arnndn answers 480 samples late; the wrapper takes // them back) and its determinism (arnndn's flushed last frame is garbage that // differs run to run; the wrapper pads it off). Run: node --test scripts/voice.test.mjs import { test, after } from "node:test"; import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs"; import os from "node:os"; import path from "node:path"; const WRAPPER = path.join(import.meta.dirname, "voice.mjs"); const MODEL = path.join(import.meta.dirname, "voice-models", "sh.rnnn"); const dir = mkdtempSync(path.join(os.tmpdir(), "voice-wrapper-")); after(() => rmSync(dir, { recursive: true, force: true })); let ffmpeg = true; try { execFileSync("ffmpeg", ["-version"], { stdio: "ignore" }); } catch { ffmpeg = false; } const run = (args) => spawnSync(process.execPath, [WRAPPER, ...args], { encoding: "utf8" }); test("the argv is checked before anything runs", () => { assert.match(run(["--op", "remix", "--engine-kind", "rnnoise"]).stderr, /--op must be enhance or convert/); assert.match(run(["--op", "enhance", "--engine-kind", "seed-vc"]).stderr, /--engine-kind for enhance is one of rnnoise, deepfilternet/); assert.match(run(["--op", "enhance", "--engine-kind", "rnnoise"]).stderr, /--input and --output are required/); assert.match(run(["--op", "enhance", "--engine-kind", "rnnoise", "--input", "/a", "--output", "/b", "--params", "{"]).stderr, /--params must be JSON/); // The pitch a conversion lands on is seed-vc's to follow, and semitones move a followed one. const conv = (kind, ...more) => run(["--op", "convert", "--engine-kind", kind, "--input", "/a", "--output", "/b", ...more]); assert.match(conv("seed-vc", "--pitch", "higher").stderr, /--pitch is one of target, source, contour \(got 'higher'\)/); assert.match(conv("seed-vc", "--pitch", "source", "--semitones", "13").stderr, /--semitones is a number from -12 to 12 \(got '13'\)/); assert.match(conv("seed-vc", "--semitones", "2").stderr, /--semitones moves a followed pitch: it needs --pitch source or contour/); assert.match(conv("openvoice", "--pitch", "contour").stderr, /--pitch contour is seed-vc's: openvoice has no pitch control/); for (const r of [conv("seed-vc", "--pitch", "higher"), conv("openvoice", "--pitch", "contour")]) assert.equal(r.status, 2); }); test("rnnoise: 48 kHz mono, the input's exact length, aligned, the same every run", { skip: !ffmpeg && "ffmpeg absent" }, () => { // A voice-ish signal: a 200 Hz buzz switching on and off, over pink noise, at 44.1 kHz stereo. const input = path.join(dir, "in.wav"); execFileSync("ffmpeg", [ "-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "aevalsrc='0.4*sin(2*PI*200*t)*sin(2*PI*220*t)*gt(mod(t,1),0.3)':s=44100:d=2.37", "-f", "lavfi", "-i", "anoisesrc=c=pink:a=0.02:r=44100:d=2.37:seed=5", "-filter_complex", "amix=inputs=2:normalize=0", "-ac", "2", input, ]); const sums = new Set(); for (let i = 0; i < 3; i++) { const out = path.join(dir, `out${i}.wav`); const r = run(["--op", "enhance", "--engine-kind", "rnnoise", "--model", MODEL, "--input", input, "--output", out, "--params", '{"lufs":-20}']); assert.equal(r.status, 0, r.stderr); const res = JSON.parse(r.stdout.trim().split("\n").pop()); assert.equal(res.op, "enhance"); assert.equal(res.engine, "rnnoise"); assert.equal(res.sampleRate, 48000); // 2.37 s at 48 kHz. assert.equal(res.samples, 113760); const buf = readFileSync(out); assert.equal(buf.toString("ascii", 0, 4), "RIFF"); assert.equal(buf.readUInt16LE(22), 1); assert.equal(buf.readUInt32LE(24), 48000); assert.equal(buf.readUInt16LE(34), 16); assert.equal(buf.toString("ascii", 36, 40), "data"); assert.equal(buf.readUInt32LE(40), 113760 * 2); sums.add(createHash("sha256").update(buf).digest("hex")); // The buzz starts at 0.3 s in the input: within 2 ms of it in the output // (unaligned, arnndn's frame would put it 10 ms late). let first = -1; for (let k = 0; k < 113760; k++) { if (Math.abs(buf.readInt16LE(44 + 2 * k)) > 3000) { first = k; break; } } assert.ok(Math.abs(first - 0.3 * 48000) < 96, `the onset is at sample ${first}`); } assert.equal(sums.size, 1, "the same input gives the same output"); // No scratch left behind. assert.deepEqual(readdirSync(dir).filter((f) => f.includes(".voice-")), []); }); test("--more runs further files the same way, in the same run", { skip: !ffmpeg && "ffmpeg absent" }, () => { const a = path.join(dir, "m-a.wav"); const b = path.join(dir, "m-b.wav"); execFileSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "sine=f=300:d=1:r=48000", a]); execFileSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "sine=f=300:d=0.5:r=22050", b]); const outB = path.join(dir, "m-parts", "p02.wav"); const r = run(["--op", "enhance", "--engine-kind", "rnnoise", "--model", MODEL, "--input", a, "--output", path.join(dir, "m-out.wav"), "--more", JSON.stringify([{ input: b, output: outB }])]); assert.equal(r.status, 0, r.stderr); const res = JSON.parse(r.stdout.trim().split("\n").pop()); assert.equal(res.samples, 48000); assert.equal(res.more.length, 1); assert.equal(res.more[0].output, outB); assert.equal(res.more[0].samples, 24000); assert.equal(readFileSync(outB).readUInt32LE(40), 48000); assert.match(run(["--op", "enhance", "--engine-kind", "rnnoise", "--input", a, "--output", "/x.wav", "--more", '[{"input":1}]']).stderr, /--more is a list of \{input, output\}/); }); // scripts/voice_seedvc.py's contour shift over a batch (a take and its parts): measured once, on the first file with // a voiced frame. The function is stdlib-only (torch and the model are imported inside main). let python = true; try { execFileSync("python3", ["-c", "import math"], { stdio: "ignore" }); } catch { python = false; } test("seed-vc's contour shift: set by the first file with a voiced frame, never locked at 0 by a silent one", { skip: !python && "python3 absent" }, () => { const code = ` import json, math, sys sys.path.insert(0, ${JSON.stringify(import.meta.dirname)}) from voice_seedvc import pitch_move ref, src = math.log(200.0), math.log(100.0) out = {} # A silent first file: nothing measured, a factor of the semitones alone. m, hz, f = pitch_move(None, None, None, ref, "contour", 0) out["silent"] = [m, hz, f] # The next, voiced: an octave up to the reference's median, and it is kept. m, hz, f = pitch_move(m, hz, src, ref, "contour", 0) out["voiced"] = [round(m, 6), hz, round(f, 6)] m2, hz2, f2 = pitch_move(m, hz, math.log(150.0), ref, "contour", 0) out["later"] = [round(m2, 6), hz2, round(f2, 6)] # Source: no move, the semitones alone; contour with semitones: both. out["source"] = [round(x, 6) if isinstance(x, float) else x for x in pitch_move(None, None, src, ref, "source", 12)] out["both"] = round(pitch_move(None, None, src, ref, "contour", -12)[2], 6) # A silent file after the move is set does not unset it. out["after"] = round(pitch_move(m, hz, None, ref, "contour", 0)[0], 6) print(json.dumps(out)) `; const out = JSON.parse(execFileSync("python3", ["-c", code], { encoding: "utf8", env: { ...process.env, PYTHONDONTWRITEBYTECODE: "1" } })); assert.deepEqual(out.silent, [null, null, 1]); assert.deepEqual(out.voiced, [0.693147, 100, 2]); assert.deepEqual(out.later, [0.693147, 100, 2]); assert.deepEqual(out.source, [0, 100, 2]); assert.equal(out.both, 1); assert.equal(out.after, 0.693147); });