@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,267 @@
1
+ import { HAND_TITLES_MODULE, LAID_MODULE, parseLaidRecord, parseRetouchedRecord, soundWithLaid } from '@openartifactsai/motion-core';
2
+ import { addAssets } from "./assets.js";
3
+ import { captionLayer, readNarration } from "./captions.js";
4
+ import { addClipCaptions } from "./clip-captions.js";
5
+ import { narrationOverlaps, playedClipSound, soundingClips } from "./clips.js";
6
+ import { errorPhase, failedToLoad, reportError, setErrorPhase, setLateHandler } from "./console.js";
7
+ import { chaptersOf, readCut } from "./cut.js";
8
+ import { filmTime, realNow, reseed, setFilmTime } from "./determinism.js";
9
+ import { film } from "./film.js";
10
+ import { fillStacks, fontFamilies, fontsIn, resetFonts } from "./fonts.js";
11
+ import { installFx, namesFx, ownsFx } from "./fx/index.js";
12
+ import { setTitles } from "./hand-titles/index.js";
13
+ import { holdEntrances } from "./held.js";
14
+ import { laidParts, makeLaidLayer } from "./laid/layer.js";
15
+ import { makeKit } from "./kit/index.js";
16
+ import { addModules, findModule, injectScript, setScriptKit, tryScript } from "./modules.js";
17
+ import { say } from "./port.js";
18
+ import { filmParts, playedScore, plainClock, sceneClock } from "./recut.js";
19
+ import { forgetReading } from "./layers/scan.js";
20
+ import { takeRecord } from "./questions.js";
21
+ import { afterSample, busy, hasPending, isPlaying, onShow, renderAt, renderState, showsAsked, syncClips, unretouched } from "./render.js";
22
+ import { noteBox } from "./retouch/retouch.js";
23
+ import { recordScore } from "./score.js";
24
+ import { scanStage } from "./scan.js";
25
+ import { installScenes } from "./scenes/install.js";
26
+ import { setPlateHold } from "./snapshot/plate.js";
27
+ import { sketchScene } from "./sketch/pen.js";
28
+ import { buildStage, builtStage } from "./stage.js";
29
+ import { stageSize } from "./stage-size.js";
30
+ import { tellTexts } from "./texts.js";
31
+ const messageOf = (error) => String(error instanceof Error ? error.message : error);
32
+ let begun = false;
33
+ /**
34
+ * Told of each scene's box as it comes up: the classes in it, so a class counts as an element's own alike in every
35
+ * runner. Noted whether or not the film came with the record of what a person did by hand to how things look: the
36
+ * editor's stage is given that after, and may be given one for the first time while it is open.
37
+ */
38
+ export function sceneBox(node) {
39
+ try {
40
+ noteBox(node);
41
+ }
42
+ catch {
43
+ unretouched();
44
+ }
45
+ }
46
+ /**
47
+ * What a person did to the scenes by hand moves what the page is told (length, chapters, score), never what the
48
+ * cut sees. A fault costs their changes, not the film.
49
+ */
50
+ function recut(cut, chapters, ids) {
51
+ const record = findModule('recut.json');
52
+ try {
53
+ const clock = sceneClock(filmParts(cut.duration, chapters, ids), record ? record.text : null);
54
+ if (clock.aside)
55
+ console.info('a person’s scene lengths (modules/recut.json) would take the film past 300 s, so every scene plays at its own length.');
56
+ if (clock.moved)
57
+ console.info('a person changed scene lengths (modules/recut.json): the film plays ' +
58
+ clock.duration +
59
+ ' s, its own time is ' +
60
+ Math.round(cut.duration * 1e6) / 1e6 +
61
+ ' s. Times given to and by this player are as played; render(t) and scenes.json are the film’s own. meta.parts lists both.');
62
+ return clock;
63
+ }
64
+ catch {
65
+ if (record)
66
+ console.warn('what a person did to the scenes (modules/recut.json) could not be applied, so the film plays as written.');
67
+ return plainClock(cut.duration);
68
+ }
69
+ }
70
+ function recordedScore(cut, kit) {
71
+ const write = cut.score;
72
+ if (!write)
73
+ return null;
74
+ try {
75
+ return recordScore((builder) => {
76
+ write(builder, kit);
77
+ });
78
+ }
79
+ catch (error) {
80
+ console.error('score(S) threw: ' + messageOf(error));
81
+ return null;
82
+ }
83
+ }
84
+ function narrationOf(cut, given, clock) {
85
+ const limits = given.narration;
86
+ if (!limits || cut.narration === undefined || cut.narration === null)
87
+ return null;
88
+ try {
89
+ const clips = new Set(given.assets.filter((asset) => asset.mime.startsWith('video/')).map((asset) => asset.name));
90
+ const read = readNarration(cut.narration, cut.duration, limits, clips);
91
+ if (!read)
92
+ return null;
93
+ // A line starts on the action it speaks of, so its start moves with the picture; its length is the take's own.
94
+ const lines = read.lines.filter((line) => !clock.hides(line.at)).map((line) => ({
95
+ at: clock.back(line.at),
96
+ dur: line.dur,
97
+ text: line.text,
98
+ audio: line.audio,
99
+ ...(line.from !== undefined ? { from: line.from } : {}),
100
+ }));
101
+ if (read.captions !== false)
102
+ film.captions = captionLayer(lines, read.captions.style, Math.min(cut.width, cut.height), fontFamilies.sans);
103
+ return { lines, duck: read.duck };
104
+ }
105
+ catch (error) {
106
+ film.captions = null;
107
+ console.error('the narration could not be read, so the film plays without it: ' + messageOf(error));
108
+ return null;
109
+ }
110
+ }
111
+ /**
112
+ * A sample the first frame's draw began: once it has painted, the moment on show is drawn again, unless a frame is on
113
+ * its way or a pull holds the stage, and the clips and the words follow it. After 'ready': the page takes an error
114
+ * before it for a failed load.
115
+ */
116
+ function afterReady(sampled) {
117
+ const again = afterSample(sampled, onShow, () => !hasPending() && !busy.framing);
118
+ if (!again)
119
+ return;
120
+ const after = () => {
121
+ try {
122
+ if (!hasPending() && !busy.framing && !isPlaying())
123
+ void syncClips('hold');
124
+ // The words wait for the page's first render: until then the stage shows moment 0, which it never asked for.
125
+ if (showsAsked())
126
+ tellTexts();
127
+ }
128
+ catch {
129
+ // The next frame does it.
130
+ }
131
+ };
132
+ void again.then((a) => {
133
+ if (a.q)
134
+ a.q.then(after, (error) => {
135
+ reportError(errorPhase(), error);
136
+ after();
137
+ });
138
+ else
139
+ after();
140
+ });
141
+ }
142
+ /** Runs the film's script, builds its stage, records its score and draws its first frame; then says 'ready'. */
143
+ export async function loadFilm(given, hold) {
144
+ if (begun) {
145
+ reportError('load', new Error('already loaded; mount a fresh runner'));
146
+ return;
147
+ }
148
+ begun = true;
149
+ const began = realNow();
150
+ try {
151
+ resetFonts();
152
+ setPlateHold(hold, given.clippedTextWaitMs);
153
+ film.mode = given.mode;
154
+ addAssets(given.assets);
155
+ addModules(given.modules);
156
+ fillStacks(given.faces);
157
+ const texts = [given.script, ...given.modules.map((module) => module.text)];
158
+ if (namesFx(texts) && !ownsFx(texts))
159
+ installFx({ filmTime, reseed, ground: () => getComputedStyle(builtStage().stage).backgroundColor, fit: () => stageSize.fit });
160
+ const kit = makeKit();
161
+ const scenes = installScenes(kit, {
162
+ run: (name, scopedKit, own) => {
163
+ const module = findModule(name);
164
+ return module ? tryScript(module.text, 'module ' + name, scopedKit, own) : new Error('no module ' + name);
165
+ },
166
+ reseed,
167
+ late: setLateHandler,
168
+ now: realNow,
169
+ resetFx: () => builtStage().fxWatch.reset(),
170
+ voice: given.narration !== null,
171
+ box: sceneBox,
172
+ sketch: sketchScene,
173
+ mode: () => film.mode,
174
+ });
175
+ Object.assign(kit, scenes.api);
176
+ setScriptKit(kit);
177
+ film.kit = kit;
178
+ buildStage(1920, 1080, '#ffffff');
179
+ setErrorPhase('load');
180
+ setFilmTime(0);
181
+ Reflect.set(window, 'CUT', undefined);
182
+ injectScript(given.script, 'cut.js');
183
+ // The SyntaxError or throw was reported with its line.
184
+ if (failedToLoad())
185
+ return;
186
+ if (builtStage().stage.childNodes.length > 3)
187
+ console.warn('cut.js built DOM at top level; it is discarded when the stage is sized, so build the scene in init(kit)');
188
+ const cut = readCut(Reflect.get(window, 'CUT'));
189
+ if (typeof cut === 'string') {
190
+ reportError('load', new Error(cut));
191
+ return;
192
+ }
193
+ film.cut = cut;
194
+ const { stage } = buildStage(cut.width, cut.height, cut.background);
195
+ // Fonts are in before init() measures anything.
196
+ await fontsIn();
197
+ if (cut.init)
198
+ await cut.init(kit);
199
+ try {
200
+ await document.fonts.ready;
201
+ }
202
+ catch {
203
+ // No font loading API: carry on.
204
+ }
205
+ // The pictures that scenes come on over, made while nothing is on show yet.
206
+ await holdEntrances(scenes.entrances);
207
+ const painted = getComputedStyle(stage);
208
+ const states = scenes.states();
209
+ const chapters = chaptersOf(cut);
210
+ const score = recordedScore(cut, kit);
211
+ const clock = recut(cut, chapters, states ? states.map((state) => state.id) : null);
212
+ if (clock.moved)
213
+ film.clock = clock;
214
+ const meta = {
215
+ width: cut.width,
216
+ height: cut.height,
217
+ fps: cut.fps,
218
+ duration: clock.moved ? clock.duration : cut.duration,
219
+ filmDuration: cut.duration,
220
+ chapters: chapters.map((chapter) => {
221
+ const part = clock.parts.find((each) => Math.abs(each.a0 - chapter.t) <= 0.05 && each.own === chapter.label);
222
+ return { t: clock.back(chapter.t), label: part ? part.label : chapter.label };
223
+ }),
224
+ scenes: states,
225
+ background: [painted.backgroundColor, painted.backgroundImage],
226
+ parts: clock.parts,
227
+ entrances: scenes.entrances.all(),
228
+ };
229
+ film.meta = meta;
230
+ // No record clears one: a look may have been tried ahead of the load.
231
+ const looks = findModule('retouched.json');
232
+ takeRecord(looks ? parseRetouchedRecord(looks.text) : null);
233
+ setTitles(findModule(HAND_TITLES_MODULE)?.text ?? null);
234
+ forgetReading();
235
+ const narration = narrationOf(cut, given, clock);
236
+ addClipCaptions(cut, narration, clock);
237
+ const laid = findModule(LAID_MODULE);
238
+ const laidRecord = laid ? parseLaidRecord(laid.text) : null;
239
+ film.laid = makeLaidLayer(laidRecord, clock.parts);
240
+ setErrorPhase('render');
241
+ const sampled = renderState.gavePromise;
242
+ // track() catches this promise's failure, so the window never hears of it: it is reported here.
243
+ renderAt(0)?.then(undefined, (error) => reportError(errorPhase(), error));
244
+ void syncClips('hold');
245
+ scanStage();
246
+ // After the first render: a clip that render moves with kit.seek is known by now.
247
+ const sounding = soundingClips(cut.duration);
248
+ for (const overlap of narrationOverlaps(cut.narration, sounding))
249
+ console.warn(overlap);
250
+ const own = playedClipSound(sounding, clock);
251
+ const clips = laidRecord === null ? own : soundWithLaid(own, laidRecord, laidParts(clock.parts), film.laid.plan.sounds);
252
+ film.loaded = true;
253
+ say({
254
+ type: 'ready',
255
+ meta,
256
+ score: score ? playedScore(score, clock) : null,
257
+ narration,
258
+ loadMs: Math.round(realNow() - began),
259
+ ...(clips.length > 0 ? { clips } : {}),
260
+ });
261
+ say({ type: 'clip-sound', own });
262
+ afterReady(sampled);
263
+ }
264
+ catch (error) {
265
+ reportError('load', error);
266
+ }
267
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,90 @@
1
+ import { captureConsole, catchErrors } from "./console.js";
2
+ import { installFilmClock, realNow } from "./determinism.js";
3
+ import { film } from "./film.js";
4
+ import { containFocus, holdLinks, watchNavigation } from "./focus.js";
5
+ import { pullFrame } from "./frame.js";
6
+ import { guardFilm } from "./guard.js";
7
+ import { fxRepaint } from "./fx/index.js";
8
+ import { setTitles } from "./hand-titles/index.js";
9
+ import { loadFilm } from "./load.js";
10
+ import { connect } from "./port.js";
11
+ import { lookAt, readLayers, tryLook } from "./questions.js";
12
+ import { askRender, isPlaying, redraw, showsAsked, syncClips, takeTurn } from "./render.js";
13
+ import { scanStage } from "./scan.js";
14
+ import { makeHoldFirstDraw } from "./snapshot/hold-first-draw.js";
15
+ import { watchForcedColors } from "./snapshot/serialize.js";
16
+ import { fit, stageParts } from "./stage.js";
17
+ import { retell, setTextsGuard, setWatch, tellTexts } from "./texts.js";
18
+ // Both are taken before the film clock and the film's own code can replace what they read.
19
+ const hold = makeHoldFirstDraw();
20
+ watchForcedColors();
21
+ installFilmClock();
22
+ watchNavigation();
23
+ captureConsole();
24
+ catchErrors();
25
+ containFocus();
26
+ holdLinks();
27
+ guardFilm();
28
+ setTextsGuard(showsAsked);
29
+ window.addEventListener('resize', () => {
30
+ fit();
31
+ fxRepaint(realNow);
32
+ retell();
33
+ });
34
+ /** What is laid over the film, drawn again on the moment on show without rendering the film. */
35
+ function drawLaid() {
36
+ const parts = stageParts();
37
+ if (!film.loaded || parts === null)
38
+ return;
39
+ film.laid?.draw(film.played, parts.stage);
40
+ if (!isPlaying())
41
+ void syncClips('hold');
42
+ }
43
+ function dispatch(message) {
44
+ switch (message.type) {
45
+ case 'load':
46
+ void loadFilm(message.film, hold);
47
+ return;
48
+ case 'render':
49
+ askRender({ t: message.t, seq: message.seq, playing: message.playing });
50
+ return;
51
+ case 'mode':
52
+ film.mode = message.mode;
53
+ if (film.loaded)
54
+ redraw();
55
+ return;
56
+ case 'frame':
57
+ pullFrame(message);
58
+ return;
59
+ case 'watch':
60
+ setWatch(message.on, message.places);
61
+ // Before the page's first render of a load the stage shows moment 0, which it never asked for: that render tells.
62
+ if (showsAsked())
63
+ tellTexts();
64
+ return;
65
+ case 'scan':
66
+ scanStage();
67
+ return;
68
+ case 'layers':
69
+ readLayers(message);
70
+ return;
71
+ case 'retouch':
72
+ tryLook(message.record);
73
+ return;
74
+ case 'laid':
75
+ film.laid?.set(message.record);
76
+ drawLaid();
77
+ return;
78
+ case 'look':
79
+ lookAt(message);
80
+ return;
81
+ case 'titles':
82
+ takeTurn(async () => setTitles(message.text));
83
+ return;
84
+ default: {
85
+ const never = message;
86
+ void never;
87
+ }
88
+ }
89
+ }
90
+ connect(dispatch);
@@ -0,0 +1,23 @@
1
+ import type { StageModule, StageModuleKind } from '../protocol.js';
2
+ interface HeldModule {
3
+ kind: StageModuleKind;
4
+ text: string;
5
+ }
6
+ /** The global through which injected code finds the kit it was given: read once, at the top of each script. */
7
+ export declare const KIT_GLOBAL = "__stageKit";
8
+ export declare function addModules(list: readonly StageModule[]): void;
9
+ export declare const moduleNames: () => string[];
10
+ /** By its whole name, or by a bare stem that only one module has: "scenes" finds "scenes.json". */
11
+ export declare function findModule(name: string): HeldModule | null;
12
+ /**
13
+ * Runs code in the page with `kit` in reach. What it throws surfaces through the window's error event. `own`: strict
14
+ * code in a function's scope, so that no top-level name reaches window, where another module's could meet it.
15
+ */
16
+ export declare function injectScript(code: string, label: string, own?: boolean): void;
17
+ export declare function setScriptKit(kit: unknown): void;
18
+ /**
19
+ * Runs code with `kit` as its kit and gives back what it threw, or null: a script put into the page throws to the
20
+ * window's error event during the call, never to the caller, so a try alone would catch nothing.
21
+ */
22
+ export declare function tryScript(code: string, label: string, kit: unknown, own: boolean): unknown;
23
+ export {};
@@ -0,0 +1,53 @@
1
+ import { guarded } from "./console.js";
2
+ const modules = new Map();
3
+ /** The global through which injected code finds the kit it was given: read once, at the top of each script. */
4
+ export const KIT_GLOBAL = '__stageKit';
5
+ export function addModules(list) {
6
+ for (const module of list)
7
+ modules.set(module.name, { kind: module.kind, text: module.text });
8
+ }
9
+ export const moduleNames = () => [...modules.keys()];
10
+ /** By its whole name, or by a bare stem that only one module has: "scenes" finds "scenes.json". */
11
+ export function findModule(name) {
12
+ const exact = modules.get(name);
13
+ if (exact)
14
+ return exact;
15
+ if (name.includes('.'))
16
+ return null;
17
+ const hits = [...modules.keys()].filter((key) => key.startsWith(name + '.') && key.indexOf('.', name.length + 1) < 0);
18
+ const only = hits.length === 1 ? hits[0] : undefined;
19
+ return only === undefined ? null : (modules.get(only) ?? null);
20
+ }
21
+ /**
22
+ * Runs code in the page with `kit` in reach. What it throws surfaces through the window's error event. `own`: strict
23
+ * code in a function's scope, so that no top-level name reaches window, where another module's could meet it.
24
+ */
25
+ export function injectScript(code, label, own = false) {
26
+ const script = document.createElement('script');
27
+ script.text =
28
+ (own ? `(function () { "use strict"; const kit = window.${KIT_GLOBAL};\n` : `{ const kit = window.${KIT_GLOBAL};\n`) +
29
+ code +
30
+ (own ? '\n})();' : '\n}') +
31
+ '\n//# sourceURL=' +
32
+ label.replace(/[^A-Za-z0-9_./ -]/g, '_') +
33
+ '\n';
34
+ document.body.appendChild(script);
35
+ script.remove();
36
+ }
37
+ export function setScriptKit(kit) {
38
+ Reflect.set(window, KIT_GLOBAL, kit);
39
+ }
40
+ /**
41
+ * Runs code with `kit` as its kit and gives back what it threw, or null: a script put into the page throws to the
42
+ * window's error event during the call, never to the caller, so a try alone would catch nothing.
43
+ */
44
+ export function tryScript(code, label, kit, own) {
45
+ const outerKit = Reflect.get(window, KIT_GLOBAL);
46
+ setScriptKit(kit);
47
+ try {
48
+ return guarded(() => injectScript(code, label, own));
49
+ }
50
+ finally {
51
+ setScriptKit(outerKit);
52
+ }
53
+ }
@@ -0,0 +1,8 @@
1
+ import type { HostToStage, StageToHost } from '../protocol.js';
2
+ /** What the stage says before the host's port has come waits for it. */
3
+ export declare function say(message: StageToHost): void;
4
+ /**
5
+ * The host posts the port to this window once, on the frame's load. The frame is sandboxed with allow-scripts only,
6
+ * so the port is the one way in: the first one from the parent is kept, any other ignored.
7
+ */
8
+ export declare function connect(onMessage: (message: HostToStage) => void): void;
@@ -0,0 +1,46 @@
1
+ import { STAGE_PROTOCOL_VERSION, isHostToStage, isStageConnect } from "../protocol.js";
2
+ // Taken before the film runs: a film may replace MessagePort's methods to read or drop what the stage says.
3
+ const realPost = MessagePort.prototype.postMessage;
4
+ const realParent = window.parent;
5
+ const MOST_QUEUED = 200;
6
+ let port = null;
7
+ const queued = [];
8
+ /** Never with a transfer list: building one runs Array.prototype[Symbol.iterator], which a film can make throw. */
9
+ function post(to, message) {
10
+ try {
11
+ Reflect.apply(realPost, to, [message]);
12
+ }
13
+ catch {
14
+ // The browser refused to clone it: dropped.
15
+ }
16
+ }
17
+ /** What the stage says before the host's port has come waits for it. */
18
+ export function say(message) {
19
+ if (port)
20
+ post(port, message);
21
+ else if (queued.length < MOST_QUEUED)
22
+ queued.push(message);
23
+ }
24
+ /**
25
+ * The host posts the port to this window once, on the frame's load. The frame is sandboxed with allow-scripts only,
26
+ * so the port is the one way in: the first one from the parent is kept, any other ignored.
27
+ */
28
+ export function connect(onMessage) {
29
+ const take = (event) => {
30
+ if (port || event.source !== realParent || !isStageConnect(event.data))
31
+ return;
32
+ const given = event.ports[0];
33
+ if (!given)
34
+ return;
35
+ port = given;
36
+ window.removeEventListener('message', take, true);
37
+ given.onmessage = (incoming) => {
38
+ if (isHostToStage(incoming.data))
39
+ onMessage(incoming.data);
40
+ };
41
+ post(given, { type: 'hello', version: STAGE_PROTOCOL_VERSION });
42
+ for (const message of queued.splice(0))
43
+ post(given, message);
44
+ };
45
+ window.addEventListener('message', take, true);
46
+ }
@@ -0,0 +1,24 @@
1
+ import type { RetouchedRecord } from '@openartifactsai/motion-core';
2
+ import type { HostToStage } from '../protocol.js';
3
+ type LayersAsk = Extract<HostToStage, {
4
+ type: 'layers';
5
+ }>;
6
+ type LookAsk = Extract<HostToStage, {
7
+ type: 'look';
8
+ }>;
9
+ /** Takes the record of what a person changed by hand; a fault costs their changes, not the film. */
10
+ export declare function takeRecord(record: RetouchedRecord | null): void;
11
+ /**
12
+ * A look that is being tried (a drag on a slider), or the record a load brought. It takes the marks off the one
13
+ * stage, so it takes its turn with the frame pulls.
14
+ */
15
+ export declare function tryLook(record: RetouchedRecord | null): void;
16
+ /** A reading takes its turn with the frame pulls, since it draws on the one stage. A newer question, or a stop, ends one under way. */
17
+ export declare function readLayers(d: LayersAsk): void;
18
+ /**
19
+ * How the film itself draws one thing, for the controls that change it. d.t: the moment the page has on show, drawn
20
+ * first, which sets the marks the answer tells of, and again at the end. A question a newer one has overtaken by its
21
+ * turn is answered with no look, and draws nothing.
22
+ */
23
+ export declare function lookAt(d: LookAsk): void;
24
+ export {};
@@ -0,0 +1,162 @@
1
+ import { filmTime } from "./determinism.js";
2
+ import { film } from "./film.js";
3
+ import { scanLayers } from "./layers/scan.js";
4
+ import { say } from "./port.js";
5
+ import { afterSample, drawOwn, holdStage, onShow, releaseStage, renderAt, renderState, rendersSettle, setShown, syncClips, takeTurn, unretouched } from "./render.js";
6
+ import { ask, lookOn } from "./retouch/ask.js";
7
+ import { kinFor, lostIn, partsFor, setRecord, stageOf, underHideIn } from "./retouch/retouch.js";
8
+ import { laidOut, whenLaidOut } from "./stage.js";
9
+ import { stretchOf } from "./stretch.js";
10
+ const messageOf = (error) => String(error instanceof Error ? error.message : error).slice(0, 200);
11
+ const quiet = (q) => {
12
+ q?.then(undefined, () => undefined);
13
+ };
14
+ /** A moment of this stretch and of no other: short of its end, the next one's first moment, rounded down, and not before its start. */
15
+ const momentIn = (s, T) => Math.max(s.lo, Math.floor(Math.min(s.hi - 1 / 240, T - s.base) * 1000) / 1000);
16
+ /** Takes the record of what a person changed by hand; a fault costs their changes, not the film. */
17
+ export function takeRecord(record) {
18
+ try {
19
+ setRecord(record);
20
+ }
21
+ catch {
22
+ unretouched();
23
+ }
24
+ }
25
+ /**
26
+ * A look that is being tried (a drag on a slider), or the record a load brought. It takes the marks off the one
27
+ * stage, so it takes its turn with the frame pulls.
28
+ */
29
+ export function tryLook(record) {
30
+ takeTurn(async () => {
31
+ const wait = rendersSettle();
32
+ if (wait)
33
+ await wait;
34
+ takeRecord(record);
35
+ if (!film.loaded)
36
+ return;
37
+ const sampled = renderState.gavePromise;
38
+ quiet(renderAt(onShow()));
39
+ // A sample this draw began: the moment on show is drawn again after it, unless a live frame is on its way.
40
+ const again = afterSample(sampled, onShow);
41
+ if (again)
42
+ quiet((await again).q);
43
+ });
44
+ }
45
+ /**
46
+ * Draws a moment at once for a reading. A render that returns a promise has not drawn yet when it returns, so what
47
+ * stands on the stage is of no one moment: better no list than boxes that lie elsewhere.
48
+ */
49
+ function drawNow(t) {
50
+ const drawn = renderAt(t);
51
+ if (drawn) {
52
+ quiet(drawn);
53
+ throw new Error('render(t) returns a promise');
54
+ }
55
+ }
56
+ let layersWanted = 0;
57
+ /** A new frame is laid out a moment after it is shown: a reading asked for before then waits this long for it. */
58
+ const LAYOUT_WAIT_MS = 2000;
59
+ /** A reading takes its turn with the frame pulls, since it draws on the one stage. A newer question, or a stop, ends one under way. */
60
+ export function readLayers(d) {
61
+ layersWanted = d.seq;
62
+ if (d.stop)
63
+ return;
64
+ takeTurn(async () => {
65
+ let report = null;
66
+ if (film.loaded && d.seq === layersWanted && !laidOut())
67
+ await whenLaidOut(LAYOUT_WAIT_MS);
68
+ const stage = stageOf();
69
+ if (film.loaded && stage && d.seq === layersWanted) {
70
+ holdStage();
71
+ try {
72
+ report = await scanLayers(d, {
73
+ stage,
74
+ render: drawNow,
75
+ look: (el) => {
76
+ const s = stretchOf(filmTime());
77
+ const k = lookOn(el, s.lo, s.hi, s.name);
78
+ if (k)
79
+ k.at = momentIn(s, filmTime());
80
+ return k;
81
+ },
82
+ kin: kinFor,
83
+ parts: partsFor,
84
+ lost: () => {
85
+ const s = stretchOf(filmTime());
86
+ return s.name === null ? [] : lostIn(s.name, s.lo, s.hi);
87
+ },
88
+ underHide: () => {
89
+ const s = stretchOf(filmTime());
90
+ return s.name === null ? [] : underHideIn(s.name, s.lo, s.hi);
91
+ },
92
+ }, () => d.seq !== layersWanted);
93
+ }
94
+ catch (error) {
95
+ console.warn('layers: ' + messageOf(error));
96
+ }
97
+ finally {
98
+ releaseStage();
99
+ }
100
+ }
101
+ say({ type: 'layers', report: report ?? { seq: d.seq, from: d.from, to: d.to, at: d.at, lost: [], underHide: [], rows: null } });
102
+ });
103
+ }
104
+ let lookWanted = 0;
105
+ /**
106
+ * How the film itself draws one thing, for the controls that change it. d.t: the moment the page has on show, drawn
107
+ * first, which sets the marks the answer tells of, and again at the end. A question a newer one has overtaken by its
108
+ * turn is answered with no look, and draws nothing.
109
+ */
110
+ export function lookAt(d) {
111
+ lookWanted = d.seq;
112
+ takeTurn(async () => {
113
+ let look = null;
114
+ let lost = [];
115
+ if (film.loaded && d.seq === lookWanted) {
116
+ holdStage();
117
+ try {
118
+ const wait = rendersSettle();
119
+ if (wait)
120
+ await wait;
121
+ const t = d.t !== null && Number.isFinite(d.t) ? d.t : onShow();
122
+ const sampled = renderState.gavePromise;
123
+ let away = false;
124
+ try {
125
+ const drawn = renderAt(t);
126
+ const s = stretchOf(filmTime());
127
+ const at = momentIn(s, filmTime());
128
+ const name = s.name;
129
+ if (drawn)
130
+ await drawn.then(undefined, () => undefined);
131
+ else if (renderState.ok && name !== null) {
132
+ away = true;
133
+ look = ask(name, d.id, s.lo, s.hi, at, (x) => drawOwn(s.base + Math.max(s.lo, Math.min(s.hi - 1 / 240, x))));
134
+ lost = lostIn(name, s.lo, s.hi);
135
+ }
136
+ }
137
+ catch (error) {
138
+ look = null;
139
+ console.warn('How the film draws a thing could not be read, so the page offers no way to change it: ' + messageOf(error));
140
+ }
141
+ if (away) {
142
+ setShown(t);
143
+ quiet(renderAt(t));
144
+ quiet(syncClips('hold'));
145
+ }
146
+ // A sample its draws began goes first, and the moment is drawn again before the answer.
147
+ const again = afterSample(sampled, t);
148
+ if (again) {
149
+ quiet((await again).q);
150
+ quiet(syncClips('hold'));
151
+ }
152
+ }
153
+ catch {
154
+ // The next frame draws it.
155
+ }
156
+ finally {
157
+ releaseStage();
158
+ }
159
+ }
160
+ say({ type: 'looked', seq: d.seq, id: d.id, look, lost });
161
+ });
162
+ }