@openartifactsai/motion-stage 0.0.0-stage → 0.23.0-rc.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.
Files changed (225) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +70 -4
  5. package/src/build.d.ts +18 -0
  6. package/src/build.js +73 -0
  7. package/src/host.d.ts +37 -0
  8. package/src/host.js +76 -0
  9. package/src/index.d.ts +11 -0
  10. package/src/index.js +6 -0
  11. package/src/layers.d.ts +30 -0
  12. package/src/layers.js +56 -0
  13. package/src/offline/audio.d.ts +19 -0
  14. package/src/offline/audio.js +20 -0
  15. package/src/offline/data.d.ts +26 -0
  16. package/src/offline/data.js +71 -0
  17. package/src/offline/main.d.ts +9 -0
  18. package/src/offline/main.js +52 -0
  19. package/src/offline/player.d.ts +23 -0
  20. package/src/offline/player.js +157 -0
  21. package/src/protocol/clip-guards.d.ts +5 -0
  22. package/src/protocol/clip-guards.js +22 -0
  23. package/src/protocol/guards.d.ts +6 -0
  24. package/src/protocol/guards.js +257 -0
  25. package/src/protocol/layer-guards.d.ts +7 -0
  26. package/src/protocol/layer-guards.js +100 -0
  27. package/src/protocol/layers.d.ts +135 -0
  28. package/src/protocol/layers.js +1 -0
  29. package/src/protocol/types.d.ts +340 -0
  30. package/src/protocol/types.js +7 -0
  31. package/src/protocol.d.ts +2 -0
  32. package/src/protocol.js +2 -0
  33. package/src/raster.d.ts +4 -0
  34. package/src/raster.js +2 -0
  35. package/src/runtime/assets.d.ts +19 -0
  36. package/src/runtime/assets.js +63 -0
  37. package/src/runtime/caption-box.d.ts +3 -0
  38. package/src/runtime/caption-box.js +16 -0
  39. package/src/runtime/captions.d.ts +31 -0
  40. package/src/runtime/captions.js +115 -0
  41. package/src/runtime/clip-captions.d.ts +9 -0
  42. package/src/runtime/clip-captions.js +27 -0
  43. package/src/runtime/clips.d.ts +83 -0
  44. package/src/runtime/clips.js +259 -0
  45. package/src/runtime/console.d.ts +13 -0
  46. package/src/runtime/console.js +99 -0
  47. package/src/runtime/cut.d.ts +7 -0
  48. package/src/runtime/cut.js +63 -0
  49. package/src/runtime/determinism.d.ts +12 -0
  50. package/src/runtime/determinism.js +47 -0
  51. package/src/runtime/film.d.ts +43 -0
  52. package/src/runtime/film.js +31 -0
  53. package/src/runtime/focus.d.ts +8 -0
  54. package/src/runtime/focus.js +71 -0
  55. package/src/runtime/fonts.d.ts +22 -0
  56. package/src/runtime/fonts.js +109 -0
  57. package/src/runtime/frame.d.ts +9 -0
  58. package/src/runtime/frame.js +140 -0
  59. package/src/runtime/fx/backdrop.d.ts +60 -0
  60. package/src/runtime/fx/backdrop.js +264 -0
  61. package/src/runtime/fx/color.d.ts +19 -0
  62. package/src/runtime/fx/color.js +84 -0
  63. package/src/runtime/fx/css-color.d.ts +6 -0
  64. package/src/runtime/fx/css-color.js +44 -0
  65. package/src/runtime/fx/curve.d.ts +12 -0
  66. package/src/runtime/fx/curve.js +34 -0
  67. package/src/runtime/fx/gl.d.ts +29 -0
  68. package/src/runtime/fx/gl.js +207 -0
  69. package/src/runtime/fx/glsl.d.ts +35 -0
  70. package/src/runtime/fx/glsl.js +113 -0
  71. package/src/runtime/fx/index.d.ts +23 -0
  72. package/src/runtime/fx/index.js +151 -0
  73. package/src/runtime/fx/key.d.ts +11 -0
  74. package/src/runtime/fx/key.js +21 -0
  75. package/src/runtime/fx/numbers.d.ts +14 -0
  76. package/src/runtime/fx/numbers.js +57 -0
  77. package/src/runtime/fx/pace.d.ts +17 -0
  78. package/src/runtime/fx/pace.js +26 -0
  79. package/src/runtime/fx/paint.d.ts +28 -0
  80. package/src/runtime/fx/paint.js +150 -0
  81. package/src/runtime/guard.d.ts +4 -0
  82. package/src/runtime/guard.js +141 -0
  83. package/src/runtime/hand-titles/index.d.ts +9 -0
  84. package/src/runtime/hand-titles/index.js +73 -0
  85. package/src/runtime/hand-titles/layer.d.ts +20 -0
  86. package/src/runtime/hand-titles/layer.js +118 -0
  87. package/src/runtime/hand-titles/place.d.ts +27 -0
  88. package/src/runtime/hand-titles/place.js +45 -0
  89. package/src/runtime/held-store.d.ts +35 -0
  90. package/src/runtime/held-store.js +63 -0
  91. package/src/runtime/held.d.ts +19 -0
  92. package/src/runtime/held.js +225 -0
  93. package/src/runtime/kit/camera.d.ts +59 -0
  94. package/src/runtime/kit/camera.js +81 -0
  95. package/src/runtime/kit/chip.d.ts +25 -0
  96. package/src/runtime/kit/chip.js +75 -0
  97. package/src/runtime/kit/count.d.ts +37 -0
  98. package/src/runtime/kit/count.js +100 -0
  99. package/src/runtime/kit/cursor.d.ts +46 -0
  100. package/src/runtime/kit/cursor.js +108 -0
  101. package/src/runtime/kit/dom.d.ts +14 -0
  102. package/src/runtime/kit/dom.js +46 -0
  103. package/src/runtime/kit/geometry.d.ts +24 -0
  104. package/src/runtime/kit/geometry.js +56 -0
  105. package/src/runtime/kit/index.d.ts +113 -0
  106. package/src/runtime/kit/index.js +157 -0
  107. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  108. package/src/runtime/kit/mark-strokes.js +226 -0
  109. package/src/runtime/kit/marks.d.ts +21 -0
  110. package/src/runtime/kit/marks.js +165 -0
  111. package/src/runtime/kit/math.d.ts +23 -0
  112. package/src/runtime/kit/math.js +35 -0
  113. package/src/runtime/kit/paper.d.ts +7 -0
  114. package/src/runtime/kit/paper.js +27 -0
  115. package/src/runtime/kit/random.d.ts +8 -0
  116. package/src/runtime/kit/random.js +32 -0
  117. package/src/runtime/kit/text-fx.d.ts +18 -0
  118. package/src/runtime/kit/text-fx.js +55 -0
  119. package/src/runtime/kit/typing.d.ts +21 -0
  120. package/src/runtime/kit/typing.js +42 -0
  121. package/src/runtime/laid/layer.d.ts +44 -0
  122. package/src/runtime/laid/layer.js +370 -0
  123. package/src/runtime/layers/describe.d.ts +41 -0
  124. package/src/runtime/layers/describe.js +327 -0
  125. package/src/runtime/layers/item.d.ts +123 -0
  126. package/src/runtime/layers/item.js +150 -0
  127. package/src/runtime/layers/parts.d.ts +53 -0
  128. package/src/runtime/layers/parts.js +158 -0
  129. package/src/runtime/layers/rows.d.ts +4 -0
  130. package/src/runtime/layers/rows.js +233 -0
  131. package/src/runtime/layers/scan.d.ts +24 -0
  132. package/src/runtime/layers/scan.js +204 -0
  133. package/src/runtime/layers/walk.d.ts +16 -0
  134. package/src/runtime/layers/walk.js +337 -0
  135. package/src/runtime/load.d.ts +10 -0
  136. package/src/runtime/load.js +267 -0
  137. package/src/runtime/main.d.ts +1 -0
  138. package/src/runtime/main.js +90 -0
  139. package/src/runtime/modules.d.ts +23 -0
  140. package/src/runtime/modules.js +53 -0
  141. package/src/runtime/port.d.ts +8 -0
  142. package/src/runtime/port.js +46 -0
  143. package/src/runtime/questions.d.ts +24 -0
  144. package/src/runtime/questions.js +162 -0
  145. package/src/runtime/recut.d.ts +81 -0
  146. package/src/runtime/recut.js +325 -0
  147. package/src/runtime/render.d.ts +80 -0
  148. package/src/runtime/render.js +358 -0
  149. package/src/runtime/retouch/ask.d.ts +17 -0
  150. package/src/runtime/retouch/ask.js +171 -0
  151. package/src/runtime/retouch/entry.d.ts +77 -0
  152. package/src/runtime/retouch/entry.js +1 -0
  153. package/src/runtime/retouch/judge.d.ts +25 -0
  154. package/src/runtime/retouch/judge.js +209 -0
  155. package/src/runtime/retouch/look.d.ts +39 -0
  156. package/src/runtime/retouch/look.js +135 -0
  157. package/src/runtime/retouch/own-styles.d.ts +19 -0
  158. package/src/runtime/retouch/own-styles.js +141 -0
  159. package/src/runtime/retouch/parts.d.ts +22 -0
  160. package/src/runtime/retouch/parts.js +166 -0
  161. package/src/runtime/retouch/retouch.d.ts +37 -0
  162. package/src/runtime/retouch/retouch.js +238 -0
  163. package/src/runtime/retouch/seat.d.ts +40 -0
  164. package/src/runtime/retouch/seat.js +228 -0
  165. package/src/runtime/retouch/state.d.ts +37 -0
  166. package/src/runtime/retouch/state.js +153 -0
  167. package/src/runtime/retouch/type-style.d.ts +19 -0
  168. package/src/runtime/retouch/type-style.js +111 -0
  169. package/src/runtime/scan.d.ts +2 -0
  170. package/src/runtime/scan.js +49 -0
  171. package/src/runtime/scenes/card.d.ts +12 -0
  172. package/src/runtime/scenes/card.js +56 -0
  173. package/src/runtime/scenes/install.d.ts +69 -0
  174. package/src/runtime/scenes/install.js +528 -0
  175. package/src/runtime/scenes/read.d.ts +27 -0
  176. package/src/runtime/scenes/read.js +73 -0
  177. package/src/runtime/scenes/scoped.d.ts +13 -0
  178. package/src/runtime/scenes/scoped.js +46 -0
  179. package/src/runtime/score.d.ts +32 -0
  180. package/src/runtime/score.js +262 -0
  181. package/src/runtime/sketch/pen.d.ts +3 -0
  182. package/src/runtime/sketch/pen.js +371 -0
  183. package/src/runtime/sketch/strokes.d.ts +41 -0
  184. package/src/runtime/sketch/strokes.js +187 -0
  185. package/src/runtime/snapshot/draw.d.ts +14 -0
  186. package/src/runtime/snapshot/draw.js +125 -0
  187. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  188. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  189. package/src/runtime/snapshot/lift.d.ts +41 -0
  190. package/src/runtime/snapshot/lift.js +161 -0
  191. package/src/runtime/snapshot/plate.d.ts +20 -0
  192. package/src/runtime/snapshot/plate.js +79 -0
  193. package/src/runtime/snapshot/serialize.d.ts +51 -0
  194. package/src/runtime/snapshot/serialize.js +286 -0
  195. package/src/runtime/snapshot/styles.d.ts +8 -0
  196. package/src/runtime/snapshot/styles.js +109 -0
  197. package/src/runtime/snapshot/xml.d.ts +6 -0
  198. package/src/runtime/snapshot/xml.js +110 -0
  199. package/src/runtime/stage-size.d.ts +7 -0
  200. package/src/runtime/stage-size.js +3 -0
  201. package/src/runtime/stage.d.ts +27 -0
  202. package/src/runtime/stage.js +154 -0
  203. package/src/runtime/stretch.d.ts +13 -0
  204. package/src/runtime/stretch.js +16 -0
  205. package/src/runtime/texts.d.ts +24 -0
  206. package/src/runtime/texts.js +211 -0
  207. package/src/runtime/video-sync.d.ts +51 -0
  208. package/src/runtime/video-sync.js +218 -0
  209. package/src/stills.d.ts +90 -0
  210. package/src/stills.js +329 -0
  211. package/src/tools/film.d.ts +13 -0
  212. package/src/tools/film.js +116 -0
  213. package/src/tools/levels.d.ts +8 -0
  214. package/src/tools/levels.js +28 -0
  215. package/src/tools/main.d.ts +1 -0
  216. package/src/tools/main.js +223 -0
  217. package/src/tools/score.d.ts +4 -0
  218. package/src/tools/score.js +35 -0
  219. package/src/tools/types.d.ts +84 -0
  220. package/src/tools/types.js +2 -0
  221. package/src/tools.d.ts +6 -0
  222. package/src/tools.js +4 -0
  223. package/src/virtual.d.ts +11 -0
  224. package/src/vite.d.ts +8 -0
  225. package/src/vite.js +43 -0
