@biunow/agent-biutalk 0.0.0-stage → 1.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/bin/server.js +355 -0
- package/engine/AGENTS.md +1414 -0
- package/engine/CLAUDE.md +1 -0
- package/engine/LICENSE +21 -0
- package/engine/README.md +216 -0
- package/engine/bin/explainroo.js +19 -0
- package/engine/dev/audio-lab.mjs +268 -0
- package/engine/docs/media/contact-sheet.jpg +0 -0
- package/engine/docs/media/dns-poster.jpg +0 -0
- package/engine/docs/media/intro-poster.jpg +0 -0
- package/engine/docs/media/logo-dark.png +0 -0
- package/engine/docs/media/logo-light.png +0 -0
- package/engine/docs/media/looks-dark.webp +0 -0
- package/engine/docs/media/looks-light.webp +0 -0
- package/engine/docs/media/product-demo.webp +0 -0
- package/engine/docs/media/rebase-poster.jpg +0 -0
- package/engine/docs/media/sizes-dark.webp +0 -0
- package/engine/docs/media/sizes-light.webp +0 -0
- package/engine/engine/audio/dsp.js +69 -0
- package/engine/engine/audio/instruments.js +147 -0
- package/engine/engine/audio/music.js +576 -0
- package/engine/engine/audio/pcm.js +93 -0
- package/engine/engine/audio/rng.js +40 -0
- package/engine/engine/audio/sfx.js +287 -0
- package/engine/engine/audio/soundtrack.js +201 -0
- package/engine/engine/audio/synth.js +721 -0
- package/engine/engine/audio/theory.js +105 -0
- package/engine/engine/audio/wav.js +47 -0
- package/engine/engine/captions.js +131 -0
- package/engine/engine/clips.js +107 -0
- package/engine/engine/code.js +179 -0
- package/engine/engine/director.js +475 -0
- package/engine/engine/frame.html +135 -0
- package/engine/engine/icons/LICENSE-lucide.txt +43 -0
- package/engine/engine/icons/lucide-tags.json +1 -0
- package/engine/engine/icons/lucide.json +1 -0
- package/engine/engine/layout.js +69 -0
- package/engine/engine/pathdata.js +234 -0
- package/engine/engine/pen.js +240 -0
- package/engine/engine/player.html +67 -0
- package/engine/engine/player.js +219 -0
- package/engine/engine/readability.js +9 -0
- package/engine/engine/runtime.js +690 -0
- package/engine/engine/stage.js +2247 -0
- package/engine/engine/studio.html +216 -0
- package/engine/engine/text.js +81 -0
- package/engine/engine/themes.js +360 -0
- package/engine/engine/transitions.js +161 -0
- package/engine/engine/ui.js +1042 -0
- package/engine/engine/upload.js +32 -0
- package/engine/engine/util.js +204 -0
- package/engine/examples/callbacks-to-async-await/code-versions.js +34 -0
- package/engine/examples/callbacks-to-async-await/scenes.js +106 -0
- package/engine/examples/callbacks-to-async-await/script.md +22 -0
- package/engine/examples/callbacks-to-async-await/video.json +16 -0
- package/engine/examples/circle-area/scenes.js +228 -0
- package/engine/examples/circle-area/script.md +37 -0
- package/engine/examples/circle-area/video.json +14 -0
- package/engine/examples/explainroo-intro/scenes.js +133 -0
- package/engine/examples/explainroo-intro/script.md +27 -0
- package/engine/examples/explainroo-intro/video.json +8 -0
- package/engine/examples/how-dns-works/scenes.js +130 -0
- package/engine/examples/how-dns-works/script.md +28 -0
- package/engine/examples/how-dns-works/video.json +8 -0
- package/engine/examples/leap-years/scenes.js +156 -0
- package/engine/examples/leap-years/script.md +25 -0
- package/engine/examples/leap-years/video.json +16 -0
- package/engine/examples/leap-years-ja/scenes.js +194 -0
- package/engine/examples/leap-years-ja/script.md +32 -0
- package/engine/examples/leap-years-ja/video.json +17 -0
- package/engine/examples/merge-vs-rebase/scenes.js +129 -0
- package/engine/examples/merge-vs-rebase/script.md +22 -0
- package/engine/examples/merge-vs-rebase/video.json +8 -0
- package/engine/examples/password-storage-zh/scenes.js +185 -0
- package/engine/examples/password-storage-zh/script.md +25 -0
- package/engine/examples/password-storage-zh/video.json +19 -0
- package/engine/examples/sqlite-demo/README.md +19 -0
- package/engine/examples/sqlite-demo/scenes.js +423 -0
- package/engine/examples/sqlite-demo/script.md +35 -0
- package/engine/examples/sqlite-demo/video.json +17 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-400.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-500.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-600.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-700.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-OFL.txt +93 -0
- package/engine/examples/unspar-demo/assets/unspar-logo.svg +23 -0
- package/engine/examples/unspar-demo/scenes.js +367 -0
- package/engine/examples/unspar-demo/script.md +25 -0
- package/engine/examples/unspar-demo/video.json +21 -0
- package/engine/examples/why-is-the-sky-blue-es/scenes.js +160 -0
- package/engine/examples/why-is-the-sky-blue-es/script.md +22 -0
- package/engine/examples/why-is-the-sky-blue-es/video.json +15 -0
- package/engine/fonts/ArchitectsDaughter-400-latin-ext.woff2 +0 -0
- package/engine/fonts/ArchitectsDaughter-400-latin.woff2 +0 -0
- package/engine/fonts/CabinSketch-400-latin.woff2 +0 -0
- package/engine/fonts/CabinSketch-700-latin.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-italic-latin-ext.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-italic-latin.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-normal-latin-ext.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-normal-latin.woff2 +0 -0
- package/engine/fonts/Inter-400-latin-ext.woff2 +0 -0
- package/engine/fonts/Inter-400-latin.woff2 +0 -0
- package/engine/fonts/JetBrainsMono-400-latin-ext.woff2 +0 -0
- package/engine/fonts/JetBrainsMono-400-latin.woff2 +0 -0
- package/engine/fonts/Kalam-400-latin-ext.woff2 +0 -0
- package/engine/fonts/Kalam-400-latin.woff2 +0 -0
- package/engine/fonts/Kalam-700-latin-ext.woff2 +0 -0
- package/engine/fonts/Kalam-700-latin.woff2 +0 -0
- package/engine/fonts/NotoSansDevanagari-400.ttf +0 -0
- package/engine/fonts/NotoSansDevanagari-700.ttf +0 -0
- package/engine/fonts/PatrickHand-400-latin-ext.woff2 +0 -0
- package/engine/fonts/PatrickHand-400-latin.woff2 +0 -0
- package/engine/fonts/PlusJakartaSans-600-latin-ext.woff2 +0 -0
- package/engine/fonts/PlusJakartaSans-600-latin.woff2 +0 -0
- package/engine/fonts/SpaceGrotesk-500-latin-ext.woff2 +0 -0
- package/engine/fonts/SpaceGrotesk-500-latin.woff2 +0 -0
- package/engine/fonts/fonts.json +216 -0
- package/engine/fonts/licenses/ArchitectsDaughter-OFL.txt +93 -0
- package/engine/fonts/licenses/CabinSketch-OFL.txt +93 -0
- package/engine/fonts/licenses/InstrumentSerif-OFL.txt +93 -0
- package/engine/fonts/licenses/Inter-OFL.txt +93 -0
- package/engine/fonts/licenses/JetBrainsMono-OFL.txt +93 -0
- package/engine/fonts/licenses/Kalam-OFL.txt +93 -0
- package/engine/fonts/licenses/NotoSansDevanagari-OFL.txt +118 -0
- package/engine/fonts/licenses/PatrickHand-OFL.txt +93 -0
- package/engine/fonts/licenses/PlusJakartaSans-OFL.txt +93 -0
- package/engine/fonts/licenses/SpaceGrotesk-OFL.txt +93 -0
- package/engine/package-lock.json +1201 -0
- package/engine/package.json +67 -0
- package/engine/scripts/build-icons.mjs +74 -0
- package/engine/scripts/fetch-fonts.mjs +68 -0
- package/engine/skills/explainroo/SKILL.md +51 -0
- package/engine/src/align.js +357 -0
- package/engine/src/browser.js +121 -0
- package/engine/src/capture.js +315 -0
- package/engine/src/chapters.js +32 -0
- package/engine/src/cli.js +421 -0
- package/engine/src/clips.js +19 -0
- package/engine/src/elevenlabs.js +90 -0
- package/engine/src/env.js +16 -0
- package/engine/src/export.js +187 -0
- package/engine/src/ffmpeg.js +103 -0
- package/engine/src/fonts.js +86 -0
- package/engine/src/g2p.py +76 -0
- package/engine/src/images.js +151 -0
- package/engine/src/licenses/misaki-Apache-2.0.txt +201 -0
- package/engine/src/math.js +83 -0
- package/engine/src/models.js +161 -0
- package/engine/src/phonemes.js +162 -0
- package/engine/src/pipeline.js +63 -0
- package/engine/src/project.js +331 -0
- package/engine/src/qa.js +186 -0
- package/engine/src/recordings.js +89 -0
- package/engine/src/render.js +156 -0
- package/engine/src/script.js +266 -0
- package/engine/src/server.js +288 -0
- package/engine/src/subtitles.js +78 -0
- package/engine/src/timeline.js +55 -0
- package/engine/src/voice.js +240 -0
- package/engine/src/wav.js +98 -0
- package/engine/templates/starter/scenes.js +31 -0
- package/engine/templates/starter/script.md +14 -0
- package/engine/test/align.test.js +108 -0
- package/engine/test/brand.test.js +41 -0
- package/engine/test/browser.test.js +89 -0
- package/engine/test/camera-browser.test.js +225 -0
- package/engine/test/caption-phrases.test.js +35 -0
- package/engine/test/capture-browser.test.js +90 -0
- package/engine/test/capture-fixture.js +46 -0
- package/engine/test/capture.test.js +62 -0
- package/engine/test/chapters.test.js +41 -0
- package/engine/test/cjk-browser.test.js +101 -0
- package/engine/test/cjk.test.js +232 -0
- package/engine/test/cli-exit.test.js +25 -0
- package/engine/test/cli.test.js +116 -0
- package/engine/test/clips-browser.test.js +125 -0
- package/engine/test/clips.test.js +67 -0
- package/engine/test/code-morph-browser.test.js +99 -0
- package/engine/test/code-morph.test.js +110 -0
- package/engine/test/contrast-browser.test.js +63 -0
- package/engine/test/contrast.test.js +80 -0
- package/engine/test/director.test.js +316 -0
- package/engine/test/engine-pure.test.js +29 -0
- package/engine/test/export-browser.test.js +224 -0
- package/engine/test/export.test.js +96 -0
- package/engine/test/fixtures/capture.html +55 -0
- package/engine/test/formats-pace.test.js +104 -0
- package/engine/test/languages-browser.test.js +78 -0
- package/engine/test/languages.test.js +140 -0
- package/engine/test/looks-browser.test.js +109 -0
- package/engine/test/looks-preview.test.js +57 -0
- package/engine/test/looks.test.js +130 -0
- package/engine/test/math-browser.test.js +114 -0
- package/engine/test/math.test.js +54 -0
- package/engine/test/multi-size-browser.test.js +133 -0
- package/engine/test/multi-size.test.js +116 -0
- package/engine/test/phonemes.test.js +92 -0
- package/engine/test/readability-browser.test.js +36 -0
- package/engine/test/readability.test.js +20 -0
- package/engine/test/render-options.test.js +52 -0
- package/engine/test/script.test.js +67 -0
- package/engine/test/server.test.js +256 -0
- package/engine/test/stills-browser.test.js +70 -0
- package/engine/test/subtitles.test.js +83 -0
- package/engine/test/timeline.test.js +31 -0
- package/engine/test/transparent-browser.test.js +123 -0
- package/engine/test/ui-kit.test.js +96 -0
- package/engine/test/upload-browser.test.js +55 -0
- package/engine/test/voice-providers.test.js +338 -0
- package/lib/host.mjs +480 -0
- package/manifest.json +54 -0
- package/package.json +8 -4
- package/skills/biutalk/SKILL.md +21 -0
- package/studio/app.js +209 -0
- package/studio/assert-panel-permit.mjs +67 -0
- package/studio/composer.css +354 -0
- package/studio/composer.js +169 -0
- package/studio/index.html +82 -0
- package/studio/looks/blueprint.svg +21 -0
- package/studio/looks/chalk.svg +15 -0
- package/studio/looks/clean.svg +20 -0
- package/studio/looks/editorial.svg +18 -0
- package/studio/looks/midnight.svg +17 -0
- package/studio/looks/paper.svg +11 -0
- package/studio/select-menu.js +268 -0
- package/studio/styles.css +4 -0
- package/README.md +0 -3
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Keys, scales, chords and voice leading for the music generator.
|
|
2
|
+
|
|
3
|
+
export const KEYS = {
|
|
4
|
+
C: 0, 'C#': 1, Db: 1, D: 2, 'D#': 3, Eb: 3, E: 4, F: 5, 'F#': 6, Gb: 6,
|
|
5
|
+
G: 7, 'G#': 8, Ab: 8, A: 9, 'A#': 10, Bb: 10, B: 11,
|
|
6
|
+
};
|
|
7
|
+
export const KEY_LABELS = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B'];
|
|
8
|
+
export const MAJOR = [0, 2, 4, 5, 7, 9, 11];
|
|
9
|
+
export const PENTATONIC = [0, 2, 4, 7, 9];
|
|
10
|
+
|
|
11
|
+
const mod = (n, m) => ((n % m) + m) % m;
|
|
12
|
+
|
|
13
|
+
// Accepts 'F', 'Bb', 'f#', 'Eb major' or a pitch-class number.
|
|
14
|
+
export function parseKey(key) {
|
|
15
|
+
if (key == null || key === '') return null;
|
|
16
|
+
if (typeof key === 'number' && Number.isFinite(key)) return mod(Math.round(key), 12);
|
|
17
|
+
const raw = String(key).trim().replace(/\s*maj(or)?$/i, '');
|
|
18
|
+
const norm = raw.charAt(0).toUpperCase() + raw.slice(1);
|
|
19
|
+
if (!(norm in KEYS)) {
|
|
20
|
+
throw new Error(`Unknown music key "${key}". Use one of: ${Object.keys(KEYS).join(', ')}`);
|
|
21
|
+
}
|
|
22
|
+
return KEYS[norm];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// MIDI note of a major-scale degree (0 = tonic; may be negative or above 6)
|
|
26
|
+
// relative to the tonic MIDI note.
|
|
27
|
+
export function scaleNote(tonicMidi, degree) {
|
|
28
|
+
return tonicMidi + Math.floor(degree / 7) * 12 + MAJOR[mod(degree, 7)];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// MIDI note of a major-pentatonic step relative to the tonic MIDI note.
|
|
32
|
+
export function pentaNote(tonicMidi, step) {
|
|
33
|
+
return tonicMidi + Math.floor(step / 5) * 12 + PENTATONIC[mod(step, 5)];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A chord is { deg, seventh?, ninth?, sus2?, sus4? } on the major scale.
|
|
37
|
+
// Returns pitch classes with the root first and the colour tones last.
|
|
38
|
+
export function chordPcs(keyRoot, chord) {
|
|
39
|
+
const degs = [0, 2, 4];
|
|
40
|
+
if (chord.sus4) degs[1] = 3;
|
|
41
|
+
if (chord.sus2) degs[1] = 1;
|
|
42
|
+
if (chord.seventh) degs.push(6);
|
|
43
|
+
if (chord.ninth) degs.push(8);
|
|
44
|
+
return degs.map((d) => mod(keyRoot + MAJOR[mod(chord.deg + d, 7)], 12));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const rootPc = (keyRoot, chord) => mod(keyRoot + MAJOR[mod(chord.deg, 7)], 12);
|
|
48
|
+
|
|
49
|
+
// Bass register A1..G#2.
|
|
50
|
+
export const bassMidi = (pc) => 33 + mod(pc - 9, 12);
|
|
51
|
+
|
|
52
|
+
// Nearest MIDI note with pitch class `pc` to `near`.
|
|
53
|
+
export function nearestWithPc(pc, near) {
|
|
54
|
+
const base = near - mod(near - pc, 12);
|
|
55
|
+
return near - base <= 6 ? base : base + 12;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Choose `voices` chord tones inside [low, high] that cover the chord and move
|
|
59
|
+
// as little as possible from the previous voicing.
|
|
60
|
+
export function voiceChord(pcs, prev, { low = 53, high = 77, voices = 4 } = {}) {
|
|
61
|
+
const cands = [];
|
|
62
|
+
for (let m = low; m <= high; m++) if (pcs.includes(m % 12)) cands.push(m);
|
|
63
|
+
const n = Math.min(voices, cands.length);
|
|
64
|
+
let best = null;
|
|
65
|
+
let bestCost = Infinity;
|
|
66
|
+
const combo = [];
|
|
67
|
+
const center = (low + high) / 2;
|
|
68
|
+
|
|
69
|
+
const evaluate = () => {
|
|
70
|
+
const covered = new Set(combo.map((m) => m % 12));
|
|
71
|
+
let cost = 0;
|
|
72
|
+
for (let k = 1; k < pcs.length; k++) if (!covered.has(pcs[k])) cost += 6;
|
|
73
|
+
if (!covered.has(pcs[0])) cost += 1.5;
|
|
74
|
+
for (let i = 1; i < combo.length; i++) {
|
|
75
|
+
const gap = combo[i] - combo[i - 1];
|
|
76
|
+
if (gap < 2) cost += 5;
|
|
77
|
+
if (gap > 9) cost += gap - 9;
|
|
78
|
+
if (combo[i - 1] < 57 && gap < 4) cost += 3; // muddy low seconds and thirds
|
|
79
|
+
}
|
|
80
|
+
if (prev && prev.length === combo.length) {
|
|
81
|
+
for (let i = 0; i < combo.length; i++) cost += Math.abs(combo[i] - prev[i]) * 0.7;
|
|
82
|
+
} else {
|
|
83
|
+
const avg = combo.reduce((a, b) => a + b, 0) / combo.length;
|
|
84
|
+
cost += Math.abs(avg - center) * 0.5;
|
|
85
|
+
}
|
|
86
|
+
if (cost < bestCost) {
|
|
87
|
+
bestCost = cost;
|
|
88
|
+
best = combo.slice();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const walk = (start) => {
|
|
93
|
+
if (combo.length === n) {
|
|
94
|
+
evaluate();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
for (let i = start; i < cands.length; i++) {
|
|
98
|
+
combo.push(cands[i]);
|
|
99
|
+
walk(i + 1);
|
|
100
|
+
combo.pop();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
walk(0);
|
|
104
|
+
return best || [];
|
|
105
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// 16-bit PCM stereo WAV encoder for rendered AudioBuffers.
|
|
2
|
+
|
|
3
|
+
const LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
4
|
+
|
|
5
|
+
const toInt16 = (x) => {
|
|
6
|
+
const s = x > 1 ? 1 : x < -1 ? -1 : x;
|
|
7
|
+
return s < 0 ? Math.round(s * 32768) : Math.round(s * 32767);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function encodeWav(buffer) {
|
|
11
|
+
const n = buffer.length;
|
|
12
|
+
const sr = buffer.sampleRate;
|
|
13
|
+
const left = buffer.getChannelData(0);
|
|
14
|
+
const right = buffer.numberOfChannels > 1 ? buffer.getChannelData(1) : left;
|
|
15
|
+
const dataBytes = n * 4;
|
|
16
|
+
const out = new ArrayBuffer(44 + dataBytes);
|
|
17
|
+
const v = new DataView(out);
|
|
18
|
+
const str = (o, s) => {
|
|
19
|
+
for (let i = 0; i < s.length; i++) v.setUint8(o + i, s.charCodeAt(i));
|
|
20
|
+
};
|
|
21
|
+
str(0, 'RIFF');
|
|
22
|
+
v.setUint32(4, 36 + dataBytes, true);
|
|
23
|
+
str(8, 'WAVE');
|
|
24
|
+
str(12, 'fmt ');
|
|
25
|
+
v.setUint32(16, 16, true);
|
|
26
|
+
v.setUint16(20, 1, true);
|
|
27
|
+
v.setUint16(22, 2, true);
|
|
28
|
+
v.setUint32(24, sr, true);
|
|
29
|
+
v.setUint32(28, sr * 4, true);
|
|
30
|
+
v.setUint16(32, 4, true);
|
|
31
|
+
v.setUint16(34, 16, true);
|
|
32
|
+
str(36, 'data');
|
|
33
|
+
v.setUint32(40, dataBytes, true);
|
|
34
|
+
if (LITTLE_ENDIAN) {
|
|
35
|
+
const pcm = new Int16Array(out, 44, n * 2);
|
|
36
|
+
for (let i = 0, j = 0; i < n; i++, j += 2) {
|
|
37
|
+
pcm[j] = toInt16(left[i]);
|
|
38
|
+
pcm[j + 1] = toInt16(right[i]);
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
for (let i = 0, o = 44; i < n; i++, o += 4) {
|
|
42
|
+
v.setInt16(o, toInt16(left[i]), true);
|
|
43
|
+
v.setInt16(o + 2, toInt16(right[i]), true);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Burned-in captions built from the narration's word timings. The current
|
|
2
|
+
// phrase is shown; words light up as they are spoken.
|
|
3
|
+
import { fontString } from './themes.js';
|
|
4
|
+
import { clamp, ease } from './util.js';
|
|
5
|
+
|
|
6
|
+
// Some words belong to the word before them, so a phrase or line should not
|
|
7
|
+
// start with one: Japanese particles and verb endings (short hiragana pieces
|
|
8
|
+
// such as は, ごと, せん, or ones starting like ません), a one-kanji suffix
|
|
9
|
+
// such as the 暦 of グレゴリオ暦, Chinese particles such as 的, and counters
|
|
10
|
+
// after a number (4年, 365天).
|
|
11
|
+
const JAPANESE_ENDINGS = /^(ませ|まし|でし|です|ます|しょう|られ|させ|され|なかっ|ながら|ような|ように|という|として|について|によって|くらい|ぐらい)/;
|
|
12
|
+
export function clingsToPrevious(language) {
|
|
13
|
+
const counter = (text, before) => /\d$/.test(before) && /^[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}]/u.test(text);
|
|
14
|
+
if (language === 'ja') {
|
|
15
|
+
return (text, before = '') => {
|
|
16
|
+
const word = text.replace(/[、。!?「」『』()]+$/, '');
|
|
17
|
+
const ending = /^[\u3041-\u3096]+$/.test(word) && (word.length <= 2 || JAPANESE_ENDINGS.test(word));
|
|
18
|
+
const suffix = /^\p{Script=Han}$/u.test(word) && /[\p{Script=Han}\p{Script=Katakana}]$/u.test(before);
|
|
19
|
+
return ending || suffix || counter(text, before);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (language === 'zh') return (text, before = '') => /^[的了吗呢吧啊着过得地们][,。!?、]*$/.test(text) || counter(text, before);
|
|
23
|
+
return () => false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function buildPhrases(timeline, maxChars) {
|
|
27
|
+
const cjk = ['ja', 'zh'].includes(timeline.language);
|
|
28
|
+
const sentenceEnd = cjk ? /[.!?;:।。!?]["'’)」』)]*$/ : /[.!?;:।]["'’)]*$/;
|
|
29
|
+
if (cjk) maxChars = Math.min(maxChars, 18);
|
|
30
|
+
const phrases = [];
|
|
31
|
+
for (const sc of timeline.scenes) {
|
|
32
|
+
// A sentence (or a run of words between long pauses) is split into as few
|
|
33
|
+
// phrases as fit, balanced in length, so no phrase is one lonely word.
|
|
34
|
+
let run = [];
|
|
35
|
+
const flush = () => {
|
|
36
|
+
for (const words of balancePhrases(run, maxChars, cjk ? '' : ' ', clingsToPrevious(timeline.language))) phrases.push({ words, ...(cjk ? { separator: '' } : {}), start: words[0].start, end: words[words.length - 1].end });
|
|
37
|
+
run = [];
|
|
38
|
+
};
|
|
39
|
+
for (const w of sc.words) {
|
|
40
|
+
const g = { text: w.text, start: sc.start + w.start, end: sc.start + w.end };
|
|
41
|
+
if (run.length && g.start - run[run.length - 1].end > 0.55) flush();
|
|
42
|
+
run.push(g);
|
|
43
|
+
if (sentenceEnd.test(g.text)) flush();
|
|
44
|
+
}
|
|
45
|
+
if (run.length) flush();
|
|
46
|
+
}
|
|
47
|
+
phrases.forEach((p, i) => {
|
|
48
|
+
const next = phrases[i + 1];
|
|
49
|
+
p.until = next ? Math.min(next.start, p.end + 0.9) : p.end + 0.9;
|
|
50
|
+
});
|
|
51
|
+
return phrases;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Splits words into the fewest phrases of at most maxChars characters, with
|
|
55
|
+
// lengths as even as possible and breaks after commas where they fit, never
|
|
56
|
+
// before a word that clings to the one before it if that can be helped.
|
|
57
|
+
export function balancePhrases(words, maxChars, separator = ' ', clings = () => false) {
|
|
58
|
+
if (!words.length) return [];
|
|
59
|
+
const len = (a, b) => words.slice(a, b).reduce((n, w) => n + w.text.length + separator.length, -separator.length);
|
|
60
|
+
if (len(0, words.length) <= maxChars) return [words];
|
|
61
|
+
const punctuation = separator === '' ? /[,;:–—、,;:]["'’)」』)]*$/ : /[,;:–—]["'’)]*$/;
|
|
62
|
+
const n = words.length;
|
|
63
|
+
const best = [{ parts: 0, cost: 0, from: -1 }];
|
|
64
|
+
for (let b = 1; b <= n; b++) {
|
|
65
|
+
best[b] = null;
|
|
66
|
+
for (let a = b - 1; a >= 0; a--) {
|
|
67
|
+
const l = len(a, b);
|
|
68
|
+
if (l > maxChars && b - a > 1) break;
|
|
69
|
+
if (!best[a]) continue;
|
|
70
|
+
const comma = b < n && punctuation.test(words[b - 1].text) ? 150 : 0;
|
|
71
|
+
const glued = b < n && !comma && clings(words[b].text, words[b - 1].text) ? 250 : 0;
|
|
72
|
+
const c = { parts: best[a].parts + 1, cost: best[a].cost + (maxChars - l) ** 2 - comma + glued, from: a };
|
|
73
|
+
if (!best[b] || c.parts < best[b].parts || (c.parts === best[b].parts && c.cost < best[b].cost)) best[b] = c;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const out = [];
|
|
77
|
+
for (let b = n; b > 0; b = best[b].from) out.unshift(words.slice(best[b].from, b));
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// `area` ({ cx, bottom, maxWidth }) places the captions inside a platform's
|
|
82
|
+
// safe area, right above the band the app covers with its own text.
|
|
83
|
+
export function drawCaptions(ctx, phrases, T, theme, W, H, area = null) {
|
|
84
|
+
const ph = phrases.find((p) => T >= p.start - 0.08 && T < p.until);
|
|
85
|
+
if (!ph) return;
|
|
86
|
+
const vertical = H > W;
|
|
87
|
+
const size = vertical ? 58 : 44;
|
|
88
|
+
const font = fontString(theme, 'body', size, theme.fonts.body.family === 'Inter' ? 600 : undefined);
|
|
89
|
+
ctx.save();
|
|
90
|
+
ctx.font = font;
|
|
91
|
+
ctx.textBaseline = 'middle';
|
|
92
|
+
ctx.textAlign = 'left';
|
|
93
|
+
const space = ctx.measureText(ph.separator ?? ' ').width;
|
|
94
|
+
const widths = ph.words.map((w) => ctx.measureText(w.text).width);
|
|
95
|
+
const maxLine = area ? area.maxWidth - size * 1.1 : W * (vertical ? 0.8 : 0.7);
|
|
96
|
+
const lines = [[]];
|
|
97
|
+
let lw = 0;
|
|
98
|
+
ph.words.forEach((w, i) => {
|
|
99
|
+
const add = (lines[lines.length - 1].length ? space : 0) + widths[i];
|
|
100
|
+
if (lw + add > maxLine && lines[lines.length - 1].length) {
|
|
101
|
+
lines.push([]);
|
|
102
|
+
lw = 0;
|
|
103
|
+
}
|
|
104
|
+
lines[lines.length - 1].push(i);
|
|
105
|
+
lw += (lines[lines.length - 1].length > 1 ? space : 0) + widths[i];
|
|
106
|
+
});
|
|
107
|
+
const lh = size * 1.3;
|
|
108
|
+
const boxW = Math.max(...lines.map((l) => l.reduce((a, i, k) => a + widths[i] + (k ? space : 0), 0))) + size * 1.1;
|
|
109
|
+
const boxH = lines.length * lh + size * 0.55;
|
|
110
|
+
const cx = area ? area.cx : W / 2;
|
|
111
|
+
const cy = area ? area.bottom - boxH / 2 : vertical ? H * 0.74 : H - Math.max(96, H * 0.1) - boxH / 2 + lh / 2;
|
|
112
|
+
const fadeIn = ease.out(clamp((T - (ph.start - 0.08)) / 0.15));
|
|
113
|
+
const fadeOut = 1 - clamp((T - (ph.until - 0.15)) / 0.15);
|
|
114
|
+
ctx.globalAlpha = Math.min(fadeIn, fadeOut);
|
|
115
|
+
ctx.fillStyle = theme.caption.bg;
|
|
116
|
+
ctx.beginPath();
|
|
117
|
+
ctx.roundRect(cx - boxW / 2, cy - boxH / 2, boxW, boxH, Math.min(22, boxH / 2));
|
|
118
|
+
ctx.fill();
|
|
119
|
+
lines.forEach((l, li) => {
|
|
120
|
+
const total = l.reduce((a, i, k) => a + widths[i] + (k ? space : 0), 0);
|
|
121
|
+
let x = cx - total / 2;
|
|
122
|
+
const y = cy - boxH / 2 + size * 0.275 + lh * li + lh / 2;
|
|
123
|
+
for (const i of l) {
|
|
124
|
+
const w = ph.words[i];
|
|
125
|
+
ctx.fillStyle = T >= w.start ? theme.caption.active : theme.caption.fg;
|
|
126
|
+
ctx.fillText(w.text, x, y);
|
|
127
|
+
x += widths[i] + space;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
ctx.restore();
|
|
131
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export const VIDEO_FILE = /\.(mp4|mov|webm|m4v)$/i;
|
|
2
|
+
|
|
3
|
+
export function clipFrame(info, t, { at = 0, from = 0, speed = 1, loop = false } = {}) {
|
|
4
|
+
if (t < at) return null;
|
|
5
|
+
let time = from + (t - at) * speed;
|
|
6
|
+
if (loop) time = ((time % info.duration) + info.duration) % info.duration;
|
|
7
|
+
return Math.max(0, Math.min(info.frames - 1, Math.floor(time * info.fps + 1e-8)));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function clipURL(kind, src, frame) {
|
|
11
|
+
return `/__clip/${kind}?path=${encodeURIComponent(src)}${frame === undefined ? '' : '&frame=' + frame}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function loadClips(assets, state = null) {
|
|
15
|
+
const clips = new Map();
|
|
16
|
+
await Promise.all(assets.filter((a) => VIDEO_FILE.test(a)).map(async (src) => {
|
|
17
|
+
if (state) {
|
|
18
|
+
const video = document.createElement('video');
|
|
19
|
+
video.hidden = true;
|
|
20
|
+
video.muted = true;
|
|
21
|
+
video.playsInline = true;
|
|
22
|
+
video.preload = 'auto';
|
|
23
|
+
video.src = './project/' + src.split('/').map(encodeURIComponent).join('/');
|
|
24
|
+
document.body.append(video);
|
|
25
|
+
const loaded = () => new Promise((resolve, reject) => {
|
|
26
|
+
video.onloadeddata = resolve;
|
|
27
|
+
video.onerror = () => reject(new Error(`clip "${src}" could not be loaded. Use a browser-supported video format.`));
|
|
28
|
+
});
|
|
29
|
+
await loaded();
|
|
30
|
+
// Servers without byte ranges cannot seek a media URL in Chrome.
|
|
31
|
+
if (!video.seekable.length || video.seekable.end(video.seekable.length - 1) < video.duration - 0.1) {
|
|
32
|
+
const res = await fetch(video.src);
|
|
33
|
+
if (!res.ok) throw new Error(`clip "${src}" could not be loaded`);
|
|
34
|
+
video.src = URL.createObjectURL(await res.blob());
|
|
35
|
+
await loaded();
|
|
36
|
+
}
|
|
37
|
+
clips.set(src, { src, video, duration: video.duration, width: video.videoWidth, height: video.videoHeight, fps: state.config.fps, frames: Math.max(1, Math.ceil(video.duration * state.config.fps)) });
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const res = await fetch(clipURL('info', src));
|
|
41
|
+
if (!res.ok) throw new Error(`clip "${src}" could not be loaded. Put a video in the project's assets/ folder.`);
|
|
42
|
+
clips.set(src, { src, ...await res.json() });
|
|
43
|
+
}));
|
|
44
|
+
return clips;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class ClipFrames {
|
|
48
|
+
constructor() {
|
|
49
|
+
this.frames = new Map();
|
|
50
|
+
this.pending = new Map();
|
|
51
|
+
this.errors = new Map();
|
|
52
|
+
this.closed = false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
key(clip, frame) { return `${clip.src}|${frame}`; }
|
|
56
|
+
|
|
57
|
+
async load(clip, frame) {
|
|
58
|
+
const key = this.key(clip, frame);
|
|
59
|
+
if (this.frames.has(key)) return;
|
|
60
|
+
if (!this.pending.has(key)) {
|
|
61
|
+
const job = (async () => {
|
|
62
|
+
const res = await fetch(clipURL('frame', clip.src, frame));
|
|
63
|
+
if (!res.ok) throw new Error(`clip "${clip.src}" frame ${frame} could not be loaded`);
|
|
64
|
+
const bitmap = await createImageBitmap(await res.blob());
|
|
65
|
+
if (this.closed) { bitmap.close(); return; }
|
|
66
|
+
this.frames.set(key, { clip, frame, bitmap });
|
|
67
|
+
})();
|
|
68
|
+
this.pending.set(key, job);
|
|
69
|
+
job.finally(() => this.pending.delete(key)).catch(() => {});
|
|
70
|
+
}
|
|
71
|
+
return this.pending.get(key);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get(clip, frame) {
|
|
75
|
+
const key = this.key(clip, frame);
|
|
76
|
+
const entry = this.frames.get(key);
|
|
77
|
+
if (entry) {
|
|
78
|
+
this.frames.delete(key);
|
|
79
|
+
this.frames.set(key, entry);
|
|
80
|
+
return entry.bitmap;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
nearest(clip, frame) {
|
|
86
|
+
let best;
|
|
87
|
+
for (const entry of this.frames.values()) {
|
|
88
|
+
if (entry.clip.src === clip.src && (!best || Math.abs(entry.frame - frame) < Math.abs(best.frame - frame))) best = entry;
|
|
89
|
+
}
|
|
90
|
+
return best?.bitmap;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
trim(keep = new Set()) {
|
|
94
|
+
for (const [key, entry] of this.frames) {
|
|
95
|
+
if (this.frames.size <= Math.max(24, keep.size)) break;
|
|
96
|
+
if (keep.has(key)) continue;
|
|
97
|
+
entry.bitmap.close();
|
|
98
|
+
this.frames.delete(key);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
close() {
|
|
103
|
+
this.closed = true;
|
|
104
|
+
for (const entry of this.frames.values()) entry.bitmap.close();
|
|
105
|
+
this.frames.clear();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// A small syntax highlighter for code on screen. It colors comments, strings,
|
|
2
|
+
// numbers, keywords and function calls for the common languages; anything it
|
|
3
|
+
// does not know stays plain.
|
|
4
|
+
import { clamp, ease, lerp } from './util.js';
|
|
5
|
+
|
|
6
|
+
const KEYWORDS = {
|
|
7
|
+
js: 'async await break case catch class const continue default delete do else export extends false finally for from function if import in instanceof let new null of return static super switch this throw true try typeof undefined var void while yield',
|
|
8
|
+
py: 'and as assert async await break class continue def del elif else except False finally for from global if import in is lambda None nonlocal not or pass raise return True try while with yield self',
|
|
9
|
+
go: 'break case chan const continue default defer else fallthrough for func go goto if import interface map package range return select struct switch type var nil true false err',
|
|
10
|
+
rust: 'as async await break const continue crate else enum extern false fn for if impl in let loop match mod move mut pub ref return self Self static struct super trait true type unsafe use where while',
|
|
11
|
+
php: 'abstract and array as break callable case catch class clone const continue declare default do echo else elseif empty extends final finally fn for foreach function global if implements include instanceof interface isset list match namespace new null or print private protected public readonly require return static switch throw trait true false try use var while yield',
|
|
12
|
+
sh: 'if then else elif fi for while do done case esac in function return export local echo exit sudo cd npm npx node git curl',
|
|
13
|
+
sql: 'select from where join left right inner outer on group by order having limit insert into values update set delete create table index as and or not null is in like count sum avg distinct',
|
|
14
|
+
json: 'true false null',
|
|
15
|
+
};
|
|
16
|
+
KEYWORDS.ts = KEYWORDS.js + ' interface type enum implements private public readonly as';
|
|
17
|
+
KEYWORDS.jsx = KEYWORDS.js;
|
|
18
|
+
KEYWORDS.tsx = KEYWORDS.ts;
|
|
19
|
+
KEYWORDS.bash = KEYWORDS.sh;
|
|
20
|
+
KEYWORDS.shell = KEYWORDS.sh;
|
|
21
|
+
KEYWORDS.python = KEYWORDS.py;
|
|
22
|
+
KEYWORDS.javascript = KEYWORDS.js;
|
|
23
|
+
KEYWORDS.typescript = KEYWORDS.ts;
|
|
24
|
+
|
|
25
|
+
const sets = {};
|
|
26
|
+
function keywordSet(lang) {
|
|
27
|
+
const k = String(lang || 'js').toLowerCase();
|
|
28
|
+
if (!sets[k]) sets[k] = new Set((KEYWORDS[k] || KEYWORDS.js).split(' ').map((w) => (k === 'sql' ? w.toLowerCase() : w)));
|
|
29
|
+
return sets[k];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function commentStart(lang) {
|
|
33
|
+
const k = String(lang || 'js').toLowerCase();
|
|
34
|
+
if (['py', 'python', 'sh', 'bash', 'shell'].includes(k)) return ['#'];
|
|
35
|
+
if (k === 'sql') return ['--'];
|
|
36
|
+
if (k === 'php') return ['//', '#'];
|
|
37
|
+
if (k === 'json') return [];
|
|
38
|
+
return ['//'];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Markdown, including explainroo's script syntax: headings, [#marks],
|
|
42
|
+
// [pause] and {shown|spoken}.
|
|
43
|
+
function highlightMarkdown(line) {
|
|
44
|
+
if (/^\s*#/.test(line)) return [{ text: line, type: 'keyword' }];
|
|
45
|
+
if (/^\s*>/.test(line)) return [{ text: line, type: 'comment' }];
|
|
46
|
+
const out = [];
|
|
47
|
+
const re = /(\[#[^\]]*\]|\[pause[^\]]*\])|(\{[^}]*\})|(`[^`]*`)/g;
|
|
48
|
+
let last = 0;
|
|
49
|
+
let m;
|
|
50
|
+
while ((m = re.exec(line)) !== null) {
|
|
51
|
+
if (m.index > last) out.push({ text: line.slice(last, m.index), type: 'plain' });
|
|
52
|
+
out.push({ text: m[0], type: m[1] ? 'string' : m[2] ? 'fn' : 'number' });
|
|
53
|
+
last = m.index + m[0].length;
|
|
54
|
+
}
|
|
55
|
+
if (last < line.length) out.push({ text: line.slice(last), type: 'plain' });
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// One line -> [{ text, type }] with type in comment|string|number|keyword|fn|punct|plain.
|
|
60
|
+
export function highlightLine(line, lang) {
|
|
61
|
+
if (['md', 'markdown'].includes(String(lang).toLowerCase())) return highlightMarkdown(line);
|
|
62
|
+
const out = [];
|
|
63
|
+
const kw = keywordSet(lang);
|
|
64
|
+
const isSql = String(lang).toLowerCase() === 'sql';
|
|
65
|
+
const cstarts = commentStart(lang);
|
|
66
|
+
let i = 0;
|
|
67
|
+
const push = (text, type) => {
|
|
68
|
+
if (!text) return;
|
|
69
|
+
const last = out[out.length - 1];
|
|
70
|
+
if (last && last.type === type) last.text += text;
|
|
71
|
+
else out.push({ text, type });
|
|
72
|
+
};
|
|
73
|
+
while (i < line.length) {
|
|
74
|
+
const rest = line.slice(i);
|
|
75
|
+
const cs = cstarts.find((c) => rest.startsWith(c));
|
|
76
|
+
if (cs || rest.startsWith('/*')) {
|
|
77
|
+
push(rest, 'comment');
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
const ch = line[i];
|
|
81
|
+
if (ch === '"' || ch === "'" || ch === '`') {
|
|
82
|
+
let j = i + 1;
|
|
83
|
+
while (j < line.length && line[j] !== ch) j += line[j] === '\\' ? 2 : 1;
|
|
84
|
+
push(line.slice(i, j + 1), 'string');
|
|
85
|
+
i = j + 1;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const num = /^(0x[0-9a-f]+|\d+(\.\d+)?)/i.exec(rest);
|
|
89
|
+
if (num && !/[A-Za-z_$]/.test(line[i - 1] || '')) {
|
|
90
|
+
push(num[0], 'number');
|
|
91
|
+
i += num[0].length;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const id = /^[A-Za-z_$][\w$]*/.exec(rest);
|
|
95
|
+
if (id) {
|
|
96
|
+
const word = id[0];
|
|
97
|
+
const next = line.slice(i + word.length).trimStart()[0];
|
|
98
|
+
if (kw.has(isSql ? word.toLowerCase() : word)) push(word, 'keyword');
|
|
99
|
+
else if (next === '(') push(word, 'fn');
|
|
100
|
+
else push(word, 'plain');
|
|
101
|
+
i += word.length;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (/[{}()[\];,.:=<>+\-*/%!&|?]/.test(ch)) {
|
|
105
|
+
push(ch, 'punct');
|
|
106
|
+
i++;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
push(ch, 'plain');
|
|
110
|
+
i++;
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function codeTokens(lines, lang) {
|
|
116
|
+
const tokens = [];
|
|
117
|
+
lines.forEach((line, row) => {
|
|
118
|
+
let column = 0;
|
|
119
|
+
for (const tok of highlightLine(line, lang)) {
|
|
120
|
+
// The highlighter merges adjacent plain text and punctuation. Separate
|
|
121
|
+
// identifiers so a rename does not replace the rest of the line, and keep
|
|
122
|
+
// operators like => whole so the font draws them the same while moving.
|
|
123
|
+
const parts = ['plain', 'punct'].includes(tok.type)
|
|
124
|
+
? tok.text.match(/\s+|[A-Za-z_$][\w$]*|===|!==|=>|==|!=|<=|>=|&&|\|\||\?\?|\.\.\.|->|::|[^\s]/g) || [] : [tok.text];
|
|
125
|
+
for (const text of parts) {
|
|
126
|
+
if (/\S/.test(text)) tokens.push({ text, type: tok.type, row, column });
|
|
127
|
+
column += text.length;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
return tokens;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Indices into the original sequences, with whitespace left out of the LCS.
|
|
135
|
+
export function matchTokens(before, after) {
|
|
136
|
+
const a = before.map((t, i) => i).filter((i) => /\S/.test(before[i].text));
|
|
137
|
+
const b = after.map((t, i) => i).filter((i) => /\S/.test(after[i].text));
|
|
138
|
+
const same = (i, j) => before[a[i]].text === after[b[j]].text && before[a[i]].type === after[b[j]].type;
|
|
139
|
+
const cols = b.length + 1;
|
|
140
|
+
const lengths = new Uint32Array((a.length + 1) * cols);
|
|
141
|
+
for (let i = a.length - 1; i >= 0; i--) {
|
|
142
|
+
for (let j = b.length - 1; j >= 0; j--) {
|
|
143
|
+
lengths[i * cols + j] = same(i, j) ? 1 + lengths[(i + 1) * cols + j + 1]
|
|
144
|
+
: Math.max(lengths[(i + 1) * cols + j], lengths[i * cols + j + 1]);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const pairs = [];
|
|
148
|
+
let i = 0;
|
|
149
|
+
let j = 0;
|
|
150
|
+
while (i < a.length && j < b.length) {
|
|
151
|
+
if (same(i, j)) { pairs.push([a[i++], b[j++]]); }
|
|
152
|
+
else if (lengths[(i + 1) * cols + j] >= lengths[i * cols + j + 1]) i++;
|
|
153
|
+
else j++;
|
|
154
|
+
}
|
|
155
|
+
return pairs;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function resolveCodeSteps(steps, o, time) {
|
|
159
|
+
if (!steps.length) throw new Error('code needs at least one step');
|
|
160
|
+
let highlight = o.highlight;
|
|
161
|
+
return steps.map((step, i) => {
|
|
162
|
+
if (!step || typeof step.code !== 'string') throw new Error(`code step ${i + 1} needs a code string`);
|
|
163
|
+
if (i && step.at === undefined) throw new Error(`code step ${i + 1} needs an at time`);
|
|
164
|
+
const at = time(step.at ?? o.at ?? 0);
|
|
165
|
+
if (!Number.isFinite(at)) throw new Error(`code step ${i + 1} needs a finite at time`);
|
|
166
|
+
if (step.highlight !== undefined) highlight = step.highlight;
|
|
167
|
+
return { at, lines: step.code.replace(/\t/g, ' ').split('\n'), highlight };
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function codeMorphState(steps, t, dur = 0.8, pace = 1) {
|
|
172
|
+
let to = 0;
|
|
173
|
+
for (let i = 1; i < steps.length; i++) if (t >= steps[i].at) to = i;
|
|
174
|
+
const from = Math.max(0, to - 1);
|
|
175
|
+
const raw = to ? (dur > 0 ? clamp((t - steps[to].at) / (dur / pace)) : 1) : 1;
|
|
176
|
+
const p = ease.inOut(raw);
|
|
177
|
+
return { from, to, p, removed: 1 - ease.inOut(clamp(raw * 2)), added: ease.inOut(clamp(raw * 2 - 1)),
|
|
178
|
+
lines: lerp(steps[from].lines.length, steps[to].lines.length, p) };
|
|
179
|
+
}
|