commit 2b7b0c7c8ddfde64a2d15c0178be6b128523a3da
parent a7a48b349b170ac7a05a6b41faa920a1adb16655
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Wed, 28 Oct 2026 09:31:06 -0400
Merge mk/dedup-ums: swap-clip refuses a clip the plan already plays or cannot render; fit-hooks measures with the ceiling off
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/umtool/song/fit-hooks.mjs b/umtool/song/fit-hooks.mjs
@@ -91,9 +91,22 @@ const rms = (x, a, b) => {
// mk-mix-hooks resolves EVERY path against SONG_DATA, so it must be handed the
// relative names it was given -- an absolute path comes back doubled
// (".../song/home/<user>/.../song/mk-overlays.json") and it dies on ENOENT.
+// The two sides are measured with the mixer's ceiling OFF and both renders turned down
+// by the same HEADROOM. With the ceiling on, the hooks-only render is scaled back as
+// soon as its loudest hook passes LIMIT, so every hook's ratio stalls at whatever the
+// ceiling allows (1.45 here) and each pass multiplies the gains up again chasing it:
+// targets of 1.6 / 1.8 / 2.0 all reported 1.452 and delivered +3 / +6 / +9 dB. Off
+// alone is not enough -- the wav is 16-bit and a hot hook would clip, which caps it
+// the same way -- hence the headroom, which scales both sides alike. The real mix
+// scales the WHOLE master at its ceiling, hooks and body together, so this ratio is
+// the one it delivers.
+const HEADROOM = 0.25;
const mix = (rel, env) => {
execFileSync("node", [path.join(DIR, "mk-mix-hooks.mjs"), SONG, OVL, rel],
- { env: { ...process.env, DRIVE, ...env }, stdio: ["ignore", "ignore", "inherit"] });
+ { env: { ...process.env, DRIVE, LIMIT: "1e9",
+ SONG_GAIN: String(HEADROOM * Number(process.env.SONG_GAIN ?? 1)),
+ HOOK_GAIN: String(HEADROOM * Number(process.env.HOOK_GAIN ?? 1)), ...env },
+ stdio: ["ignore", "ignore", "inherit"] });
return readWav(path.join(SONG_DATA, rel));
};
diff --git a/umtool/song/swap-clip.mjs b/umtool/song/swap-clip.mjs
@@ -20,10 +20,12 @@
//
// F0_MAX=<Hz> reject candidates above this (default 200; 115 is the gate
// arrange-poly uses to separate a guest from Jer)
+// FORCE=<video@start> use this clip regardless of rank; refused if it overlaps a clip
+// already in the plan unless FORCE_REUSE=1
// FLAT_MAX=<f> reject candidates whose spectral flatness exceeds this.
// Measured lazily, best-scoring candidate first, because it
// is an FFT per clip and the pool is thousands of clips.
-import { readFileSync, writeFileSync, readdirSync } from "node:fs";
+import { existsSync, readFileSync, writeFileSync, readdirSync } from "node:fs";
import path from "node:path";
import { SONG_DATA } from "./paths.mjs";
import { readWavMono, spectralFlatness } from "./flatness.mjs";
@@ -78,6 +80,16 @@ all = all.filter((c) => acc.has(key(c)) && !seen.has(key(c)) && seen.add(key(c))
// every clip already sounding anywhere in this plan
const used = new Set(plan.voices.flatMap((x) => x.plan.map((p) => `${p.video}@${p.srcStart}`)));
+// The same clip is not only the same KEY: a trim (hush-head, a nudged window) moves a
+// note's srcStart by milliseconds, and mk-plan-fatal5 played OlY74Y-NmMw@700.006 and
+// @700.002 on two consecutive notes. Any overlap with a clip already in the plan is
+// the same sound.
+const usedSpans = plan.voices.flatMap((x) => x.plan.filter((p) => p.video).map((p) => [p.video, p.srcStart, p.srcEnd]));
+const overlapsUsed = (c) => usedSpans.some(([vid, a, b]) => vid === c.video && c.start < b && a < c.end);
+// A clip whose source is not on this machine cannot be rendered: the palette and
+// cand2/ outlive a re-download, and render-poly reads media/ and wav48/.
+const renderable = (vid) => existsSync(path.join(SONG_DATA, "wav48", `${vid}.wav`)) &&
+ existsSync(path.join(SONG_DATA, "media", `${vid}.mp4`));
// ---- spectral flatness, for the STATIC case --------------------------------
// The measure itself lives in flatness.mjs, shared with plan-qa.mjs and the
@@ -182,7 +194,8 @@ let skippedWords = 0;
let skippedSuspect = 0;
for (const c of all) {
const k = key(c);
- if (used.has(`${c.video}@${c.start}`)) continue;
+ if (used.has(`${c.video}@${c.start}`) || overlapsUsed(c)) continue;
+ if (!renderable(c.video)) continue;
if (IMPURE.has(k)) continue; // plain clips only
if (!ALLOW_WORDS && wordsInside(c).length) { skippedWords += 1; continue; }
if (SUSPECT_KEYS.has(k)) { skippedSuspect += 1; continue; }
@@ -230,6 +243,9 @@ let best = null;
if (FORCE) {
const c = all.find((x) => `${x.video}@${(+x.start).toFixed(2)}` === FORCE || `${x.video}@${x.start}` === FORCE);
if (!c) throw new Error(`FORCE clip ${FORCE} is not in the palette`);
+ // FORCE skipped the reuse check entirely, which is how one clip played twice in a row.
+ if (overlapsUsed(c) && process.env.FORCE_REUSE !== "1")
+ throw new Error(`FORCE clip ${FORCE} is already in the plan (FORCE_REUSE=1 to allow it)`);
const core = CORE[key(c)];
const dur = core ? core.dur : c.dur;
const f0 = FORCE_F0 || (core ? core.f0 : c.f0);