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.

TL0d: in progress: a timeline an item declares is read and checked (sdk/timeline readTimeline): tracks of clips over an item's own data, made whole (whole frames, a kind for each track), with why for what isn't well formed; 59 tests

shinyobjectzc4d3900d68ee
diff --git a/sdk/timeline/src/read.ts b/sdk/timeline/src/read.ts
index e327f5ed..1eddb142 100644
--- a/sdk/timeline/src/read.ts
+++ b/sdk/timeline/src/read.ts
@@ -235,3 +235,45 @@ export function clipEdits(read: Timeline, now: Timeline): { id: string; src: str
}
return out
}
+
+// ---------------------------------------------------------------- a timeline an item declares (TL0d)
+
+/** A timeline as an agent wrote it on a feed item or a controller's field (its own data: a schedule, a set list, a
+ * shot plan), checked and made whole: what is well formed, and why the rest isn't. Frames are rounded to whole
+ * ones; a track with no kind is an overlay; ids must be there and not repeat. */
+export function readTimeline(raw: unknown): { timeline?: Timeline; errors: string[] } {
+ const errors: string[] = []
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { errors: ["a timeline is an object: { fps, tracks: [{ id, kind, clips: [{ id, start, length }] }] }"] }
+ const r = raw as Record<string, unknown>
+ const fps = typeof r.fps === "number" && r.fps > 0 ? r.fps : 30
+ if (r.fps !== undefined && fps !== r.fps) errors.push("fps is a number above 0 (30 was taken)")
+ if (!Array.isArray(r.tracks)) return { errors: [...errors, "tracks is a list of { id, kind, clips }"] }
+ const seen = new Set<string>()
+ const whole = (n: unknown) => (typeof n === "number" && Number.isFinite(n) ? Math.round(n) : NaN)
+ const tracks: Track[] = []
+ r.tracks.forEach((k, n) => {
+ const t = (k ?? {}) as Record<string, unknown>
+ const id = typeof t.id === "string" && t.id ? t.id : `track-${n + 1}`
+ if (tracks.some((o) => o.id === id)) return void errors.push(`tracks[${n}]: the id ${id} is used twice`)
+ const clips: Clip[] = []
+ for (const [m, c] of (Array.isArray(t.clips) ? t.clips : []).entries()) {
+ const x = (c ?? {}) as Record<string, unknown>
+ const at = `tracks[${n}].clips[${m}]`
+ const start = whole(x.start), len = whole(x.length)
+ if (typeof x.id !== "string" || !x.id) errors.push(`${at} has no id`)
+ else if (seen.has(x.id)) errors.push(`${at}: the id ${x.id} is used twice`)
+ else if (!(start >= 0) || !(len >= 1)) errors.push(`${at} (${x.id}) starts at 0 or later and is one frame or longer`)
+ else {
+ seen.add(x.id)
+ clips.push({ ...(x as unknown as Clip), id: x.id, kind: typeof x.kind === "string" ? x.kind : "clip", start, length: len })
+ }
+ }
+ tracks.push({ id, kind: typeof t.kind === "string" ? t.kind : "overlay", ...(typeof t.name === "string" && { name: t.name }), ...(t.locked === true && { locked: true }), clips: clips.sort((a, b) => a.start - b.start) })
+ })
+ const markers = (Array.isArray(r.markers) ? r.markers : []).flatMap((m, n) => {
+ const x = (m ?? {}) as Record<string, unknown>
+ return whole(x.at) >= 0 ? [{ id: typeof x.id === "string" ? x.id : `m${n + 1}`, at: whole(x.at), ...(typeof x.name === "string" && { name: x.name }), ...(typeof x.color === "string" && { color: x.color }) }] : []
+ })
+ // its data is where the agent put it: no magnet unless it says so
+ return { timeline: { ...empty(fps), tracks, markers, magnet: r.magnet === true, snapping: r.snapping !== false, linkage: r.linkage === true }, errors }
+}
diff --git a/sdk/timeline/test/read.test.ts b/sdk/timeline/test/read.test.ts
index b9d9d6d8..c9dc8d83 100644
--- a/sdk/timeline/test/read.test.ts
+++ b/sdk/timeline/test/read.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test"
-import { apply, clipEdits, fromRender, fromSequences, fromTracks, segmentsOf, seqEdits, type Timeline } from "../src"
+import { apply, clipEdits, fromRender, fromSequences, fromTracks, readTimeline, segmentsOf, seqEdits, type Timeline } from "../src"
const done = (t: Timeline, op: Parameters<typeof apply>[1]) => {
const r = apply(t, op)
@@ -123,3 +123,32 @@ describe("TL14b: a sound in the feed is its tracks", () => {
expect(clipEdits(t, t)).toEqual([])
})
})
+
+describe("TL0d: a feed item or a controller declares a timeline of its own data", () => {
+ test("what is well formed is a timeline the editor takes; the rest is said", () => {
+ const { timeline, errors } = readTimeline({
+ fps: 1,
+ tracks: [
+ { id: "stage", kind: "main", name: "Main stage", clips: [{ id: "doors", name: "Doors", start: 0, length: 1800 }, { id: "set-1", name: "First set", start: 1800.4, length: 2700 }] },
+ { name: "Bar", clips: [{ id: "happy", start: 0, length: 3600, props: { price: 5 } }, { id: "doors", start: 10, length: 5 }, { start: 0, length: 5 }, { id: "bad", start: -5, length: 0 }] },
+ ],
+ markers: [{ at: 1800, name: "Curfew" }],
+ })
+ expect(timeline!.fps).toBe(1)
+ expect(timeline!.tracks.map((k) => [k.id, k.kind, k.name, k.clips.map((c) => `${c.id} ${c.start}+${c.length}`)])).toEqual([
+ ["stage", "main", "Main stage", ["doors 0+1800", "set-1 1800+2700"]],
+ ["track-2", "overlay", "Bar", ["happy 0+3600"]],
+ ])
+ expect(timeline!.tracks[1].clips[0].props).toEqual({ price: 5 })
+ expect(timeline!.markers).toEqual([{ id: "m1", at: 1800, name: "Curfew" }])
+ expect(timeline!.magnet).toBe(false)
+ expect(errors).toEqual(["tracks[1].clips[1]: the id doors is used twice", "tracks[1].clips[2] has no id", "tracks[1].clips[3] (bad) starts at 0 or later and is one frame or longer"])
+ // and it is edited as any other
+ expect(done(timeline!, { op: "move", ids: ["set-1"], by: 600 }).tracks[0].clips[1].start).toBe(2400)
+ })
+
+ test("what isn't a timeline says what one is", () => {
+ expect(readTimeline("x").errors[0]).toContain("a timeline is an object")
+ expect(readTimeline({ fps: 30 })).toEqual({ errors: ["tracks is a list of { id, kind, clips }"] })
+ })
+})