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.

An <Audio> or <Video> fadeOut fades inside its clip, silent by its end, not after it (#89 bug 5). The fade began when the element left the timeline, so a clip played at full volume to its last frame and faded past the cut. A Sequence or Loop pass gives its end through MediaEndContext, the nearest of nested ends, and with endAt's: each frame's volume is turned down into it ((end - f) / (fadeOut * fps), the mixer ramping each frame's gain over the frame after it), and data-fade-out is 0 there, so the stop at the end is the mixer's 5 ms edge. Its last frame isn't 0: a silent sound isn't played, which dropped it a frame early and faded the frame before's gain over the whole fadeOut. Without an end, the fade after it's gone is as before. A 440 Hz tone in a 1 s Sequence, fadeOut 0.5 at 60 fps: -5.6 dB at 0.75 s, -12.9 dB at 0.9 s, silent from 1.01 s (was no fade in the clip and a tail to 1.6 s). sdk/raea conformance media.test.tsx: a Sequence's end, endAt's, a Loop pass's, nested ends, and none

shinyobjectzd0d71e67d580
diff --git a/docs/manual/reference/sdk/raea.md b/docs/manual/reference/sdk/raea.md
index 25616402..3ec8e7e0 100644
--- a/docs/manual/reference/sdk/raea.md
+++ b/docs/manual/reference/sdk/raea.md
@@ -1169,7 +1169,7 @@ The props of [`Loop`](#loop), tabled there.
| `volume` | `VolumeProp` | linear gain, 1 being unity (0.5 is about -6 dB, 1.78 about +5 dB), or a function of the media's own frame |
| `gainDb` | `VolumeProp` | ours: the gain in decibels (or a function of the media's own frame returning it); wins over volume |
| `fadeIn` | `number` | ours: seconds to ramp the sound in from silence at its start (applied by the renderer at the audio's rate) |
-| `fadeOut` | `number` | ours: seconds to ramp the sound out to silence at its end |
+| `fadeOut` | `number` | ours: seconds to ramp the sound out to silence by its end: the end of its Sequence or Loop pass, or endAt, whichever comes first (one removed some other way fades out after it's gone) |
| `playbackRate` | `number` | how fast it plays (1): 2 is double speed |
| `muted` | `boolean` | play no sound |
| `loop` | `boolean` | start again from `startFrom` when it reaches `endAt` |
diff --git a/docs/manual/reference/sdk/video.md b/docs/manual/reference/sdk/video.md
index fd86c1bb..3c3e6258 100644
--- a/docs/manual/reference/sdk/video.md
+++ b/docs/manual/reference/sdk/video.md
@@ -118,7 +118,7 @@ The props of [`Loop`](#loop), tabled there.
| `volume` | `VolumeProp` | linear gain, 1 being unity (0.5 is about -6 dB, 1.78 about +5 dB), or a function of the media's own frame |
| `gainDb` | `VolumeProp` | ours: the gain in decibels (or a function of the media's own frame returning it); wins over volume |
| `fadeIn` | `number` | ours: seconds to ramp the sound in from silence at its start (applied by the renderer at the audio's rate) |
-| `fadeOut` | `number` | ours: seconds to ramp the sound out to silence at its end |
+| `fadeOut` | `number` | ours: seconds to ramp the sound out to silence by its end: the end of its Sequence or Loop pass, or endAt, whichever comes first (one removed some other way fades out after it's gone) |
| `playbackRate` | `number` | how fast it plays (1): 2 is double speed |
| `muted` | `boolean` | play no sound |
| `loop` | `boolean` | start again from `startFrom` when it reaches `endAt` |
diff --git a/sdk/raea/conformance/media.test.tsx b/sdk/raea/conformance/media.test.tsx
index 48100edc..41005da3 100644
--- a/sdk/raea/conformance/media.test.tsx
+++ b/sdk/raea/conformance/media.test.tsx
@@ -1,6 +1,6 @@
// Video and Audio sound props, from spec/motion.feature ("Video and Audio"): gain above unity, gainDb, sub-frame fades.
import { expect, test } from "bun:test";
-import { Audio, Video } from "../src";
+import { Audio, Loop, Sequence, Video } from "../src";
import { renderFrame } from "../src/test";
const base = { frame: 0, fps: 30, width: 1080, height: 1920, durationInFrames: 60 };
@@ -27,3 +27,32 @@ test("fades shorter than a frame are carried in seconds for the renderer", () =>
expect(Number(attr(html, "data-fade-in"))).toBeCloseTo(0.012, 6);
expect(Number(attr(html, "data-fade-out"))).toBeCloseTo(0.012, 6);
});
+
+// #89: a fadeOut ends at its clip's end (its Sequence's, a Loop pass's, endAt), not after it. Each frame says the gain
+// the mixer ramps to over the frame after it, so frame f is (end - f) / (fadeOut * fps), and the stop at the end is the
+// mixer's 5 ms edge (data-fade-out 0): its last frame isn't 0, or the sound would be dropped a frame early
+test("fadeOut fades inside its Sequence and is silent by its end", () => {
+ // 1 s Sequence at 60 fps, fadeOut 0.5 s: full to frame 30, half at 45, 1/30 at 59, the clip's last
+ const at = (frame: number) =>
+ Number(attr(renderFrame(<Sequence from={0} durationInFrames={60}><Audio src="media/a.wav" fadeOut={0.5} /></Sequence>, { ...base, fps: 60, frame }), "data-volume"));
+ expect(at(0)).toBe(1);
+ expect(at(30)).toBe(1);
+ expect(at(45)).toBeCloseTo(0.5, 6);
+ expect(at(59)).toBeCloseTo(1 / 30, 6);
+ const last = renderFrame(<Sequence durationInFrames={60}><Audio src="media/a.wav" fadeOut={0.5} /></Sequence>, { ...base, fps: 60, frame: 59 });
+ expect(Number(attr(last, "data-fade-out"))).toBe(0);
+});
+
+test("fadeOut ends at endAt, at a Loop pass's end, and at the nearest of nested ends", () => {
+ const vol = (el: any, frame: number) => Number(attr(renderFrame(el, { ...base, frame }), "data-volume"));
+ // endAt 40 from startFrom 10 at rate 1: it stops at frame 30; fadeOut 0.5 s = 15 frames at 30 fps
+ expect(vol(<Audio src="media/a.wav" startFrom={10} endAt={40} fadeOut={0.5} />, 22)).toBeCloseTo(8 / 15, 6);
+ // a 20-frame Loop pass: frame 30 is pass 2's frame 10, 10 frames from its end
+ expect(vol(<Loop durationInFrames={20}><Audio src="media/a.wav" fadeOut={0.5} /></Loop>, 30)).toBeCloseTo(10 / 15, 6);
+ // the outer Sequence ends first
+ const nested = <Sequence durationInFrames={30}><Sequence durationInFrames={50}><Audio src="media/a.wav" fadeOut={0.5} /></Sequence></Sequence>;
+ expect(vol(nested, 20)).toBeCloseTo(10 / 15, 6);
+ // no end: the gain as given, and the mixer fades it once it's gone
+ expect(vol(<Audio src="media/a.wav" fadeOut={0.5} volume={0.8} />, 50)).toBeCloseTo(0.8, 6);
+ expect(Number(attr(renderFrame(<Audio src="media/a.wav" fadeOut={0.5} />, base), "data-fade-out"))).toBe(0.5);
+});
diff --git a/sdk/video/src/media.tsx b/sdk/video/src/media.tsx
index 48ed4448..dc84a5c3 100644
--- a/sdk/video/src/media.tsx
+++ b/sdk/video/src/media.tsx
@@ -1,6 +1,6 @@
// Video, Audio, Img and staticFile (spec: "Media plays in step with the frame"). These render as plain elements
// that carry their props for the renderer, with the timing already worked out for the frame being rendered.
-import type { CSSProperties, ImgHTMLAttributes } from "react";
+import { createContext, useContext, type CSSProperties, type ImgHTMLAttributes } from "react";
// assets stays in raea until it moves to @raea/asset (docs/sdk.feature S1d)
import { isGeneratedAsset, type GeneratedAsset } from "@raea/asset/assets";
import { useClockState, useLocalTime } from "@raea/app/clock";
@@ -27,7 +27,8 @@ export interface MediaProps {
gainDb?: VolumeProp;
/** ours: seconds to ramp the sound in from silence at its start (applied by the renderer at the audio's rate) */
fadeIn?: number;
- /** ours: seconds to ramp the sound out to silence at its end */
+ /** ours: seconds to ramp the sound out to silence by its end: the end of its Sequence or Loop pass, or endAt, whichever
+ * comes first (one removed some other way fades out after it's gone) */
fadeOut?: number;
/** how fast it plays (1): 2 is double speed */
playbackRate?: number;
@@ -130,19 +131,39 @@ function fadeSeconds(owner: string, props: MediaProps, name: "fadeIn" | "fadeOut
return v;
}
+/** The composition frame after the last one media under it plays (a Sequence's end, a Loop pass's): its fadeOut ends
+ * there, in the clip, not after it (timeline.tsx gives it; Infinity: none) */
+export const MediaEndContext = createContext(Infinity);
+
+/** `volume` turned down by the fade-out into `end` (a composition frame) at composition frame `now`. The mixer ramps
+ * each frame's gain over the frame after it, so a frame says the gain one frame on, and its last frame is 1/(fade * fps):
+ * a frame at 0 would drop the sound a frame early (a silent sound isn't played), and the stop at `end` takes it the rest
+ * of the way in the mixer's 5 ms edge. */
+function fadedOut(volume: number, fade: number, fps: number, now: number, end: number): number {
+ if (!(fade > 0) || end === Infinity) return volume;
+ return volume * Math.min(1, Math.max(0, (end - now) / (fade * fps)));
+}
+
// `src`: the file the element plays (an <Audio>'s generated sound passes its file; its props were copied to give it)
function useMediaAttrs(owner: string, props: MediaProps, src = props.src as string) {
const { fps } = useClockState(owner).config;
const { frame, origin } = useLocalTime(owner);
if (typeof src !== "string" || src === "") throw new Error(`${owner}: src is required`);
const t = timing(owner, props, frame, src);
+ // where it stops: its Sequence's (or Loop pass's) end, or endAt reached (a loop never reaches it)
+ const endAt = props.loop ? undefined : (props.endAt ?? props.trimAfter);
+ const own = endAt === undefined ? Infinity : origin + Math.ceil((endAt - (props.startFrom ?? props.trimBefore ?? 0)) / (props.playbackRate ?? 1));
+ const fadeOut = fadeSeconds(owner, props, "fadeOut", src);
+ const end = Math.min(useContext(MediaEndContext), own);
+ const volume = fadedOut(t.volume, fadeOut, fps, origin + frame, end);
const attrs: Record<string, string | number | undefined> = {
src,
"data-source-time": t.sourceFrame / fps,
"data-source-frame": t.sourceFrame,
- "data-volume": props.muted ? 0 : t.volume,
+ "data-volume": props.muted ? 0 : volume,
"data-fade-in": fadeSeconds(owner, props, "fadeIn", src),
- "data-fade-out": fadeSeconds(owner, props, "fadeOut", src),
+ // faded here into its end: the stop at the end is the mixer's edge. With no end, the mixer fades it once it's gone
+ "data-fade-out": end === Infinity ? fadeOut : 0,
"data-playback-rate": props.playbackRate ?? 1,
"data-start-from": props.startFrom ?? props.trimBefore ?? 0,
"data-end-at": props.endAt ?? props.trimAfter,
diff --git a/sdk/video/src/timeline.tsx b/sdk/video/src/timeline.tsx
index 88ae18ee..d61b3330 100644
--- a/sdk/video/src/timeline.tsx
+++ b/sdk/video/src/timeline.tsx
@@ -3,7 +3,7 @@ import { Children, Fragment, isValidElement, useContext, useRef, type CSSPropert
import { LocalTimeContext, TimeBaseContext, timeBase, useClockState, useLocalTime, type LocalTime, type TimeBase } from "@raea/app/clock";
// assets stays in raea until it moves to @raea/asset (docs/sdk.feature S1d)
import { isGeneratedAsset } from "@raea/asset/assets";
-import { Audio } from "./media";
+import { Audio, MediaEndContext } from "./media";
export interface AbsoluteFillProps {
/** merged over the fill's own style (absolute, inset 0, a flex column) */
@@ -79,9 +79,11 @@ function Shifted(props: {
label?: string;
/** the composition frame after its last, when it ends (`raea world`'s Shot, docs/direction.feature DR1) */
end?: number;
+ /** the composition frame its media stops at, when not `end` (a Loop pass's end): a fadeOut ends there */
+ stops?: number;
children?: ReactNode;
}) {
- const { owner, time, base, layout, style, className, label, end, children } = props;
+ const { owner, time, base, layout, style, className, label, end, stops = end, children } = props;
if (layout !== "absolute-fill" && layout !== "none") {
throw new RangeError(`${owner}: layout must be "absolute-fill" or "none", got "${layout}"`);
}
@@ -101,11 +103,17 @@ function Shifted(props: {
);
return (
<TimeBaseContext.Provider value={base}>
- <LocalTimeContext.Provider value={time}>{inner}</LocalTimeContext.Provider>
+ <LocalTimeContext.Provider value={time}>{stops === undefined ? inner : <StopsAt end={stops}>{inner}</StopsAt>}</LocalTimeContext.Provider>
</TimeBaseContext.Provider>
);
}
+/** Media under it stops at composition frame `end`, or sooner where an outer one does (media.tsx fadedOut) */
+function StopsAt(props: { end: number; children?: ReactNode }) {
+ const outer = useContext(MediaEndContext);
+ return <MediaEndContext.Provider value={Math.min(outer, props.end)}>{props.children}</MediaEndContext.Provider>;
+}
+
/** The same LocalTime object as last render when nothing in it changed (a Freeze's subtree, a held frame). */
function useStableTime(frame: number, origin: number, width: number, height: number): LocalTime {
const r = useRef<LocalTime | null>(null);
@@ -147,7 +155,7 @@ export function Sequence(props: SequenceProps) {
if (layout === "none" && style === undefined && className === undefined) {
return (
<TimeBaseContext.Provider value={base}>
- <LocalTimeContext.Provider value={time}>{props.children}</LocalTimeContext.Provider>
+ <LocalTimeContext.Provider value={time}>{durationInFrames === Infinity ? props.children : <StopsAt end={time.origin + durationInFrames}>{props.children}</StopsAt>}</LocalTimeContext.Provider>
</TimeBaseContext.Provider>
);
}
@@ -254,7 +262,7 @@ export function Loop(props: LoopProps) {
const time: LocalTime = p && p.frame === lf && p.origin === lo && p.width === parent.width && p.height === parent.height ? p : (r.current = { frame: lf, origin: lo, width: parent.width, height: parent.height });
const base = timeBase(pbase.origin + pass * durationInFrames, pbase.freeze === null ? null : pbase.freeze - pass * durationInFrames);
return (
- <Shifted owner="Loop" time={time} base={base} layout={layout} style={style} className={className} label={name}>
+ <Shifted owner="Loop" time={time} base={base} layout={layout} style={style} className={className} label={name} stops={lo + durationInFrames}>
{props.children}
</Shifted>
);