autokast 0.0.0 → 0.1.1
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 +29 -0
- package/README.md +165 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +300 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/inspect.d.ts +26 -0
- package/dist/inspect.js +193 -0
- package/dist/record/animations.d.ts +7 -0
- package/dist/record/animations.js +34 -0
- package/dist/record/capture.d.ts +85 -0
- package/dist/record/capture.js +202 -0
- package/dist/record/cast.d.ts +142 -0
- package/dist/record/cast.js +680 -0
- package/dist/record/cursor-style.d.ts +33 -0
- package/dist/record/cursor-style.js +252 -0
- package/dist/record/defaults.d.ts +96 -0
- package/dist/record/defaults.js +125 -0
- package/dist/record/editor.d.ts +11 -0
- package/dist/record/editor.js +48 -0
- package/dist/record/frame-pump.d.ts +91 -0
- package/dist/record/frame-pump.js +306 -0
- package/dist/record/index.d.ts +11 -0
- package/dist/record/index.js +287 -0
- package/dist/record/motion.d.ts +48 -0
- package/dist/record/motion.js +112 -0
- package/dist/record/pace.d.ts +69 -0
- package/dist/record/pace.js +133 -0
- package/dist/record/random.d.ts +12 -0
- package/dist/record/random.js +24 -0
- package/dist/record/scroll.d.ts +71 -0
- package/dist/record/scroll.js +249 -0
- package/dist/record/timeline.d.ts +34 -0
- package/dist/record/timeline.js +44 -0
- package/dist/render/background.d.ts +12 -0
- package/dist/render/background.js +209 -0
- package/dist/render/browser.d.ts +46 -0
- package/dist/render/browser.js +223 -0
- package/dist/render/camera.d.ts +119 -0
- package/dist/render/camera.js +372 -0
- package/dist/render/captions.d.ts +114 -0
- package/dist/render/captions.js +254 -0
- package/dist/render/color.d.ts +23 -0
- package/dist/render/color.js +78 -0
- package/dist/render/compositor.d.ts +185 -0
- package/dist/render/compositor.js +656 -0
- package/dist/render/cursor.d.ts +85 -0
- package/dist/render/cursor.js +314 -0
- package/dist/render/cursors/shapes.d.ts +34 -0
- package/dist/render/cursors/shapes.js +268 -0
- package/dist/render/defaults.d.ts +91 -0
- package/dist/render/defaults.js +139 -0
- package/dist/render/easing.d.ts +13 -0
- package/dist/render/easing.js +40 -0
- package/dist/render/effects.d.ts +39 -0
- package/dist/render/effects.js +91 -0
- package/dist/render/encoder.d.ts +52 -0
- package/dist/render/encoder.js +226 -0
- package/dist/render/index.d.ts +19 -0
- package/dist/render/index.js +138 -0
- package/dist/render/keycaps.d.ts +67 -0
- package/dist/render/keycaps.js +270 -0
- package/dist/render/scene.d.ts +59 -0
- package/dist/render/scene.js +230 -0
- package/dist/render/source.d.ts +21 -0
- package/dist/render/source.js +57 -0
- package/dist/render/spring.d.ts +11 -0
- package/dist/render/spring.js +47 -0
- package/dist/render/stage.d.ts +59 -0
- package/dist/render/stage.js +91 -0
- package/dist/render/timeline.d.ts +70 -0
- package/dist/render/timeline.js +157 -0
- package/dist/session.d.ts +14 -0
- package/dist/session.js +33 -0
- package/dist/types.d.ts +708 -0
- package/dist/types.js +1 -0
- package/package.json +61 -3
- package/skills/autokast/SKILL.md +300 -0
- package/skills/autokast/reference/jupyterlab.md +100 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { BACKGROUND_PRESETS } from "./background.js";
|
|
2
|
+
import { autoZoomEvents, clampCamera, computeCamera, } from "./camera.js";
|
|
3
|
+
import { addressAt } from "./browser.js";
|
|
4
|
+
import { bandAt, bandTrack, CAPTION_WIDTH, captionMeasure, planCaptions, splitCaption, } from "./captions.js";
|
|
5
|
+
import { computeLayout } from "./compositor.js";
|
|
6
|
+
import { clickMarks, computeCursor } from "./cursor.js";
|
|
7
|
+
import { resolveOptions } from "./defaults.js";
|
|
8
|
+
import { spotlightAmount } from "./effects.js";
|
|
9
|
+
import { keyCueVisible, planKeys } from "./keycaps.js";
|
|
10
|
+
import { bandLayout, fillEdges, stageAt } from "./stage.js";
|
|
11
|
+
import { frameIndexAt, frameTimes, sessionTimeMap, timedEvents, } from "./timeline.js";
|
|
12
|
+
/**
|
|
13
|
+
* Arrow height of a size-1 cursor in CSS pixels: the follow camera keeps this much room.
|
|
14
|
+
*/
|
|
15
|
+
const CURSOR_EXTENT = 20;
|
|
16
|
+
/**
|
|
17
|
+
* Builds the scene. `extraTime` adds one output time to the frame grid (for a still
|
|
18
|
+
* between two frames).
|
|
19
|
+
*/
|
|
20
|
+
export function buildScene(session, options, extraTime) {
|
|
21
|
+
const map = sessionTimeMap(session);
|
|
22
|
+
const resolved = resolveOptions(session, options, BACKGROUND_PRESETS, map.duration);
|
|
23
|
+
let times = frameTimes(map.duration, resolved.fps);
|
|
24
|
+
if (extraTime !== undefined && !times.includes(extraTime)) {
|
|
25
|
+
times = Float64Array.from([...times, extraTime].sort((a, b) => a - b));
|
|
26
|
+
}
|
|
27
|
+
const events = timedEvents(session.events, map);
|
|
28
|
+
const cursor = computeCursor(times, session.cursor, session.events, map, resolved.cursor);
|
|
29
|
+
let zooms = events.filter((e) => e.type === 'zoom');
|
|
30
|
+
if (resolved.zoom.auto && zooms.length === 0) {
|
|
31
|
+
const at = (out) => {
|
|
32
|
+
const i = Math.min(times.length - 1, Math.max(0, Math.round((out * resolved.fps) / 1000)));
|
|
33
|
+
return { x: cursor.x[i], y: cursor.y[i] };
|
|
34
|
+
};
|
|
35
|
+
zooms = autoZoomEvents(events, at, session.viewport, resolved.zoom.auto, resolved.zoom.duration);
|
|
36
|
+
}
|
|
37
|
+
const layout = computeLayout(resolved, session.viewport);
|
|
38
|
+
const captions = events.filter((e) => e.type === 'caption' && e.text.trim() !== '');
|
|
39
|
+
const band = captions.length ? bandLayout(layout, resolved.captions.position) : null;
|
|
40
|
+
const measure = captionMeasure(resolved.captions.fontFamily);
|
|
41
|
+
const { cues, groups } = planCaptions(captions, (text) => splitCaption(text, measure, (band ?? layout).window.width * CAPTION_WIDTH, resolved.captions.fontSize * layout.unit));
|
|
42
|
+
const camera = computeCamera(times, zooms, { ...cursor, extent: CURSOR_EXTENT * resolved.cursor.size }, session.viewport, {
|
|
43
|
+
...resolved.zoom,
|
|
44
|
+
fit: (t) => fitRegion(resolved, layout, band, bandAt(groups, t)),
|
|
45
|
+
constrain: (state, t) => {
|
|
46
|
+
const clamped = clampCamera(state, session.viewport);
|
|
47
|
+
if (resolved.zoom.mode === 'crop' || resolved.zoom.edges === 'keep')
|
|
48
|
+
return clamped;
|
|
49
|
+
return fillEdges(layout, band, band ? bandAt(groups, t) : 0, clamped);
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
const frameIndex = new Int32Array(times.length);
|
|
53
|
+
for (let i = 0; i < times.length; i++) {
|
|
54
|
+
frameIndex[i] = frameIndexAt(session.frames, map.toRecord(times[i]));
|
|
55
|
+
}
|
|
56
|
+
const keys = resolved.keystrokes.show === 'none'
|
|
57
|
+
? []
|
|
58
|
+
: planKeys(events.filter((e) => e.type === 'key'), resolved.keystrokes.labels);
|
|
59
|
+
return {
|
|
60
|
+
session,
|
|
61
|
+
options: resolved,
|
|
62
|
+
map,
|
|
63
|
+
layout,
|
|
64
|
+
times,
|
|
65
|
+
camera,
|
|
66
|
+
cursor,
|
|
67
|
+
clicks: resolved.cursor.click.type === 'none' ? [] : clickMarks(events),
|
|
68
|
+
spotlights: events.filter((e) => e.type === 'spotlight'),
|
|
69
|
+
captions: cues,
|
|
70
|
+
keys,
|
|
71
|
+
urls: events.filter((e) => e.type === 'url'),
|
|
72
|
+
band,
|
|
73
|
+
bandOpen: bandTrack(times, groups),
|
|
74
|
+
frameIndex,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Region a fit zoom fills, relative to the rest frame: the frame box in crop mode; the
|
|
79
|
+
* whole output minus the open part of the caption band in camera mode.
|
|
80
|
+
*/
|
|
81
|
+
function fitRegion(options, layout, band, open) {
|
|
82
|
+
if (options.zoom.mode === 'crop')
|
|
83
|
+
return { x: 1, y: 1 };
|
|
84
|
+
const { width: W, height: H } = options.size;
|
|
85
|
+
const scale = stageAt(band, open).scale;
|
|
86
|
+
let height = H;
|
|
87
|
+
if (band && open > 0) {
|
|
88
|
+
const edge = band.side === 'bottom' ? H - (band.window.y + band.window.height) : band.window.y;
|
|
89
|
+
height -= edge * open;
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
x: W / (scale * layout.content.width),
|
|
93
|
+
y: height / (scale * layout.content.height),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const CAMERA_SAMPLES = 20;
|
|
97
|
+
const CURSOR_SAMPLES = 96;
|
|
98
|
+
/**
|
|
99
|
+
* Value of a per-frame track `delta` frames away from frame i (|delta| <= 1).
|
|
100
|
+
*/
|
|
101
|
+
function at(track, i, delta) {
|
|
102
|
+
const j = delta < 0 ? Math.max(0, i - 1) : Math.min(track.length - 1, i + 1);
|
|
103
|
+
return track[i] + (track[j] - track[i]) * Math.abs(delta);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Cursor coordinate `delta` frames from frame i (|delta| <= 1) on a Catmull-Rom curve
|
|
107
|
+
* through the frame positions, so sub-frame samples follow the real, curved path.
|
|
108
|
+
*/
|
|
109
|
+
function cursorAt(track, i, delta) {
|
|
110
|
+
const k = delta < 0 ? i - 1 : i;
|
|
111
|
+
const u = delta < 0 ? 1 + delta : delta;
|
|
112
|
+
const p = (j) => track[Math.min(track.length - 1, Math.max(0, j))];
|
|
113
|
+
const [p0, p1, p2, p3] = [p(k - 1), p(k), p(k + 1), p(k + 2)];
|
|
114
|
+
return (p1 + 0.5 * u * (p2 - p0 + u * (2 * p0 - 5 * p1 + 4 * p2 - p3 + u * (3 * (p1 - p2) + p3 - p0))));
|
|
115
|
+
}
|
|
116
|
+
function cameraAt(scene, i, delta) {
|
|
117
|
+
const c = scene.camera;
|
|
118
|
+
return { scale: at(c.scale, i, delta), cx: at(c.cx, i, delta), cy: at(c.cy, i, delta) };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Largest screen movement, in output pixels, of the visible content between two camera
|
|
122
|
+
* states.
|
|
123
|
+
*/
|
|
124
|
+
function cameraTravel(scene, a, b, mid, stageScale) {
|
|
125
|
+
const { width, height } = scene.session.viewport;
|
|
126
|
+
const hw = width / (2 * mid.scale);
|
|
127
|
+
const hh = height / (2 * mid.scale);
|
|
128
|
+
let d = 0;
|
|
129
|
+
for (const [x, y] of [
|
|
130
|
+
[mid.cx - hw, mid.cy - hh],
|
|
131
|
+
[mid.cx + hw, mid.cy - hh],
|
|
132
|
+
[mid.cx - hw, mid.cy + hh],
|
|
133
|
+
[mid.cx + hw, mid.cy + hh],
|
|
134
|
+
]) {
|
|
135
|
+
const dx = (x - a.cx) * a.scale - (x - b.cx) * b.scale;
|
|
136
|
+
const dy = (y - a.cy) * a.scale - (y - b.cy) * b.scale;
|
|
137
|
+
d = Math.max(d, Math.hypot(dx, dy));
|
|
138
|
+
}
|
|
139
|
+
return d * scene.layout.k * stageScale;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Sub-frame offsets (in frames) spread over the shutter, or [0] when the movement
|
|
143
|
+
* is too small to blur.
|
|
144
|
+
*/
|
|
145
|
+
function shutterOffsets(travel, shutter, pixelsPerSample, max) {
|
|
146
|
+
const n = Math.min(max, Math.ceil(travel / pixelsPerSample));
|
|
147
|
+
if (shutter <= 0 || n < 3)
|
|
148
|
+
return [0];
|
|
149
|
+
return Array.from({ length: n }, (_, j) => (j / (n - 1) - 0.5) * shutter);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Compositor inputs for output frame `i`, without the source image. More than one input
|
|
153
|
+
* means camera motion blur: the compositor averages them.
|
|
154
|
+
*/
|
|
155
|
+
export function frameInputs(scene, i) {
|
|
156
|
+
const base = frameInput(scene, i);
|
|
157
|
+
const zoomBlur = Math.min(1, Math.max(0, scene.options.zoom.motionBlur));
|
|
158
|
+
if (zoomBlur > 0) {
|
|
159
|
+
const h = zoomBlur / 2;
|
|
160
|
+
const travel = cameraTravel(scene, cameraAt(scene, i, -h), cameraAt(scene, i, h), base.camera, base.stage.scale);
|
|
161
|
+
const offsets = shutterOffsets(travel, zoomBlur, 2, CAMERA_SAMPLES);
|
|
162
|
+
if (offsets.length > 1) {
|
|
163
|
+
return offsets.map((delta) => ({
|
|
164
|
+
...base,
|
|
165
|
+
camera: cameraAt(scene, i, delta),
|
|
166
|
+
cursor: base.cursor && {
|
|
167
|
+
...base.cursor,
|
|
168
|
+
x: at(scene.cursor.x, i, delta),
|
|
169
|
+
y: at(scene.cursor.y, i, delta),
|
|
170
|
+
},
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const cursorBlur = Math.min(1, Math.max(0, scene.options.cursor.motionBlur));
|
|
175
|
+
if (cursorBlur > 0 && base.cursor) {
|
|
176
|
+
const c = scene.cursor;
|
|
177
|
+
const px = scene.layout.k * base.stage.scale * base.camera.scale;
|
|
178
|
+
const point = (delta) => ({ x: cursorAt(c.x, i, delta), y: cursorAt(c.y, i, delta) });
|
|
179
|
+
let travel = 0;
|
|
180
|
+
let last = point(-cursorBlur / 2);
|
|
181
|
+
for (let j = 1; j <= 8; j++) {
|
|
182
|
+
const next = point(cursorBlur * (j / 8 - 0.5));
|
|
183
|
+
travel += Math.hypot(next.x - last.x, next.y - last.y) * px;
|
|
184
|
+
last = next;
|
|
185
|
+
}
|
|
186
|
+
const offsets = shutterOffsets(travel, cursorBlur, 1, CURSOR_SAMPLES);
|
|
187
|
+
if (offsets.length > 1)
|
|
188
|
+
base.cursorTrail = offsets.map(point);
|
|
189
|
+
}
|
|
190
|
+
return [base];
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Compositor input for output frame `i`, without the source image or motion blur.
|
|
194
|
+
*/
|
|
195
|
+
export function frameInput(scene, i) {
|
|
196
|
+
const t = scene.times[i];
|
|
197
|
+
const c = scene.cursor;
|
|
198
|
+
const cursor = c.opacity[i] > 0
|
|
199
|
+
? {
|
|
200
|
+
x: c.x[i],
|
|
201
|
+
y: c.y[i],
|
|
202
|
+
style: c.style[i],
|
|
203
|
+
previousStyle: c.previousStyle[i],
|
|
204
|
+
styleMix: c.styleMix[i],
|
|
205
|
+
press: c.press[i],
|
|
206
|
+
opacity: c.opacity[i],
|
|
207
|
+
angle: c.angle[i],
|
|
208
|
+
}
|
|
209
|
+
: null;
|
|
210
|
+
const duration = scene.options.cursor.click.duration;
|
|
211
|
+
const band = scene.bandOpen[i];
|
|
212
|
+
return {
|
|
213
|
+
t,
|
|
214
|
+
band,
|
|
215
|
+
stage: stageAt(scene.band, band),
|
|
216
|
+
camera: { scale: scene.camera.scale[i], cx: scene.camera.cx[i], cy: scene.camera.cy[i] },
|
|
217
|
+
cursor,
|
|
218
|
+
clicks: scene.clicks
|
|
219
|
+
.filter((m) => t >= m.out && t < m.out + duration)
|
|
220
|
+
.map((mark) => ({ mark, age: t - mark.out })),
|
|
221
|
+
spotlights: scene.spotlights
|
|
222
|
+
.map((event) => ({ event, amount: spotlightAmount(event, t) }))
|
|
223
|
+
.filter((s) => s.amount > 0),
|
|
224
|
+
captions: scene.captions.filter((c) => t >= c.textIn && t <= c.textOut + 250),
|
|
225
|
+
keys: scene.keys.filter((k) => keyCueVisible(k, t)),
|
|
226
|
+
address: scene.options.frame.chrome === 'browser'
|
|
227
|
+
? addressAt(scene.urls, t, scene.options.frame, scene.session.url)
|
|
228
|
+
: undefined,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Image } from '@napi-rs/canvas';
|
|
2
|
+
import type { FrameRef } from '../types.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Decodes captured frames on demand. Decoding a 2560x1440 JPEG takes about 20 ms, so
|
|
5
|
+
* frames are decoded once, kept while in use, and decoded ahead in parallel.
|
|
6
|
+
*/
|
|
7
|
+
export declare class FrameSource {
|
|
8
|
+
private readonly dir;
|
|
9
|
+
private readonly frames;
|
|
10
|
+
private readonly cache;
|
|
11
|
+
constructor(dir: string, frames: FrameRef[]);
|
|
12
|
+
get(index: number): Promise<Image | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Keeps `current` and starts decoding `upcoming`; drops every other frame.
|
|
15
|
+
*/
|
|
16
|
+
retain(current: number, upcoming: number[]): void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The next `count` distinct source frames after output frame `from`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function upcomingFrames(frameIndex: Int32Array, from: number, count: number): number[];
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { loadImage } from '@napi-rs/canvas';
|
|
4
|
+
/**
|
|
5
|
+
* Decodes captured frames on demand. Decoding a 2560x1440 JPEG takes about 20 ms, so
|
|
6
|
+
* frames are decoded once, kept while in use, and decoded ahead in parallel.
|
|
7
|
+
*/
|
|
8
|
+
export class FrameSource {
|
|
9
|
+
dir;
|
|
10
|
+
frames;
|
|
11
|
+
cache = new Map();
|
|
12
|
+
constructor(dir, frames) {
|
|
13
|
+
this.dir = dir;
|
|
14
|
+
this.frames = frames;
|
|
15
|
+
}
|
|
16
|
+
get(index) {
|
|
17
|
+
if (index < 0 || index >= this.frames.length)
|
|
18
|
+
return Promise.resolve(null);
|
|
19
|
+
let image = this.cache.get(index);
|
|
20
|
+
if (!image) {
|
|
21
|
+
const file = path.resolve(this.dir, this.frames[index].file);
|
|
22
|
+
image = readFile(file)
|
|
23
|
+
.then((data) => loadImage(data))
|
|
24
|
+
.catch((error) => {
|
|
25
|
+
throw new Error(`Cannot read frame "${file}": ${error.message}`);
|
|
26
|
+
});
|
|
27
|
+
image.catch(() => { });
|
|
28
|
+
this.cache.set(index, image);
|
|
29
|
+
}
|
|
30
|
+
return image;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Keeps `current` and starts decoding `upcoming`; drops every other frame.
|
|
34
|
+
*/
|
|
35
|
+
retain(current, upcoming) {
|
|
36
|
+
const keep = new Set([current, ...upcoming]);
|
|
37
|
+
for (const key of this.cache.keys())
|
|
38
|
+
if (!keep.has(key))
|
|
39
|
+
this.cache.delete(key);
|
|
40
|
+
for (const index of upcoming)
|
|
41
|
+
void this.get(index);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The next `count` distinct source frames after output frame `from`.
|
|
46
|
+
*/
|
|
47
|
+
export function upcomingFrames(frameIndex, from, count) {
|
|
48
|
+
const result = [];
|
|
49
|
+
let last = frameIndex[from];
|
|
50
|
+
for (let i = from + 1; i < frameIndex.length && result.length < count; i++) {
|
|
51
|
+
if (frameIndex[i] !== last) {
|
|
52
|
+
last = frameIndex[i];
|
|
53
|
+
result.push(last);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advances a critically damped spring by `dt` seconds toward a constant `target`.
|
|
3
|
+
* Uses the exact solution, so it is stable for any step size. Returns [position, velocity].
|
|
4
|
+
*/
|
|
5
|
+
export declare function springStep(x: number, v: number, target: number, omega: number, dt: number): [number, number];
|
|
6
|
+
/**
|
|
7
|
+
* Zero-phase smoothing of a sampled signal: a critically damped spring run forward,
|
|
8
|
+
* then backward. Unlike a one-way spring, it adds no lag. `breaks[i]` restarts the
|
|
9
|
+
* spring at sample i (for example after a cut).
|
|
10
|
+
*/
|
|
11
|
+
export declare function springSmooth(times: ArrayLike<number>, values: ArrayLike<number>, omega: number, breaks?: ArrayLike<boolean>): Float64Array;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advances a critically damped spring by `dt` seconds toward a constant `target`.
|
|
3
|
+
* Uses the exact solution, so it is stable for any step size. Returns [position, velocity].
|
|
4
|
+
*/
|
|
5
|
+
export function springStep(x, v, target, omega, dt) {
|
|
6
|
+
const d = x - target;
|
|
7
|
+
const c = v + omega * d;
|
|
8
|
+
const decay = Math.exp(-omega * dt);
|
|
9
|
+
return [target + (d + c * dt) * decay, (v - omega * c * dt) * decay];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Zero-phase smoothing of a sampled signal: a critically damped spring run forward,
|
|
13
|
+
* then backward. Unlike a one-way spring, it adds no lag. `breaks[i]` restarts the
|
|
14
|
+
* spring at sample i (for example after a cut).
|
|
15
|
+
*/
|
|
16
|
+
export function springSmooth(times, values, omega, breaks) {
|
|
17
|
+
const n = values.length;
|
|
18
|
+
const forward = new Float64Array(n);
|
|
19
|
+
const out = new Float64Array(n);
|
|
20
|
+
if (n === 0)
|
|
21
|
+
return out;
|
|
22
|
+
let x = values[0];
|
|
23
|
+
let v = 0;
|
|
24
|
+
for (let i = 0; i < n; i++) {
|
|
25
|
+
if (i === 0 || breaks?.[i]) {
|
|
26
|
+
x = values[i];
|
|
27
|
+
v = 0;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
[x, v] = springStep(x, v, values[i], omega, (times[i] - times[i - 1]) / 1000);
|
|
31
|
+
}
|
|
32
|
+
forward[i] = x;
|
|
33
|
+
}
|
|
34
|
+
x = forward[n - 1];
|
|
35
|
+
v = 0;
|
|
36
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
37
|
+
if (i === n - 1 || breaks?.[i + 1]) {
|
|
38
|
+
x = forward[i];
|
|
39
|
+
v = 0;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
[x, v] = springStep(x, v, forward[i], omega, (times[i + 1] - times[i]) / 1000);
|
|
43
|
+
}
|
|
44
|
+
out[i] = x;
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Rect } from '../types.ts';
|
|
2
|
+
import type { CameraState } from './camera.ts';
|
|
3
|
+
import type { Layout } from './compositor.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Similarity that moves the rest layout: output = scale * rest + (x, y).
|
|
6
|
+
*/
|
|
7
|
+
export interface StageTransform {
|
|
8
|
+
scale: number;
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
}
|
|
12
|
+
export declare const IDENTITY: StageTransform;
|
|
13
|
+
/**
|
|
14
|
+
* Where things go when the caption band is fully open.
|
|
15
|
+
*/
|
|
16
|
+
export interface BandLayout {
|
|
17
|
+
side: 'bottom' | 'top';
|
|
18
|
+
height: number;
|
|
19
|
+
/**
|
|
20
|
+
* Rest window with the band open.
|
|
21
|
+
*/
|
|
22
|
+
window: Rect;
|
|
23
|
+
stage: StageTransform;
|
|
24
|
+
/**
|
|
25
|
+
* Vertical center of the caption text block.
|
|
26
|
+
*/
|
|
27
|
+
center: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The band takes the place of the padding on its side and grows it to BAND of the short
|
|
31
|
+
* side; the window (chrome and shadow included) scales down uniformly to fit the rest.
|
|
32
|
+
*/
|
|
33
|
+
export declare function bandLayout(layout: Layout, side: 'bottom' | 'top'): BandLayout;
|
|
34
|
+
/**
|
|
35
|
+
* Stage transform with the band open by `p` (0 to 1).
|
|
36
|
+
*/
|
|
37
|
+
export declare function stageAt(band: BandLayout | null, p: number): StageTransform;
|
|
38
|
+
export declare function applyStage(s: StageTransform, r: Rect): Rect;
|
|
39
|
+
/**
|
|
40
|
+
* Camera scale by which edges 'fill' has moved every page edge past the output edge.
|
|
41
|
+
*/
|
|
42
|
+
export declare const FILL_SCALE = 1.35;
|
|
43
|
+
/**
|
|
44
|
+
* The plain affine map from a camera state to output pixels: page point p lands at
|
|
45
|
+
* (ex + p.x * ks, ey + p.y * ks). No per-frame correction.
|
|
46
|
+
*/
|
|
47
|
+
export declare function contentMap(layout: Layout, stage: StageTransform, camera: CameraState): {
|
|
48
|
+
ks: number;
|
|
49
|
+
ex: number;
|
|
50
|
+
ey: number;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Edges 'fill' for one camera state. The gap allowed between the page and each output edge
|
|
54
|
+
* blends from the rest gap at scale 1 to a small bleed past the edge by FILL_SCALE (the
|
|
55
|
+
* bleed hides the scaled corner radius); the center moves by the smallest amount that
|
|
56
|
+
* respects it. On the caption band side the edge is the band. Applied to end states and
|
|
57
|
+
* held states only, so a transition between them stays one pivot zoom.
|
|
58
|
+
*/
|
|
59
|
+
export declare function fillEdges(layout: Layout, band: BandLayout | null, open: number, camera: CameraState): CameraState;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export const IDENTITY = { scale: 1, x: 0, y: 0 };
|
|
2
|
+
/**
|
|
3
|
+
* Band height as a fraction of the short side of the output: room for one line of text.
|
|
4
|
+
*/
|
|
5
|
+
const BAND = 0.125;
|
|
6
|
+
/**
|
|
7
|
+
* The band takes the place of the padding on its side and grows it to BAND of the short
|
|
8
|
+
* side; the window (chrome and shadow included) scales down uniformly to fit the rest.
|
|
9
|
+
*/
|
|
10
|
+
export function bandLayout(layout, side) {
|
|
11
|
+
const { width: W, height: H } = layout.size;
|
|
12
|
+
const pad = layout.padding;
|
|
13
|
+
const height = BAND * Math.min(W, H);
|
|
14
|
+
const w0 = layout.window;
|
|
15
|
+
const availW = W - 2 * pad;
|
|
16
|
+
const availH = H - height - pad;
|
|
17
|
+
const scale = Math.min(1, availW / w0.width, availH / w0.height);
|
|
18
|
+
const width = w0.width * scale;
|
|
19
|
+
const h = w0.height * scale;
|
|
20
|
+
const top = side === 'bottom' ? pad : height;
|
|
21
|
+
const window = { x: (W - width) / 2, y: top + (availH - h) / 2, width, height: h };
|
|
22
|
+
return {
|
|
23
|
+
side,
|
|
24
|
+
height,
|
|
25
|
+
window,
|
|
26
|
+
stage: { scale, x: window.x - scale * w0.x, y: window.y - scale * w0.y },
|
|
27
|
+
center: side === 'bottom' ? window.y + window.height + height / 2 : window.y - height / 2,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Stage transform with the band open by `p` (0 to 1).
|
|
32
|
+
*/
|
|
33
|
+
export function stageAt(band, p) {
|
|
34
|
+
if (!band || p <= 0)
|
|
35
|
+
return IDENTITY;
|
|
36
|
+
const s = band.stage;
|
|
37
|
+
return { scale: 1 + (s.scale - 1) * p, x: s.x * p, y: s.y * p };
|
|
38
|
+
}
|
|
39
|
+
export function applyStage(s, r) {
|
|
40
|
+
return {
|
|
41
|
+
x: s.scale * r.x + s.x,
|
|
42
|
+
y: s.scale * r.y + s.y,
|
|
43
|
+
width: s.scale * r.width,
|
|
44
|
+
height: s.scale * r.height,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Camera scale by which edges 'fill' has moved every page edge past the output edge.
|
|
49
|
+
*/
|
|
50
|
+
export const FILL_SCALE = 1.35;
|
|
51
|
+
/**
|
|
52
|
+
* The plain affine map from a camera state to output pixels: page point p lands at
|
|
53
|
+
* (ex + p.x * ks, ey + p.y * ks). No per-frame correction.
|
|
54
|
+
*/
|
|
55
|
+
export function contentMap(layout, stage, camera) {
|
|
56
|
+
const c = layout.content;
|
|
57
|
+
const ks = layout.k * stage.scale * camera.scale;
|
|
58
|
+
return {
|
|
59
|
+
ks,
|
|
60
|
+
ex: stage.x + stage.scale * (c.x + c.width / 2) - camera.cx * ks,
|
|
61
|
+
ey: stage.y + stage.scale * (c.y + c.height / 2) - camera.cy * ks,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Edges 'fill' for one camera state. The gap allowed between the page and each output edge
|
|
66
|
+
* blends from the rest gap at scale 1 to a small bleed past the edge by FILL_SCALE (the
|
|
67
|
+
* bleed hides the scaled corner radius); the center moves by the smallest amount that
|
|
68
|
+
* respects it. On the caption band side the edge is the band. Applied to end states and
|
|
69
|
+
* held states only, so a transition between them stays one pivot zoom.
|
|
70
|
+
*/
|
|
71
|
+
export function fillEdges(layout, band, open, camera) {
|
|
72
|
+
const v = Math.min(1, Math.max(0, (camera.scale - 1) / (FILL_SCALE - 1)));
|
|
73
|
+
const w = v * v * v * (v * (6 * v - 15) + 10);
|
|
74
|
+
if (w <= 0)
|
|
75
|
+
return camera;
|
|
76
|
+
const stage = stageAt(band, open);
|
|
77
|
+
const { width: W, height: H } = layout.size;
|
|
78
|
+
const c = layout.content;
|
|
79
|
+
const { ks, ex, ey } = contentMap(layout, stage, camera);
|
|
80
|
+
const bleed = 0.32 * layout.radius * stage.scale * camera.scale + 2 * layout.unit;
|
|
81
|
+
const top = band && band.side === 'top' ? band.window.y * open : 0;
|
|
82
|
+
const bottom = band && band.side === 'bottom' ? H + (band.window.y + band.window.height - H) * open : H;
|
|
83
|
+
const rest = applyStage(stage, c);
|
|
84
|
+
const gap = (restGap) => restGap + (-bleed - restGap) * w;
|
|
85
|
+
const shift = (lo, hi) => lo <= hi ? Math.min(Math.max(0, lo), hi) : (lo + hi) / 2;
|
|
86
|
+
const right = ex + layout.viewport.width * ks;
|
|
87
|
+
const lower = ey + layout.viewport.height * ks;
|
|
88
|
+
const dx = shift(W - gap(W - rest.x - rest.width) - right, gap(rest.x) - ex);
|
|
89
|
+
const dy = shift(bottom - gap(bottom - rest.y - rest.height) - lower, top + gap(rest.y - top) - ey);
|
|
90
|
+
return { scale: camera.scale, cx: camera.cx - dx / ks, cy: camera.cy - dy / ks };
|
|
91
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { FrameRef, Session, TimelineEvent } from '../types.ts';
|
|
2
|
+
interface Segment {
|
|
3
|
+
recStart: number;
|
|
4
|
+
recEnd: number;
|
|
5
|
+
outStart: number;
|
|
6
|
+
outEnd: number;
|
|
7
|
+
/**
|
|
8
|
+
* Playback rate; Infinity for a cut.
|
|
9
|
+
*/
|
|
10
|
+
rate: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Piecewise-linear map between record time and output time, built from `cut` and
|
|
14
|
+
* `speed` events. Overlapping speed ranges multiply; a cut wins over a speed range.
|
|
15
|
+
*/
|
|
16
|
+
export declare class TimeMap {
|
|
17
|
+
readonly segments: Segment[];
|
|
18
|
+
/**
|
|
19
|
+
* Output duration in milliseconds.
|
|
20
|
+
*/
|
|
21
|
+
readonly duration: number;
|
|
22
|
+
constructor(events: TimelineEvent[], recordDuration: number);
|
|
23
|
+
/**
|
|
24
|
+
* Output time of a record time. A time inside a cut maps to the cut point.
|
|
25
|
+
*/
|
|
26
|
+
toOutput(rec: number): number;
|
|
27
|
+
/**
|
|
28
|
+
* Record time shown at an output time. At a cut point it returns the end of the cut.
|
|
29
|
+
*/
|
|
30
|
+
toRecord(out: number): number;
|
|
31
|
+
/**
|
|
32
|
+
* True when a record time lies inside a cut.
|
|
33
|
+
*/
|
|
34
|
+
inCut(rec: number): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* True when a cut lies between two record times (a < b).
|
|
37
|
+
*/
|
|
38
|
+
cutBetween(a: number, b: number): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Playback rate at a record time (1 outside speed ranges).
|
|
41
|
+
*/
|
|
42
|
+
rateAt(rec: number): number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A timeline event with its output time.
|
|
46
|
+
*/
|
|
47
|
+
export type TimedEvent<E extends TimelineEvent = TimelineEvent> = E & {
|
|
48
|
+
out: number;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Sorts the events, adds output times and drops the events inside cuts.
|
|
52
|
+
* A zoom inside a cut is kept with duration 0, so it takes effect at the cut point.
|
|
53
|
+
* A caption inside a cut is kept and starts at the cut point: with the frame-perfect
|
|
54
|
+
* clock, a caption set just before `cast.cut` has the same time as the start of the cut.
|
|
55
|
+
* A page address inside a cut is kept too: the page has it when the video continues.
|
|
56
|
+
*/
|
|
57
|
+
export declare function timedEvents(events: TimelineEvent[], map: TimeMap): TimedEvent[];
|
|
58
|
+
/**
|
|
59
|
+
* Index of the latest frame with `t <= time`, or 0 when the time is before the first frame.
|
|
60
|
+
*/
|
|
61
|
+
export declare function frameIndexAt(frames: FrameRef[], time: number): number;
|
|
62
|
+
/**
|
|
63
|
+
* Builds the time map of a session.
|
|
64
|
+
*/
|
|
65
|
+
export declare function sessionTimeMap(session: Session): TimeMap;
|
|
66
|
+
/**
|
|
67
|
+
* Times of the output frames, in output milliseconds.
|
|
68
|
+
*/
|
|
69
|
+
export declare function frameTimes(duration: number, fps: number): Float64Array;
|
|
70
|
+
export {};
|