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.

TL10a: in progress: an edited film plays from its own frames (sdk/timeline play.ts)

shinyobjectz08f6f621d26d
diff --git a/sdk/timeline/src/index.ts b/sdk/timeline/src/index.ts
index 39be912f..041796dd 100644
--- a/sdk/timeline/src/index.ts
+++ b/sdk/timeline/src/index.ts
@@ -8,3 +8,4 @@ export * from "./snap"
export * from "./read"
export * from "./cache"
export * from "./registry"
+export * from "./play"
diff --git a/sdk/timeline/src/play.ts b/sdk/timeline/src/play.ts
new file mode 100644
index 00000000..ff395b7a
--- /dev/null
+++ b/sdk/timeline/src/play.ts
@@ -0,0 +1,64 @@
+// Playing an edited timeline of footage without rendering it (docs/timeline.feature TL10a, for a film's own
+// frames): which source frame shows at a timeline frame, where the player goes when a clip ends, and which
+// timeline ranges are ready given the source ranges that are (the player's buffer). The main track is the picture.
+import type { Range } from "./cache"
+import { joined } from "./cache"
+import { type Clip, end, type Frames, speedOf, type Timeline } from "./model"
+import { played, sourceAt as curveAt } from "./speed"
+
+const main = (t: Timeline): Clip[] => (t.tracks.find((k) => k.kind === "main" && !k.hidden)?.clips ?? []).filter((c) => !c.off && c.in !== undefined).sort((a, b) => a.start - b.start)
+
+export interface Showing {
+ clip: Clip
+ /** the source frame to show */
+ source: number
+ /** how fast the source runs there (the player's rate) */
+ rate: number
+}
+
+/** What shows at timeline frame `frame`: the main-track clip over it, the source frame, and the rate. Undefined
+ * in a gap or past the end. */
+export function showing(t: Timeline, frame: Frames): Showing | undefined {
+ const c = main(t).find((x) => x.start <= frame && frame < end(x))
+ if (!c) return undefined
+ const into = frame - c.start
+ if (c.curve) {
+ const span = c.shows ?? c.length / played(c.curve, 1)
+ const at = curveAt(c.curve, span, into)
+ // the rate at that place: the source covered by the next frame
+ return { clip: c, source: c.in! + at, rate: Math.max(0.1, curveAt(c.curve, span, into + 1) - at) }
+ }
+ return { clip: c, source: c.in! + into * speedOf(c), rate: speedOf(c) }
+}
+
+/** The timeline frame at which clip `id` shows source frame `source`, when it does (a constant-speed clip). */
+export function frameOf(t: Timeline, id: string, source: number): Frames | undefined {
+ const c = main(t).find((x) => x.id === id)
+ if (!c || c.curve) return undefined
+ const f = c.start + (source - c.in!) / speedOf(c)
+ return f >= c.start - 0.5 && f < end(c) + 0.5 ? Math.min(end(c), Math.max(c.start, f)) : undefined
+}
+
+/** Where the player goes when what it is playing ends at timeline frame `frame`: the next clip's first frame (at
+ * or after it), or undefined at the film's end. */
+export function next(t: Timeline, frame: Frames): Frames | undefined {
+ return main(t).find((x) => x.start >= frame)?.start
+}
+
+/** The timeline's length as it plays: its main track's end. */
+export const playLength = (t: Timeline): Frames => Math.max(0, ...main(t).map(end))
+
+/** The timeline ranges that are ready to play, given the source ranges that are (frames of the file the player has
+ * buffered): each main-track clip's part whose source is in one of them. */
+export function ready(t: Timeline, source: Range[]): Range[] {
+ const out: Range[] = []
+ for (const c of main(t)) {
+ if (c.curve) continue
+ const rate = speedOf(c), from = c.in!, to = c.in! + c.length * rate
+ for (const [a, b] of source) {
+ const lo = Math.max(a, from), hi = Math.min(b, to)
+ if (hi > lo) out.push([Math.floor(c.start + (lo - from) / rate), Math.ceil(c.start + (hi - from) / rate)])
+ }
+ }
+ return joined(out)
+}
diff --git a/sdk/timeline/test/play.test.ts b/sdk/timeline/test/play.test.ts
new file mode 100644
index 00000000..fb107f39
--- /dev/null
+++ b/sdk/timeline/test/play.test.ts
@@ -0,0 +1,48 @@
+import { describe, expect, test } from "bun:test"
+import { apply, frameOf, fromRender, next, playLength, ready, showing, 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
+}
+// a 10 s film at 30 fps cut at 3 s and 6 s
+const film = () => fromRender({ frames: 300, fps: 30, cuts: [90, 180], sound: true })
+
+describe("TL10a: an edited film plays from its own frames, with no render", () => {
+ test("unedited, each frame shows itself", () => {
+ const t = film()
+ expect(showing(t, 0)).toMatchObject({ source: 0, rate: 1 })
+ expect(showing(t, 200)).toMatchObject({ source: 200, rate: 1 })
+ expect(showing(t, 300)).toBeUndefined()
+ expect(playLength(t)).toBe(300)
+ })
+
+ test("a scene removed: the film is shorter and plays on from the next scene's frames", () => {
+ const t = done(film(), { op: "remove", ids: ["scene-2"] })
+ expect(playLength(t)).toBe(210)
+ expect(showing(t, 89)).toMatchObject({ source: 89 })
+ expect(showing(t, 90)).toMatchObject({ source: 180, clip: { id: "scene-3" } })
+ // the player, playing scene 1, reaches its end at frame 90: it goes to the next clip there
+ expect(next(t, 90)).toBe(90)
+ expect(next(t, 210)).toBeUndefined()
+ })
+
+ test("scenes swapped and one sped up: source frames, the rate, and the way back from the player's time", () => {
+ let t = done(film(), { op: "move", ids: ["scene-1"], by: 100 })
+ expect(t.tracks[0].clips.map((c) => c.id)).toEqual(["scene-2", "scene-1", "scene-3"])
+ expect(showing(t, 0)).toMatchObject({ source: 90 })
+ expect(showing(t, 100)).toMatchObject({ source: 10, clip: { id: "scene-1" } })
+ t = done(t, { op: "set", id: "scene-1", set: { speed: 2 } })
+ expect(showing(t, 100)).toMatchObject({ source: 20, rate: 2 })
+ expect(frameOf(t, "scene-1", 20)).toBe(100)
+ expect(frameOf(t, "scene-1", 250)).toBeUndefined()
+ })
+
+ test("what is ready to play follows the clips: the file's buffered frames, where the edit put them", () => {
+ const t = done(film(), { op: "remove", ids: ["scene-2"] })
+ // the file is buffered from 0 to 4 s and 7 to 10 s
+ expect(ready(t, [[0, 120], [210, 300]])).toEqual([[0, 90], [120, 210]])
+ expect(ready(film(), [[0, 300]])).toEqual([[0, 300]])
+ })
+})