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.

TL6b: in progress: a clip under a speed curve keeps how many source frames it shows, so the curve taken off gives its exact length back (every preset, tested); a trimmed or split curve clip keeps its share

shinyobjectzb2a52f6cc1b7
diff --git a/sdk/timeline/src/model.ts b/sdk/timeline/src/model.ts
index 6c3f694a..2fc3da28 100644
--- a/sdk/timeline/src/model.ts
+++ b/sdk/timeline/src/model.ts
@@ -48,6 +48,8 @@ export interface Clip {
/** a constant speed (1 when unset), or a curve, which wins (TL6) */
speed?: number
curve?: SpeedPoint[]
+ /** under a curve: how many source frames the clip shows (its length is the time the curve takes over them) */
+ shows?: Frames
/** turned off: drawn dimmed, left out of the picture and the sound (TL3g) */
off?: boolean
/** it can't be changed here, and why ("set in code", TL0c) */
diff --git a/sdk/timeline/src/ops.ts b/sdk/timeline/src/ops.ts
index 4febe0b4..be6bde43 100644
--- a/sdk/timeline/src/ops.ts
+++ b/sdk/timeline/src/ops.ts
@@ -89,7 +89,7 @@ function fresh(t: Timeline, id: string, taken: Set<string> = new Set()): string
return n
}
/** How many source frames a clip shows. */
-const shown = (c: Clip): number => (c.curve ? c.length / played(c.curve, 1) : c.length * speedOf(c))
+const shown = (c: Clip): number => (c.curve ? (c.shows ?? c.length / played(c.curve, 1)) : c.length * speedOf(c))
const isMain = (k: Track) => k.kind === "main"
/** `clip` on the timeline: on `prefer` if it has room there, else the first track of `kind` with room, else a new
@@ -138,12 +138,12 @@ function run(t: Timeline, op: Exclude<Op, { op: "restore" }>): Timeline {
const start = Math.min(end(clip) - 1, Math.max(to, least, before, 0))
const d = start - clip.start
if (!d) return t
- return change(t, clip.id, (c) => ({ ...c, start, length: c.length - d, ...(c.in !== undefined && { in: Math.max(0, c.in + Math.round(d * speed)) }), ...(c.keys && { keys: shiftKeys(c.keys, d) }), ...(c.markers && { markers: c.markers.map((m) => ({ ...m, at: m.at - d })).filter((m) => m.at >= 0) }) }))
+ return change(t, clip.id, (c) => ({ ...c, start, length: c.length - d, ...(c.shows !== undefined && { shows: (c.shows * (c.length - d)) / c.length }), ...(c.in !== undefined && { in: Math.max(0, c.in + Math.round(d * speed)) }), ...(c.keys && { keys: shiftKeys(c.keys, d) }), ...(c.markers && { markers: c.markers.map((m) => ({ ...m, at: m.at - d })).filter((m) => m.at >= 0) }) }))
}
const most = clip.source === undefined || clip.curve ? Infinity : clip.start + Math.floor((clip.source - (clip.in ?? 0)) / speed)
const after = isMain(track) && t.magnet ? Infinity : Math.min(Infinity, ...track.clips.filter((c) => c.id !== clip.id && c.start >= end(clip)).map((c) => c.start))
const stop = Math.max(clip.start + 1, Math.min(to, most, after))
- return stop === end(clip) ? t : change(t, clip.id, (c) => ({ ...c, length: stop - c.start }))
+ return stop === end(clip) ? t : change(t, clip.id, (c) => ({ ...c, length: stop - c.start, ...(c.shows !== undefined && { shows: (c.shows * (stop - c.start)) / c.length }) }))
}
case "split": {
const at = whole(op.at)
@@ -201,7 +201,7 @@ function run(t: Timeline, op: Exclude<Op, { op: "restore" }>): Timeline {
if (rest.speed !== undefined && !(rest.speed > 0)) no("a speed is more than 0")
const points = curve === undefined ? clip.curve : (curve ?? undefined)
// the same frames of source, at the new pace
- c = { ...c, curve: points, length: Math.max(1, whole(points ? played(points, source) : source / speedOf(c))) }
+ c = { ...c, curve: points, shows: points ? source : undefined, length: Math.max(1, whole(points ? played(points, source) : source / speedOf(c))) }
}
return change(t, clip.id, () => c)
}
@@ -271,8 +271,9 @@ function halves(c: Clip, at: Frames, id: string): [Clip, Clip] {
const ks = Object.fromEntries(parts.filter(([, h]) => h[i].length).map(([p, h]) => [p, h[i]]))
return Object.keys(ks).length ? ks : undefined
}
- const left: Clip = { ...c, length: d, fadeOut: undefined, keys: keysOf(0), markers: c.markers?.filter((m) => m.at < d) }
- const right: Clip = { ...c, id, start: at, length: c.length - d, fadeIn: undefined, keys: keysOf(1), markers: c.markers?.filter((m) => m.at >= d).map((m) => ({ ...m, at: m.at - d })), ...(c.in !== undefined && { in: c.in + Math.round(d * speedOf(c)) }) }
+ const share = (len: number) => (c.shows === undefined ? {} : { shows: (c.shows * len) / c.length })
+ const left: Clip = { ...c, ...share(d), length: d, fadeOut: undefined, keys: keysOf(0), markers: c.markers?.filter((m) => m.at < d) }
+ const right: Clip = { ...c, ...share(c.length - d), id, start: at, length: c.length - d, fadeIn: undefined, keys: keysOf(1), markers: c.markers?.filter((m) => m.at >= d).map((m) => ({ ...m, at: m.at - d })), ...(c.in !== undefined && { in: c.in + Math.round(d * speedOf(c)) }) }
return [left, right]
}
diff --git a/sdk/timeline/test/ops.test.ts b/sdk/timeline/test/ops.test.ts
index a60c52cb..f105b46e 100644
--- a/sdk/timeline/test/ops.test.ts
+++ b/sdk/timeline/test/ops.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test"
-import { apply, applyAll, type Clip, empty, find, length, type Op, type Timeline, valueAt } from "../src"
+import { apply, applyAll, type Clip, CURVES, empty, find, length, type Op, type Timeline, valueAt } from "../src"
const clip = (id: string, start: number, len: number, more: Partial<Clip> = {}): Clip => ({ id, kind: "video", start, length: len, ...more })
/** a main track of three clips (a 0-30, b 30-90, c 90-120), a title over b, music under all of it */
@@ -162,5 +162,11 @@ describe("TL6: speed", () => {
const t = done(film(), { op: "set", id: "b", set: { curve: [{ at: 0, speed: 2 }, { at: 1, speed: 2 }] } })
expect(at(t, "b").length).toBe(30)
expect(at(done(t, { op: "set", id: "b", set: { curve: null } }), "b").length).toBe(60)
+ // every preset, on and off again: the clip is exactly as long as it was
+ for (const [name, curve] of Object.entries(CURVES)) {
+ const on = done(film(), { op: "set", id: "b", set: { curve } })
+ expect(at(done(on, { op: "set", id: "b", set: { curve: null } }), "b").length, name).toBe(60)
+ expect(at(done(on, { op: "set", id: "b", set: { curve: null } }), "b").shows).toBeUndefined()
+ }
})
})