@open-artifact/motion-stage 0.0.0-stage → 0.12.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 +69 -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 +155 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +208 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +97 -0
- package/src/protocol/layers.d.ts +123 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +261 -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/captions.d.ts +26 -0
- package/src/runtime/captions.js +103 -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 +40 -0
- package/src/runtime/film.js +29 -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 +123 -0
- package/src/runtime/kit/camera.d.ts +40 -0
- package/src/runtime/kit/camera.js +56 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -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 +22 -0
- package/src/runtime/kit/geometry.js +46 -0
- package/src/runtime/kit/index.d.ts +95 -0
- package/src/runtime/kit/index.js +149 -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/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +121 -0
- package/src/runtime/layers/item.js +149 -0
- package/src/runtime/layers/parts.d.ts +50 -0
- package/src/runtime/layers/parts.js +155 -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 +202 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +333 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +228 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +69 -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 +35 -0
- package/src/runtime/recut.js +158 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +342 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +156 -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/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 +47 -0
- package/src/runtime/scenes/install.js +387 -0
- package/src/runtime/scenes/read.d.ts +15 -0
- package/src/runtime/scenes/read.js +57 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +40 -0
- package/src/runtime/score.d.ts +26 -0
- package/src/runtime/score.js +221 -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/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +49 -0
- package/src/runtime/snapshot/serialize.js +271 -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 +210 -0
- package/src/runtime/video-sync.d.ts +26 -0
- package/src/runtime/video-sync.js +126 -0
- package/src/stills.d.ts +41 -0
- package/src/stills.js +253 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +216 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +29 -0
- package/src/tools/types.d.ts +69 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +5 -0
- package/src/tools.js +3 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** The global a standalone page's audio script sets, before the player's script runs. */
|
|
2
|
+
export const OFFLINE_AUDIO_GLOBAL = 'filmAudio';
|
|
3
|
+
export function injectedAudio() {
|
|
4
|
+
const factory = Reflect.get(window, OFFLINE_AUDIO_GLOBAL);
|
|
5
|
+
if (typeof factory !== 'function')
|
|
6
|
+
return null;
|
|
7
|
+
try {
|
|
8
|
+
const made = Reflect.apply(factory, window, []);
|
|
9
|
+
return isScorePlayer(made) ? made : null;
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
console.warn('the film’s sound could not be set up, so it plays silent: ' + String(error));
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function isScorePlayer(value) {
|
|
17
|
+
if (value === null || typeof value !== 'object')
|
|
18
|
+
return false;
|
|
19
|
+
return ['prepare', 'start', 'stop', 'now'].every((key) => typeof Reflect.get(value, key) === 'function');
|
|
20
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { StageFilm } from '../protocol.ts';
|
|
2
|
+
/** The page's film, written into a standalone page as JSON: bytes go as base64, since JSON has none. */
|
|
3
|
+
export interface OfflineAsset {
|
|
4
|
+
name: string;
|
|
5
|
+
mime: string;
|
|
6
|
+
w: number;
|
|
7
|
+
h: number;
|
|
8
|
+
base64?: string;
|
|
9
|
+
url?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface OfflineFilm extends Omit<StageFilm, 'assets'> {
|
|
12
|
+
assets: OfflineAsset[];
|
|
13
|
+
}
|
|
14
|
+
export interface OfflineData {
|
|
15
|
+
version: 1;
|
|
16
|
+
film: OfflineFilm;
|
|
17
|
+
}
|
|
18
|
+
/** Where a standalone page keeps its parts: the film as JSON, and the stage runtime as text. */
|
|
19
|
+
export declare const OFFLINE_DATA_ID = "film-data";
|
|
20
|
+
export declare const OFFLINE_RUNTIME_ID = "stage-runtime";
|
|
21
|
+
export declare function toBase64(bytes: ArrayBuffer): string;
|
|
22
|
+
export declare function fromBase64(text: string): ArrayBuffer;
|
|
23
|
+
/** The text for the page's `#film-data` element: JSON that cannot end its <script> early. */
|
|
24
|
+
export declare function offlineDataText(film: StageFilm): string;
|
|
25
|
+
/** The film a standalone page carries, checked as the stage checks a load; or why it cannot be read. */
|
|
26
|
+
export declare function readOfflineData(text: string): StageFilm | string;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { isHostToStage } from "../protocol.js";
|
|
2
|
+
/** Where a standalone page keeps its parts: the film as JSON, and the stage runtime as text. */
|
|
3
|
+
export const OFFLINE_DATA_ID = 'film-data';
|
|
4
|
+
export const OFFLINE_RUNTIME_ID = 'stage-runtime';
|
|
5
|
+
const CHUNK = 0x8000;
|
|
6
|
+
export function toBase64(bytes) {
|
|
7
|
+
const all = new Uint8Array(bytes);
|
|
8
|
+
let text = '';
|
|
9
|
+
for (let i = 0; i < all.length; i += CHUNK)
|
|
10
|
+
text += String.fromCharCode(...all.subarray(i, Math.min(i + CHUNK, all.length)));
|
|
11
|
+
return btoa(text);
|
|
12
|
+
}
|
|
13
|
+
export function fromBase64(text) {
|
|
14
|
+
const binary = atob(text);
|
|
15
|
+
const bytes = new Uint8Array(binary.length);
|
|
16
|
+
for (let i = 0; i < binary.length; i++)
|
|
17
|
+
bytes[i] = binary.charCodeAt(i);
|
|
18
|
+
return bytes.buffer;
|
|
19
|
+
}
|
|
20
|
+
/** The text for the page's `#film-data` element: JSON that cannot end its <script> early. */
|
|
21
|
+
export function offlineDataText(film) {
|
|
22
|
+
const assets = film.assets.map((asset) => {
|
|
23
|
+
const { bytes, ...rest } = asset;
|
|
24
|
+
return bytes ? { ...rest, base64: toBase64(bytes) } : rest;
|
|
25
|
+
});
|
|
26
|
+
const data = { version: 1, film: { ...film, assets } };
|
|
27
|
+
return JSON.stringify(data).replace(/<\//g, '<\\/').replace(/<!-{2}/g, '<\\u0021--');
|
|
28
|
+
}
|
|
29
|
+
const fields = (value) => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : null;
|
|
30
|
+
/** Its fields as given, with the bytes decoded: the stage's guard checks the rest. */
|
|
31
|
+
function assetOf(value) {
|
|
32
|
+
const given = fields(value);
|
|
33
|
+
if (!given)
|
|
34
|
+
return null;
|
|
35
|
+
const { base64, ...rest } = given;
|
|
36
|
+
if (base64 === undefined)
|
|
37
|
+
return rest;
|
|
38
|
+
if (typeof base64 !== 'string')
|
|
39
|
+
return null;
|
|
40
|
+
try {
|
|
41
|
+
return { ...rest, bytes: fromBase64(base64) };
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** The film a standalone page carries, checked as the stage checks a load; or why it cannot be read. */
|
|
48
|
+
export function readOfflineData(text) {
|
|
49
|
+
let parsed;
|
|
50
|
+
try {
|
|
51
|
+
parsed = JSON.parse(text);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return 'the film data is not JSON';
|
|
55
|
+
}
|
|
56
|
+
const data = fields(parsed);
|
|
57
|
+
if (!data || data['version'] !== 1)
|
|
58
|
+
return 'the film data is not version 1';
|
|
59
|
+
const film = fields(data['film']);
|
|
60
|
+
if (!film || !Array.isArray(film['assets']))
|
|
61
|
+
return 'the film data has no film';
|
|
62
|
+
const given = film['assets'];
|
|
63
|
+
const assets = given.map(assetOf);
|
|
64
|
+
if (assets.some((asset) => asset === null))
|
|
65
|
+
return 'an asset of the film data cannot be read';
|
|
66
|
+
// Every field but the bytes is checked by the stage's own guard.
|
|
67
|
+
const message = { type: 'load', film: { ...film, assets } };
|
|
68
|
+
if (!isHostToStage(message) || message.type !== 'load')
|
|
69
|
+
return 'the film data is not a film the stage can load';
|
|
70
|
+
return message.film;
|
|
71
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { StageFilm, StageMeta } from '../protocol.ts';
|
|
2
|
+
/** What a standalone page gives the scripts round it, as `window.film`. */
|
|
3
|
+
export interface OfflineFilmHandle {
|
|
4
|
+
data: StageFilm;
|
|
5
|
+
ready: Promise<StageMeta>;
|
|
6
|
+
seek(t: number): void;
|
|
7
|
+
play(): void;
|
|
8
|
+
pause(): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { injectedAudio } from "./audio.js";
|
|
2
|
+
import { OFFLINE_DATA_ID, OFFLINE_RUNTIME_ID, readOfflineData } from "./data.js";
|
|
3
|
+
import { mountPlayer } from "./player.js";
|
|
4
|
+
function fail(why) {
|
|
5
|
+
const note = document.createElement('p');
|
|
6
|
+
note.textContent = 'This film cannot play: ' + why + '.';
|
|
7
|
+
note.style.cssText = 'font:14px/1.4 system-ui,sans-serif;margin:24px;color:#b00020';
|
|
8
|
+
document.body.appendChild(note);
|
|
9
|
+
}
|
|
10
|
+
function start() {
|
|
11
|
+
const dataText = document.getElementById(OFFLINE_DATA_ID)?.textContent ?? null;
|
|
12
|
+
const runtime = document.getElementById(OFFLINE_RUNTIME_ID)?.textContent ?? null;
|
|
13
|
+
if (dataText === null || runtime === null) {
|
|
14
|
+
fail('the page has no film');
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const film = readOfflineData(dataText);
|
|
18
|
+
if (typeof film === 'string') {
|
|
19
|
+
fail(film);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const container = document.getElementById('film') ?? document.body;
|
|
23
|
+
const player = mountPlayer({ container, film: { ...film, mode: 'final' }, runtime, audio: injectedAudio() });
|
|
24
|
+
const handle = { data: film, ready: player.ready, seek: player.seek, play: player.play, pause: player.pause };
|
|
25
|
+
Reflect.set(window, 'film', handle);
|
|
26
|
+
// The frame keeps clicks on the picture to itself, so play has a button of its own beside it.
|
|
27
|
+
const button = document.createElement('button');
|
|
28
|
+
button.type = 'button';
|
|
29
|
+
button.style.cssText =
|
|
30
|
+
'position:fixed;left:16px;bottom:16px;z-index:1;font:600 14px/1 system-ui,sans-serif;padding:10px 16px;border:0;border-radius:999px;background:rgba(0,0,0,.6);color:#fff;cursor:pointer';
|
|
31
|
+
const label = () => {
|
|
32
|
+
button.textContent = player.playing ? 'Pause' : 'Play';
|
|
33
|
+
};
|
|
34
|
+
label();
|
|
35
|
+
button.addEventListener('click', () => {
|
|
36
|
+
player.toggle();
|
|
37
|
+
label();
|
|
38
|
+
});
|
|
39
|
+
player.onChange = label;
|
|
40
|
+
document.body.appendChild(button);
|
|
41
|
+
document.addEventListener('keydown', (event) => {
|
|
42
|
+
if (event.key !== ' ' || event.repeat || event.target === button)
|
|
43
|
+
return;
|
|
44
|
+
event.preventDefault();
|
|
45
|
+
player.toggle();
|
|
46
|
+
label();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (document.readyState === 'loading')
|
|
50
|
+
document.addEventListener('DOMContentLoaded', start, { once: true });
|
|
51
|
+
else
|
|
52
|
+
start();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PlaySeconds } from '@open-artifact/motion-core';
|
|
2
|
+
import type { StageFilm, StageMeta } from '../protocol.ts';
|
|
3
|
+
import type { ScorePlayer } from './audio.ts';
|
|
4
|
+
export interface PlayerOptions {
|
|
5
|
+
container: HTMLElement;
|
|
6
|
+
film: StageFilm;
|
|
7
|
+
/** The stage runtime's script text. */
|
|
8
|
+
runtime: string;
|
|
9
|
+
audio: ScorePlayer | null;
|
|
10
|
+
}
|
|
11
|
+
export interface OfflinePlayer {
|
|
12
|
+
ready: Promise<StageMeta>;
|
|
13
|
+
seek(t: number): void;
|
|
14
|
+
play(): void;
|
|
15
|
+
pause(): void;
|
|
16
|
+
toggle(): void;
|
|
17
|
+
readonly playing: boolean;
|
|
18
|
+
readonly time: PlaySeconds;
|
|
19
|
+
/** Told when playing starts or stops, by a call or at the end. */
|
|
20
|
+
onChange: (() => void) | null;
|
|
21
|
+
}
|
|
22
|
+
/** A film on its own page: the stage in a sandboxed frame, a clock, and the sound it is given. */
|
|
23
|
+
export declare function mountPlayer(options: PlayerOptions): OfflinePlayer;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { STAGE_SANDBOX, openStagePort, stageDocument } from "../host.js";
|
|
2
|
+
import { STAGE_PROTOCOL_VERSION } from "../protocol.js";
|
|
3
|
+
const played = (t) => t;
|
|
4
|
+
/** A film on its own page: the stage in a sandboxed frame, a clock, and the sound it is given. */
|
|
5
|
+
export function mountPlayer(options) {
|
|
6
|
+
const { container, film, audio } = options;
|
|
7
|
+
const frame = document.createElement('iframe');
|
|
8
|
+
frame.setAttribute('sandbox', STAGE_SANDBOX);
|
|
9
|
+
frame.title = 'Film';
|
|
10
|
+
frame.style.cssText = 'display:block;width:100%;height:100%;border:0;background:transparent';
|
|
11
|
+
frame.srcdoc = stageDocument(options.runtime);
|
|
12
|
+
let port = null;
|
|
13
|
+
let meta = null;
|
|
14
|
+
let seq = 0;
|
|
15
|
+
let at = 0;
|
|
16
|
+
let playing = false;
|
|
17
|
+
let wantsPlay = false;
|
|
18
|
+
/** While playing without sound: the real moment and played time it started from. */
|
|
19
|
+
let started = { real: 0, t: 0 };
|
|
20
|
+
let ticking = 0;
|
|
21
|
+
let settle = { resolve: () => undefined, reject: () => undefined };
|
|
22
|
+
const ready = new Promise((resolve, reject) => {
|
|
23
|
+
settle = { resolve, reject };
|
|
24
|
+
});
|
|
25
|
+
// A failed load is told through the console as well; an unheard rejection would only add noise.
|
|
26
|
+
ready.catch(() => undefined);
|
|
27
|
+
const duration = () => meta?.duration ?? 0;
|
|
28
|
+
const show = () => port?.send({ type: 'render', seq: ++seq, t: played(at), playing });
|
|
29
|
+
const tick = () => {
|
|
30
|
+
ticking = 0;
|
|
31
|
+
if (!playing)
|
|
32
|
+
return;
|
|
33
|
+
const heard = audio?.now() ?? null;
|
|
34
|
+
at = heard ?? started.t + (performance.now() - started.real) / 1000;
|
|
35
|
+
if (at >= duration()) {
|
|
36
|
+
at = duration();
|
|
37
|
+
stop();
|
|
38
|
+
show();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
show();
|
|
42
|
+
ticking = requestAnimationFrame(tick);
|
|
43
|
+
};
|
|
44
|
+
const begin = () => {
|
|
45
|
+
if (!meta) {
|
|
46
|
+
wantsPlay = true;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (at >= duration())
|
|
50
|
+
at = 0;
|
|
51
|
+
playing = true;
|
|
52
|
+
started = { real: performance.now(), t: at };
|
|
53
|
+
try {
|
|
54
|
+
audio?.start(played(at));
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.warn('the film’s sound did not start: ' + String(error));
|
|
58
|
+
}
|
|
59
|
+
if (!ticking)
|
|
60
|
+
ticking = requestAnimationFrame(tick);
|
|
61
|
+
handle.onChange?.();
|
|
62
|
+
};
|
|
63
|
+
function stop() {
|
|
64
|
+
wantsPlay = false;
|
|
65
|
+
if (!playing)
|
|
66
|
+
return;
|
|
67
|
+
playing = false;
|
|
68
|
+
audio?.stop();
|
|
69
|
+
if (ticking)
|
|
70
|
+
cancelAnimationFrame(ticking);
|
|
71
|
+
ticking = 0;
|
|
72
|
+
handle.onChange?.();
|
|
73
|
+
}
|
|
74
|
+
const hear = (message) => {
|
|
75
|
+
switch (message.type) {
|
|
76
|
+
case 'hello':
|
|
77
|
+
if (message.version !== STAGE_PROTOCOL_VERSION)
|
|
78
|
+
settle.reject(new Error('the stage speaks version ' + message.version + ', not ' + STAGE_PROTOCOL_VERSION));
|
|
79
|
+
return;
|
|
80
|
+
case 'ready': {
|
|
81
|
+
meta = message.meta;
|
|
82
|
+
const loaded = message.meta;
|
|
83
|
+
void (audio?.prepare(message.score, message.narration, loaded.duration) ?? Promise.resolve()).then(() => undefined, (error) => console.warn('the film’s sound could not be prepared, so it plays silent: ' + String(error)));
|
|
84
|
+
settle.resolve(loaded);
|
|
85
|
+
show();
|
|
86
|
+
if (wantsPlay)
|
|
87
|
+
begin();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
case 'console':
|
|
91
|
+
console[message.level](message.text);
|
|
92
|
+
return;
|
|
93
|
+
case 'error':
|
|
94
|
+
console.error(message.message + (message.stack ? '\n' + message.stack : ''));
|
|
95
|
+
if (message.phase === 'load' && !meta)
|
|
96
|
+
settle.reject(new Error(message.message));
|
|
97
|
+
return;
|
|
98
|
+
case 'link':
|
|
99
|
+
window.open(message.href, '_blank', 'noopener,noreferrer');
|
|
100
|
+
return;
|
|
101
|
+
case 'navigating':
|
|
102
|
+
stop();
|
|
103
|
+
return;
|
|
104
|
+
case 'presented':
|
|
105
|
+
case 'frame':
|
|
106
|
+
case 'frame-error':
|
|
107
|
+
case 'texts':
|
|
108
|
+
case 'layers':
|
|
109
|
+
case 'looked':
|
|
110
|
+
return;
|
|
111
|
+
default: {
|
|
112
|
+
const never = message;
|
|
113
|
+
void never;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
frame.addEventListener('load', () => {
|
|
118
|
+
port = openStagePort(frame, hear);
|
|
119
|
+
port?.send({ type: 'load', film });
|
|
120
|
+
}, { once: true });
|
|
121
|
+
container.appendChild(frame);
|
|
122
|
+
const handle = {
|
|
123
|
+
ready,
|
|
124
|
+
seek(t) {
|
|
125
|
+
at = Math.max(0, Math.min(duration(), Number(t) || 0));
|
|
126
|
+
if (playing) {
|
|
127
|
+
audio?.stop();
|
|
128
|
+
started = { real: performance.now(), t: at };
|
|
129
|
+
audio?.start(played(at));
|
|
130
|
+
}
|
|
131
|
+
show();
|
|
132
|
+
},
|
|
133
|
+
play: begin,
|
|
134
|
+
pause() {
|
|
135
|
+
stop();
|
|
136
|
+
show();
|
|
137
|
+
},
|
|
138
|
+
toggle() {
|
|
139
|
+
if (playing) {
|
|
140
|
+
stop();
|
|
141
|
+
show();
|
|
142
|
+
}
|
|
143
|
+
else
|
|
144
|
+
begin();
|
|
145
|
+
},
|
|
146
|
+
get playing() {
|
|
147
|
+
return playing;
|
|
148
|
+
},
|
|
149
|
+
get time() {
|
|
150
|
+
return played(at);
|
|
151
|
+
},
|
|
152
|
+
onChange: null,
|
|
153
|
+
};
|
|
154
|
+
return handle;
|
|
155
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HostToStage, StageConnect, StageToHost } from './types.ts';
|
|
2
|
+
export declare function isStageConnect(value: unknown): value is StageConnect;
|
|
3
|
+
/** Checked in the frame before any message is acted on. */
|
|
4
|
+
export declare function isHostToStage(value: unknown): value is HostToStage;
|
|
5
|
+
/** Checked by the host: everything from the frame is the film's to forge. */
|
|
6
|
+
export declare function isStageToHost(value: unknown): value is StageToHost;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { isAskedLook, isLayersReport, isRetouchedRecord } from "./layer-guards.js";
|
|
2
|
+
import { STAGE_CONNECT } from "./types.js";
|
|
3
|
+
const isFields = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
4
|
+
const isNumber = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
5
|
+
const isString = (value) => typeof value === 'string';
|
|
6
|
+
const isBoolean = (value) => typeof value === 'boolean';
|
|
7
|
+
const isOptional = (value, is) => value === undefined || is(value);
|
|
8
|
+
const isNullable = (value, is) => value === null || is(value);
|
|
9
|
+
const isListOf = (value, is) => Array.isArray(value) && value.every(is);
|
|
10
|
+
const isNumberPair = (value) => Array.isArray(value) && value.length === 2 && value.every(isNumber);
|
|
11
|
+
const MODULE_KINDS = ['js', 'css', 'html', 'json', 'text'];
|
|
12
|
+
function isModule(value) {
|
|
13
|
+
return isFields(value) && isString(value['name']) && isString(value['kind']) && MODULE_KINDS.includes(value['kind']) && isString(value['text']);
|
|
14
|
+
}
|
|
15
|
+
function isAsset(value) {
|
|
16
|
+
return (isFields(value) &&
|
|
17
|
+
isString(value['name']) &&
|
|
18
|
+
isString(value['mime']) &&
|
|
19
|
+
isNumber(value['w']) &&
|
|
20
|
+
isNumber(value['h']) &&
|
|
21
|
+
(value['bytes'] instanceof ArrayBuffer || isString(value['url'])));
|
|
22
|
+
}
|
|
23
|
+
function isFace(value) {
|
|
24
|
+
return (isFields(value) &&
|
|
25
|
+
isString(value['family']) &&
|
|
26
|
+
isString(value['url']) &&
|
|
27
|
+
isString(value['weight']) &&
|
|
28
|
+
isString(value['style']) &&
|
|
29
|
+
isOptional(value['range'], isString) &&
|
|
30
|
+
isOptional(value['box'], isNumberPair) &&
|
|
31
|
+
isOptional(value['sample'], isString));
|
|
32
|
+
}
|
|
33
|
+
function isFaces(value) {
|
|
34
|
+
if (!isFields(value) || !isListOf(value['fonts'], isFace))
|
|
35
|
+
return false;
|
|
36
|
+
const slots = value['slots'];
|
|
37
|
+
return isFields(slots) && ['sans', 'serif', 'mono', 'display'].every((stack) => isOptional(slots[stack], isString));
|
|
38
|
+
}
|
|
39
|
+
function isLimits(value) {
|
|
40
|
+
return isFields(value) && isNumber(value['maxLines']) && isNumber(value['maxLineSeconds']);
|
|
41
|
+
}
|
|
42
|
+
const isMode = (value) => value === 'rough' || value === 'final';
|
|
43
|
+
function isFilm(value) {
|
|
44
|
+
return (isFields(value) &&
|
|
45
|
+
isString(value['script']) &&
|
|
46
|
+
isListOf(value['modules'], isModule) &&
|
|
47
|
+
isListOf(value['assets'], isAsset) &&
|
|
48
|
+
isNullable(value['faces'], isFaces) &&
|
|
49
|
+
isNullable(value['narration'], isLimits) &&
|
|
50
|
+
isNumber(value['clippedTextWaitMs']) &&
|
|
51
|
+
isMode(value['mode']));
|
|
52
|
+
}
|
|
53
|
+
export function isStageConnect(value) {
|
|
54
|
+
return isFields(value) && value['type'] === STAGE_CONNECT && isNumber(value['version']);
|
|
55
|
+
}
|
|
56
|
+
/** Checked in the frame before any message is acted on. */
|
|
57
|
+
export function isHostToStage(value) {
|
|
58
|
+
if (!isFields(value) || !isString(value['type']))
|
|
59
|
+
return false;
|
|
60
|
+
const message = value;
|
|
61
|
+
switch (message.type) {
|
|
62
|
+
case 'load':
|
|
63
|
+
return isFilm(message['film']);
|
|
64
|
+
case 'render':
|
|
65
|
+
return isNumber(message['seq']) && isNumber(message['t']) && isBoolean(message['playing']);
|
|
66
|
+
case 'mode':
|
|
67
|
+
return isMode(message['mode']);
|
|
68
|
+
case 'frame':
|
|
69
|
+
return (isNumber(message['seq']) &&
|
|
70
|
+
isNumber(message['t']) &&
|
|
71
|
+
isNumber(message['scale']) &&
|
|
72
|
+
isBoolean(message['smallMedia']) &&
|
|
73
|
+
isBoolean(message['readFx']) &&
|
|
74
|
+
isBoolean(message['smooth']));
|
|
75
|
+
case 'watch':
|
|
76
|
+
return isBoolean(message['on']) && isBoolean(message['places']);
|
|
77
|
+
case 'scan':
|
|
78
|
+
return true;
|
|
79
|
+
case 'layers':
|
|
80
|
+
return (isNumber(message['seq']) &&
|
|
81
|
+
isNumber(message['from']) &&
|
|
82
|
+
isNumber(message['to']) &&
|
|
83
|
+
isNumber(message['at']) &&
|
|
84
|
+
isNullable(message['scene'], isString) &&
|
|
85
|
+
isBoolean(message['stop']));
|
|
86
|
+
case 'retouch':
|
|
87
|
+
return isNullable(message['record'], isRetouchedRecord);
|
|
88
|
+
case 'look':
|
|
89
|
+
return isNumber(message['seq']) && isString(message['id']) && isNullable(message['t'], isNumber);
|
|
90
|
+
default: {
|
|
91
|
+
const never = message.type;
|
|
92
|
+
void never;
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const isChapter = (value) => isFields(value) && isNumber(value['t']) && isString(value['label']);
|
|
98
|
+
function isPart(value) {
|
|
99
|
+
return (isFields(value) &&
|
|
100
|
+
isNullable(value['key'], isString) &&
|
|
101
|
+
isString(value['label']) &&
|
|
102
|
+
isString(value['own']) &&
|
|
103
|
+
isNumber(value['a0']) &&
|
|
104
|
+
isNumber(value['nat']) &&
|
|
105
|
+
isNumber(value['p0']) &&
|
|
106
|
+
isNumber(value['dur']));
|
|
107
|
+
}
|
|
108
|
+
const SCENE_STATES = ['todo', 'failed', 'rough', 'final'];
|
|
109
|
+
const isSceneReport = (value) => isFields(value) && isString(value['id']) && isString(value['state']) && SCENE_STATES.includes(value['state']);
|
|
110
|
+
function isMeta(value) {
|
|
111
|
+
const background = isFields(value) ? value['background'] : null;
|
|
112
|
+
return (isFields(value) &&
|
|
113
|
+
isNumber(value['width']) &&
|
|
114
|
+
isNumber(value['height']) &&
|
|
115
|
+
(value['fps'] === 24 || value['fps'] === 30 || value['fps'] === 60) &&
|
|
116
|
+
isNumber(value['duration']) &&
|
|
117
|
+
isNumber(value['filmDuration']) &&
|
|
118
|
+
isListOf(value['chapters'], isChapter) &&
|
|
119
|
+
isNullable(value['scenes'], (inner) => isListOf(inner, isSceneReport)) &&
|
|
120
|
+
Array.isArray(background) &&
|
|
121
|
+
background.length === 2 &&
|
|
122
|
+
background.every(isString) &&
|
|
123
|
+
isListOf(value['parts'], isPart));
|
|
124
|
+
}
|
|
125
|
+
const SCORE_KINDS = ['note', 'drum', 'fx'];
|
|
126
|
+
function isScoreEvent(value) {
|
|
127
|
+
return (isFields(value) &&
|
|
128
|
+
isNumber(value['t']) &&
|
|
129
|
+
isNumber(value['dur']) &&
|
|
130
|
+
isString(value['kind']) &&
|
|
131
|
+
SCORE_KINDS.includes(value['kind']) &&
|
|
132
|
+
isString(value['patch']) &&
|
|
133
|
+
isNumber(value['midi']) &&
|
|
134
|
+
isNumber(value['vel']) &&
|
|
135
|
+
(value['params'] === undefined || (isFields(value['params']) && Object.values(value['params']).every(isNumber))));
|
|
136
|
+
}
|
|
137
|
+
function isScore(value) {
|
|
138
|
+
if (!isFields(value) || !Array.isArray(value['events']) || !value['events'].every(isScoreEvent))
|
|
139
|
+
return false;
|
|
140
|
+
const master = value['master'];
|
|
141
|
+
const tempo = value['tempo'];
|
|
142
|
+
return (isFields(master) &&
|
|
143
|
+
isNumber(master['gain']) &&
|
|
144
|
+
isNumber(master['fadeOut']) &&
|
|
145
|
+
(master['loudness'] === undefined || isNullable(master['loudness'], isNumber)) &&
|
|
146
|
+
(tempo === undefined || (isFields(tempo) && isNumber(tempo['bpm']) && isNumber(tempo['t0']))));
|
|
147
|
+
}
|
|
148
|
+
function isNarration(value) {
|
|
149
|
+
return (isFields(value) &&
|
|
150
|
+
isNullable(value['duck'], isNumber) &&
|
|
151
|
+
isListOf(value['lines'], (line) => isFields(line) && isNumber(line['at']) && isNumber(line['dur']) && isString(line['text']) && isString(line['audio'])));
|
|
152
|
+
}
|
|
153
|
+
function isStageText(value) {
|
|
154
|
+
return (isFields(value) &&
|
|
155
|
+
isString(value['text']) &&
|
|
156
|
+
['x', 'y', 'w', 'h', 'line'].every((key) => isOptional(value[key], isNumber)) &&
|
|
157
|
+
isOptional(value['id'], isString) &&
|
|
158
|
+
isOptional(value['seen'], isBoolean));
|
|
159
|
+
}
|
|
160
|
+
const CONSOLE_LEVELS = ['log', 'info', 'warn', 'error'];
|
|
161
|
+
/** Checked by the host: everything from the frame is the film's to forge. */
|
|
162
|
+
export function isStageToHost(value) {
|
|
163
|
+
if (!isFields(value) || !isString(value['type']))
|
|
164
|
+
return false;
|
|
165
|
+
const message = value;
|
|
166
|
+
switch (message.type) {
|
|
167
|
+
case 'hello':
|
|
168
|
+
return isNumber(message['version']);
|
|
169
|
+
case 'ready':
|
|
170
|
+
return (isMeta(message['meta']) &&
|
|
171
|
+
isNullable(message['score'], isScore) &&
|
|
172
|
+
isNullable(message['narration'], isNarration) &&
|
|
173
|
+
isNumber(message['loadMs']));
|
|
174
|
+
case 'presented':
|
|
175
|
+
return isNumber(message['seq']) && isNumber(message['t']) && isBoolean(message['ok']) && isNumber(message['ms']);
|
|
176
|
+
case 'frame':
|
|
177
|
+
return (isNumber(message['seq']) &&
|
|
178
|
+
isNumber(message['t']) &&
|
|
179
|
+
isString(message['svg']) &&
|
|
180
|
+
isNumber(message['pw']) &&
|
|
181
|
+
isNumber(message['ph']) &&
|
|
182
|
+
isNumber(message['ms']));
|
|
183
|
+
case 'frame-error':
|
|
184
|
+
return isNumber(message['seq']) && isBoolean(message['hold']) && isBoolean(message['env']) && isString(message['message']);
|
|
185
|
+
case 'console':
|
|
186
|
+
return isString(message['level']) && CONSOLE_LEVELS.includes(message['level']) && isString(message['text']);
|
|
187
|
+
case 'error':
|
|
188
|
+
return (message['phase'] === 'load' || message['phase'] === 'render') && isString(message['message']) && isString(message['stack']);
|
|
189
|
+
case 'navigating':
|
|
190
|
+
return true;
|
|
191
|
+
case 'texts':
|
|
192
|
+
return isListOf(message['nodes'], isStageText);
|
|
193
|
+
case 'layers':
|
|
194
|
+
return isLayersReport(message['report']);
|
|
195
|
+
case 'looked':
|
|
196
|
+
return (isNumber(message['seq']) &&
|
|
197
|
+
isString(message['id']) &&
|
|
198
|
+
isNullable(message['look'], isAskedLook) &&
|
|
199
|
+
isListOf(message['lost'], isString));
|
|
200
|
+
case 'link':
|
|
201
|
+
return isString(message['href']);
|
|
202
|
+
default: {
|
|
203
|
+
const never = message.type;
|
|
204
|
+
void never;
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RetouchedRecord } from '@open-artifact/motion-core';
|
|
2
|
+
import type { AskedLook, LayerRow, LayersReport } from './layers.ts';
|
|
3
|
+
export declare function isLayerRow(value: unknown): value is LayerRow;
|
|
4
|
+
export declare function isLayersReport(value: unknown): value is LayersReport;
|
|
5
|
+
export declare function isAskedLook(value: unknown): value is AskedLook;
|
|
6
|
+
/** A record as `parseRetouchedRecord` gives it: each entry one the player would keep. The frame parses it again. */
|
|
7
|
+
export declare function isRetouchedRecord(value: unknown): value is RetouchedRecord;
|