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.

TL4c: footage's own sound taken out onto a sound track (Cmd+Shift+S in CapCut). sdk/timeline gains the extract operation: a sound clip over the same span, from the same place in the file at the same speed, curve, direction, level and fades (props.of names the footage it came from), the footage marked muted with its level and fades moved to the sound; refused for sound, words, stills and footage already taken out. A clip's muted is read from a muted <Video> (SeqRow.muted), and seqEdits turns the operation into the host's extract edit (the <Video> muted, an <Audio> of its file in a new sequence from at for frames); a sequence holding footage is pointed at its <Video>. The frame cache's look says muted only when set, so kept ranges' keys are unchanged. Reference page regenerated (Clip.muted, SeqRow.muted). Tests: four for the operation and its inverse, one for the composition's edit and its reading back (75 pass)

shinyobjectz173e397113d0
diff --git a/docs/manual/reference/sdk/editor.md b/docs/manual/reference/sdk/editor.md
index 88130c86..401bb549 100644
--- a/docs/manual/reference/sdk/editor.md
+++ b/docs/manual/reference/sdk/editor.md
@@ -542,6 +542,7 @@ A span of something on a track: footage, a sound, words, a composition's sequenc
| `reverse` | `boolean` | footage played backward: its last source frame first |
| `freeze` | `boolean` | a still: the source frame `in` held for the clip's whole length |
| `off` | `boolean` | turned off: drawn dimmed, left out of the picture and the sound |
+| `muted` | `boolean` | footage whose own sound was taken out onto a sound track: its picture plays silent |
| `locked` | `string` | it can't be changed here, and why ("set in code", TL0c) |
| `group` | `string` | clips with one group pick and move as one |
| `props` | `Record<string, Value \| boolean>` | its still values by property (x, y, scale, rotate, opacity, text, a plugin's): a keyed one is ignored |
@@ -711,6 +712,7 @@ the lane it sits on (nested ones below their parent, overlapping ones apart), an
| `media` | `"video" \| "audio"` | a <Video> or an <Audio> of its own: it plays where the sequence it is in does, from `startFrom` of its file, at its own level |
| `startFrom` | `number` | its media's first frame used, in frames of its file |
| `volume` | `number` | its media's level, 1 as recorded |
+| `muted` | `boolean` | a <Video> whose own sound was taken out (muted): its sound is an <Audio> of its own |
### `Showing`
diff --git a/sdk/timeline/src/cache.ts b/sdk/timeline/src/cache.ts
index fd92650c..e0082ae7 100644
--- a/sdk/timeline/src/cache.ts
+++ b/sdk/timeline/src/cache.ts
@@ -33,7 +33,8 @@ function look(k: Track, n: number, c: Clip): string {
const phase = still ? `p${c.in! - c.start * (c.speed && c.speed > 0 ? c.speed : 1)}` : `s${c.start}+${c.length}`
const keys = Object.entries(c.keys ?? {}).map(([p, ks]) => [p, ks.map((x) => [x.at + c.start, x.value, x.ease ?? "linear"])])
const fades = [c.fadeIn ? [c.start, c.fadeIn] : 0, c.fadeOut ? [end(c), c.fadeOut] : 0]
- return JSON.stringify([n, k.kind, c.kind, c.freeze ? `f${c.in}` : c.reverse ? `r${c.start}+${c.length}:${c.in}` : phase, c.speed ?? 1, c.curve ?? null, c.shows ?? null, c.props ?? null, keys, c.volume ?? 1, fades, c.at ?? null])
+ // footage whose sound was taken out says so only then, so what was kept before it was a field still matches
+ return JSON.stringify([n, k.kind, c.kind, c.freeze ? `f${c.in}` : c.reverse ? `r${c.start}+${c.length}:${c.in}` : phase, c.speed ?? 1, c.curve ?? null, c.shows ?? null, c.props ?? null, keys, c.volume ?? 1, fades, c.at ?? null, ...(c.muted ? ["muted"] : [])])
}
/** Every drawn thing on the timeline as the span it covers and what it draws there. */
diff --git a/sdk/timeline/src/model.ts b/sdk/timeline/src/model.ts
index abcfb838..1a042aa4 100644
--- a/sdk/timeline/src/model.ts
+++ b/sdk/timeline/src/model.ts
@@ -74,6 +74,8 @@ export interface Clip {
freeze?: boolean
/** turned off: drawn dimmed, left out of the picture and the sound (TL3g) */
off?: boolean
+ /** footage whose own sound was taken out onto a sound track (TL4c): its picture plays silent */
+ muted?: boolean
/** it can't be changed here, and why ("set in code", TL0c) */
locked?: string
/** clips with one group pick and move as one (TL3g) */
diff --git a/sdk/timeline/src/ops.ts b/sdk/timeline/src/ops.ts
index c0a5e7aa..52dc9f14 100644
--- a/sdk/timeline/src/ops.ts
+++ b/sdk/timeline/src/ops.ts
@@ -25,6 +25,10 @@ export type Op =
| { op: "uncompound"; id: string }
/** a clip put on the timeline: on `track`, else the first of `kind` with room, else a new track of that kind */
| { op: "add"; kind: TrackKind; clip: Clip; track?: string }
+ /** footage's own sound taken out onto a sound track (Cmd+Shift+S, TL4c): a sound clip `sound` (an id made from the
+ * footage's when unnamed) over the same span, from the same place in the file at the same pace and level, and the
+ * footage silent from then on, so its sound moves, trims and fades on its own */
+ | { op: "extract"; id: string; sound?: string }
/** a clip's own fields; `props` are merged into its props; a speed or a curve changes its length (TL6) */
/** a still of the frame under `at` on a clip (the main track's, when none is named), `length` frames long, put in
* there: the clip is split and the still goes between its halves (TL3h) */
@@ -235,6 +239,20 @@ function run(t: Timeline, op: Exclude<Op, { op: "restore" }>): Timeline {
const after = parts[1] ? { ...parts[1], start: at + len } : undefined
return withTracks(t, (k) => (k.id !== host.track.id ? k : { ...k, clips: [...k.clips.flatMap((x) => (x.id === c.id ? [parts[0], still, after].filter((y): y is Clip => !!y) : [x.start >= end(c) ? { ...x, start: x.start + len } : x]))] }))
}
+ case "extract": {
+ const { track, clip } = open(t, op.id)
+ const name = clip.name ?? clip.id
+ if (track.kind === "sound" || clip.kind === "audio") no(`${name} is sound already`)
+ if (clip.in === undefined || clip.freeze) no(`${name} isn't footage: it has no sound of its own to take out`)
+ if (clip.muted) no(`${name}'s sound is on a sound track already`)
+ const id = op.sound ?? fresh(t, `${clip.id}-sound`)
+ if (find(t, id)) no(`there is already a clip ${id}`)
+ // the same stretch of the file, played the same way: speed or curve, backward, level and fades
+ const pick = <K extends keyof Clip>(...keys: K[]) => Object.fromEntries(keys.filter((k) => clip[k] !== undefined).map((k) => [k, clip[k]])) as Pick<Clip, K>
+ const sound: Clip = { id, kind: "audio", name, start: clip.start, length: clip.length, ...pick("in", "source", "speed", "curve", "shows", "reverse", "volume", "fadeIn", "fadeOut", "off"), props: { of: clip.id } }
+ const silent = change(t, clip.id, (c) => ({ ...c, muted: true, volume: undefined, fadeIn: undefined, fadeOut: undefined }))
+ return place(silent, "sound", sound)
+ }
case "add": {
if (find(t, op.clip.id)) no(`there is already a clip ${op.clip.id}`)
if (!(op.clip.length >= 1) || op.clip.start < 0) no("a clip is at least one frame long and starts in the film")
diff --git a/sdk/timeline/src/read.ts b/sdk/timeline/src/read.ts
index ca3aef8e..572d3219 100644
--- a/sdk/timeline/src/read.ts
+++ b/sdk/timeline/src/read.ts
@@ -71,6 +71,8 @@ export interface SeqRow {
startFrom?: number
/** its media's level, 1 as recorded */
volume?: number
+ /** a <Video> whose own sound was taken out (muted): its sound is an <Audio> of its own (TL4c) */
+ muted?: boolean
}
/** A sequence's still placement: its <Nudge>'s props (sdk/video nudge.tsx). `crop` is left, top, right, bottom. */
export interface SeqLook {
@@ -118,6 +120,9 @@ export type SeqEdit =
| { op: "edge"; i: number; side: "in" | "out"; kind: string; frames: number }
/** a <Video>'s or an <Audio>'s own level (TL8a) */
| { op: "level"; i: number; volume: number }
+ /** a <Video>'s own sound taken out (TL4c): the <Video> muted, and an <Audio> of the same file, from the same place
+ * at the same pace and level, in a new sequence from `at` for `frames` after the composition's last */
+ | { op: "extract"; i: number; at: number; frames: number }
/** The transitions a composition's <Transition> draws (sdk/video transition.tsx). */
export const CODE_KINDS = ["fade", "slide", "wipe", "zoom"]
@@ -160,7 +165,7 @@ export function fromSequences(rows: SeqRow[], fps: number): Timeline {
const mediaTracks = (kind: "video" | "audio"): Track[] => {
const out: Track[] = []
for (const r of media.filter((m) => m.media === kind).sort((a, b) => a.from - b.from)) {
- const c: Clip = { id: seqId(r.i), kind, name: r.label ?? r.name, start: r.from, length: Math.max(1, r.to - r.from), in: r.startFrom ?? 0, ...(r.speed && r.speed !== 1 && { speed: r.speed }), ...(r.volume !== undefined && { volume: r.volume }), ...(r.locked && { locked: r.locked }) }
+ const c: Clip = { id: seqId(r.i), kind, name: r.label ?? r.name, start: r.from, length: Math.max(1, r.to - r.from), in: r.startFrom ?? 0, ...(r.speed && r.speed !== 1 && { speed: r.speed }), ...(r.volume !== undefined && { volume: r.volume }), ...(r.muted && kind === "video" && { muted: true }), ...(r.locked && { locked: r.locked }) }
const home = out.find((k) => k.clips.every((o) => end(o) <= c.start || o.start >= end(c)))
if (home) home.clips.push(c)
else out.push({ id: `${kind === "audio" ? "sound" : "footage"}-${out.length + 1}`, kind: kind === "audio" ? "sound" : "overlay", name: kind === "audio" ? "Sound" : "Footage", clips: [c] })
@@ -209,6 +214,16 @@ export function seqEdits(t: Timeline, op: Op): SeqEdit[] | { why: string } {
// a <Video> or an <Audio> plays where its sequence does: its level is its own, everything else its sequence's
const isMedia = (id: string) => ["video", "audio"].includes(find(t, id)?.clip.kind ?? "") && /^(footage|sound)-/.test(find(t, id)?.track.id ?? "")
const ids = "ids" in op && op.ids ? op.ids : "id" in op && typeof op.id === "string" ? [op.id] : []
+ // footage's sound taken out: its <Video> muted and an <Audio> of the file beside it
+ if (op.op === "extract") {
+ const at = find(t, op.id)
+ if (!at || !isMedia(op.id) || at.clip.kind !== "video") return { why: at?.clip.kind === "sequence" ? "a sequence's sound is the footage in it: take the sound out of its <Video> on the footage track" : "only a <Video>'s sound is taken out of a composition here" }
+ const i = seqOf(op.id)
+ if (i === undefined) return { why: "that isn't one of the composition's elements" }
+ const tried = apply(t, op)
+ if (!tried.ok) return { why: tried.why }
+ return [{ op: "extract", i, at: at.clip.start, frames: at.clip.length }]
+ }
if (ids.some(isMedia)) {
if (op.op === "set" && Object.keys(op.set).filter((k) => op.set[k as keyof typeof op.set] !== undefined).join() === "volume") return ids.flatMap((id) => { const i = seqOf(id); return i === undefined ? [] : [{ op: "level", i, volume: Math.round(Math.max(0, op.set.volume!) * 1000) / 1000 } as SeqEdit] })
return { why: "footage and sound play where their sequence does: move, trim or speed the sequence; here only the level is its own" }
diff --git a/sdk/timeline/test/ops.test.ts b/sdk/timeline/test/ops.test.ts
index 63f6d8b9..542e43a9 100644
--- a/sdk/timeline/test/ops.test.ts
+++ b/sdk/timeline/test/ops.test.ts
@@ -35,6 +35,7 @@ describe("TL0b: an operation is a pure function with an inverse", () => {
{ op: "key", id: "b", prop: "scale", at: 10, value: 1.2 },
{ op: "mark", marker: { id: "m1", at: 45 } },
{ op: "track", id: "sound-1", set: { muted: true } },
+ { op: "extract", id: "b" },
{ op: "transition", transition: { id: "x1", kind: "dissolve", after: "a", length: 12 } },
{ op: "option", set: { magnet: false } },
]
@@ -191,3 +192,46 @@ describe("TL6: speed", () => {
}
})
})
+
+describe("TL4c: footage's own sound taken out (Cmd+Shift+S)", () => {
+ test("becomes a sound clip over the same span, from the same place in the file, at the same pace and level; the footage plays silent", () => {
+ const t = done(done(film(), { op: "set", id: "b", set: { volume: 0.5, fadeIn: 6 } }), { op: "extract", id: "b" })
+ expect(at(t, "b")).toMatchObject({ muted: true, start: 30, length: 60, in: 10 })
+ expect(at(t, "b").volume).toBeUndefined()
+ expect(at(t, "b").fadeIn).toBeUndefined()
+ // a sound track with room has it: the music's is full, so a new one under it
+ const sound = at(t, "b-sound+")
+ expect(sound).toEqual({ id: "b-sound+", kind: "audio", name: "b", start: 30, length: 60, in: 10, source: 300, volume: 0.5, fadeIn: 6, props: { of: "b" } })
+ expect(find(t, "b-sound+")!.track.kind).toBe("sound")
+ expect(t.tracks.map((k) => k.id)).toEqual(["text-1", "main", "sound-1", "sound-2"])
+ })
+
+ test("its speed, curve and direction go with it, and it can be named", () => {
+ const fast = done(film(), { op: "set", id: "b", set: { speed: 2, reverse: true } })
+ const t = done(fast, { op: "extract", id: "b", sound: "b-voice" })
+ expect(at(t, "b-voice")).toMatchObject({ speed: 2, reverse: true, length: 30, in: 10 })
+ })
+
+ test("from then on the sound is its own: moved or removed without the footage, and the footage without it", () => {
+ const t = done(film(), { op: "extract", id: "b" })
+ const moved = done(t, { op: "move", ids: ["b-sound+"], by: 15 })
+ expect(spans(moved, "main")).toEqual(["a 0-30", "b 30-90", "c 90-120"])
+ expect(spans(moved, "sound-2")).toEqual(["b-sound+ 45-105"])
+ // the footage removed: the main track closes up, its sound stays where it was
+ expect(spans(done(t, { op: "remove", ids: ["b"] }), "sound-2")).toEqual(["b-sound+ 30-90"])
+ })
+
+ test("is refused for what has no sound of its own to take out, or has had it taken out", () => {
+ const t = done(film(), { op: "extract", id: "b" })
+ const why = (tl: Timeline, op: Op) => {
+ const r = apply(tl, op)
+ return r.ok ? "applied" : r.why
+ }
+ expect(why(t, { op: "extract", id: "b" })).toBe("b's sound is on a sound track already")
+ expect(why(film(), { op: "extract", id: "music" })).toBe("music is sound already")
+ expect(why(film(), { op: "extract", id: "title" })).toBe("title isn't footage: it has no sound of its own to take out")
+ const still = done(film(), { op: "freeze", at: 40, length: 30 })
+ expect(why(still, { op: "extract", id: "b-still+" })).toBe("Freeze isn't footage: it has no sound of its own to take out")
+ expect(why(film(), { op: "extract", id: "b", sound: "music" })).toBe("there is already a clip music")
+ })
+})
diff --git a/sdk/timeline/test/read.test.ts b/sdk/timeline/test/read.test.ts
index b4661f1c..e0b5cd43 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, readTimeline, segmentsOf, seqEdits, type Timeline } from "../src"
+import { apply, clipEdits, find, fromRender, fromSequences, fromTracks, readTimeline, segmentsOf, seqEdits, type Timeline } from "../src"
const done = (t: Timeline, op: Parameters<typeof apply>[1]) => {
const r = apply(t, op)
@@ -109,6 +109,21 @@ describe("TL0c: a timeline is read from a composition", () => {
expect(seqEdits(t, { op: "move", ids: ["seq-1"], by: 10 })).toEqual([{ op: "move", i: 1, from: 25, to: 70 }])
})
+ test("a <Video>'s sound taken out is the <Video> muted and an <Audio> of its file beside it, read back as such (TL4c)", () => {
+ const media = [...rows, { i: 4, name: "Video", label: "take.mp4", from: 90, to: 180, lane: 0, media: "video" as const, startFrom: 30 }, { i: 6, name: "Audio", label: "bed.wav", from: 0, to: 180, lane: 0, media: "audio" as const }]
+ const t = fromSequences(media, 30)
+ expect(seqEdits(t, { op: "extract", id: "seq-4" })).toEqual([{ op: "extract", i: 4, at: 90, frames: 90 }])
+ expect(seqEdits(t, { op: "extract", id: "seq-6" })).toEqual({ why: "only a <Video>'s sound is taken out of a composition here" })
+ expect(seqEdits(t, { op: "extract", id: "seq-1" })).toEqual({ why: "a sequence's sound is the footage in it: take the sound out of its <Video> on the footage track" })
+ // the code as the host writes it, read again: the <Video> muted, its sound a clip on a sound track
+ const back = fromSequences([...media.map((r) => (r.i === 4 ? { ...r, muted: true } : r)), { i: 7, name: "Audio", label: "take.mp4", from: 90, to: 180, lane: 0, media: "audio" as const, startFrom: 30 }], 30)
+ expect(find(back, "seq-4")!.clip.muted).toBe(true)
+ expect(seqEdits(back, { op: "extract", id: "seq-4" })).toEqual({ why: "take.mp4's sound is on a sound track already" })
+ expect(find(back, "seq-7")!.track.kind).toBe("sound")
+ // a muted <Audio> isn't footage whose sound was taken out
+ expect(fromSequences([{ ...media[media.length - 1], muted: true }], 30).tracks[0].clips[0].muted).toBeUndefined()
+ })
+
test("a transition on a cut is the part before going out and the part after coming in, and is read back from them", () => {
const t = fromSequences(rows, 30)
expect(seqEdits(t, { op: "transition", transition: { id: "x-seq-0", kind: "slide", after: "seq-0", length: 15 } })).toEqual([