@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.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +70 -4
- package/src/build.d.ts +18 -0
- package/src/build.js +73 -0
- package/src/host.d.ts +37 -0
- package/src/host.js +76 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +6 -0
- package/src/layers.d.ts +30 -0
- package/src/layers.js +56 -0
- package/src/offline/audio.d.ts +19 -0
- package/src/offline/audio.js +20 -0
- package/src/offline/data.d.ts +26 -0
- package/src/offline/data.js +71 -0
- package/src/offline/main.d.ts +9 -0
- package/src/offline/main.js +52 -0
- package/src/offline/player.d.ts +23 -0
- package/src/offline/player.js +157 -0
- package/src/protocol/clip-guards.d.ts +5 -0
- package/src/protocol/clip-guards.js +22 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +257 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +100 -0
- package/src/protocol/layers.d.ts +135 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +340 -0
- package/src/protocol/types.js +7 -0
- package/src/protocol.d.ts +2 -0
- package/src/protocol.js +2 -0
- package/src/raster.d.ts +4 -0
- package/src/raster.js +2 -0
- package/src/runtime/assets.d.ts +19 -0
- package/src/runtime/assets.js +63 -0
- package/src/runtime/caption-box.d.ts +3 -0
- package/src/runtime/caption-box.js +16 -0
- package/src/runtime/captions.d.ts +31 -0
- package/src/runtime/captions.js +115 -0
- package/src/runtime/clip-captions.d.ts +9 -0
- package/src/runtime/clip-captions.js +27 -0
- package/src/runtime/clips.d.ts +83 -0
- package/src/runtime/clips.js +259 -0
- package/src/runtime/console.d.ts +13 -0
- package/src/runtime/console.js +99 -0
- package/src/runtime/cut.d.ts +7 -0
- package/src/runtime/cut.js +63 -0
- package/src/runtime/determinism.d.ts +12 -0
- package/src/runtime/determinism.js +47 -0
- package/src/runtime/film.d.ts +43 -0
- package/src/runtime/film.js +31 -0
- package/src/runtime/focus.d.ts +8 -0
- package/src/runtime/focus.js +71 -0
- package/src/runtime/fonts.d.ts +22 -0
- package/src/runtime/fonts.js +109 -0
- package/src/runtime/frame.d.ts +9 -0
- package/src/runtime/frame.js +140 -0
- package/src/runtime/fx/backdrop.d.ts +60 -0
- package/src/runtime/fx/backdrop.js +264 -0
- package/src/runtime/fx/color.d.ts +19 -0
- package/src/runtime/fx/color.js +84 -0
- package/src/runtime/fx/css-color.d.ts +6 -0
- package/src/runtime/fx/css-color.js +44 -0
- package/src/runtime/fx/curve.d.ts +12 -0
- package/src/runtime/fx/curve.js +34 -0
- package/src/runtime/fx/gl.d.ts +29 -0
- package/src/runtime/fx/gl.js +207 -0
- package/src/runtime/fx/glsl.d.ts +35 -0
- package/src/runtime/fx/glsl.js +113 -0
- package/src/runtime/fx/index.d.ts +23 -0
- package/src/runtime/fx/index.js +151 -0
- package/src/runtime/fx/key.d.ts +11 -0
- package/src/runtime/fx/key.js +21 -0
- package/src/runtime/fx/numbers.d.ts +14 -0
- package/src/runtime/fx/numbers.js +57 -0
- package/src/runtime/fx/pace.d.ts +17 -0
- package/src/runtime/fx/pace.js +26 -0
- package/src/runtime/fx/paint.d.ts +28 -0
- package/src/runtime/fx/paint.js +150 -0
- package/src/runtime/guard.d.ts +4 -0
- package/src/runtime/guard.js +141 -0
- package/src/runtime/hand-titles/index.d.ts +9 -0
- package/src/runtime/hand-titles/index.js +73 -0
- package/src/runtime/hand-titles/layer.d.ts +20 -0
- package/src/runtime/hand-titles/layer.js +118 -0
- package/src/runtime/hand-titles/place.d.ts +27 -0
- package/src/runtime/hand-titles/place.js +45 -0
- package/src/runtime/held-store.d.ts +35 -0
- package/src/runtime/held-store.js +63 -0
- package/src/runtime/held.d.ts +19 -0
- package/src/runtime/held.js +225 -0
- package/src/runtime/kit/camera.d.ts +59 -0
- package/src/runtime/kit/camera.js +81 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -0
- package/src/runtime/kit/count.d.ts +37 -0
- package/src/runtime/kit/count.js +100 -0
- package/src/runtime/kit/cursor.d.ts +46 -0
- package/src/runtime/kit/cursor.js +108 -0
- package/src/runtime/kit/dom.d.ts +14 -0
- package/src/runtime/kit/dom.js +46 -0
- package/src/runtime/kit/geometry.d.ts +24 -0
- package/src/runtime/kit/geometry.js +56 -0
- package/src/runtime/kit/index.d.ts +113 -0
- package/src/runtime/kit/index.js +157 -0
- package/src/runtime/kit/mark-strokes.d.ts +65 -0
- package/src/runtime/kit/mark-strokes.js +226 -0
- package/src/runtime/kit/marks.d.ts +21 -0
- package/src/runtime/kit/marks.js +165 -0
- package/src/runtime/kit/math.d.ts +23 -0
- package/src/runtime/kit/math.js +35 -0
- package/src/runtime/kit/paper.d.ts +7 -0
- package/src/runtime/kit/paper.js +27 -0
- package/src/runtime/kit/random.d.ts +8 -0
- package/src/runtime/kit/random.js +32 -0
- package/src/runtime/kit/text-fx.d.ts +18 -0
- package/src/runtime/kit/text-fx.js +55 -0
- package/src/runtime/kit/typing.d.ts +21 -0
- package/src/runtime/kit/typing.js +42 -0
- package/src/runtime/laid/layer.d.ts +44 -0
- package/src/runtime/laid/layer.js +370 -0
- package/src/runtime/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +123 -0
- package/src/runtime/layers/item.js +150 -0
- package/src/runtime/layers/parts.d.ts +53 -0
- package/src/runtime/layers/parts.js +158 -0
- package/src/runtime/layers/rows.d.ts +4 -0
- package/src/runtime/layers/rows.js +233 -0
- package/src/runtime/layers/scan.d.ts +24 -0
- package/src/runtime/layers/scan.js +204 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +337 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +267 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +90 -0
- package/src/runtime/modules.d.ts +23 -0
- package/src/runtime/modules.js +53 -0
- package/src/runtime/port.d.ts +8 -0
- package/src/runtime/port.js +46 -0
- package/src/runtime/questions.d.ts +24 -0
- package/src/runtime/questions.js +162 -0
- package/src/runtime/recut.d.ts +81 -0
- package/src/runtime/recut.js +325 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +358 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +171 -0
- package/src/runtime/retouch/entry.d.ts +77 -0
- package/src/runtime/retouch/entry.js +1 -0
- package/src/runtime/retouch/judge.d.ts +25 -0
- package/src/runtime/retouch/judge.js +209 -0
- package/src/runtime/retouch/look.d.ts +39 -0
- package/src/runtime/retouch/look.js +135 -0
- package/src/runtime/retouch/own-styles.d.ts +19 -0
- package/src/runtime/retouch/own-styles.js +141 -0
- package/src/runtime/retouch/parts.d.ts +22 -0
- package/src/runtime/retouch/parts.js +166 -0
- package/src/runtime/retouch/retouch.d.ts +37 -0
- package/src/runtime/retouch/retouch.js +238 -0
- package/src/runtime/retouch/seat.d.ts +40 -0
- package/src/runtime/retouch/seat.js +228 -0
- package/src/runtime/retouch/state.d.ts +37 -0
- package/src/runtime/retouch/state.js +153 -0
- package/src/runtime/retouch/type-style.d.ts +19 -0
- package/src/runtime/retouch/type-style.js +111 -0
- package/src/runtime/scan.d.ts +2 -0
- package/src/runtime/scan.js +49 -0
- package/src/runtime/scenes/card.d.ts +12 -0
- package/src/runtime/scenes/card.js +56 -0
- package/src/runtime/scenes/install.d.ts +69 -0
- package/src/runtime/scenes/install.js +528 -0
- package/src/runtime/scenes/read.d.ts +27 -0
- package/src/runtime/scenes/read.js +73 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +46 -0
- package/src/runtime/score.d.ts +32 -0
- package/src/runtime/score.js +262 -0
- package/src/runtime/sketch/pen.d.ts +3 -0
- package/src/runtime/sketch/pen.js +371 -0
- package/src/runtime/sketch/strokes.d.ts +41 -0
- package/src/runtime/sketch/strokes.js +187 -0
- package/src/runtime/snapshot/draw.d.ts +14 -0
- package/src/runtime/snapshot/draw.js +125 -0
- package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
- package/src/runtime/snapshot/hold-first-draw.js +128 -0
- package/src/runtime/snapshot/lift.d.ts +41 -0
- package/src/runtime/snapshot/lift.js +161 -0
- package/src/runtime/snapshot/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +51 -0
- package/src/runtime/snapshot/serialize.js +286 -0
- package/src/runtime/snapshot/styles.d.ts +8 -0
- package/src/runtime/snapshot/styles.js +109 -0
- package/src/runtime/snapshot/xml.d.ts +6 -0
- package/src/runtime/snapshot/xml.js +110 -0
- package/src/runtime/stage-size.d.ts +7 -0
- package/src/runtime/stage-size.js +3 -0
- package/src/runtime/stage.d.ts +27 -0
- package/src/runtime/stage.js +154 -0
- package/src/runtime/stretch.d.ts +13 -0
- package/src/runtime/stretch.js +16 -0
- package/src/runtime/texts.d.ts +24 -0
- package/src/runtime/texts.js +211 -0
- package/src/runtime/video-sync.d.ts +51 -0
- package/src/runtime/video-sync.js +218 -0
- package/src/stills.d.ts +90 -0
- package/src/stills.js +329 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/levels.d.ts +8 -0
- package/src/tools/levels.js +28 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +223 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +35 -0
- package/src/tools/types.d.ts +84 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +6 -0
- package/src/tools.js +4 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { BLOB_URL_PATTERN, EXPORT_SETTINGS, RECOLORED_MODULE, RETYPED_MODULE, VOICE_SETTINGS, applyRecolored, applyRetyped, decodeFilm, filmModule, parseRecoloredRecord, parseRetypedRecord, } from '@openartifactsai/motion-core';
|
|
2
|
+
import { fromBase64 } from "../offline/data.js";
|
|
3
|
+
const streams = (mime) => mime.startsWith('video/') || mime.startsWith('audio/');
|
|
4
|
+
function documentsOf(given, problems) {
|
|
5
|
+
const documents = new Map();
|
|
6
|
+
for (const [path, value] of Object.entries(given)) {
|
|
7
|
+
if (typeof value !== 'string') {
|
|
8
|
+
documents.set(path, value);
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
documents.set(path, JSON.parse(value));
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
problems.push(`db/${path}.json is not JSON; the player ignores it.`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return documents;
|
|
19
|
+
}
|
|
20
|
+
/** The base64 of an asset's bytes: from its `data:` ref, or from the upload it names. null: not to be had. */
|
|
21
|
+
function base64Of(asset, blobs) {
|
|
22
|
+
if (asset.ref.startsWith('data:')) {
|
|
23
|
+
const comma = asset.ref.indexOf(',');
|
|
24
|
+
return comma < 0 ? null : asset.ref.slice(comma + 1);
|
|
25
|
+
}
|
|
26
|
+
const id = BLOB_URL_PATTERN.exec(asset.ref)?.[1];
|
|
27
|
+
return id === undefined ? null : (blobs[id] ?? null);
|
|
28
|
+
}
|
|
29
|
+
function stageAsset(asset, blobs) {
|
|
30
|
+
const base = { name: asset.name, mime: asset.mime, w: asset.w ?? 0, h: asset.h ?? 0 };
|
|
31
|
+
const data = base64Of(asset, blobs);
|
|
32
|
+
if (data === null)
|
|
33
|
+
return null;
|
|
34
|
+
if (streams(asset.mime))
|
|
35
|
+
return { ...base, url: `data:${asset.mime};base64,${data}` };
|
|
36
|
+
try {
|
|
37
|
+
return { ...base, bytes: fromBase64(data) };
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function recordOf(film, name, parse) {
|
|
44
|
+
const module = filmModule(film, name);
|
|
45
|
+
if (module === undefined)
|
|
46
|
+
return null;
|
|
47
|
+
try {
|
|
48
|
+
return parse(JSON.parse(module.text));
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return parse(null);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** A person's retyped lines and colors written into the code, as the page does before the stage runs it. */
|
|
55
|
+
function applied(film, problems) {
|
|
56
|
+
let script = film.script;
|
|
57
|
+
let modules = [...film.modules];
|
|
58
|
+
const retyped = recordOf(film, RETYPED_MODULE, parseRetypedRecord);
|
|
59
|
+
if (retyped !== null) {
|
|
60
|
+
try {
|
|
61
|
+
({ script, modules } = applyRetyped({ script, modules }, retyped));
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
problems.push(`A person's text (modules/${RETYPED_MODULE}) could not be swapped in, so the film shows its own.`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const recolored = recordOf(film, RECOLORED_MODULE, parseRecoloredRecord);
|
|
68
|
+
if (recolored !== null && recolored.colors.length > 0) {
|
|
69
|
+
try {
|
|
70
|
+
({ script, modules } = applyRecolored({ script, modules }, recolored));
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
problems.push(`A person's colors (modules/${RECOLORED_MODULE}) could not be swapped in, so the film shows its own.`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { script, modules };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The film the tools page loads, read from its files as the page reads its store. `retouched.json` stays among
|
|
80
|
+
* the modules: the stage lays it from there at load, as the page's own `retouch` message would.
|
|
81
|
+
*/
|
|
82
|
+
export function toolsFilm(files) {
|
|
83
|
+
const problems = [];
|
|
84
|
+
const decoded = decodeFilm(documentsOf(files.documents, problems));
|
|
85
|
+
for (const problem of decoded.problems)
|
|
86
|
+
problems.push(`db/${problem.path}.json ${problem.message}`);
|
|
87
|
+
const title = decoded.meta?.title ?? 'Film';
|
|
88
|
+
if (decoded.script.trim() === '') {
|
|
89
|
+
problems.unshift('The film has no code yet (db/film/script.json has no "code").');
|
|
90
|
+
return { film: null, title, problems };
|
|
91
|
+
}
|
|
92
|
+
const blobs = files.blobs ?? {};
|
|
93
|
+
const assets = [];
|
|
94
|
+
for (const asset of decoded.assets) {
|
|
95
|
+
const loaded = stageAsset(asset, blobs);
|
|
96
|
+
if (loaded !== null)
|
|
97
|
+
assets.push(loaded);
|
|
98
|
+
else
|
|
99
|
+
problems.push(`The asset ${asset.name} could not be loaded (${asset.ref} was not given).`);
|
|
100
|
+
}
|
|
101
|
+
const code = applied(decoded, problems);
|
|
102
|
+
const mode = files.mode ?? 'final';
|
|
103
|
+
return {
|
|
104
|
+
film: {
|
|
105
|
+
script: code.script,
|
|
106
|
+
modules: code.modules.map((module) => ({ name: module.name, kind: module.kind, text: module.text })),
|
|
107
|
+
assets,
|
|
108
|
+
faces: null,
|
|
109
|
+
narration: { maxLines: VOICE_SETTINGS.maxLines, maxLineSeconds: VOICE_SETTINGS.maxLineSeconds },
|
|
110
|
+
clippedTextWaitMs: EXPORT_SETTINGS.clippedTextWaitMs,
|
|
111
|
+
mode,
|
|
112
|
+
},
|
|
113
|
+
title,
|
|
114
|
+
problems,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PlayedScore, StageMeta } from '../protocol.js';
|
|
2
|
+
import type { SceneLevels } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The band's levels over each part of the film the stage reported (each scene, in a film of scenes). The score is
|
|
5
|
+
* already on the play clock, so it is heard on an identity timeline over the played length. Null for a score with no
|
|
6
|
+
* band, or when the levels cannot be measured.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sceneLevels(meta: Pick<StageMeta, 'duration' | 'parts'>, score: PlayedScore | null): Promise<SceneLevels[] | null>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { identityTimeline, measureSoundLevels, playScore } from '@openartifactsai/motion-audio';
|
|
2
|
+
import { filmSeconds } from '@openartifactsai/motion-core';
|
|
3
|
+
/**
|
|
4
|
+
* The band's levels over each part of the film the stage reported (each scene, in a film of scenes). The score is
|
|
5
|
+
* already on the play clock, so it is heard on an identity timeline over the played length. Null for a score with no
|
|
6
|
+
* band, or when the levels cannot be measured.
|
|
7
|
+
*/
|
|
8
|
+
export async function sceneLevels(meta, score) {
|
|
9
|
+
if (score === null || meta.parts.length === 0)
|
|
10
|
+
return null;
|
|
11
|
+
const data = { ...score, events: score.events.map((event) => ({ ...event, t: filmSeconds(event.t) })) };
|
|
12
|
+
try {
|
|
13
|
+
const played = playScore(data, identityTimeline(filmSeconds(meta.duration)));
|
|
14
|
+
const levels = await measureSoundLevels(played, meta.parts.map((part) => ({ at: part.p0, dur: part.dur })));
|
|
15
|
+
if (levels === null)
|
|
16
|
+
return null;
|
|
17
|
+
return meta.parts.map((part, index) => ({
|
|
18
|
+
scene: part.key ?? part.own,
|
|
19
|
+
at: part.p0,
|
|
20
|
+
dur: part.dur,
|
|
21
|
+
all: levels[index].all,
|
|
22
|
+
tracks: levels[index].tracks,
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { SNAPSHOT_SETTINGS } from '@openartifactsai/motion-core';
|
|
2
|
+
import { STAGE_SANDBOX, openStagePort, stageDocument } from "../host.js";
|
|
3
|
+
import { OFFLINE_RUNTIME_ID } from "../offline/data.js";
|
|
4
|
+
import { STAGE_PROTOCOL_VERSION } from "../protocol.js";
|
|
5
|
+
import { drawSnapshot, makeHoldFirstDraw, whyNoDecode } from "../raster.js";
|
|
6
|
+
import { toolsFilm } from "./film.js";
|
|
7
|
+
import { sceneLevels } from "./levels.js";
|
|
8
|
+
import { scoreReport } from "./score.js";
|
|
9
|
+
import { TOOLS_GLOBAL } from "./types.js";
|
|
10
|
+
const LOAD_TIMEOUT_MS = 60000;
|
|
11
|
+
/** The stage's own budget for a pull is 12 s, with its render; the page gives it 15. */
|
|
12
|
+
const FRAME_TIMEOUT_MS = 15000;
|
|
13
|
+
const SHOW_TIMEOUT_MS = 15000;
|
|
14
|
+
const holdFirstDraw = makeHoldFirstDraw();
|
|
15
|
+
let log = [];
|
|
16
|
+
let mounted = null;
|
|
17
|
+
let seq = 0;
|
|
18
|
+
const messageOf = (error) => (error instanceof Error ? error.message : String(error));
|
|
19
|
+
function wait(waiters, at, ms, late) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const timer = setTimeout(() => {
|
|
22
|
+
waiters.delete(at);
|
|
23
|
+
reject(new Error(late));
|
|
24
|
+
}, ms);
|
|
25
|
+
waiters.set(at, { resolve, reject, timer });
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function rejectAll(waiters, error) {
|
|
29
|
+
for (const waiter of waiters.values()) {
|
|
30
|
+
clearTimeout(waiter.timer);
|
|
31
|
+
waiter.reject(error);
|
|
32
|
+
}
|
|
33
|
+
waiters.clear();
|
|
34
|
+
}
|
|
35
|
+
function unmount(why) {
|
|
36
|
+
if (mounted === null)
|
|
37
|
+
return;
|
|
38
|
+
const gone = mounted;
|
|
39
|
+
mounted = null;
|
|
40
|
+
const error = new Error(why);
|
|
41
|
+
gone.settle.reject(error);
|
|
42
|
+
rejectAll(gone.shows, error);
|
|
43
|
+
rejectAll(gone.pulls, error);
|
|
44
|
+
gone.port?.close();
|
|
45
|
+
gone.frame.remove();
|
|
46
|
+
}
|
|
47
|
+
function hear(own, message) {
|
|
48
|
+
if (mounted !== own)
|
|
49
|
+
return;
|
|
50
|
+
switch (message.type) {
|
|
51
|
+
case 'hello':
|
|
52
|
+
if (message.version !== STAGE_PROTOCOL_VERSION)
|
|
53
|
+
own.settle.reject(new Error('the stage speaks version ' + message.version + ', not ' + STAGE_PROTOCOL_VERSION));
|
|
54
|
+
return;
|
|
55
|
+
case 'ready':
|
|
56
|
+
own.settle.resolve({ meta: message.meta, score: message.score, narration: message.narration });
|
|
57
|
+
return;
|
|
58
|
+
case 'presented':
|
|
59
|
+
for (const [at, waiter] of own.shows) {
|
|
60
|
+
if (at > message.seq)
|
|
61
|
+
continue;
|
|
62
|
+
clearTimeout(waiter.timer);
|
|
63
|
+
own.shows.delete(at);
|
|
64
|
+
waiter.resolve();
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
case 'frame': {
|
|
68
|
+
const waiter = own.pulls.get(message.seq);
|
|
69
|
+
if (waiter === undefined)
|
|
70
|
+
return;
|
|
71
|
+
clearTimeout(waiter.timer);
|
|
72
|
+
own.pulls.delete(message.seq);
|
|
73
|
+
waiter.resolve(message);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
case 'frame-error': {
|
|
77
|
+
const waiter = own.pulls.get(message.seq);
|
|
78
|
+
if (waiter === undefined)
|
|
79
|
+
return;
|
|
80
|
+
clearTimeout(waiter.timer);
|
|
81
|
+
own.pulls.delete(message.seq);
|
|
82
|
+
waiter.reject(new Error(message.message));
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
case 'console':
|
|
86
|
+
log.push({ level: message.level, text: message.text });
|
|
87
|
+
return;
|
|
88
|
+
case 'error':
|
|
89
|
+
log.push({ level: 'error', text: message.message + (message.stack ? '\n' + message.stack : ''), phase: message.phase });
|
|
90
|
+
if (message.phase === 'load' && own.loaded === null)
|
|
91
|
+
own.settle.reject(new Error(message.message));
|
|
92
|
+
return;
|
|
93
|
+
case 'navigating':
|
|
94
|
+
unmount('the stage navigated away, so the film is gone');
|
|
95
|
+
return;
|
|
96
|
+
case 'texts':
|
|
97
|
+
case 'layers':
|
|
98
|
+
case 'looked':
|
|
99
|
+
case 'link':
|
|
100
|
+
case 'clip-sound':
|
|
101
|
+
case 'titles':
|
|
102
|
+
return;
|
|
103
|
+
default: {
|
|
104
|
+
const never = message;
|
|
105
|
+
void never;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function mount(film, runtime) {
|
|
110
|
+
const frame = document.createElement('iframe');
|
|
111
|
+
frame.setAttribute('sandbox', STAGE_SANDBOX);
|
|
112
|
+
frame.title = 'Film';
|
|
113
|
+
frame.style.cssText = 'position:fixed;inset:0;width:100%;height:100%;border:0;background:transparent';
|
|
114
|
+
frame.srcdoc = stageDocument(runtime);
|
|
115
|
+
let settle = { resolve: () => undefined, reject: () => undefined };
|
|
116
|
+
const ready = new Promise((resolve, reject) => {
|
|
117
|
+
settle = { resolve, reject };
|
|
118
|
+
});
|
|
119
|
+
const own = { frame, port: null, film, loaded: null, settle, shows: new Map(), pulls: new Map() };
|
|
120
|
+
mounted = own;
|
|
121
|
+
frame.addEventListener('load', () => {
|
|
122
|
+
own.port = openStagePort(frame, (message) => hear(own, message));
|
|
123
|
+
own.port?.send({ type: 'load', film });
|
|
124
|
+
}, { once: true });
|
|
125
|
+
document.body.appendChild(frame);
|
|
126
|
+
const timer = setTimeout(() => settle.reject(new Error('the film did not load within ' + LOAD_TIMEOUT_MS / 1000 + ' s')), LOAD_TIMEOUT_MS);
|
|
127
|
+
return { own, ready: ready.finally(() => clearTimeout(timer)) };
|
|
128
|
+
}
|
|
129
|
+
function ready() {
|
|
130
|
+
const own = mounted;
|
|
131
|
+
if (own === null || own.loaded === null)
|
|
132
|
+
throw new Error('no film is loaded: call load(files) first, and wait for it');
|
|
133
|
+
return { own, loaded: own.loaded };
|
|
134
|
+
}
|
|
135
|
+
const within = (t, meta) => Math.max(0, Math.min(meta.duration, Number.isFinite(t) ? t : 0));
|
|
136
|
+
/** The snapshot drawn as an export draws it, read back as a PNG. */
|
|
137
|
+
function raster(reply, waitMs) {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
const picture = new Image();
|
|
140
|
+
picture.onload = () => {
|
|
141
|
+
holdFirstDraw(reply.svg, waitMs, () => {
|
|
142
|
+
drawSnapshot(picture, reply.pw, reply.ph)
|
|
143
|
+
.then((surface) => {
|
|
144
|
+
let canvas = surface;
|
|
145
|
+
if (!(canvas instanceof HTMLCanvasElement)) {
|
|
146
|
+
const copy = document.createElement('canvas');
|
|
147
|
+
copy.width = reply.pw;
|
|
148
|
+
copy.height = reply.ph;
|
|
149
|
+
copy.getContext('2d')?.drawImage(canvas, 0, 0);
|
|
150
|
+
canvas.close();
|
|
151
|
+
canvas = copy;
|
|
152
|
+
}
|
|
153
|
+
resolve(canvas.toDataURL('image/png'));
|
|
154
|
+
})
|
|
155
|
+
.catch((error) => reject(new Error('the snapshot could not be drawn: ' + messageOf(error))));
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
picture.onerror = () => reject(new Error('snapshot decode failed (foreignObject)' + whyNoDecode(reply.svg)));
|
|
159
|
+
picture.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(reply.svg);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
const tools = {
|
|
163
|
+
version: 1,
|
|
164
|
+
async load(files) {
|
|
165
|
+
unmount('another film was loaded');
|
|
166
|
+
log = [];
|
|
167
|
+
const runtime = document.getElementById(OFFLINE_RUNTIME_ID)?.textContent ?? null;
|
|
168
|
+
if (runtime === null)
|
|
169
|
+
return { error: { message: 'this page has no stage runtime' }, problems: [] };
|
|
170
|
+
const read = toolsFilm(files);
|
|
171
|
+
if (read.film === null)
|
|
172
|
+
return { error: { message: read.problems[0] ?? 'the film cannot be read' }, problems: read.problems.slice(1) };
|
|
173
|
+
const { own, ready: loading } = mount(read.film, runtime);
|
|
174
|
+
try {
|
|
175
|
+
const loaded = await loading;
|
|
176
|
+
own.loaded = loaded;
|
|
177
|
+
return { meta: loaded.meta, problems: read.problems };
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (mounted === own)
|
|
181
|
+
unmount('the film failed to load');
|
|
182
|
+
return { error: { message: messageOf(error) }, problems: read.problems };
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
seek(t) {
|
|
186
|
+
const { own, loaded } = ready();
|
|
187
|
+
const at = ++seq;
|
|
188
|
+
const shown = wait(own.shows, at, SHOW_TIMEOUT_MS, 'the stage did not show ' + t + ' s in time');
|
|
189
|
+
own.port?.send({ type: 'render', seq: at, t: within(t, loaded.meta), playing: false });
|
|
190
|
+
return shown;
|
|
191
|
+
},
|
|
192
|
+
async frame(t, options = {}) {
|
|
193
|
+
const { own, loaded } = ready();
|
|
194
|
+
const scale = options.scale ?? 1;
|
|
195
|
+
if (!(scale > 0 && scale <= 4))
|
|
196
|
+
throw new Error('scale must be above 0 and at most 4, not ' + String(options.scale));
|
|
197
|
+
const at = ++seq;
|
|
198
|
+
const reply = wait(own.pulls, at, FRAME_TIMEOUT_MS, 'the stage did not answer for the frame at ' + t + ' s in time');
|
|
199
|
+
const ask = {
|
|
200
|
+
type: 'frame',
|
|
201
|
+
seq: at,
|
|
202
|
+
t: within(t, loaded.meta),
|
|
203
|
+
scale,
|
|
204
|
+
smallMedia: false,
|
|
205
|
+
readFx: SNAPSHOT_SETTINGS.fx === 'read',
|
|
206
|
+
smooth: false,
|
|
207
|
+
};
|
|
208
|
+
own.port?.send(ask);
|
|
209
|
+
return raster(await reply, own.film.clippedTextWaitMs);
|
|
210
|
+
},
|
|
211
|
+
scoreReport() {
|
|
212
|
+
const loaded = mounted?.loaded ?? null;
|
|
213
|
+
return loaded === null ? null : scoreReport(loaded.score, loaded.narration);
|
|
214
|
+
},
|
|
215
|
+
async soundLevels() {
|
|
216
|
+
const loaded = mounted?.loaded ?? null;
|
|
217
|
+
return loaded === null ? null : sceneLevels(loaded.meta, loaded.score);
|
|
218
|
+
},
|
|
219
|
+
consoleLog() {
|
|
220
|
+
return log.slice();
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
Reflect.set(window, TOOLS_GLOBAL, tools);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { NarrationReport, PlayedScore } from '../protocol.js';
|
|
2
|
+
import type { ScoreReport } from './types.js';
|
|
3
|
+
/** The score and narration a load reported, counted: enough to tell a silent or broken score at a glance. */
|
|
4
|
+
export declare function scoreReport(score: PlayedScore | null, narration: NarrationReport | null): ScoreReport;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DRUM_NAMES, FX_NAMES, PATCH_NAMES, bandInstrument, isBandTrack, isQuietBandNote } from '@openartifactsai/motion-core';
|
|
2
|
+
const VOICES = { note: PATCH_NAMES, drum: DRUM_NAMES, fx: FX_NAMES };
|
|
3
|
+
const played = (t) => t;
|
|
4
|
+
/** Whether a note names no voice: a pitched band track plays its usual instrument for a name it does not know, the others stay quiet. */
|
|
5
|
+
function unvoiced(event) {
|
|
6
|
+
if (isBandTrack(event.track))
|
|
7
|
+
return bandInstrument(event.track, event.patch) === null && !isQuietBandNote(event.track, event.patch);
|
|
8
|
+
return !VOICES[event.kind].includes(event.patch);
|
|
9
|
+
}
|
|
10
|
+
/** The score and narration a load reported, counted: enough to tell a silent or broken score at a glance. */
|
|
11
|
+
export function scoreReport(score, narration) {
|
|
12
|
+
const events = score?.events ?? [];
|
|
13
|
+
const unknown = new Set();
|
|
14
|
+
const counts = { note: 0, drum: 0, fx: 0 };
|
|
15
|
+
let first = Infinity;
|
|
16
|
+
let last = -Infinity;
|
|
17
|
+
for (const event of events) {
|
|
18
|
+
counts[event.kind]++;
|
|
19
|
+
if (unvoiced(event))
|
|
20
|
+
unknown.add(event.kind + ':' + event.patch);
|
|
21
|
+
first = Math.min(first, event.t);
|
|
22
|
+
last = Math.max(last, event.t + event.dur);
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
events: events.length,
|
|
26
|
+
notes: counts.note,
|
|
27
|
+
drums: counts.drum,
|
|
28
|
+
fx: counts.fx,
|
|
29
|
+
unknown: [...unknown],
|
|
30
|
+
first: played(Number.isFinite(first) ? first : 0),
|
|
31
|
+
last: played(Number.isFinite(last) ? last : 0),
|
|
32
|
+
tempo: score?.tempo ?? null,
|
|
33
|
+
narration: narration === null ? null : { lines: narration.lines.length, seconds: narration.lines.reduce((sum, line) => sum + line.dur, 0) },
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { BandTrack, PlaySeconds, ScoreTempo } from '@openartifactsai/motion-core';
|
|
2
|
+
import type { ConsoleLevel, ErrorPhase, RenderMode, StageMeta } from '../protocol.js';
|
|
3
|
+
/** The global the tools page sets: `window.filmTools`. */
|
|
4
|
+
export declare const TOOLS_GLOBAL = "filmTools";
|
|
5
|
+
/**
|
|
6
|
+
* A video artifact's film as it lies on disk: the documents under `db/` and the uploads under `_blobs/`.
|
|
7
|
+
* `documents` is keyed by path under `db/` without `.json` (`film/meta`, `film/script`, `modules/scenes.json`,
|
|
8
|
+
* `assets/logo.png`); each value is the file's JSON text or the value it parses to. `blobs` is keyed by upload id
|
|
9
|
+
* (the 32 hex digits of `_blob/<id>`), each the file's bytes as base64.
|
|
10
|
+
*/
|
|
11
|
+
export interface ToolsFiles {
|
|
12
|
+
documents: Record<string, unknown>;
|
|
13
|
+
blobs?: Record<string, string>;
|
|
14
|
+
/** final (the default): every written scene draws as it is, as in an export. rough: rough scenes draw as sketches. */
|
|
15
|
+
mode?: RenderMode;
|
|
16
|
+
}
|
|
17
|
+
export interface ToolsLoaded {
|
|
18
|
+
meta: StageMeta;
|
|
19
|
+
/** What of the film the player drops or cannot have (unreadable documents, missing uploads, records not applied). */
|
|
20
|
+
problems: string[];
|
|
21
|
+
}
|
|
22
|
+
export interface ToolsFailed {
|
|
23
|
+
error: {
|
|
24
|
+
message: string;
|
|
25
|
+
};
|
|
26
|
+
problems: string[];
|
|
27
|
+
}
|
|
28
|
+
export type ToolsLoad = ToolsLoaded | ToolsFailed;
|
|
29
|
+
export interface ToolsFrameOptions {
|
|
30
|
+
/** Times the film's own size: 0.5 makes a 960 × 540 PNG of a 1080p film. 1 by default. */
|
|
31
|
+
scale?: number;
|
|
32
|
+
}
|
|
33
|
+
/** The stage's console and the film's errors since the last load, in order. */
|
|
34
|
+
export interface ToolsLogLine {
|
|
35
|
+
level: ConsoleLevel;
|
|
36
|
+
text: string;
|
|
37
|
+
/** Set on the film's own errors: whether loading or a render threw. */
|
|
38
|
+
phase?: ErrorPhase;
|
|
39
|
+
}
|
|
40
|
+
/** The score `kit.score` recorded, counted, and the narration's lines. */
|
|
41
|
+
export interface ScoreReport {
|
|
42
|
+
events: number;
|
|
43
|
+
notes: number;
|
|
44
|
+
drums: number;
|
|
45
|
+
fx: number;
|
|
46
|
+
/** `kind:patch` of events whose patch the player has no voice for: they play as silence. */
|
|
47
|
+
unknown: string[];
|
|
48
|
+
/** When the first sound starts and the last ends, played seconds. */
|
|
49
|
+
first: PlaySeconds;
|
|
50
|
+
last: PlaySeconds;
|
|
51
|
+
tempo: ScoreTempo | null;
|
|
52
|
+
/** null: the film has no narration (or this page reads none). */
|
|
53
|
+
narration: {
|
|
54
|
+
lines: number;
|
|
55
|
+
seconds: number;
|
|
56
|
+
} | null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* How loud the band is over one scene, in dB against the whole band's average over the film: `all` for every track
|
|
60
|
+
* together, and per track heard there its average and its loudest 0.4 s. A track under -45 dB is left out.
|
|
61
|
+
*/
|
|
62
|
+
export interface SceneLevels {
|
|
63
|
+
/** The scene's id (or the part's name in a film not made of scenes). */
|
|
64
|
+
scene: string;
|
|
65
|
+
/** Where it starts and how long it plays, played seconds. */
|
|
66
|
+
at: PlaySeconds;
|
|
67
|
+
dur: number;
|
|
68
|
+
all: number;
|
|
69
|
+
tracks: Partial<Record<BandTrack, readonly [mean: number, loudest: number]>>;
|
|
70
|
+
}
|
|
71
|
+
/** What the tools page sets as `window.filmTools`. Every call that waits returns a promise. */
|
|
72
|
+
export interface FilmTools {
|
|
73
|
+
version: 1;
|
|
74
|
+
load(files: ToolsFiles): Promise<ToolsLoad>;
|
|
75
|
+
/** Shows the film at `t` (played seconds) and resolves once it is on show. */
|
|
76
|
+
seek(t: number): Promise<void>;
|
|
77
|
+
/** The frame at `t` as an export draws it (the stage's snapshot, rastered), as a PNG data URL. */
|
|
78
|
+
frame(t: number, options?: ToolsFrameOptions): Promise<string>;
|
|
79
|
+
/** null before a load succeeds. */
|
|
80
|
+
scoreReport(): ScoreReport | null;
|
|
81
|
+
/** The band's levels scene by scene; null for a score with no band, before a load, or when they cannot be measured. */
|
|
82
|
+
soundLevels(): Promise<SceneLevels[] | null>;
|
|
83
|
+
consoleLog(): ToolsLogLine[];
|
|
84
|
+
}
|
package/src/tools.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { TOOLS_GLOBAL } from './tools/types.js';
|
|
2
|
+
export type { FilmTools, SceneLevels, ScoreReport, ToolsFailed, ToolsFiles, ToolsFrameOptions, ToolsLoad, ToolsLoaded, ToolsLogLine } from './tools/types.js';
|
|
3
|
+
export { toolsFilm } from './tools/film.js';
|
|
4
|
+
export type { ToolsFilm } from './tools/film.js';
|
|
5
|
+
export { scoreReport } from './tools/score.js';
|
|
6
|
+
export { sceneLevels } from './tools/levels.js';
|
package/src/tools.js
ADDED
package/src/virtual.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** The stage runtime's script text, from the `motionStageRuntime()` Vite plugin. */
|
|
2
|
+
declare module 'virtual:motion-stage-runtime' {
|
|
3
|
+
const runtime: string;
|
|
4
|
+
export default runtime;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** The standalone player's script text (it mounts the runtime the page keeps), from the `motionStageRuntime()` Vite plugin. */
|
|
8
|
+
declare module 'virtual:motion-stage-player' {
|
|
9
|
+
const player: string;
|
|
10
|
+
export default player;
|
|
11
|
+
}
|
package/src/vite.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
export declare const STAGE_RUNTIME_MODULE = "virtual:motion-stage-runtime";
|
|
3
|
+
export declare const STAGE_PLAYER_MODULE = "virtual:motion-stage-player";
|
|
4
|
+
/**
|
|
5
|
+
* Gives the page the stage runtime as a string, `import runtime from 'virtual:motion-stage-runtime'`, and the
|
|
6
|
+
* standalone player a saved page runs, `import player from 'virtual:motion-stage-player'`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function motionStageRuntime(): Plugin;
|
package/src/vite.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { buildOfflinePlayer, buildStageRuntimeFresh } from "./build.js";
|
|
2
|
+
export const STAGE_RUNTIME_MODULE = 'virtual:motion-stage-runtime';
|
|
3
|
+
export const STAGE_PLAYER_MODULE = 'virtual:motion-stage-player';
|
|
4
|
+
const RESOLVED = '\0' + STAGE_RUNTIME_MODULE;
|
|
5
|
+
const RESOLVED_PLAYER = '\0' + STAGE_PLAYER_MODULE;
|
|
6
|
+
/**
|
|
7
|
+
* Gives the page the stage runtime as a string, `import runtime from 'virtual:motion-stage-runtime'`, and the
|
|
8
|
+
* standalone player a saved page runs, `import player from 'virtual:motion-stage-player'`.
|
|
9
|
+
*/
|
|
10
|
+
export function motionStageRuntime() {
|
|
11
|
+
let watched = new Set();
|
|
12
|
+
return {
|
|
13
|
+
name: 'motion-stage-runtime',
|
|
14
|
+
resolveId(id) {
|
|
15
|
+
if (id === STAGE_RUNTIME_MODULE)
|
|
16
|
+
return RESOLVED;
|
|
17
|
+
if (id === STAGE_PLAYER_MODULE)
|
|
18
|
+
return RESOLVED_PLAYER;
|
|
19
|
+
return null;
|
|
20
|
+
},
|
|
21
|
+
async load(id) {
|
|
22
|
+
if (id === RESOLVED_PLAYER)
|
|
23
|
+
return 'export default ' + JSON.stringify(await buildOfflinePlayer()) + ';\n';
|
|
24
|
+
if (id !== RESOLVED)
|
|
25
|
+
return null;
|
|
26
|
+
const built = await buildStageRuntimeFresh();
|
|
27
|
+
watched = new Set(built.inputs);
|
|
28
|
+
for (const input of built.inputs)
|
|
29
|
+
this.addWatchFile(input);
|
|
30
|
+
return 'export default ' + JSON.stringify(built.script) + ';\n';
|
|
31
|
+
},
|
|
32
|
+
// A runtime source is not in the page's module graph: its change makes the virtual module stale by hand.
|
|
33
|
+
handleHotUpdate({ file, server }) {
|
|
34
|
+
if (!watched.has(file))
|
|
35
|
+
return;
|
|
36
|
+
const module = server.moduleGraph.getModuleById(RESOLVED);
|
|
37
|
+
if (!module)
|
|
38
|
+
return;
|
|
39
|
+
server.moduleGraph.invalidateModule(module);
|
|
40
|
+
return [module];
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|