// pauses.mjs: where the quiet is in a stretch of speech, and an edge moved // into it -- forward for an end (through its sentence), back for a start, // to the nearest for the clip editor; bounded, and said. import assert from "node:assert/strict"; import test from "node:test"; import { MIN_WORD, PAUSE_MIN_GAP, SNAP_HARD_ROOM, SNAP_MAX_SHIFT, endsSentence, normWords, pausePoints, pauseSpans, snapEdge, wordGaps, } from "./pauses.mjs"; const close = (a, b, eps = 1e-3) => assert.ok(Math.abs(a - b) <= eps, `${a} ≉ ${b}`); test("the constants: a pause is the build's silencedetect minimum, an edge moves at most a second", () => { assert.equal(PAUSE_MIN_GAP, 0.09); assert.equal(SNAP_MAX_SHIFT, 1); assert.equal(SNAP_HARD_ROOM, 0.1); assert.equal(MIN_WORD, 0.1); }); test("endsSentence: . ? ! with closing quotes or brackets after; a comma does not", () => { for (const t of ["man.", "it?", "stop!", 'said."', "end.)", "so.” "]) assert.ok(endsSentence(t), t); for (const t of ["So,", "[ __ ]", "every", "", null]) assert.ok(!endsSentence(t), String(t)); }); test("wordGaps: gaps between TIMED words at least minGap long; start-only words add none", () => { const ws = [{ start: 0, end: 0.4 }, { start: 0.45, end: 0.9 }, { start: 1.3, end: 1.6 }, { start: 1.6, end: 2 }]; assert.deepEqual(wordGaps(ws, PAUSE_MIN_GAP), [{ from: 0.9, to: 1.3 }]); assert.equal(wordGaps(ws, 0).length, 3, "every gap, the touching one too"); assert.deepEqual(wordGaps([{ start: 0 }, { start: 1 }], 0), []); }); test("pauseSpans: silences ({s,e} or {from,to}) and word gaps, merged where they touch, in range", () => { const p = pauseSpans({ silences: [{ s: 5, e: 5.4 }, { from: 1, to: 1.2 }, { s: 5.3, e: 5.6 }, { s: 9, e: 8 }], words: [{ start: 0, end: 1.1 }, { start: 1.5, end: 2 }, { start: 3, end: 3.5 }], }); assert.deepEqual(p, [{ from: 1, to: 1.5 }, { from: 2, to: 3 }, { from: 5, to: 5.6 }]); assert.deepEqual(pauseSpans({ silences: [{ s: 1, e: 2 }, { s: 7, e: 8 }] }, { from: 3, to: 9 }), [{ from: 7, to: 8 }]); }); test("pausePoints: the middle of every word gap (touching words too) and silence, deduplicated within 20 ms", () => { const pts = pausePoints({ words: [{ start: 0, end: 1 }, { start: 1, end: 2 }, { start: 2.5, end: 3 }], silences: [{ from: 2.1, to: 2.4 }] }, 0, 10); assert.deepEqual(pts, [1, 2.25]); }); test("normWords: w / word / text, starts in order, an end only when there is one", () => { assert.deepEqual(normWords([{ w: "b", start: 2, end: 2.3 }, { word: "a", start: 1 }, { text: "x", start: Number.NaN }]), [ { start: 1, text: "a" }, { start: 2, end: 2.3, text: "b" }, ]); }); // The speech of the tests below: "... every [ __ ] type. You don't ..." -- // a pause between "[ __ ]" and "type." and another after "type.". const WORDS = [ { start: 9.28, text: "every" }, { start: 10.24, text: "[ __ ]" }, { start: 11.4, text: "type." }, { start: 12.16, text: "You" }, { start: 12.32, text: "don't" }, ]; const PAUSES = [{ from: 10.9, to: 11.44 }, { from: 11.8, to: 12.26 }]; test("an end is carried through its sentence when it ends within maxShift, into the pause after it, at from + room", () => { const r = snapEdge(11.2, "end", { pauses: PAUSES, words: WORDS, room: 0.3 }); assert.equal(r.moved, true); // 11.8 + 0.3 = 12.1, under the pause's end and "You" (12.16). close(r.at, 12.1); assert.equal(r.word, "type."); assert.equal(r.sentence, true); // Without the words it is already in quiet: it stays. const bare = snapEdge(11.2, "end", { pauses: PAUSES, room: 0.3 }); assert.equal(bare.moved, false); assert.equal(bare.reason, "fits"); }); test("an end never passes the next word's start, nor the pause's end", () => { // A pause shorter than the fade: the edge goes to its end. const short = snapEdge(1.05, "end", { pauses: [{ from: 1.1, to: 1.25 }], words: [{ start: 0.6, text: "man." }, { start: 1.3, text: "So," }], room: 0.3 }); close(short.at, 1.25); // A word the captions start inside the pause: the edge stops there. const capped = snapEdge(1.05, "end", { pauses: [{ from: 1.1, to: 1.6 }], words: [{ start: 0.6, text: "is." }, { start: 1.3, text: "Stop" }], room: 0.3 }); close(capped.at, 1.3); }); test("a pause that starts before the last word has sounded (MIN_WORD) is the quiet before it, not after", () => { // "man." starts at 28.48; a dip at 28.53 is inside it, so the edge goes to the pause after "So,". const r = snapEdge(28.57, "end", { pauses: [{ from: 28.53, to: 28.63 }, { from: 28.76, to: 28.9 }], words: [{ start: 28.48, text: "man." }, { start: 28.64, text: "So," }, { start: 28.88, text: "I'm" }], room: 0.3, }); close(r.at, 28.88); assert.equal(r.word, "So,"); }); test("bounded: more than maxShift away stays (far); past hi stops at hi inside the pause, or stays (bound)", () => { const far = snapEdge(10, "end", { pauses: [{ from: 11.2, to: 12 }], room: 0.3 }); assert.deepEqual([far.moved, far.reason, far.at], [false, "far", 10]); const held = snapEdge(10, "end", { pauses: [{ from: 10.2, to: 11 }], room: 0.3, hi: 10.35 }); assert.deepEqual([held.moved, held.bounded, held.at], [true, true, 10.35]); const bound = snapEdge(10, "end", { pauses: [{ from: 10.5, to: 11 }], room: 0.3, hi: 10.4 }); assert.deepEqual([bound.moved, bound.reason], [false, "bound"]); const none = snapEdge(10, "end", { pauses: [{ from: 8, to: 9 }], room: 0.3 }); assert.equal(none.reason, "none"); }); test("a start goes back into the last pause before it, at to − room, bounded by lo", () => { const ps = [{ from: 6.4, to: 6.85 }]; const r = snapEdge(6.78, "start", { pauses: ps, words: [{ start: 6.72, text: "I" }, { start: 7.04, text: "am" }], room: 0.3 }); close(r.at, 6.55); assert.equal(r.word, "I"); // Room enough after it already: it stays. assert.equal(snapEdge(6.5, "start", { pauses: ps, room: 0.3 }).reason, "fits"); // lo: stops there inside the pause. const lo = snapEdge(6.78, "start", { pauses: ps, room: 0.3, lo: 6.7 }); assert.deepEqual([lo.at, lo.bounded], [6.7, true]); assert.equal(snapEdge(6.78, "start", { pauses: [{ from: 5, to: 5.4 }], room: 0.3 }).reason, "far"); }); test("nearest: the middle of the pause nearest the edge, within maxShift and the bounds (the clip editor's snap)", () => { const ps = [{ from: 1, to: 1.2 }, { from: 2, to: 2.6 }, { from: 5, to: 5.2 }]; close(snapEdge(1.9, "nearest", { pauses: ps }).at, 2.3); close(snapEdge(1.4, "nearest", { pauses: ps }).at, 1.1); assert.equal(snapEdge(2.1, "nearest", { pauses: ps }).reason, "fits"); assert.equal(snapEdge(3.6, "nearest", { pauses: ps, maxShift: 1 }).reason, "far"); close(snapEdge(1.4, "nearest", { pauses: ps, lo: 1.5 }).at, 2.3); });