// A LOCKED edge is the author's, exact (mute.mjs lockedEdges / soundEdges): // nothing past a locked end is heard -- not the snapped tail, not the outgoing // half of a crossfade -- nothing before a locked start, and the snap moves a // locked edge only inside the silence it already sits in. One rule, read by // the build's joins (cutJoins → segmentSoundEdits → joinInputChain), the // podcast, and Play the cut (cut-points route → playPlan → audioSpans); the // last test checks the build and Play mute at the same cut second. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { cutJoins, cutRecordPath, joinInputChain, muteAudioFilter, soundFromAudioFilter, xfadeGraph } from "./build-video.mjs"; import { MUTE_FADE, segmentSoundEdits } from "./deck.mjs"; import { lockedEdges, soundEdges, soundOnSegment } from "./mute.mjs"; import { renderCut, snap, snapCut } from "./snap.mjs"; import { playPlan } from "../lib/report/playCut.mjs"; import { audioSpans, levelAt } from "../lib/report/playSchedule.mjs"; // ag1 of emberparadox/in-her-words: locked 579.68–589.21; the next sentence // ("She is …") starts at 589.92. const AG1 = { id: "ag1", type: "clip", video: "VMspX2rOyVg", start: 579.68, end: 589.21, lock: true, lockEnd: true }; const R = { fps: 30, transition: 0.4, snapLead: 0.45, snapTail: 0.55 }; test("lockedEdges: lock locks both, lockStart / lockEnd one; a cut is locked by lockCut; off with render.lockedEdges false or a montage", () => { assert.deepEqual(lockedEdges(AG1), { start: true, end: true }); assert.deepEqual(lockedEdges({ ...AG1, lock: undefined }), { start: false, end: true }); assert.deepEqual(lockedEdges({ ...AG1, lock: undefined, lockEnd: undefined, lockStart: true }), { start: true, end: false }); assert.deepEqual(lockedEdges({ ...AG1, lock: undefined, lockEnd: undefined }), { start: false, end: false }); assert.deepEqual(lockedEdges({ ...AG1, cutStart: 581, cutEnd: 585 }), { start: false, end: false }, "lock is the extent's"); assert.deepEqual(lockedEdges({ ...AG1, cutStart: 581, cutEnd: 585, lockCut: true }), { start: true, end: true }); assert.deepEqual(lockedEdges(AG1, { lockedEdges: false }), { start: false, end: false }); assert.deepEqual(lockedEdges(AG1, { montage: {} }), { start: false, end: false }); assert.deepEqual(lockedEdges({ ...AG1, type: "card" }), { start: false, end: false }); }); test("soundEdges: a locked end mutes at the end, an earlier muteFrom wins, an unlocked end is unchanged", () => { assert.deepEqual(soundEdges(AG1, R), { muteFrom: 589.21, soundFrom: 579.68, implicit: true }); assert.deepEqual(soundEdges({ ...AG1, muteFrom: 587 }, R), { muteFrom: 587, soundFrom: 579.68, implicit: false }, "the earlier mark wins"); assert.deepEqual(soundEdges({ ...AG1, muteFrom: 589.21 }, R).muteFrom, 589.21); const open = { ...AG1, lock: undefined, lockEnd: undefined }; assert.deepEqual(soundEdges(open, R), { muteFrom: null, soundFrom: null, implicit: false }); assert.deepEqual(soundEdges({ ...open, muteFrom: 588 }, R), { muteFrom: 588, soundFrom: null, implicit: false }); assert.deepEqual(soundEdges(AG1, { ...R, lockedEdges: false }), { muteFrom: null, soundFrom: null, implicit: false }); // A locked cut: its play window's edges (the lead-in before cutStart). assert.deepEqual(soundEdges({ ...AG1, cutStart: 581, cutEnd: 585, lockCut: true }, R), { muteFrom: 585, soundFrom: 580.6, implicit: true }); }); test("soundOnSegment: the locked end only where the segment reaches past it; the locked start only where it reaches before", () => { // The draft-3 cut: snapped 579.289–590.5 (11.233 s). assert.deepEqual(soundOnSegment(AG1, R, 579.289, 11.233), { mute: 9.921, soundFrom: 0.391, edges: { muteFrom: 589.21, soundFrom: 579.68, implicit: true }, }); // A segment that ends at the locked end (or before it): nothing to mute. assert.equal(soundOnSegment(AG1, R, 579.289, 9.921).mute, null); // One that starts AT the locked start: nothing before it. assert.equal(soundOnSegment(AG1, R, 579.68, 9.53).soundFrom, null); // The clip's own mark is placed wherever it falls, as it always was. assert.equal(soundOnSegment({ ...AG1, lock: false, lockEnd: false, muteFrom: 590.4 }, R, 579.289, 11).mute, 11.111); }); test("snap: a locked edge moves only inside its own silence -- never across a sound", () => { // Silences: one after "respect." that does NOT contain the locked end, and // the breath before the first word that DOES contain the locked start. const sil = [{ s: 0.2, e: 0.45 }, { s: 10.66, e: 11.5 }]; // Unlocked, the end reaches across 0.74 s of sound to the next silence. assert.deepEqual(snap(9.92, sil, "end", 1.6, R), { at: 11.21, snapped: true }); assert.deepEqual(snap(9.92, sil, "end", 1.6, R, { pinned: true }), { at: 9.92, snapped: false }); // Inside its silence it keeps the breath, outward, as ever. assert.equal(snap(0.39, sil, "start", 1.6, R, { pinned: true }).at, 0.2); assert.equal(snap(10.8, sil, "end", 1.6, R, { pinned: true }).at, 11.21); // Never inward: a long "silence" (quiet last words read as one -- ag1's // 589.21 sat in one from 587.92, and the snap cut it to 588.47) leaves it. assert.deepEqual(snap(11.4, sil, "end", 1.6, R, { pinned: true }), { at: 11.4, snapped: false }); assert.deepEqual(snap(11.4, sil, "end", 1.6, R), { at: 11.21, snapped: true }, "unlocked: inward, as before"); assert.deepEqual(snap(0.5, [{ s: 0.2, e: 2 }], "start", 1.6, R, { pinned: true }), { at: 0.5, snapped: false }); const c = snapCut({ sil, wantA: 0.39, wantB: 9.92, render: R, locked: { start: true, end: true } }); assert.equal(c.cutB, 9.92); assert.equal(c.b.snapped, false); const rc = renderCut({ sil, fetchStart: 579.29, playFrom: 579.68, playTo: 589.21, render: R, locked: { end: true } }); assert.equal(rc.end, 589.21); assert.equal(renderCut({ sil, fetchStart: 579.29, playFrom: 579.68, playTo: 589.21, render: R }).end, 590.5, "unlocked: as before"); }); test("segmentSoundEdits: from a matching cut record only; a missing record mutes no locked edge and says so", () => { const record = { video: AG1.video, start: 579.289, end: 590.5 }; const ed = segmentSoundEdits({ entry: AG1, record, render: R, seconds: 11.2 }); assert.equal(ed.mute, 9.921); assert.equal(ed.soundFrom, 0.391); const none = segmentSoundEdits({ entry: AG1, record: null, render: R, seconds: 11.2 }); assert.equal(none.mute, null); assert.equal(none.soundFrom, null); assert.match(none.notes.join("\n"), /locked edges are not muted -- no cut record/); // An explicit mark still places itself from the unsnapped start, as ever. const own = segmentSoundEdits({ entry: { ...AG1, lock: false, lockEnd: false, muteFrom: 588 }, record: null, render: R, seconds: 11.2 }); assert.equal(own.mute, 8.32); }); test("joinInputChain: the locked start's fade rises from it, the mute's fade ends at it", () => { assert.equal(soundFromAudioFilter(0.391), `afade=t=in:st=0.391:d=${MUTE_FADE}`); const c = joinInputChain(0, { hold: 0, move: null, soundFrom: 0.391, mute: 9.921 }, { fps: 30 }); assert.deepEqual(c.parts, [`[0:a]${soundFromAudioFilter(0.391)},${muteAudioFilter(9.921)}[j0a]`]); }); const haveFfmpeg = spawnSync("ffmpeg", ["-version"], { stdio: "ignore" }).status === 0; const ff = (args, opts = {}) => { const r = spawnSync("ffmpeg", ["-nostdin", "-v", "error", "-y", ...args], { maxBuffer: 1 << 28, ...opts }); assert.equal(r.status, 0, String(r.stderr)); return r.stdout; }; test("build/play parity: the build's join and Play's envelope go silent at the same cut second, and sound from the same one", { skip: !haveFfmpeg }, async () => { const dir = mkdtempSync(path.join(tmpdir(), "locked-edges-")); try { // Two 2 s segments: a (locked 100.3–101.4, cut from 100.0 to 102.0) and b. // As the build writes them (mp4, a cut record beside each), and losslessly // (mov) for the sound the graph is measured on. const make = (name, hz, ext) => { const f = path.join(dir, `${name}.${ext}`); const codec = ext === "mov" ? ["-c:v", "ffv1", "-c:a", "pcm_s16le"] : ["-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac"]; ff(["-f", "lavfi", "-i", "testsrc2=s=64x36:r=30:d=2", "-f", "lavfi", "-i", `sine=frequency=${hz}:sample_rate=48000:duration=2`, ...codec, "-ac", "2", f]); return f; }; const segs = [make("a", 440, "mp4"), make("b", 660, "mp4")]; const lossless = [make("a", 440, "mov"), make("b", 660, "mov")]; const entries = [ { id: "a", type: "clip", video: "va", start: 100.3, end: 101.4, lock: true }, { id: "b", type: "clip", video: "vb", start: 200, end: 202 }, ]; writeFileSync(cutRecordPath(segs[0]), JSON.stringify({ version: 1, id: "a", video: "va", start: 100, end: 102 })); const render = { fps: 30, transition: 0.4 }; const joins = await cutJoins({ entries, segments: segs, render }); assert.deepEqual(joins[0], { hold: 0, move: null, mute: 1.4, soundFrom: 0.3 }); assert.equal(joins[1], null); // With the knob off, the joins are what they were: none. assert.equal(await cutJoins({ entries, segments: segs, render: { ...render, lockedEdges: false } }), null); // The build: a's sound, crossfaded into b over 0.4 s. const g = xfadeGraph([2, 2], 0.4, joins, render, { video: false }); const pcm = ff([...lossless.flatMap((s) => ["-i", s]), "-filter_complex", g.parts.join(";"), "-map", g.alab, "-f", "s16le", "-ac", "1", "-ar", "48000", "-"], { encoding: "buffer" }); const peak = (a, b) => { let m = 0; for (let i = Math.round(a * 48000); i < Math.min(pcm.length / 2, Math.round(b * 48000)); i += 1) m = Math.max(m, Math.abs(pcm.readInt16LE(i * 2))); return m; }; assert.equal(peak(0, 0.3), 0, "nothing before the locked start"); assert.ok(peak(0.35, 1.35) > 1000, "the locked window sounds"); // From the locked end to the dissolve's end: a is gone; b fades in from 1.6. assert.equal(peak(1.4, 1.6), 0, "nothing past the locked end before b comes in"); // Play the cut: the route's row as cutPoints.mjs builds it, through the plan. const on = soundOnSegment(entries[0], render, 100, 2); const cuts = { a: { cutStart: 100, cutEnd: 102, footage: 2, muteFrom: on.edges.muteFrom, soundFrom: on.edges.soundFrom }, b: { cutStart: 200, cutEnd: 202, footage: 2 }, }; const plan = playPlan([{ id: "a", start: 0, duration: 2 }, { id: "b", start: 1.6, duration: 2 }], entries, { cuts }); const span = audioSpans(plan).find((s) => s.id === "a"); // The same cut seconds as the build's join: a starts at 0 on both clocks. const muteAt = joins[0].mute; const soundAt = joins[0].soundFrom; assert.equal(levelAt(span.env, soundAt - 0.01), 0); assert.equal(levelAt(span.env, soundAt), 0); assert.ok(Math.abs(levelAt(span.env, soundAt + MUTE_FADE) - 1) < 1e-9); assert.ok(Math.abs(levelAt(span.env, muteAt - MUTE_FADE) - 1) < 1e-9); assert.equal(levelAt(span.env, muteAt), 0); assert.equal(levelAt(span.env, 1.8), 0, "silent under the dissolve"); } finally { rmSync(dir, { recursive: true, force: true }); } });