// The Play view's sound on one clock (playSchedule.mjs): clips abut sample // for sample at a hard cut, a crossfade is two linear ramps over the overlap, // room's muted edges, the gain, the mute mark, a seek into the middle of a // clip (and of a ramp), and a speed. // // Run with: node --test lib/report/playSchedule.test.mjs import assert from "node:assert/strict"; import test from "node:test"; import { estimateSchedule } from "umtool-report-to-video/deck"; import { playPlan } from "./playCut.mjs"; import { audioSpans, cutTimeAtCtx, decodeSpanOf, levelAt, multiply, scheduleFrom } from "./playSchedule.mjs"; const manifest = (transition) => ({ slug: "t", render: { width: 1920, height: 1080, fps: 30, transition }, timeline: [ { type: "clip", id: "a", video: "v1", start: 10, end: 14 }, { type: "clip", id: "b", video: "v2", start: 100, end: 103 }, { type: "card", id: "k", seconds: 2, heading: "card" }, { type: "clip", id: "c", video: "v3", start: 50, end: 52.5 }, ], }); const planOf = (transition, cuts = null) => { const m = manifest(transition); return playPlan(estimateSchedule(m).segments, m.timeline, { cuts }); }; const near = (a, b, what, tol = 1e-6) => assert.ok(Math.abs(a - b) <= tol, `${what}: ${a} vs ${b}`); test("hard cuts: each clip's sound abuts the next to the sample, at its segment's start; a card is silence", () => { const spans = audioSpans(planOf(0)); assert.deepEqual(spans.map((s) => [s.id, s.from, s.to, s.src]), [["a", 0, 4, 10], ["b", 4, 7, 100], ["c", 9, 11.5, 50]]); const sch = scheduleFrom(spans, 0); // a ends exactly where b starts, on the wall clock; at 48 kHz, the same sample. assert.equal(sch[0].atWall + sch[0].durWall, sch[1].atWall); assert.equal(Math.round((sch[0].atWall + sch[0].durWall) * 48000), Math.round(sch[1].atWall * 48000)); // The card's two seconds: nothing scheduled over them. assert.equal(sch[2].atWall - (sch[1].atWall + sch[1].durWall), 2); // No ramps: full level throughout. assert.deepEqual(sch[0].env, [[0, 1], [4, 1]]); }); test("a crossfade: the outgoing ramps down and the incoming up over the same overlap, summing to one", () => { const spans = audioSpans(planOf(0.5)); const [a, b] = spans; // b's segment starts 0.5 s before a's ends. assert.equal(b.from, 3.5); assert.equal(a.to, 4); for (const t of [3.5, 3.6, 3.75, 3.9, 4]) near(levelAt(a.env, t) + levelAt(b.env, t), 1, `sum at ${t}`); near(levelAt(a.env, 3.75), 0.5, "a mid-fade"); near(levelAt(b.env, 3.5), 0, "b silent at its start"); near(levelAt(b.env, 4), 1, "b full when a is out"); }); test("a seek mid-clip and mid-ramp: the node starts now, at the source second the cut shows, at the ramp's level", () => { const spans = audioSpans(planOf(0.5)); const sch = scheduleFrom(spans, 3.7); const [a, b] = sch; assert.equal(a.atWall, 0); near(a.src, 13.7, "a's offset"); near(a.durWall, 0.3, "a's rest"); near(a.env[0][1], 0.6, "a starts 0.2 s into its fade down"); assert.equal(b.atWall, 0); near(b.src, 100.2, "b's offset"); near(b.env[0][1], 0.4, "b starts 0.2 s into its fade up"); // Nothing before the seek is scheduled. assert.equal(scheduleFrom(spans, 5).length, 2); assert.equal(scheduleFrom(spans, 5)[0].id, "b"); }); test("at a speed: wall times divide by the rate, source seconds do not", () => { const spans = audioSpans(planOf(0)); const sch = scheduleFrom(spans, 1, 2); near(sch[0].durWall, 1.5, "a's 3 s at 2x"); near(sch[0].src, 11, "a from source 11"); near(sch[1].atWall, 1.5, "b at 1.5 s of wall"); near(sch[1].durWall, 1.5, "b's 3 s at 2x"); assert.equal(sch[0].atWall + sch[0].durWall, sch[1].atWall); near(cutTimeAtCtx(1, 10, 11.5, 2), 4, "the clock at 2x"); }); test("the render's edges, room, loudness, the clip's knobs and the mute mark shape the level", () => { const cuts = { a: { cutStart: 9.6, cutEnd: 14.4, gainDb: -6, sound: [10, 14] }, b: { cutStart: 100, cutEnd: 103, audio: { gain: 6, fadeIn: 0.5, offset: 200 }, muteFrom: 102 }, }; const spans = audioSpans(planOf(0, cuts)); const [a, b] = spans; assert.deepEqual([a.from, a.to, a.src], [0, 4.8, 9.6]); near(a.gain, 10 ** (-6 / 20), "a's gain"); // Room: silent before source 10 (cut 0.4), its fade in ending there; silent after 14 + fade. near(levelAt(a.env, 0.2), 0, "a's muted head"); near(levelAt(a.env, 0.4), 1, "a heard from its sound's start"); near(levelAt(a.env, 4.4), 1, "a heard to its sound's end"); near(levelAt(a.env, 4.7), 0, "a's muted tail"); // b: +6 dB, fading in over 0.5 s, read 0.2 s earlier (its sound later), silent from source 102. near(b.gain, 10 ** (6 / 20), "b's gain"); near(b.src, 99.8, "b read 0.2 s earlier"); near(levelAt(b.env, b.from), 0, "b fades in"); near(levelAt(b.env, b.from + 0.5), 1, "b in"); near(levelAt(b.env, b.from + 2), 0, "b silent from its mute mark"); near(levelAt(b.env, b.from + 2 - 0.04), 1, "b full until the mute fade"); }); test("a segment longer than its source (whole frames) is silent past the source's end", () => { const spans = audioSpans(planOf(0, { a: { cutStart: 10, cutEnd: 13.99, footage: 4 } })); assert.equal(spans[0].to, 3.99); assert.equal(spans[1].from, 4); }); test("envelopes multiply on the union of their points; a decode span is the sound and a pad", () => { assert.deepEqual(multiply([[[0, 0], [1, 1]], [[0.5, 1], [1.5, 0]]], 0, 2), [[0, 0], [0.5, 0.5], [1, 0.5], [1.5, 0], [2, 0]]); assert.deepEqual(decodeSpanOf({ src: 10, srcTo: 14 }), { from: 9.75, to: 14.25 }); assert.deepEqual(decodeSpanOf({ src: 0.1, srcTo: 400 }, { max: 120 }), { from: 0, to: 120 }); });