Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

TL0c: in progress: a timeline read from a composition's sequences or a film's scenes (sdk/timeline read.ts); seqEdits maps move, trim, split and remove to the host's sequence edits; a move past the film's first frame stops there; 37 tests

shinyobjectz1c3cb953bda2
diff --git a/sdk/timeline/src/index.ts b/sdk/timeline/src/index.ts
index 3235aaf7..e34c6ec3 100644
--- a/sdk/timeline/src/index.ts
+++ b/sdk/timeline/src/index.ts
@@ -5,3 +5,4 @@ export * from "./ops"
export * from "./keys"
export * from "./speed"
export * from "./snap"
+export * from "./read"
diff --git a/sdk/timeline/src/ops.ts b/sdk/timeline/src/ops.ts
index 66b4b8ae..1f9c69a4 100644
--- a/sdk/timeline/src/ops.ts
+++ b/sdk/timeline/src/ops.ts
@@ -114,10 +114,10 @@ function place(t: Timeline, kind: TrackKind, clip: Clip, prefer?: string): Timel
function run(t: Timeline, op: Exclude<Op, { op: "restore" }>): Timeline {
switch (op.op) {
case "move": {
- const by = whole(op.by)
const moving = op.ids.map((id) => open(t, id))
if (!moving.length) no("nothing is picked")
- if (Math.min(...moving.map((m) => m.clip.start)) + by < 0) no("it would start before the film does")
+ // no further back than the film's first frame: a drag past it stops there
+ const by = Math.max(whole(op.by), -Math.min(...moving.map((m) => m.clip.start)))
const ids = new Set(op.ids)
// lifted off their tracks, then each put down where it lands: its own track, the one named, or one with room
let next = withTracks(t, (k) => ({ ...k, clips: k.clips.filter((c) => !ids.has(c.id)) }))
diff --git a/sdk/timeline/src/read.ts b/sdk/timeline/src/read.ts
new file mode 100644
index 00000000..e59446a3
--- /dev/null
+++ b/sdk/timeline/src/read.ts
@@ -0,0 +1,86 @@
+// A timeline read from what is there (docs/timeline.feature TL0c, TL14a): a composition's sequences as the host
+// lists them (raea-feed host/sequences.ts), or a film with no composition as its scenes and its sound. And an
+// operation on a sequences' timeline as the host's own edits to those elements (docs/editing.feature E3b), until
+// every operation is a code edit of its own (edits.ts).
+import { type Clip, empty, end, find, type Frames, type Timeline, type Track } from "./model"
+import type { Op } from "./ops"
+
+/** A <Sequence> as the host lists it: its place among the file's elements, its span in the composition's frames,
+ * the lane it sits on (nested ones below their parent, overlapping ones apart), and why it can't be rewritten. */
+export interface SeqRow {
+ i: number
+ name: string
+ label?: string
+ from: number
+ to: number
+ lane: number
+ locked?: string
+}
+/** The host's edit to a sequence: its new span, a split, a delete. */
+export type SeqEdit = { op: "move"; i: number; from: number; to: number } | { op: "split"; i: number; at: number } | { op: "delete"; i: number }
+
+const seqId = (i: number) => `seq-${i}`
+const seqOf = (id: string): number | undefined => (/^seq-\d+$/.test(id) ? Number(id.slice(4)) : undefined)
+
+/** The composition's sequences as a timeline: a track a lane, the first lane on top as the strip draws it. The first
+ * lane is the main track, its magnet on, only when its clips already run from frame 0 with no gap: a composition's
+ * sequences are where its code puts them, and nothing here moves one that wasn't dragged. */
+export function fromSequences(rows: SeqRow[], fps: number): Timeline {
+ const lanes = [...new Set(rows.map((r) => r.lane))].sort((a, b) => a - b)
+ const clipsOf = (lane: number): Clip[] => rows.filter((r) => r.lane === lane).sort((a, b) => a.from - b.from).map((r) => ({ id: seqId(r.i), kind: "sequence", name: r.label ?? r.name, start: r.from, length: Math.max(1, r.to - r.from), ...(r.locked && { locked: r.locked }) }))
+ const first = lanes.length ? clipsOf(lanes[0]) : []
+ const gapless = first.length > 0 && first.every((c, n) => c.start === (n ? end(first[n - 1]) : 0))
+ const tracks: Track[] = lanes.map((lane, n) => ({ id: n === 0 && gapless ? "main" : `overlay-${lane}`, kind: n === 0 && gapless ? "main" : "overlay", clips: clipsOf(lane) }))
+ // lanes below the first are nested deeper: drawn under it, as the strip has them
+ return { ...empty(fps), tracks, magnet: gapless }
+}
+
+/** A film with no composition behind it: its scenes as clips of one main track (cut where the picture cuts), and
+ * its sound as one clip under them. Its magnet is on: a scene removed closes up, as a cut of footage does. */
+export function fromRender(o: { frames: Frames; fps: number; cuts?: Frames[]; sound?: boolean }): Timeline {
+ const edges = [0, ...[...new Set(o.cuts ?? [])].filter((c) => c > 0 && c < o.frames).sort((a, b) => a - b), o.frames]
+ const scenes: Clip[] = edges.slice(0, -1).map((from, n) => ({ id: `scene-${n + 1}`, kind: "video", name: `Scene ${n + 1}`, start: from, length: edges[n + 1] - from, in: from, source: o.frames }))
+ const tracks: Track[] = [{ id: "main", kind: "main", clips: scenes }]
+ if (o.sound) tracks.push({ id: "sound-1", kind: "sound", clips: [{ id: "sound", kind: "audio", name: "Sound", start: 0, length: o.frames, in: 0, source: o.frames }] })
+ return { ...empty(o.fps), tracks }
+}
+
+/** A render's cut, as the frames of the source played in order (the hand edit's `segments`): what a film's main
+ * track is after its scenes were moved, trimmed, split and removed. */
+export function segmentsOf(t: Timeline): [Frames, Frames][] {
+ const main = t.tracks.find((k) => k.kind === "main")
+ return (main?.clips ?? []).filter((c) => !c.off && c.in !== undefined).map((c) => [c.in!, c.in! + c.length] as [Frames, Frames])
+}
+
+/** What the host is asked to do to the composition's sequences for `op` on timeline `t` (read from them), in order;
+ * or why this operation isn't one the sequences can take yet. Each edit names sequences as they are before it. */
+export function seqEdits(t: Timeline, op: Op): SeqEdit[] | { why: string } {
+ const row = (id: string) => {
+ const i = seqOf(id), at = find(t, id)
+ return i !== undefined && at ? { i, clip: at.clip } : undefined
+ }
+ const each = (ids: string[], f: (r: { i: number; clip: Clip }) => SeqEdit): SeqEdit[] | { why: string } => {
+ const rows = ids.map(row)
+ return rows.every((r) => r) ? rows.map((r) => f(r!)) : { why: "that isn't one of the composition's sequences" }
+ }
+ switch (op.op) {
+ case "move":
+ if (op.track) return { why: "a sequence's lane is its place in the code: it moves in time only" }
+ return each(op.ids, (r) => ({ op: "move", i: r.i, from: r.clip.start + Math.round(op.by), to: end(r.clip) + Math.round(op.by) }))
+ case "trim":
+ return each([op.id], (r) => (op.edge === "start" ? { op: "move", i: r.i, from: Math.min(end(r.clip) - 1, Math.max(0, Math.round(op.to))), to: end(r.clip) } : { op: "move", i: r.i, from: r.clip.start, to: Math.max(r.clip.start + 1, Math.round(op.to)) }))
+ case "split": {
+ const at = Math.round(op.at)
+ const ids = op.ids ?? t.tracks.flatMap((k) => k.clips.filter((c) => c.start < at && at < end(c) && !c.locked).map((c) => c.id))
+ // a split makes a sequence after the one it cuts, so later ones are named first
+ const r = each(ids, (x) => ({ op: "split", i: x.i, at }))
+ return Array.isArray(r) ? r.sort((a, b) => b.i - a.i) : r
+ }
+ case "remove": {
+ const r = each(op.ids, (x) => ({ op: "delete", i: x.i }))
+ return Array.isArray(r) ? r.sort((a, b) => b.i - a.i) : r
+ }
+ default:
+ return { why: `the composition's sequences don't take "${op.op}" yet` }
+ }
+}
diff --git a/sdk/timeline/test/read.test.ts b/sdk/timeline/test/read.test.ts
new file mode 100644
index 00000000..8c54b8b1
--- /dev/null
+++ b/sdk/timeline/test/read.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, test } from "bun:test"
+import { apply, fromRender, fromSequences, segmentsOf, seqEdits, type Timeline } from "../src"
+
+const done = (t: Timeline, op: Parameters<typeof apply>[1]) => {
+ const r = apply(t, op)
+ if (!r.ok) throw new Error(r.why)
+ return r.timeline
+}
+
+describe("TL0c: a timeline is read from a composition", () => {
+ const rows = [
+ { i: 0, name: "Sequence", label: "First scene", from: 0, to: 90, lane: 0 },
+ { i: 3, name: "Sequence", label: "Second scene", from: 90, to: 180, lane: 0 },
+ { i: 1, name: "Sequence", label: "Title", from: 15, to: 60, lane: 1 },
+ { i: 5, name: "Sequence", label: "Outro", from: 150, to: 180, lane: 1, locked: "set in code" },
+ ]
+
+ test("a lane is a track; a first lane with no gap from frame 0 is the main track, its magnet on", () => {
+ const t = fromSequences(rows, 30)
+ expect(t.tracks.map((k) => [k.id, k.kind, k.clips.map((c) => `${c.name} ${c.start}+${c.length}`)])).toEqual([
+ ["main", "main", ["First scene 0+90", "Second scene 90+90"]],
+ ["overlay-1", "overlay", ["Title 15+45", "Outro 150+30"]],
+ ])
+ expect(t.magnet).toBe(true)
+ expect(t.tracks[1].clips[1].locked).toBe("set in code")
+ // a gap in the first lane: it is where the code put it, no magnet
+ const gapped = fromSequences([{ ...rows[0], to: 80 }, rows[1]], 30)
+ expect([gapped.magnet, gapped.tracks[0].kind]).toEqual([false, "overlay"])
+ })
+
+ test("an operation on it is the host's edit to those elements", () => {
+ const t = fromSequences(rows, 30)
+ expect(seqEdits(t, { op: "move", ids: ["seq-1"], by: 10 })).toEqual([{ op: "move", i: 1, from: 25, to: 70 }])
+ expect(seqEdits(t, { op: "trim", id: "seq-3", edge: "start", to: 100 })).toEqual([{ op: "move", i: 3, from: 100, to: 180 }])
+ expect(seqEdits(t, { op: "trim", id: "seq-0", edge: "end", to: 75 })).toEqual([{ op: "move", i: 0, from: 0, to: 75 }])
+ // every clip under the playhead that can be changed, the later one first
+ expect(seqEdits(t, { op: "split", at: 45 })).toEqual([{ op: "split", i: 1, at: 45 }, { op: "split", i: 0, at: 45 }])
+ expect(seqEdits(t, { op: "remove", ids: ["seq-0", "seq-3"] })).toEqual([{ op: "delete", i: 3 }, { op: "delete", i: 0 }])
+ expect(seqEdits(t, { op: "key", id: "seq-0", prop: "scale", at: 0, value: 1 })).toEqual({ why: 'the composition\'s sequences don\'t take "key" yet' })
+ })
+})
+
+describe("TL14a: a film with no composition is its scenes and its sound", () => {
+ test("scenes cut where the picture cuts, and the cut they make after edits", () => {
+ const t = fromRender({ frames: 300, fps: 30, cuts: [90, 210, 210, 400], sound: true })
+ expect(t.tracks[0].clips.map((c) => [c.id, c.start, c.length])).toEqual([["scene-1", 0, 90], ["scene-2", 90, 120], ["scene-3", 210, 90]])
+ expect(t.tracks[1].clips[0]).toMatchObject({ kind: "audio", length: 300 })
+ expect(segmentsOf(t)).toEqual([[0, 90], [90, 210], [210, 300]])
+ // the second scene removed, the third trimmed by 30 frames at its start, then moved first
+ let cut = done(t, { op: "remove", ids: ["scene-2"] })
+ cut = done(cut, { op: "trim", id: "scene-3", edge: "start", to: 120 })
+ cut = done(cut, { op: "move", ids: ["scene-3"], by: -120 })
+ expect(segmentsOf(cut)).toEqual([[240, 300], [0, 90]])
+ })
+})