@motionhub/runtime 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -7
- package/index.d.ts +43 -8
- package/index.js +199 -13
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -15,11 +15,11 @@ You do not use this package on its own: an engine binding provides it.
|
|
|
15
15
|
| `useTokens()` | The motion's `tokens.json` resolved: `color.ink` (a CSS colour), `font.display` (a CSS stack), `duration.light` (seconds), `easing.light` (`[x1, y1, x2, y2]`), `caption.*` |
|
|
16
16
|
| `seededRandom(seed)` | A stream of numbers in [0, 1) for a string or number seed: the same seed draws the same picture in every engine |
|
|
17
17
|
| `<Video src startAt trimBefore playbackRate objectFit>` | The engine's video: shown from `startAt` s of the sequence, its source from `trimBefore` s |
|
|
18
|
-
| `<Caption opacity>` | One spoken line, drawn from the `caption` tokens (font, size, weight, color, background, radius, padding-y, padding-x, bottom, max-width, shadow, rule-left or rule-top) |
|
|
18
|
+
| `<Caption line opacity>` | One spoken line, drawn from the `caption` tokens (font, size, weight, color, background, radius, padding-y, padding-x, bottom, max-width, shadow, rule-left or rule-top); with `line`, that line of the film's while it is said |
|
|
19
19
|
| `<Sequence from duration>`, `interpolate(t, input, output, {easing})`, `Easing` | As Remotion's, in seconds: a span whose children count time from its start, a value mapped through a curve, and the curves (`Easing.bezier` takes a cubicBezier token). For composing components, as a reel does |
|
|
20
20
|
| `<MotionRoot tokens>` | A full-frame box carrying the tokens as CSS custom properties |
|
|
21
21
|
| `<MotionScope tokens root>` | Its children drawn in another motion, on the engine's clock: `useTokens()` under it gives that motion's tokens (a `tokens.json`, or tokens already resolved), set as custom properties on a full-frame root (`root={false}` leaves it out). A film in several motions wraps each scene, or each part of a frame, in one |
|
|
22
|
-
| `<Film>`, `<Scene id duration>`, `<Layer id x y w h z>`, `<Line id text
|
|
22
|
+
| `<Film captions>`, `<Scene id duration>`, `<Layer id x y w h z>`, `<Line id text at dur>`, `<Sound src at …>` | A film made of scenes, for an engine that plays a whole film rather than one component: below |
|
|
23
23
|
| `resolveTokens(dtcg)`, `tokenVariables(tokens)` | `tokens.json` (W3C design tokens) as values, and those values as custom properties: `--color-ink`, `--font-display`, `--duration-light`, `--easing-light`, `--caption-size` |
|
|
24
24
|
|
|
25
25
|
A component colours with `var(--color-<name>)` and sets type with `var(--font-<name>)`, reads its time from `useTime()`, never from a clock, a timer or `Math.random`, and takes what changes (words, a progress from 0 to 1, a video's `src`) as props. So the same moment draws the same picture every time.
|
|
@@ -46,19 +46,39 @@ check it scene by scene without running it:
|
|
|
46
46
|
- **`<Layer id x y w h z>`**: a box of the frame in its pixels (the whole frame when left out), stacked by `z`, shown for the whole of its
|
|
47
47
|
scene. How its content moves is the content's own, from `useTime()`. A layer that is a child of `<Film>` is the film's own: drawn across
|
|
48
48
|
every scene, on the film's clock, and no scene's layer may take its id, since a picked element is told by its layer.
|
|
49
|
-
- **`<Line id text>`**: what a scene says, its words as children (`text` where the caption differs). It draws nothing; an engine may
|
|
50
|
-
it or caption it.
|
|
49
|
+
- **`<Line id text at dur>`**: what a scene says, its words as children (`text` where the caption differs). It draws nothing; an engine may
|
|
50
|
+
speak it or caption it. It is said from `at` seconds into its scene (without one, where the line before it ends) for `dur` seconds
|
|
51
|
+
(without one, an equal share of the time to the next line that gives its `at`, or to the scene's end), so lines that give no time share
|
|
52
|
+
their scene evenly. `<Film captions>` draws the line being said as a `<Caption>`, over everything, fading in and out; `<Caption line="id">`
|
|
53
|
+
draws one line's caption where a component wants it, while that line is said.
|
|
54
|
+
- **A narration**: `<Film voice="provider:name">` names the voice a film's lines are spoken in, and the engine that speaks them gives each
|
|
55
|
+
voiced line's speech in its runtime (`voice: {[line id]: {seconds, words: [{text, start, end}]}}`, word times into the speech). A voiced
|
|
56
|
+
line lasts as long as its speech, after a lead (0.4 s, before a scene's first line) or a hold (0.4 s, between lines) when it gives no
|
|
57
|
+
`at` (`NARRATION`); its `dur` gives way to the speech. Speech that runs past its scene's end, or into the next line that gives its `at`,
|
|
58
|
+
is cut there, and the line says by how much (`over`), for the engine to tell; lines it leaves no time are said where it ends, in none.
|
|
59
|
+
Speech never makes the outline fail, since its length is the engine's; times the author wrote that cannot all hold still do. A voiced line in the outline has `voiced` and its `words`, in the film's seconds. `useLine(id)` gives a
|
|
60
|
+
line's `{start, end, words}` in its scene's time, so a picture can move with what is said; `<Film captions="words">` draws a voiced line
|
|
61
|
+
word by word, each full once it is said.
|
|
62
|
+
- **`<Sound src at dur trim loop volume fadeIn fadeOut duck>`**: a sound the film plays, the file `src` (a path the engine serves), from
|
|
63
|
+
`at` seconds of its scene, or of the film when it is one of `<Film>`'s own; `dur` seconds of it, from `trim` seconds into the file (the
|
|
64
|
+
whole file when left out); with `loop`, again and again to the end of its scene or of the film; at `volume` dB (0 is the file as it is),
|
|
65
|
+
faded in and out over `fadeIn` and `fadeOut` seconds, and `duck` dB lower while a narration speaks. It never plays past the end of its
|
|
66
|
+
scene (or the film). It draws nothing: the engine plays it from the structure, so a `<Sound>` is a child of `<Film>` or a `<Scene>`.
|
|
51
67
|
- Anything else among `<Film>`'s children is drawn across the whole film.
|
|
52
68
|
- `<FilmScene id>` around a film draws its scene `id` alone, on the engine's clock, with the film's own layers on the film's: for an
|
|
53
69
|
engine that places each scene itself. A Remotion project exported from the Open Motion Studio puts each scene in a `Series.Sequence`
|
|
54
70
|
this way, so the scenes are named clips on Remotion Studio's timeline. A scene the film does not have is an error.
|
|
55
71
|
- `filmOutline(children, {width, height})` reads that structure from the elements themselves, without drawing them: `{seconds, scenes:
|
|
56
|
-
[{id, start, duration, layers, lines}], layers
|
|
72
|
+
[{id, start, duration, layers, lines, sounds}], layers, sounds, captions, voice}`, each line `{id, text, start, end}` (and, voiced,
|
|
73
|
+
`voiced`, `words` and `over`) and each sound `{src, at, dur, until, trim, loop, volume, fadeIn, fadeOut,
|
|
74
|
+
duck?}` in the film's seconds (`dur` null for the whole file). A line's and a word's times are in whole milliseconds. `<Film>` reports it to the engine's `onFilm` while it is drawn, once for each structure (a new `onFilm` function on every
|
|
57
75
|
render is not called again for the same structure), when its value has one. A scene without an
|
|
58
|
-
id or a positive duration,
|
|
76
|
+
id or a positive duration, an id used twice (scenes, lines, layers in one scene, the film's own layers), a sound without a `src`, a line
|
|
77
|
+
that starts past its scene's end or before the line before it ends, lines that would be left no time, and a time that is no number of
|
|
78
|
+
seconds, is an error.
|
|
59
79
|
|
|
60
80
|
## Sound
|
|
61
81
|
|
|
62
82
|
`@motionhub/runtime/sound` synthesises a motion's sound, with no React and no audio API: `renderSound({seconds, cues, sound, rate})` returns mono samples from the motion's `react/sound.json` (a bed at a tempo, and a voice for each role: `land`, `move`, `draw`, `count`, `cut`, `out`) and a list of cues (`[{at, role, dur}]`, a reel's `export const cues`). The same input gives the same samples, so a player plays them and a renderer can write them to a WAV for Remotion's `<Audio>` or a HyperFrames track.
|
|
63
83
|
|
|
64
|
-
An engine binding passes `<MotionRuntimeProvider value>` an object with `useTime` and `useCompositionTime` (hooks returning seconds), `fps`, `width`, `height`, `tokens` (resolved), if it plays video `Video`,
|
|
84
|
+
An engine binding passes `<MotionRuntimeProvider value>` an object with `useTime` and `useCompositionTime` (hooks returning seconds), `fps`, `width`, `height`, `tokens` (resolved), if it plays video `Video`, if it wants a `<Film>`'s structure, `onFilm(outline)`, and, if it speaks the film's lines, `voice`.
|
package/index.d.ts
CHANGED
|
@@ -28,6 +28,8 @@ export type MotionRuntime = {
|
|
|
28
28
|
Video?: FC<VideoProps>;
|
|
29
29
|
/** called with a <Film>'s structure when it is drawn */
|
|
30
30
|
onFilm?(outline: FilmOutline): void;
|
|
31
|
+
/** Each voiced line's speech, by line id: how long it is and its words, each from and to seconds into it */
|
|
32
|
+
voice?: Record<string, {seconds: number; words?: {text: string; start: number; end: number}[]}>;
|
|
31
33
|
};
|
|
32
34
|
export declare const MotionRuntimeProvider: FC<{value: MotionRuntime; children?: ReactNode}>;
|
|
33
35
|
|
|
@@ -40,7 +42,8 @@ export declare const Video: FC<VideoProps>;
|
|
|
40
42
|
export declare const MotionRoot: FC<{tokens?: Tokens; style?: CSSProperties; children?: ReactNode}>;
|
|
41
43
|
/** Its children drawn in another motion: its tokens (a tokens.json, or resolved), keeping the engine's time; one per scene or part of a frame */
|
|
42
44
|
export declare const MotionScope: FC<{tokens: unknown; root?: boolean; children?: ReactNode}>;
|
|
43
|
-
|
|
45
|
+
/** One spoken line, styled by the caption tokens; with `line`, that line of the film's, while it is said */
|
|
46
|
+
export declare const Caption: FC<{line?: string; words?: boolean; children?: ReactNode; opacity?: number; style?: CSSProperties}>;
|
|
44
47
|
|
|
45
48
|
export type EasingFn = (x: number) => number;
|
|
46
49
|
export declare const Easing: {linear: EasingFn; bezier(x1: number, y1: number, x2: number, y2: number): EasingFn; inOutSine: EasingFn; outCubic: EasingFn; outQuart: EasingFn; inOutCubic: EasingFn; outBack: EasingFn};
|
|
@@ -49,19 +52,51 @@ export declare const Sequence: FC<{from?: number; duration?: number; children?:
|
|
|
49
52
|
|
|
50
53
|
/** A box of the frame, in its pixels */
|
|
51
54
|
export type LayerBox = {id: string; x: number; y: number; w: number; h: number; z: number};
|
|
52
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* A <Sound> as the engine plays it, in the film's seconds: from `at`, `dur` seconds of the file from `trim` (null: the whole file, or with
|
|
57
|
+
* `loop` until `until`), never past `until` (the end of its scene, or of the film); `volume` and `duck` in dB, fades in seconds
|
|
58
|
+
*/
|
|
59
|
+
export type FilmSound = {src: string; at: number; dur: number | null; until: number; trim: number; loop: boolean; volume: number; fadeIn: number; fadeOut: number; duck?: number};
|
|
60
|
+
/** A <Film>'s structure: its length, its scenes in order (each from `start` seconds), and its own layers and sounds, across every scene */
|
|
53
61
|
export type FilmOutline = {
|
|
54
62
|
seconds: number;
|
|
55
|
-
scenes: {id: string; start: number; duration: number; layers: LayerBox[]; lines:
|
|
63
|
+
scenes: {id: string; start: number; duration: number; layers: LayerBox[]; lines: FilmLine[]; sounds: FilmSound[]}[];
|
|
56
64
|
layers: LayerBox[];
|
|
65
|
+
sounds: FilmSound[];
|
|
66
|
+
/** Whether the film draws its lines as captions, whole or word by word */
|
|
67
|
+
captions: boolean | 'words';
|
|
68
|
+
/** The film's voice, `provider:name`, or null */
|
|
69
|
+
voice: string | null;
|
|
57
70
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
/** A word of a voiced line, and when it is said */
|
|
72
|
+
export type FilmWord = {text: string; start: number; end: number};
|
|
73
|
+
/**
|
|
74
|
+
* A line of a scene, and when it is said, in the film's seconds; a voiced one with its words, and `over`, the seconds of its speech
|
|
75
|
+
* past its scene's end
|
|
76
|
+
*/
|
|
77
|
+
export type FilmLine = {id: string; text: string; start: number; end: number; voiced?: true; words?: FilmWord[]; over?: number};
|
|
78
|
+
/** How long a scene waits before its first spoken line, and between spoken lines, in seconds */
|
|
79
|
+
export declare const NARRATION: {lead: number; hold: number};
|
|
80
|
+
/** When a line is said, in its scene's time (as useTime() counts there), with its words where it is voiced */
|
|
81
|
+
export declare function useLine(id: string): {start: number; end: number; words: FilmWord[]};
|
|
82
|
+
export declare function filmOutline(
|
|
83
|
+
children: ReactNode,
|
|
84
|
+
frame?: {width?: number; height?: number; captions?: boolean | 'words'; voice?: MotionRuntime['voice']; speaker?: string},
|
|
85
|
+
): FilmOutline;
|
|
86
|
+
/**
|
|
87
|
+
* Its <Scene>s laid end to end, each drawn in its span on its own clock; anything else drawn across them all; with `captions`, its lines as
|
|
88
|
+
* captions (`"words"`: word by word, where they are voiced); `voice`, the voice its lines are spoken in (`provider:name`)
|
|
89
|
+
*/
|
|
90
|
+
export declare const Film: FC<{captions?: boolean | 'words'; voice?: string; children?: ReactNode}>;
|
|
61
91
|
export declare const Scene: FC<{id: string; duration: number; children?: ReactNode}>;
|
|
62
92
|
/** A box of the frame (`x`, `y`, `w`, `h` in pixels, the whole frame when left out), stacked by `z`, shown for its whole scene */
|
|
63
93
|
export declare const Layer: FC<{id: string; x?: number; y?: number; w?: number; h?: number; z?: number; style?: CSSProperties; children?: ReactNode}>;
|
|
64
94
|
/** The <Film> under it drawn as its scene `id` alone, on the engine's clock (its own layers on the film's), for an engine that places each scene */
|
|
65
95
|
export declare const FilmScene: FC<{id: string; children?: ReactNode}>;
|
|
66
|
-
/** What a scene says; it draws nothing */
|
|
67
|
-
export declare const Line: FC<{id: string; text?: string; children?: ReactNode}>;
|
|
96
|
+
/** What a scene says, from `at` seconds into it for `dur` seconds (shared evenly when left out); it draws nothing */
|
|
97
|
+
export declare const Line: FC<{id: string; text?: string; at?: number; dur?: number; children?: ReactNode}>;
|
|
98
|
+
/**
|
|
99
|
+
* A sound the film plays, from `at` seconds of its scene (or the film), `dur` seconds of it from `trim` into the file, looped with `loop`, at
|
|
100
|
+
* `volume` dB, faded over `fadeIn`/`fadeOut` seconds, `duck` dB lower under a narration. It draws nothing; the engine plays it
|
|
101
|
+
*/
|
|
102
|
+
export declare const Sound: FC<{src: string; at?: number; dur?: number; trim?: number; loop?: boolean; volume?: number; fadeIn?: number; fadeOut?: number; duck?: number}>;
|
package/index.js
CHANGED
|
@@ -123,13 +123,141 @@ const named = (what, id) => {
|
|
|
123
123
|
return id;
|
|
124
124
|
};
|
|
125
125
|
|
|
126
|
+
/** Seconds as an error says them */
|
|
127
|
+
const round = (value) => Math.round(value * 1000) / 1000;
|
|
128
|
+
|
|
129
|
+
/** A finite number of seconds from `min` (above it when `above`), or `fallback` when left out */
|
|
130
|
+
const seconds = (where, name, value, fallback, {min = 0, above = false} = {}) => {
|
|
131
|
+
if (value === undefined) return fallback;
|
|
132
|
+
if (!(Number.isFinite(value) && (above ? value > min : value >= min))) throw new Error(`${where}: ${name} is a number of seconds ${above ? 'above' : 'from'} ${min}`);
|
|
133
|
+
return value;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A <Sound> as an engine plays it, in the film's time: from `at` (its parent's start, a scene's or the film's, plus its own `at`), for
|
|
138
|
+
* `dur` seconds of its file (null: the whole file, or with `loop` until `until`), never past `until` (the end of its scene, or of the film).
|
|
139
|
+
*/
|
|
140
|
+
const sound = (e, start, until) => {
|
|
141
|
+
const {src, at, dur, trim, loop = false, volume = 0, fadeIn, fadeOut, duck} = e.props;
|
|
142
|
+
if (typeof src !== 'string' || !src) throw new Error('a <Sound> needs a src: the file it plays');
|
|
143
|
+
const where = `<Sound src="${src}">`;
|
|
144
|
+
if (!Number.isFinite(volume)) throw new Error(`${where}: volume is in dB, 0 for the file as it is`);
|
|
145
|
+
if (duck !== undefined && !(Number.isFinite(duck) && duck >= 0)) throw new Error(`${where}: duck is how many dB it drops under a narration, 0 or more`);
|
|
146
|
+
return {
|
|
147
|
+
src,
|
|
148
|
+
at: start + seconds(where, 'at', at, 0),
|
|
149
|
+
dur: seconds(where, 'dur', dur, null, {above: true}),
|
|
150
|
+
until,
|
|
151
|
+
trim: seconds(where, 'trim', trim, 0),
|
|
152
|
+
loop: loop === true,
|
|
153
|
+
volume,
|
|
154
|
+
fadeIn: seconds(where, 'fadeIn', fadeIn, 0),
|
|
155
|
+
fadeOut: seconds(where, 'fadeOut', fadeOut, 0),
|
|
156
|
+
...(duck !== undefined ? {duck} : {}),
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** How long a scene waits before its first spoken line, and between spoken lines, in seconds */
|
|
161
|
+
export const NARRATION = {lead: 0.4, hold: 0.4};
|
|
162
|
+
|
|
163
|
+
/** A line's clip as the engine gives it (`voice[id]`): its length in seconds and its words, each from and to seconds into it */
|
|
164
|
+
const clipOf = (where, clip) => {
|
|
165
|
+
if (clip === undefined) return undefined;
|
|
166
|
+
if (!clip || !(Number.isFinite(clip.seconds) && clip.seconds > 0)) throw new Error(`${where}: its voice is no length of speech`);
|
|
167
|
+
// Within the speech, and each ending after it starts: an engine's word times can be a little off.
|
|
168
|
+
const within = (t) => Math.min(clip.seconds, Math.max(0, t));
|
|
169
|
+
const words = Array.isArray(clip.words)
|
|
170
|
+
? clip.words
|
|
171
|
+
.filter((w) => w && typeof w.text === 'string' && Number.isFinite(w.start) && Number.isFinite(w.end) && w.end >= w.start)
|
|
172
|
+
.map((w) => ({text: w.text, start: within(w.start), end: within(w.end)}))
|
|
173
|
+
: [];
|
|
174
|
+
return {seconds: clip.seconds, words};
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* When each of a scene's lines is said, `start` and `end` in the film's seconds. A line's `at` is seconds into its scene,
|
|
179
|
+
* and without one it starts where the line before it ends (the first, at the scene's start); a line without `dur` takes an
|
|
180
|
+
* equal share of the time to the next line that gives its `at`, or to the scene's end. So lines that give no time share
|
|
181
|
+
* their scene evenly. A line the engine has a voice for (`voice[id]`) lasts as long as its speech, after a lead (the first)
|
|
182
|
+
* or a hold (the rest) when it gives no `at`; speech that runs past the scene's end is cut there, and `over` says by how much.
|
|
183
|
+
*/
|
|
184
|
+
const timeLines = (lines, start, duration, voice = {}) => {
|
|
185
|
+
const given = lines.map((e, i) => {
|
|
186
|
+
const where = `<Line id="${e.props.id}">`;
|
|
187
|
+
const at = seconds(where, 'at', e.props.at, undefined);
|
|
188
|
+
if (at !== undefined && at >= duration) throw new Error(`${where}: at is ${at} s, past the end of its scene (${duration} s)`);
|
|
189
|
+
const clip = clipOf(where, voice[e.props.id]);
|
|
190
|
+
if (clip) return {where, at, dur: clip.seconds, gap: at === undefined ? (i === 0 ? NARRATION.lead : NARRATION.hold) : 0, clip};
|
|
191
|
+
return {where, at, dur: seconds(where, 'dur', e.props.dur, undefined, {above: true}), gap: 0};
|
|
192
|
+
});
|
|
193
|
+
// Lines are said in the order they are written.
|
|
194
|
+
let last = -Infinity;
|
|
195
|
+
for (const g of given) {
|
|
196
|
+
if (g.at === undefined) continue;
|
|
197
|
+
if (g.at < last) throw new Error(`${g.where}: at is ${g.at} s, before the line before it (at ${round(last)} s)`);
|
|
198
|
+
last = g.at;
|
|
199
|
+
}
|
|
200
|
+
const times = [];
|
|
201
|
+
let cursor = 0;
|
|
202
|
+
for (let i = 0; i < given.length;) {
|
|
203
|
+
// The run of lines from here to the next that gives its at: what they do not give, they share.
|
|
204
|
+
let next = i + 1;
|
|
205
|
+
while (next < given.length && given[next].at === undefined) next++;
|
|
206
|
+
const from = given[i].at ?? cursor;
|
|
207
|
+
// Lines are said one after another: one that starts before the one before it ends would be hidden under it.
|
|
208
|
+
if (from < cursor - 1e-9) throw new Error(`${given[i].where}: at is ${from} s, before the line before it ends (${round(cursor)} s into the scene)`);
|
|
209
|
+
const to = next < given.length ? given[next].at : duration;
|
|
210
|
+
const run = given.slice(i, next);
|
|
211
|
+
const fixed = run.reduce((sum, g) => sum + (g.dur ?? 0) + g.gap, 0);
|
|
212
|
+
const free = run.filter((g) => g.dur === undefined);
|
|
213
|
+
const left = to - from - fixed;
|
|
214
|
+
// Lines that give no time would be said in none, and never shown: the author's to fix, unless it is speech that takes the time,
|
|
215
|
+
// whose length the author does not write. Then they are said where it ends, in what is left, and none if nothing is.
|
|
216
|
+
if (free.length && left <= 1e-9 && !run.some((g) => g.clip)) {
|
|
217
|
+
throw new Error(`${free[0].where}: no time is left for it before ${next < given.length ? `${given[next].where} at ${to} s` : `the end of its scene (${to} s)`}; give it an at, or shorten the lines before it`);
|
|
218
|
+
}
|
|
219
|
+
const share = free.length ? Math.max(0, left) / free.length : 0;
|
|
220
|
+
cursor = from;
|
|
221
|
+
for (const g of run) {
|
|
222
|
+
// Speech is cut where the next line that gives its at begins, or the scene ends, and says by how much (over); a dur the
|
|
223
|
+
// author wrote runs on, and one that runs into the next line is an error there.
|
|
224
|
+
const limit = g.clip ? to : duration;
|
|
225
|
+
const begin = Math.min(limit, cursor + g.gap);
|
|
226
|
+
const wanted = begin + (g.dur ?? share);
|
|
227
|
+
const end = Math.min(limit, wanted);
|
|
228
|
+
const at = start + begin;
|
|
229
|
+
// In whole milliseconds, so the times read as written and add up as they should.
|
|
230
|
+
times.push({
|
|
231
|
+
start: round(at),
|
|
232
|
+
end: round(start + end),
|
|
233
|
+
...(g.clip
|
|
234
|
+
? {
|
|
235
|
+
voiced: true,
|
|
236
|
+
words: g.clip.words
|
|
237
|
+
.filter((w) => begin + w.start < end)
|
|
238
|
+
.map((w) => ({text: w.text, start: round(at + w.start), end: round(at + Math.min(w.end, end - begin))})),
|
|
239
|
+
...(wanted - limit > 1e-9 ? {over: round(wanted - limit)} : {}),
|
|
240
|
+
}
|
|
241
|
+
: {}),
|
|
242
|
+
});
|
|
243
|
+
cursor = end;
|
|
244
|
+
}
|
|
245
|
+
i = next;
|
|
246
|
+
}
|
|
247
|
+
return times;
|
|
248
|
+
};
|
|
249
|
+
|
|
126
250
|
/**
|
|
127
251
|
* The structure of a <Film>'s children, read from the elements themselves without drawing them, so it is known before the first frame:
|
|
128
|
-
* `{seconds, scenes: [{id, start, duration, layers, lines}], layers}`, a layer `{id, x, y, w, h, z}` in the frame's
|
|
129
|
-
* fills the frame), a line `{id, text}
|
|
130
|
-
*
|
|
252
|
+
* `{seconds, scenes: [{id, start, duration, layers, lines, sounds}], layers, sounds, captions}`, a layer `{id, x, y, w, h, z}` in the frame's
|
|
253
|
+
* pixels (a box left out fills the frame), a line `{id, text, start, end}` (when it is said, in the film's seconds), a sound `{src, at, dur, until, trim, loop, volume, fadeIn, fadeOut, duck?}` in the
|
|
254
|
+
* film's seconds. `layers` and `sounds` are the film's own, across every scene. A scene without an id or a positive duration, an id used
|
|
255
|
+
* twice where it must be unique, and a sound with no file or a time that is no number of seconds, are errors.
|
|
131
256
|
*/
|
|
132
|
-
export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
257
|
+
export function filmOutline(children, {width = 1920, height = 1080, captions = false, voice = {}, speaker} = {}) {
|
|
258
|
+
if (speaker !== undefined && !(typeof speaker === 'string' && /^[\w.-]+:\S+$/.test(speaker))) {
|
|
259
|
+
throw new Error(`<Film voice="${speaker}">: a voice is its provider and its name, as voice="edge:en-US-AriaNeural"`);
|
|
260
|
+
}
|
|
133
261
|
const box = (e) => ({id: named('Layer', e.props.id), x: e.props.x ?? 0, y: e.props.y ?? 0, w: e.props.w ?? width, h: e.props.h ?? height, z: e.props.z ?? 0});
|
|
134
262
|
const unique = (what, items) => {
|
|
135
263
|
const seen = new Set();
|
|
@@ -141,6 +269,8 @@ export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
|
141
269
|
};
|
|
142
270
|
const scenes = [];
|
|
143
271
|
const layers = [];
|
|
272
|
+
// The film's own sounds wait for its length, which is known once every scene is read.
|
|
273
|
+
const own = [];
|
|
144
274
|
let start = 0;
|
|
145
275
|
for (const e of elements(children)) {
|
|
146
276
|
if (!isValidElement(e)) continue;
|
|
@@ -149,11 +279,16 @@ export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
|
149
279
|
const {duration} = e.props;
|
|
150
280
|
if (!(Number.isFinite(duration) && duration > 0)) throw new Error(`<Scene id="${id}"> needs its duration in seconds: duration={4}`);
|
|
151
281
|
const inner = elements(e.props.children).filter(isValidElement);
|
|
152
|
-
const
|
|
153
|
-
|
|
282
|
+
const said = inner.filter((c) => c.type === Line);
|
|
283
|
+
const times = timeLines(said, start, duration, voice ?? {});
|
|
284
|
+
const lines = said.map((c, i) => ({id: named('Line', c.props.id), text: c.props.text ?? words(c.props.children), ...times[i]}));
|
|
285
|
+
const sounds = inner.filter((c) => c.type === Sound).map((c) => sound(c, start, start + duration));
|
|
286
|
+
scenes.push({id, start, duration, layers: unique(`layers in scene "${id}"`, inner.filter((c) => c.type === Layer).map(box)), lines, sounds});
|
|
154
287
|
start += duration;
|
|
155
288
|
} else if (e.type === Layer) {
|
|
156
289
|
layers.push(box(e));
|
|
290
|
+
} else if (e.type === Sound) {
|
|
291
|
+
own.push(e);
|
|
157
292
|
}
|
|
158
293
|
}
|
|
159
294
|
if (!scenes.length) throw new Error('a <Film> needs at least one <Scene>');
|
|
@@ -165,7 +300,14 @@ export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
|
165
300
|
const taken = scene.layers.find((layer) => layers.some((own) => own.id === layer.id));
|
|
166
301
|
if (taken) throw new Error(`scene "${scene.id}" has a layer called "${taken.id}", the name of one of the film's own layers`);
|
|
167
302
|
}
|
|
168
|
-
return {
|
|
303
|
+
return {
|
|
304
|
+
seconds: start,
|
|
305
|
+
scenes,
|
|
306
|
+
layers,
|
|
307
|
+
sounds: own.map((e) => sound(e, 0, start)),
|
|
308
|
+
captions: captions === true || captions === 'words' ? captions : false,
|
|
309
|
+
voice: speaker ?? null,
|
|
310
|
+
};
|
|
169
311
|
}
|
|
170
312
|
|
|
171
313
|
/**
|
|
@@ -173,12 +315,12 @@ export function filmOutline(children, {width = 1920, height = 1080} = {}) {
|
|
|
173
315
|
* at the film's end), and anything else among its children (a <Layer>, a background) drawn across them all. It reports its structure
|
|
174
316
|
* (filmOutline) to the engine's `onFilm`, if the engine has one, while it is drawn: the engine learns the length from the scenes.
|
|
175
317
|
*/
|
|
176
|
-
export const Film = ({children}) => {
|
|
318
|
+
export const Film = ({captions = false, voice, children}) => {
|
|
177
319
|
const r = runtime();
|
|
178
320
|
// Read on every frame (it is cheap: a film's children are new elements each frame), and reported while drawing, once for each
|
|
179
321
|
// structure, whether or not the engine's onFilm is the same function each time: an engine that renders the film once, off the
|
|
180
322
|
// page, learns it before the first frame is shown.
|
|
181
|
-
const outline = filmOutline(children, r);
|
|
323
|
+
const outline = filmOutline(children, {...r, captions, voice: r.voice, speaker: voice});
|
|
182
324
|
const reported = JSON.stringify(outline);
|
|
183
325
|
const onFilm = r.onFilm;
|
|
184
326
|
useMemo(() => onFilm?.(JSON.parse(reported)), [reported]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
@@ -190,8 +332,12 @@ export const Film = ({children}) => {
|
|
|
190
332
|
// Drawn one scene at a time (an engine that places each scene itself, as Remotion does in a <Series>), the scene keeps the engine's clock
|
|
191
333
|
// and the rest of the film, its own layers, is drawn on the film's.
|
|
192
334
|
const engine = only === null ? r : {...r, useTime: r.useCompositionTime};
|
|
335
|
+
// The film's time, for its lines: the engine's, or, drawn as one scene, that scene's start on from the engine's.
|
|
336
|
+
const now = only === null ? t : (outline.scenes.find((scene) => scene.id === only)?.start ?? 0) + t;
|
|
337
|
+
const lines = outline.scenes.flatMap((scene) => scene.lines.map((line) => ({...line, scene: scene.start})));
|
|
338
|
+
const spoken = lines.find((line) => now >= line.start && now < line.end);
|
|
193
339
|
let index = 0;
|
|
194
|
-
return createElement(Runtime.Provider, {value: engine}, createElement(InFilm.Provider, {value: true}, elements(children).map((e) => {
|
|
340
|
+
return createElement(Runtime.Provider, {value: engine}, createElement(InFilm.Provider, {value: true}, createElement(Said.Provider, {value: {lines, now}}, elements(children).map((e) => {
|
|
195
341
|
if (!isValidElement(e) || e.type !== Scene) return e;
|
|
196
342
|
const scene = outline.scenes[index++];
|
|
197
343
|
const key = e.key ?? scene.id;
|
|
@@ -200,9 +346,27 @@ export const Film = ({children}) => {
|
|
|
200
346
|
if (t < scene.start || (t >= scene.start + scene.duration && !(last && t <= outline.seconds))) return null;
|
|
201
347
|
const value = {...r, useTime: () => r.useTime() - scene.start};
|
|
202
348
|
return createElement(Runtime.Provider, {key, value}, e);
|
|
203
|
-
})
|
|
349
|
+
}), captions && spoken ? createElement('div', {key: 'captions', style: {position: 'absolute', inset: 0, zIndex: 2147483647, pointerEvents: 'none'}},
|
|
350
|
+
createElement(Caption, {line: spoken.id, words: outline.captions === 'words'})) : null)));
|
|
204
351
|
};
|
|
205
352
|
|
|
353
|
+
/** The film's lines and its time, for a <Caption line> anywhere in it */
|
|
354
|
+
const Said = createContext(null);
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* When a line of the film is said, in the time of its scene (as useTime() counts there): `{start, end, words}`, each word `{text, start,
|
|
358
|
+
* end}` where the engine has its voice (none otherwise). So a picture can move with what is said: `const {start} = useLine('intro-1')`.
|
|
359
|
+
*/
|
|
360
|
+
export const useLine = (id) => {
|
|
361
|
+
const said = useContext(Said);
|
|
362
|
+
const line = said?.lines.find((l) => l.id === id);
|
|
363
|
+
if (!line) throw new Error(`useLine("${id}"): ${said ? 'the film has no line by that id' : 'a line is read inside a <Film>'}`);
|
|
364
|
+
const shift = (t) => round(t - line.scene);
|
|
365
|
+
return {start: shift(line.start), end: shift(line.end), words: (line.words ?? []).map((w) => ({text: w.text, start: shift(w.start), end: shift(w.end)}))};
|
|
366
|
+
};
|
|
367
|
+
/** How long a line's caption takes to come and go, in seconds */
|
|
368
|
+
const CAPTION_FADE = 0.15;
|
|
369
|
+
|
|
206
370
|
const OnlyScene = createContext(null);
|
|
207
371
|
/**
|
|
208
372
|
* The <Film> under it drawn as its scene `id` alone, on the engine's clock, with the film's own layers on the film's: for an engine that places
|
|
@@ -232,6 +396,14 @@ export const Layer = ({id, x = 0, y = 0, w, h, z = 0, style, children}) => {
|
|
|
232
396
|
/** What a scene says: its words (the children, or `text` where the caption differs). It draws nothing; an engine may speak or caption it */
|
|
233
397
|
export const Line = () => null;
|
|
234
398
|
|
|
399
|
+
/**
|
|
400
|
+
* A sound the film plays: the file `src`, from `at` seconds of its scene (of the film, for one of the film's own), `dur` seconds of it from
|
|
401
|
+
* `trim` seconds into the file (the whole file without `dur`; with `loop`, again and again to the end of its scene or the film), at `volume`
|
|
402
|
+
* dB (0: as the file is), faded in and out over `fadeIn` and `fadeOut` seconds, and `duck` dB lower while a narration speaks. It draws
|
|
403
|
+
* nothing: the engine plays it, from the film's structure.
|
|
404
|
+
*/
|
|
405
|
+
export const Sound = () => null;
|
|
406
|
+
|
|
235
407
|
/** The ground a motion's components expect: the tokens as custom properties, filling the frame */
|
|
236
408
|
export const MotionRoot = ({tokens, style, children}) => createElement('div', {
|
|
237
409
|
style: {position: 'absolute', inset: 0, overflow: 'hidden', ...tokenVariables(tokens), ...style},
|
|
@@ -257,10 +429,24 @@ export const MotionScope = ({tokens, root = true, children}) => {
|
|
|
257
429
|
/**
|
|
258
430
|
* The motion's caption: one spoken line on the frame, drawn from the caption group of its tokens: font, size, weight, color, background, radius,
|
|
259
431
|
* padding-y, padding-x, bottom, max-width, shadow (a text shadow), and a rule: rule-left (a bar down its left side) or rule-top (a short bar above it).
|
|
260
|
-
* `opacity` fades it; it stays a function of what you pass.
|
|
432
|
+
* `opacity` fades it; it stays a function of what you pass. With `line`, the id of one of its <Film>'s lines, it draws that line's words while
|
|
433
|
+
* it is said, fading in and out, and nothing at other times.
|
|
261
434
|
*/
|
|
262
|
-
export const Caption = ({children, opacity = 1, style}) => {
|
|
435
|
+
export const Caption = ({line, words = false, children, opacity = 1, style}) => {
|
|
436
|
+
// Both read before anything branches, so a caption calls the same hooks with a line or without.
|
|
437
|
+
const said = useContext(Said);
|
|
263
438
|
const c = useTokens().caption ?? {};
|
|
439
|
+
if (line !== undefined) {
|
|
440
|
+
const spoken = said?.lines.find((l) => l.id === line);
|
|
441
|
+
if (!spoken) throw new Error(`<Caption line="${line}">: ${said ? 'the film has no line by that id' : 'a caption of a line is drawn inside a <Film>'}`);
|
|
442
|
+
const fade = Math.min(1, (said.now - spoken.start) / CAPTION_FADE, (spoken.end - said.now) / CAPTION_FADE);
|
|
443
|
+
if (fade <= 0) return null;
|
|
444
|
+
// Word by word, where the line is voiced: each word full once it is said, the rest waiting faint.
|
|
445
|
+
const shown = words && spoken.words?.length
|
|
446
|
+
? spoken.words.map((w, i) => createElement('span', {key: i, style: {opacity: said.now >= w.start ? 1 : 0.35}}, `${i ? ' ' : ''}${w.text}`))
|
|
447
|
+
: spoken.text;
|
|
448
|
+
return createElement(Caption, {opacity: opacity * fade, style}, children ?? shown);
|
|
449
|
+
}
|
|
264
450
|
const py = c['padding-y'] ?? 10, pad = `${py}px ${c['padding-x'] ?? 26}px`;
|
|
265
451
|
const rule = c['rule-left']
|
|
266
452
|
? createElement('div', {style: {position: 'absolute', left: 0, top: py * 0.6, bottom: py * 0.6, width: 4, borderRadius: 2, background: c['rule-left']}})
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motionhub/runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "What a motion's React components draw with (time, frame, tokens, seeded randomness, video, caption), in any engine that provides it.",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "What a motion's React components draw with (time, frame, tokens, seeded randomness, video, caption), and a film of scenes and sounds, in any engine that provides it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/motionhub-dev/
|
|
8
|
+
"url": "git+https://github.com/motionhub-dev/motionhub.git",
|
|
9
9
|
"directory": "packages/runtime"
|
|
10
10
|
},
|
|
11
11
|
"homepage": "https://motionhub.dev",
|