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.

TL14b: in progress: a sound's tracks as a timeline, and its clip edits (sdk/timeline read.ts)

shinyobjectz7caf1bb5966d
diff --git a/sdk/timeline/src/read.ts b/sdk/timeline/src/read.ts
index c1161a07..96bfc782 100644
--- a/sdk/timeline/src/read.ts
+++ b/sdk/timeline/src/read.ts
@@ -108,3 +108,61 @@ export function seqEdits(t: Timeline, op: Op): SeqEdit[] | { why: string } {
return { why: `the composition's sequences don't take "${op.op}" yet` }
}
}
+
+// ---------------------------------------------------------------- a sound's own tracks (TL14b)
+
+/** A track of a sound in the feed as its host lists it (raea-feed host/sounds.ts), in seconds of the render. */
+export interface SoundTrack {
+ id: string
+ name: string
+ kind: "notes" | "clips" | "lines"
+ bus: string
+ clips: { id: string; from: number; to: number; label: string; src?: string; offset?: number; gainDb?: number }[]
+}
+/** A clip's span and level, in seconds and dB: what the host writes back for a clip that was moved, trimmed or
+ * turned up or down (raea-feed host/clips.ts). */
+export interface ClipSpan {
+ start: number
+ end: number
+ offset: number
+ gainDb: number
+}
+
+const level = (db: number) => Math.round(10 ** (db / 20) * 1000) / 1000
+const dB = (v: number) => Math.round(20 * Math.log10(Math.max(v, 0.001)) * 10) / 10
+
+/** A sound's tracks as a timeline: a sound track each, by its bus; a clip of a file keeps its place in the file
+ * (its offset) and its level. Notes and lines are clips too, locked here: they are edited where they are written
+ * (the roll, the words). No magnet: a mix's clips are where they were put. */
+export function fromTracks(tracks: SoundTrack[], fps: number): Timeline {
+ const f = (s: number) => Math.round(s * fps)
+ return {
+ ...empty(fps),
+ magnet: false,
+ tracks: tracks.map((k) => ({
+ id: k.id,
+ kind: "sound" as const,
+ name: k.name,
+ clips: k.clips
+ .map((c): Clip => ({ id: c.id, kind: k.kind === "clips" ? "audio" : k.kind, name: c.label, start: f(c.from), length: Math.max(1, f(c.to) - f(c.from)), ...(c.src ? { in: f(c.offset ?? 0), at: c.src, volume: level(c.gainDb ?? 0) } : { locked: k.kind === "notes" ? "edited on its roll" : "edited in its words" }) }))
+ .sort((a, b) => a.start - b.start),
+ })),
+ }
+}
+
+/** A clip of a file as the host is told it: where it plays, how far into its file, how loud. */
+const spanOf = (c: Clip, fps: number): ClipSpan => ({ start: c.start / fps, end: end(c) / fps, offset: (c.in ?? 0) / fps, gainDb: dB(c.volume ?? 1) })
+
+/** The file clips that differ between the timeline as it was read and as it stands, each with its span before and
+ * after: what a sound's hand edit carries (its `clips`). A clip that was removed or made here isn't one of these. */
+export function clipEdits(read: Timeline, now: Timeline): { id: string; src: string; from: ClipSpan; to: ClipSpan }[] {
+ const out: { id: string; src: string; from: ClipSpan; to: ClipSpan }[] = []
+ for (const k of read.tracks)
+ for (const was of k.clips) {
+ const is = find(now, was.id)?.clip
+ if (!was.at || !is) continue
+ const a = spanOf(was, read.fps), b = spanOf(is, now.fps)
+ if (a.start !== b.start || a.end !== b.end || a.offset !== b.offset || a.gainDb !== b.gainDb) out.push({ id: was.id, src: was.at, from: a, to: b })
+ }
+ return out
+}
diff --git a/sdk/timeline/test/read.test.ts b/sdk/timeline/test/read.test.ts
index c5444ba8..4a72e74a 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, fromRender, fromSequences, segmentsOf, seqEdits, type Timeline } from "../src"
+import { apply, clipEdits, fromRender, fromSequences, fromTracks, segmentsOf, seqEdits, type Timeline } from "../src"
const done = (t: Timeline, op: Parameters<typeof apply>[1]) => {
const r = apply(t, op)
@@ -62,3 +62,27 @@ describe("TL14a: a film with no composition is its scenes and its sound", () =>
expect(segmentsOf(cut)).toEqual([[240, 300], [0, 90]])
})
})
+
+describe("TL14b: a sound in the feed is its tracks", () => {
+ const tracks = [
+ { id: "music", name: "Music", kind: "clips" as const, bus: "music", clips: [{ id: "bed", from: 0, to: 8, label: "bed.wav", src: "audio/bed.wav", offset: 1, gainDb: -6 }] },
+ { id: "lead", name: "Lead", kind: "notes" as const, bus: "music", clips: [{ id: "lead-1", from: 2, to: 6, label: "Lead" }] },
+ ]
+
+ test("a file clip keeps its place in its file and its level; notes are shown and edited on their roll", () => {
+ const t = fromTracks(tracks, 30)
+ expect(t.magnet).toBe(false)
+ expect(t.tracks[0].clips[0]).toMatchObject({ id: "bed", kind: "audio", start: 0, length: 240, in: 30, at: "audio/bed.wav", volume: 0.501 })
+ expect(t.tracks[1].clips[0]).toMatchObject({ kind: "notes", locked: "edited on its roll" })
+ expect(apply(t, { op: "move", ids: ["lead-1"], by: 10 })).toEqual({ ok: false, why: "Lead is edited on its roll" })
+ })
+
+ test("a clip moved, trimmed and turned up is the host's clip edit, in seconds and dB", () => {
+ const t = fromTracks(tracks, 30)
+ let now = done(t, { op: "move", ids: ["bed"], by: 30 })
+ now = done(now, { op: "trim", id: "bed", edge: "start", to: 60 })
+ now = done(now, { op: "set", id: "bed", set: { volume: 1 } })
+ expect(clipEdits(t, now)).toEqual([{ id: "bed", src: "audio/bed.wav", from: { start: 0, end: 8, offset: 1, gainDb: -6 }, to: { start: 2, end: 9, offset: 2, gainDb: 0 } }])
+ expect(clipEdits(t, t)).toEqual([])
+ })
+})