@@ -0,0 +1,63 @@
1
+ import { addFontAsset } from "./fonts.js";
2
+ const assets = new Map();
3
+ const images = new Map();
4
+ const CHUNK = 0x8000;
5
+ export function dataUrl(mime, bytes) {
6
+ const all = new Uint8Array(bytes);
7
+ let text = '';
8
+ for (let i = 0; i < all.length; i += CHUNK)
9
+ text += String.fromCharCode(...all.subarray(i, Math.min(i + CHUNK, all.length)));
10
+ return 'data:' + mime + ';base64,' + btoa(text);
11
+ }
12
+ /** Pictures and fonts become data: URLs, which a snapshot can see; a clip keeps its URL and streams. */
13
+ export function addAssets(list) {
14
+ for (const asset of list) {
15
+ const url = asset.bytes ? dataUrl(asset.mime, asset.bytes) : asset.url;
16
+ if (url === undefined)
17
+ continue;
18
+ assets.set(asset.name, { url, mime: asset.mime, bytes: asset.bytes ?? null, w: asset.w, h: asset.h });
19
+ if (/^font\//.test(asset.mime))
20
+ addFontAsset(asset.name, url);
21
+ }
22
+ }
23
+ export const assetUrl = (name) => assets.get(name)?.url ?? null;
24
+ export const assetStore = {
25
+ has: (name) => assets.has(name),
26
+ list: () => [...assets.keys()],
27
+ url(name) {
28
+ const asset = assets.get(name);
29
+ if (asset)
30
+ return asset.url;
31
+ console.error('kit.assets: no asset ' + name);
32
+ return '';
33
+ },
34
+ mime: (name) => assets.get(name)?.mime ?? '',
35
+ bytes(name) {
36
+ const bytes = assets.get(name)?.bytes;
37
+ return bytes ? new Uint8Array(bytes.slice(0)) : null;
38
+ },
39
+ text(name) {
40
+ const bytes = assets.get(name)?.bytes;
41
+ return bytes ? new TextDecoder().decode(bytes) : null;
42
+ },
43
+ image(name) {
44
+ const cached = images.get(name);
45
+ if (cached)
46
+ return cached;
47
+ const asset = assets.get(name);
48
+ if (!asset)
49
+ return Promise.reject(new Error('no asset ' + name));
50
+ const loading = new Promise((resolve, reject) => {
51
+ const image = new Image();
52
+ image.onload = () => resolve(image);
53
+ image.onerror = () => reject(new Error('asset ' + name + ' did not decode'));
54
+ image.src = asset.url;
55
+ });
56
+ images.set(name, loading);
57
+ return loading;
58
+ },
59
+ size(name) {
60
+ const asset = assets.get(name);
61
+ return asset && asset.w ? { w: asset.w, h: asset.h } : null;
62
+ },
63
+ };
@@ -0,0 +1,3 @@
1
+ import type { StageCaption } from '../protocol.js';
2
+ /** The caption the stage shows now, its words and their box in film pixels; `null` when none shows. */
3
+ export declare function captionBox(stage: HTMLElement): StageCaption | null;
@@ -0,0 +1,16 @@
1
+ import { srect } from "./kit/geometry.js";
2
+ /** The caption the stage shows now, its words and their box in film pixels; `null` when none shows. */
3
+ export function captionBox(stage) {
4
+ let layer = null;
5
+ for (const child of Array.from(stage.children))
6
+ if (child instanceof HTMLElement && child.classList.contains('film-caption'))
7
+ layer = child;
8
+ const words = layer?.firstElementChild;
9
+ if (layer === null || layer.style.visibility === 'hidden' || !(words instanceof HTMLElement))
10
+ return null;
11
+ const text = words.textContent ?? '';
12
+ if (!/\S/.test(text))
13
+ return null;
14
+ const box = srect(words);
15
+ return box.w >= 1 && box.h >= 1 ? { text, x: box.x, y: box.y, w: box.w, h: box.h } : null;
16
+ }
@@ -0,0 +1,31 @@
1
+ import type { NarrationLine, PlaySeconds } from '@openartifactsai/motion-core';
2
+ import type { NarrationLimits, PlayedNarrationLine } from '../protocol.js';
3
+ export interface ReadNarration {
4
+ lines: NarrationLine[];
5
+ duck: number | null;
6
+ captions: false | {
7
+ style?: string;
8
+ };
9
+ }
10
+ export interface CaptionCue {
11
+ from: PlaySeconds;
12
+ to: PlaySeconds;
13
+ text: string;
14
+ lang?: string;
15
+ }
16
+ /**
17
+ * The cut's narration, held to the host's limits: the page applies the same, so a line dropped here is named and
18
+ * not captioned. `clips` are the film's video assets, whose sound a line plays only as a cut (with `from`).
19
+ */
20
+ export declare function readNarration(narration: unknown, duration: number, limits: NarrationLimits, clips?: ReadonlySet<string>): ReadNarration | null;
21
+ /** Words shown over the film for a while: a narration line, or a clip's (kit.video's captions), which plays no take. */
22
+ export type CaptionLine = Pick<PlayedNarrationLine, 'at' | 'dur' | 'text'> & Partial<PlayedNarrationLine> & {
23
+ lang?: string;
24
+ };
25
+ /** A line over LONGEST_CUE characters shows in parts of about equal length, split at spaces, each for its share of the line's time. */
26
+ export declare function captionCues(lines: readonly CaptionLine[]): CaptionCue[];
27
+ export interface CaptionLayer {
28
+ draw(t: PlaySeconds, stage: HTMLElement): void;
29
+ }
30
+ /** `shortSide`, the stage's shorter side, sizes the type. What draw shows depends only on t. */
31
+ export declare function captionLayer(lines: readonly CaptionLine[], style: string | undefined, shortSide: number, sans: string): CaptionLayer;
@@ -0,0 +1,115 @@
1
+ const LONGEST_CUE = 76;
2
+ const fields = (value) => value !== null && typeof value === 'object' ? value : null;
3
+ /**
4
+ * The cut's narration, held to the host's limits: the page applies the same, so a line dropped here is named and
5
+ * not captioned. `clips` are the film's video assets, whose sound a line plays only as a cut (with `from`).
6
+ */
7
+ export function readNarration(narration, duration, limits, clips = new Set()) {
8
+ const given = fields(narration);
9
+ if (!given || !Array.isArray(given['lines']))
10
+ return null;
11
+ const all = given['lines'];
12
+ if (all.length > limits.maxLines)
13
+ console.error('narration has ' + all.length + ' lines; only the first ' + limits.maxLines + ' are used');
14
+ const lines = [];
15
+ all.slice(0, limits.maxLines).forEach((entry, i) => {
16
+ const line = fields(entry);
17
+ const at = line?.['at'];
18
+ const dur = line?.['dur'];
19
+ const text = line?.['text'];
20
+ const audio = line?.['audio'];
21
+ const from = line?.['from'];
22
+ if (typeof at !== 'number' ||
23
+ typeof dur !== 'number' ||
24
+ !(at >= 0 && at < duration) ||
25
+ !(dur > 0 && dur <= limits.maxLineSeconds) ||
26
+ typeof text !== 'string' ||
27
+ typeof audio !== 'string' ||
28
+ !audio ||
29
+ (from !== undefined && !(typeof from === 'number' && from >= 0 && Number.isFinite(from)))) {
30
+ console.error('narration line ' +
31
+ (i + 1) +
32
+ ' is left out. It needs { at, text, audio, dur }: a start in seconds inside the film, the words, the name of an audio asset, and that audio’s length in seconds (' +
33
+ limits.maxLineSeconds +
34
+ ' at most); at and dur as numbers, and from, where given, a second of that asset (0 or more)');
35
+ return;
36
+ }
37
+ if (from === undefined && clips.has(audio)) {
38
+ console.error('narration line ' + (i + 1) + ' names the clip ' + JSON.stringify(audio) + ' with no from, so it is silent: give the clip’s second its words start at as from');
39
+ }
40
+ lines.push({ at: at, text, audio, dur, ...(typeof from === 'number' ? { from } : {}) });
41
+ });
42
+ const captions = given['captions'];
43
+ const style = fields(captions)?.['style'];
44
+ return {
45
+ lines,
46
+ duck: typeof given['duck'] === 'number' ? given['duck'] : null,
47
+ captions: captions === false ? false : typeof style === 'string' ? { style } : {},
48
+ };
49
+ }
50
+ /** A line over LONGEST_CUE characters shows in parts of about equal length, split at spaces, each for its share of the line's time. */
51
+ export function captionCues(lines) {
52
+ const out = [];
53
+ for (const line of lines) {
54
+ const words = line.text.trim().split(/\s+/);
55
+ const total = words.join(' ').length;
56
+ if (!total)
57
+ continue;
58
+ const aim = total / Math.ceil(total / LONGEST_CUE);
59
+ let part = '';
60
+ let done = 0;
61
+ words.forEach((word, i) => {
62
+ part += (part ? ' ' : '') + word;
63
+ const next = words[i + 1];
64
+ if (next !== undefined && part.length + (next.length + 1) / 2 < aim)
65
+ return;
66
+ out.push({
67
+ from: (line.at + (line.dur * done) / total),
68
+ to: (line.at + line.dur * Math.min(1, (done + part.length + 1) / total)),
69
+ text: part,
70
+ ...(line.lang !== undefined ? { lang: line.lang } : {}),
71
+ });
72
+ done += part.length + 1;
73
+ part = '';
74
+ });
75
+ }
76
+ return out;
77
+ }
78
+ /** `shortSide`, the stage's shorter side, sizes the type. What draw shows depends only on t. */
79
+ export function captionLayer(lines, style, shortSide, sans) {
80
+ const cues = captionCues(lines);
81
+ const box = document.createElement('div');
82
+ const words = document.createElement('span');
83
+ box.className = 'film-caption';
84
+ box.dir = 'auto';
85
+ box.style.cssText =
86
+ 'position:absolute;left:10%;right:10%;bottom:7%;text-align:center;text-wrap:balance;pointer-events:none;z-index:2147483647;font:600 ' +
87
+ Math.round(shortSide * 0.042) +
88
+ 'px/1.62 ' +
89
+ sans;
90
+ // Cloned per row, so each row's background hugs its own words.
91
+ words.style.cssText =
92
+ 'padding:.2em .6em;border-radius:.3em;background:rgba(0,0,0,.7);color:#fff;-webkit-box-decoration-break:clone;box-decoration-break:clone' +
93
+ (style ? ';' + style : '');
94
+ box.appendChild(words);
95
+ return {
96
+ draw(t, stage) {
97
+ let cue = null;
98
+ for (const each of cues)
99
+ if (t >= each.from && t < each.to)
100
+ cue = each;
101
+ // Last in the stage, so it stays above what the cut adds.
102
+ if (stage.lastChild !== box)
103
+ stage.appendChild(box);
104
+ box.style.visibility = cue ? 'visible' : 'hidden';
105
+ if (cue && words.textContent !== cue.text)
106
+ words.textContent = cue.text;
107
+ if (cue && (words.getAttribute('lang') ?? '') !== (cue.lang ?? '')) {
108
+ if (cue.lang === undefined)
109
+ words.removeAttribute('lang');
110
+ else
111
+ words.lang = cue.lang;
112
+ }
113
+ },
114
+ };
115
+ }
@@ -0,0 +1,9 @@
1
+ import type { NarrationReport } from '../protocol.js';
2
+ import type { Cut } from './film.js';
3
+ import type { SceneClock } from './recut.js';
4
+ /**
5
+ * Lays the clips' captions (kit.video's `captions`) on the film's caption layer with the narration's, one layer, so a
6
+ * clip's words show in the player and in every export. The narration's `captions: false` turns them off too, and its
7
+ * `{ style }` styles them alike. Their times move with the picture, as the clip does.
8
+ */
9
+ export declare function addClipCaptions(cut: Cut, narration: NarrationReport | null, clock: SceneClock): void;
@@ -0,0 +1,27 @@
1
+ import { filmSeconds } from '@openartifactsai/motion-core';
2
+ import { captionLayer } from "./captions.js";
3
+ import { clipCaptions } from "./clips.js";
4
+ import { film } from "./film.js";
5
+ import { fontFamilies } from "./fonts.js";
6
+ const fields = (value) => value !== null && typeof value === 'object' ? value : null;
7
+ /**
8
+ * Lays the clips' captions (kit.video's `captions`) on the film's caption layer with the narration's, one layer, so a
9
+ * clip's words show in the player and in every export. The narration's `captions: false` turns them off too, and its
10
+ * `{ style }` styles them alike. Their times move with the picture, as the clip does.
11
+ */
12
+ export function addClipCaptions(cut, narration, clock) {
13
+ const said = clipCaptions(cut.duration);
14
+ if (said.length === 0)
15
+ return;
16
+ const settings = fields(cut.narration)?.['captions'];
17
+ if (settings === false)
18
+ return;
19
+ const style = fields(settings)?.['style'];
20
+ const lines = said.map((line) => {
21
+ const at = clock.back(line.at);
22
+ return { at, dur: Math.max(0, clock.back(filmSeconds(line.at + line.dur)) - at), text: line.text, ...(line.lang !== undefined ? { lang: line.lang } : {}) };
23
+ });
24
+ // The narration's lines are on the layer only where it made one.
25
+ const spoken = film.captions !== null && narration !== null ? narration.lines : [];
26
+ film.captions = captionLayer([...spoken, ...lines], typeof style === 'string' ? style : undefined, Math.min(cut.width, cut.height), fontFamilies.sans);
27
+ }
@@ -0,0 +1,83 @@
1
+ import type { ClipSoundUse, FilmSeconds } from '@openartifactsai/motion-core';
2
+ import type { SceneClock } from './recut.js';
3
+ import type { VideoClock } from './video-sync.js';
4
+ /**
5
+ * What `kit.video` asks of a clip beyond where it sits: which of its seconds play when, whether its sound plays, and
6
+ * the words said in it. The stage keeps the picture at its second; the host plays the sound from the list 'ready'
7
+ * carries, so a clip made after the film loaded is silent.
8
+ */
9
+ /** A clip's timing as kit.video wrote it on the element: film second `start`, its own second `from`, `span` film seconds, `speed`. */
10
+ export interface ClipTiming {
11
+ start: number;
12
+ from: number;
13
+ span: number;
14
+ speed: number;
15
+ }
16
+ /** Where a clip's sound may play: its scene's stretch of film seconds, and whether the scene failed to come up. */
17
+ export interface ClipScene {
18
+ lo: number;
19
+ hi: number;
20
+ gone(): boolean;
21
+ }
22
+ /** A clip that sounds, in film seconds: heard from `lo` to `hi`, where it shows its second `from + (t − start) × speed`. */
23
+ export interface SoundingClip {
24
+ name: string;
25
+ level: number;
26
+ loop: boolean;
27
+ start: number;
28
+ from: number;
29
+ speed: number;
30
+ lo: number;
31
+ hi: number;
32
+ }
33
+ /** A caption of a clip on the film's clock. */
34
+ export interface ClipCaption {
35
+ at: FilmSeconds;
36
+ dur: number;
37
+ text: string;
38
+ /** The language its words are in, when the clip says (kit.video's `lang`). */
39
+ lang?: string;
40
+ }
41
+ /** A BCP 47 tag in its usual form, or `null` for anything else. */
42
+ export declare function languageTag(value: unknown): string | null;
43
+ /** The data attributes kit.video writes for `start`, `from`, `dur` and `speed`; a speed out of range is said once. */
44
+ export declare function clipAttributes(options: {
45
+ start?: unknown;
46
+ from?: unknown;
47
+ dur?: unknown;
48
+ speed?: unknown;
49
+ }): Record<string, string>;
50
+ export declare function clipTiming(video: HTMLVideoElement): ClipTiming;
51
+ /** The clip's own second at film time `t` (before looping or holding at its end), and whether its `dur` has it on. */
52
+ export declare function clipSecondAt(video: HTMLVideoElement, t: number): {
53
+ second: number;
54
+ on: boolean;
55
+ };
56
+ /** The clock a clip plays on: the film's, faster or slower by its speed. */
57
+ export declare function clipClock(video: HTMLVideoElement, clock: VideoClock): VideoClock;
58
+ /** Its speed for the element: picture only, so WebKit keeps every frame as the rate moves off 1. */
59
+ export declare function prepareClip(video: HTMLVideoElement): void;
60
+ /** kit.video's `sound` and `captions`, noted for the report that goes with 'ready'. `name`: the asset, or null for a URL. */
61
+ export declare function noteClip(video: HTMLVideoElement, name: string | null, sound: unknown, captions: unknown, lang?: unknown): void;
62
+ /** Made in a scene: its sound and words stay within the scene. */
63
+ export declare function clipInScene(video: HTMLVideoElement, scene: ClipScene): void;
64
+ /** kit.seek moved the clip: its sound and words, which cannot follow, are dropped (said once). */
65
+ export declare function noteSeek(video: HTMLVideoElement): void;
66
+ /** The clips that sound, in film seconds, as the film loaded. */
67
+ export declare function soundingClips(duration: number): SoundingClip[];
68
+ /**
69
+ * The sounding clips on the play clock: each scene part a clip plays in becomes a use, at the part's rate, so a scene a
70
+ * person made longer plays its clip's sound slower, at the same pitch. Two uses of one file at one moment and second
71
+ * sound once, at the louder level.
72
+ */
73
+ export declare function playedClipSound(clips: readonly SoundingClip[], clock: SceneClock): ClipSoundUse[];
74
+ /**
75
+ * The clips' captions in film seconds: a line said at the clip's second `at` shows at `start + (at − from) / speed`
76
+ * for `dur / speed`, only while the clip is on, and once more on each pass of a looping clip whose length is known.
77
+ */
78
+ export declare function clipCaptions(duration: number): ClipCaption[];
79
+ /**
80
+ * Narration lines that play seconds of a clip (`audio` its name, `from` its second) which kit.video's `sound` plays
81
+ * too, one message each: a second of a clip is heard from one of the two, never both.
82
+ */
83
+ export declare function narrationOverlaps(narration: unknown, clips: readonly SoundingClip[]): string[];
@@ -0,0 +1,259 @@
1
+ import { MAX_CLIP_CAPTION_CHARS, MAX_CLIP_CAPTIONS, MAX_SOUNDING_CLIPS, clipSpeed, filmSeconds, heardAtSpeed, playSeconds } from '@openartifactsai/motion-core';
2
+ import { film } from "./film.js";
3
+ const TAG_SHAPE = /^[A-Za-z]{2,3}(?:-[A-Za-z0-9]{1,8})*$/;
4
+ /** A BCP 47 tag in its usual form, or `null` for anything else. */
5
+ export function languageTag(value) {
6
+ const tag = typeof value === 'string' && value.length <= 35 && TAG_SHAPE.test(value) ? value : '';
7
+ let usual = [];
8
+ if (tag !== '') {
9
+ try {
10
+ usual = Intl.getCanonicalLocales(tag);
11
+ }
12
+ catch {
13
+ usual = [];
14
+ }
15
+ }
16
+ return usual[0] ?? null;
17
+ }
18
+ const heard = [];
19
+ const said = [];
20
+ const scenes = new WeakMap();
21
+ /** Clips kit.seek has moved: their sound and words cannot follow the film's clock. */
22
+ const sought = new WeakSet();
23
+ const told = new Set();
24
+ /** A use shorter than this is not heard. */
25
+ const SHORTEST_SOUND_S = 0.05;
26
+ function warnOnce(key, message) {
27
+ if (told.has(key))
28
+ return;
29
+ told.add(key);
30
+ console.warn(message);
31
+ }
32
+ const positive = (value) => {
33
+ const n = Number(value);
34
+ return value !== undefined && value !== null && n > 0 && Number.isFinite(n) ? n : null;
35
+ };
36
+ /** The data attributes kit.video writes for `start`, `from`, `dur` and `speed`; a speed out of range is said once. */
37
+ export function clipAttributes(options) {
38
+ const attrs = { 'data-start': String(Number(options.start) || 0) };
39
+ const from = positive(options.from);
40
+ const dur = positive(options.dur);
41
+ if (from !== null)
42
+ attrs['data-from'] = String(from);
43
+ if (dur !== null)
44
+ attrs['data-dur'] = String(dur);
45
+ const given = options.speed;
46
+ if (given !== undefined && given !== null) {
47
+ const speed = clipSpeed(given);
48
+ if (positive(given) === null)
49
+ console.warn('kit.video: speed ' + String(given) + ' is not a number above 0, so the clip plays at speed 1');
50
+ else if (speed !== Number(given))
51
+ console.warn('kit.video: speed ' + String(given) + ' is outside 0.25 to 4, so the clip plays at ' + speed);
52
+ if (speed !== 1)
53
+ attrs['data-speed'] = String(speed);
54
+ }
55
+ return attrs;
56
+ }
57
+ export function clipTiming(video) {
58
+ return {
59
+ start: Number(video.getAttribute('data-start')) || 0,
60
+ from: positive(video.getAttribute('data-from')) ?? 0,
61
+ span: positive(video.getAttribute('data-dur')) ?? Infinity,
62
+ speed: clipSpeed(video.getAttribute('data-speed')),
63
+ };
64
+ }
65
+ /** The clip's own second at film time `t` (before looping or holding at its end), and whether its `dur` has it on. */
66
+ export function clipSecondAt(video, t) {
67
+ const { start, from, span, speed } = clipTiming(video);
68
+ const into = t - start;
69
+ return { second: from + Math.min(Math.max(into, 0), span) * speed, on: into >= 0 && into < span };
70
+ }
71
+ /** The clock a clip plays on: the film's, faster or slower by its speed. */
72
+ export function clipClock(video, clock) {
73
+ const { speed } = clipTiming(video);
74
+ return speed === 1 ? clock : { ...clock, rate: (clock.retimed ? clock.rate : 1) * speed, retimed: true };
75
+ }
76
+ /** Its speed for the element: picture only, so WebKit keeps every frame as the rate moves off 1. */
77
+ export function prepareClip(video) {
78
+ if (clipTiming(video).speed !== 1)
79
+ video.preservesPitch = false;
80
+ }
81
+ /** kit.video's `sound` and `captions`, noted for the report that goes with 'ready'. `name`: the asset, or null for a URL. */
82
+ export function noteClip(video, name, sound, captions, lang) {
83
+ if (sound !== undefined && sound !== null && sound !== false) {
84
+ const level = sound === true ? 1 : Number(sound);
85
+ if (name === null)
86
+ warnOnce('url', 'kit.video: sound plays only from an asset given by its name, so this clip is silent');
87
+ else if (film.loaded)
88
+ warnOnce('late:' + name, 'kit.video: ' + name + ' was made after the film loaded, so it is silent. Make a clip with sound in init');
89
+ else if (!(level > 0 && level <= 1))
90
+ warnOnce('level:' + name, 'kit.video: sound is true or a level from 0 to 1, so ' + name + ' is silent');
91
+ else if (!heardAtSpeed(clipTiming(video).speed))
92
+ warnOnce('speed:' + name, 'kit.video: ' + name + ' plays at speed ' + clipTiming(video).speed + ', where its sound is not heard (0.5 to 2 only), so it is silent and the score stays up');
93
+ else {
94
+ heard.push({ video, name, level });
95
+ video.preservesPitch = false;
96
+ }
97
+ }
98
+ if (captions === undefined || captions === null)
99
+ return;
100
+ if (film.loaded)
101
+ warnOnce('late-said', 'kit.video: captions of a clip made after the film loaded are not shown. Make the clip in init');
102
+ else if (!Array.isArray(captions))
103
+ warnOnce('said-list', 'kit.video: captions are a list of { at, dur, text }, so this clip has none');
104
+ else {
105
+ const tag = languageTag(lang);
106
+ if (lang !== undefined && tag === null)
107
+ warnOnce('lang', 'kit.video: lang is a language tag such as "es" or "pt-BR", so it is left out');
108
+ said.push({ video, lines: captions, lang: tag });
109
+ }
110
+ }
111
+ /** Made in a scene: its sound and words stay within the scene. */
112
+ export function clipInScene(video, scene) {
113
+ scenes.set(video, scene);
114
+ }
115
+ /** kit.seek moved the clip: its sound and words, which cannot follow, are dropped (said once). */
116
+ export function noteSeek(video) {
117
+ sought.add(video);
118
+ if (film.loaded && heard.some((each) => each.video === video))
119
+ warnOnce('seek-late', 'kit.seek: a clip with sound cannot be moved by kit.seek; its sound follows kit.video’s start, from and dur alone');
120
+ }
121
+ /** The film seconds a clip is on for: from its start to its dur, its scene's end and the film's end. */
122
+ function onFor(video, duration) {
123
+ const scene = scenes.get(video);
124
+ if (scene?.gone())
125
+ return null;
126
+ const { start, span } = clipTiming(video);
127
+ const lo = Math.max(start, scene ? scene.lo : 0);
128
+ const hi = Math.min(duration, scene ? scene.hi : Infinity, start + span);
129
+ return hi > lo ? { lo, hi } : null;
130
+ }
131
+ /** The clips that sound, in film seconds, as the film loaded. */
132
+ export function soundingClips(duration) {
133
+ const out = [];
134
+ for (const each of heard) {
135
+ if (sought.has(each.video)) {
136
+ warnOnce('seek:' + each.name, 'kit.seek: ' + each.name + ' is moved by kit.seek, which its sound cannot follow, so it is silent. Use kit.video’s start, from and dur');
137
+ continue;
138
+ }
139
+ const on = onFor(each.video, duration);
140
+ if (on === null)
141
+ continue;
142
+ const { start, from, speed } = clipTiming(each.video);
143
+ out.push({ name: each.name, level: each.level, loop: each.video.loop, start, from, speed, lo: on.lo, hi: on.hi });
144
+ if (out.length === MAX_SOUNDING_CLIPS)
145
+ break;
146
+ }
147
+ return out;
148
+ }
149
+ const round6 = (value) => Math.round(value * 1e6) / 1e6;
150
+ /**
151
+ * The sounding clips on the play clock: each scene part a clip plays in becomes a use, at the part's rate, so a scene a
152
+ * person made longer plays its clip's sound slower, at the same pitch. Two uses of one file at one moment and second
153
+ * sound once, at the louder level.
154
+ */
155
+ export function playedClipSound(clips, clock) {
156
+ const uses = [];
157
+ for (const clip of clips) {
158
+ for (const part of clock.parts) {
159
+ const x0 = Math.max(clip.lo, part.a0);
160
+ const x1 = Math.min(clip.hi, part.a0 + part.nat);
161
+ if (!(x1 - x0 >= SHORTEST_SOUND_S) || !(part.nat > 0) || !(part.dur > 0))
162
+ continue;
163
+ const stretch = part.dur / part.nat;
164
+ const use = {
165
+ name: clip.name,
166
+ level: clip.level,
167
+ loop: clip.loop,
168
+ at: playSeconds(round6(part.p0 + (x0 - part.a0) * stretch)),
169
+ dur: round6((x1 - x0) * stretch),
170
+ second: round6(clip.from + (x0 - clip.start) * clip.speed),
171
+ rate: round6(clip.speed / stretch),
172
+ };
173
+ const twin = uses.find((other) => other.name === use.name && Math.abs(other.at - use.at) < 1e-3 && Math.abs(other.second - use.second) < 1e-3 && Math.abs(other.rate - use.rate) < 1e-6);
174
+ if (twin === undefined)
175
+ uses.push(use);
176
+ else {
177
+ twin.level = Math.max(twin.level, use.level);
178
+ twin.dur = Math.max(twin.dur, use.dur);
179
+ }
180
+ }
181
+ }
182
+ return uses;
183
+ }
184
+ /**
185
+ * The clips' captions in film seconds: a line said at the clip's second `at` shows at `start + (at − from) / speed`
186
+ * for `dur / speed`, only while the clip is on, and once more on each pass of a looping clip whose length is known.
187
+ */
188
+ export function clipCaptions(duration) {
189
+ const out = [];
190
+ for (const each of said) {
191
+ const { video } = each;
192
+ if (sought.has(video)) {
193
+ warnOnce('seek-said', 'kit.video: a clip moved by kit.seek shows no captions. Use kit.video’s start, from and dur');
194
+ continue;
195
+ }
196
+ const on = onFor(video, duration);
197
+ if (on === null)
198
+ continue;
199
+ const { start, from, speed } = clipTiming(video);
200
+ const length = video.duration;
201
+ const known = length > 0 && Number.isFinite(length);
202
+ if (video.loop && !known)
203
+ warnOnce('loop-said', 'kit.video: the length of a looping clip was not known when the film loaded, so its captions show on its first pass only');
204
+ const passes = video.loop && known ? Math.max(1, Math.ceil(((on.hi - start) * speed + from) / length)) : 1;
205
+ each.lines.forEach((entry, i) => {
206
+ const line = entry !== null && typeof entry === 'object' ? entry : {};
207
+ const { at, dur, text } = line;
208
+ if (typeof at !== 'number' || typeof dur !== 'number' || !(at >= 0) || !(dur > 0) || typeof text !== 'string' || !text.trim()) {
209
+ console.warn('kit.video: caption ' + (i + 1) + ' is left out. It needs { at, dur, text }: the second of the clip where it is said, for how many seconds, and the words');
210
+ return;
211
+ }
212
+ for (let pass = 0; pass < passes && out.length < MAX_CLIP_CAPTIONS; pass++) {
213
+ const second = at + pass * (passes > 1 ? length : 0);
214
+ if (second < from)
215
+ continue;
216
+ const shown = Math.max(on.lo, start + (second - from) / speed);
217
+ const gone = Math.min(on.hi, start + (second - from + dur) / speed);
218
+ if (gone > shown)
219
+ out.push({ at: filmSeconds(shown), dur: gone - shown, text: text.slice(0, MAX_CLIP_CAPTION_CHARS), ...(each.lang !== null ? { lang: each.lang } : {}) });
220
+ }
221
+ });
222
+ }
223
+ return out;
224
+ }
225
+ /** The seconds of the file a sounding clip plays: from `from` at its first heard moment to where it stops. */
226
+ function heardSeconds(clip) {
227
+ return [clip.from + (clip.lo - clip.start) * clip.speed, clip.from + (clip.hi - clip.start) * clip.speed];
228
+ }
229
+ /**
230
+ * Narration lines that play seconds of a clip (`audio` its name, `from` its second) which kit.video's `sound` plays
231
+ * too, one message each: a second of a clip is heard from one of the two, never both.
232
+ */
233
+ export function narrationOverlaps(narration, clips) {
234
+ const lines = narration !== null && typeof narration === 'object' ? narration['lines'] : null;
235
+ if (!Array.isArray(lines) || clips.length === 0)
236
+ return [];
237
+ const out = [];
238
+ lines.forEach((entry, i) => {
239
+ const line = entry !== null && typeof entry === 'object' ? entry : {};
240
+ const { audio, from, dur } = line;
241
+ if (typeof audio !== 'string' || typeof from !== 'number' || typeof dur !== 'number' || !(dur > 0))
242
+ return;
243
+ for (const clip of clips) {
244
+ if (clip.name !== audio)
245
+ continue;
246
+ const [a, b] = heardSeconds(clip);
247
+ const both = clip.loop ? dur : Math.min(b, from + dur) - Math.max(a, from);
248
+ if (!(both > SHORTEST_SOUND_S))
249
+ continue;
250
+ out.push('narration line ' +
251
+ (i + 1) +
252
+ ' plays seconds of ' +
253
+ JSON.stringify(audio) +
254
+ ' that its kit.video sound plays too, so they are heard twice: give a clip with sound no narration line for those seconds, or take sound off the clip');
255
+ break;
256
+ }
257
+ });
258
+ return out;
259
+ }
@@ -0,0 +1,13 @@
1
+ import type { ErrorPhase } from '../protocol.js';
2
+ export declare const errorPhase: () => ErrorPhase;
3
+ export declare function setErrorPhase(next: ErrorPhase): void;
4
+ export declare const failedToLoad: () => boolean;
5
+ export declare function setLateHandler(handler: ((error: unknown) => void) | null): void;
6
+ /** A failure episode that ended: the next one is reported afresh. */
7
+ export declare function forgetErrors(): void;
8
+ export declare function reportError(at: ErrorPhase, error: unknown): void;
9
+ /** Runs `run` and gives back what it threw through the window's error event, or null. Guards nest. */
10
+ export declare function guarded(run: () => void): unknown;
11
+ /** Console lines go to the host as well, which shows them; the film's errors fail a check. */
12
+ export declare function captureConsole(): void;
13
+ export declare function catchErrors(): void;