@biunow/agent-biutalk 0.0.0-stage → 1.1.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/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,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "explainroo",
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Narrated explainer videos made by your coding agent. Pure JavaScript, local voice, no API keys.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"video",
|
|
7
|
+
"explainer",
|
|
8
|
+
"text-to-speech",
|
|
9
|
+
"kokoro",
|
|
10
|
+
"whisper",
|
|
11
|
+
"animation",
|
|
12
|
+
"canvas",
|
|
13
|
+
"claude-code",
|
|
14
|
+
"codex",
|
|
15
|
+
"agents",
|
|
16
|
+
"motion-graphics",
|
|
17
|
+
"ffmpeg"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/vincentsch/explainroo#readme",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/vincentsch/explainroo/issues"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"bin": {
|
|
25
|
+
"explainroo": "bin/explainroo.js"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"bin/",
|
|
29
|
+
"src/",
|
|
30
|
+
"engine/",
|
|
31
|
+
"fonts/",
|
|
32
|
+
"templates/",
|
|
33
|
+
"skills/",
|
|
34
|
+
"AGENTS.md",
|
|
35
|
+
"CLAUDE.md",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=20.11"
|
|
41
|
+
},
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"author": "Vincent Schmalbach",
|
|
44
|
+
"repository": "github:vincentsch/explainroo",
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "node --test test/*.test.js",
|
|
47
|
+
"build:icons": "node scripts/build-icons.mjs",
|
|
48
|
+
"build:fonts": "node scripts/fetch-fonts.mjs"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@huggingface/transformers": "3.8.1",
|
|
52
|
+
"harfbuzzjs": "1.6.2",
|
|
53
|
+
"kokoro-js": "1.2.1",
|
|
54
|
+
"mathjax": "4.1.3",
|
|
55
|
+
"opencc-js": "1.4.2",
|
|
56
|
+
"phonemizer": "1.2.1",
|
|
57
|
+
"pinyin-pro": "3.29.4",
|
|
58
|
+
"playwright-core": "1.63.0",
|
|
59
|
+
"roughjs": "4.6.6"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"lucide-static": "1.48.0"
|
|
63
|
+
},
|
|
64
|
+
"overrides": {
|
|
65
|
+
"sharp": "^0.35.5"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Builds engine/icons/lucide.json from the lucide-static dev dependency.
|
|
3
|
+
// Every icon becomes a list of SVG path strings in a 24x24 box, so the engine
|
|
4
|
+
// can draw it with Path2D (clean looks) or rough.js (hand-drawn looks).
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
|
|
9
|
+
const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
10
|
+
const src = path.join(root, 'node_modules', 'lucide-static');
|
|
11
|
+
const nodes = JSON.parse(fs.readFileSync(path.join(src, 'icon-nodes.json'), 'utf8'));
|
|
12
|
+
const tags = JSON.parse(fs.readFileSync(path.join(src, 'tags.json'), 'utf8'));
|
|
13
|
+
const version = JSON.parse(fs.readFileSync(path.join(src, 'package.json'), 'utf8')).version;
|
|
14
|
+
|
|
15
|
+
const n = (v) => Number(v ?? 0);
|
|
16
|
+
const r3 = (v) => Math.round(v * 1000) / 1000;
|
|
17
|
+
|
|
18
|
+
function ellipsePath(cx, cy, rx, ry) {
|
|
19
|
+
return `M${r3(cx - rx)} ${r3(cy)}a${r3(rx)} ${r3(ry)} 0 1 0 ${r3(2 * rx)} 0a${r3(rx)} ${r3(ry)} 0 1 0 ${r3(-2 * rx)} 0`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function rectPath(x, y, w, h, rx, ry) {
|
|
23
|
+
if (!rx && !ry) return `M${x} ${y}h${w}v${h}h${-w}Z`;
|
|
24
|
+
rx = Math.min(rx || ry, w / 2);
|
|
25
|
+
ry = Math.min(ry || rx, h / 2);
|
|
26
|
+
return `M${r3(x + rx)} ${y}h${r3(w - 2 * rx)}a${rx} ${ry} 0 0 1 ${rx} ${ry}v${r3(h - 2 * ry)}a${rx} ${ry} 0 0 1 ${-rx} ${ry}h${r3(-(w - 2 * rx))}a${rx} ${ry} 0 0 1 ${-rx} ${-ry}v${r3(-(h - 2 * ry))}a${rx} ${ry} 0 0 1 ${rx} ${-ry}Z`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function pointsPath(points, close) {
|
|
30
|
+
const p = String(points).trim().split(/[\s,]+/).map(Number);
|
|
31
|
+
let d = `M${p[0]} ${p[1]}`;
|
|
32
|
+
for (let i = 2; i < p.length; i += 2) d += `L${p[i]} ${p[i + 1]}`;
|
|
33
|
+
return close ? d + 'Z' : d;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function toPath([tag, a]) {
|
|
37
|
+
switch (tag) {
|
|
38
|
+
case 'path': return a.d;
|
|
39
|
+
case 'circle': return ellipsePath(n(a.cx), n(a.cy), n(a.r), n(a.r));
|
|
40
|
+
case 'ellipse': return ellipsePath(n(a.cx), n(a.cy), n(a.rx), n(a.ry));
|
|
41
|
+
case 'rect': return rectPath(n(a.x), n(a.y), n(a.width), n(a.height), n(a.rx), n(a.ry));
|
|
42
|
+
case 'line': return `M${n(a.x1)} ${n(a.y1)}L${n(a.x2)} ${n(a.y2)}`;
|
|
43
|
+
case 'polyline': return pointsPath(a.points, false);
|
|
44
|
+
case 'polygon': return pointsPath(a.points, true);
|
|
45
|
+
default: throw new Error(`unsupported lucide element <${tag}>`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const out = {};
|
|
50
|
+
for (const [name, list] of Object.entries(nodes)) out[name] = list.map(toPath);
|
|
51
|
+
|
|
52
|
+
// Older and alternative names ship as extra SVG files with the same drawing.
|
|
53
|
+
const body = (svg) => svg.replace(/<!--[\s\S]*?-->/g, '').replace(/<svg[^>]*>/, '').replace('</svg>', '').replace(/\s+/g, ' ').trim();
|
|
54
|
+
const byBody = new Map();
|
|
55
|
+
for (const name of Object.keys(out)) {
|
|
56
|
+
const file = path.join(src, 'icons', `${name}.svg`);
|
|
57
|
+
if (fs.existsSync(file)) byBody.set(body(fs.readFileSync(file, 'utf8')), name);
|
|
58
|
+
}
|
|
59
|
+
const aliases = {};
|
|
60
|
+
for (const f of fs.readdirSync(path.join(src, 'icons'))) {
|
|
61
|
+
const name = f.replace(/\.svg$/, '');
|
|
62
|
+
if (out[name]) continue;
|
|
63
|
+
const target = byBody.get(body(fs.readFileSync(path.join(src, 'icons', f), 'utf8')));
|
|
64
|
+
if (target) aliases[name] = target;
|
|
65
|
+
}
|
|
66
|
+
const outTags = {};
|
|
67
|
+
for (const name of Object.keys(out)) outTags[name] = tags[name] || [];
|
|
68
|
+
|
|
69
|
+
const dir = path.join(root, 'engine', 'icons');
|
|
70
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
71
|
+
fs.writeFileSync(path.join(dir, 'lucide.json'), JSON.stringify({ version, icons: out, aliases }));
|
|
72
|
+
fs.writeFileSync(path.join(dir, 'lucide-tags.json'), JSON.stringify(outTags));
|
|
73
|
+
fs.copyFileSync(path.join(src, 'LICENSE'), path.join(dir, 'LICENSE-lucide.txt'));
|
|
74
|
+
console.log(`wrote ${Object.keys(out).length} icons and ${Object.keys(aliases).length} aliases (lucide ${version})`);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Downloads the bundled OFL fonts from Google Fonts into fonts/ and writes
|
|
3
|
+
// fonts/fonts.json. The fonts are committed, so users never need this script;
|
|
4
|
+
// it exists so the bundle can be rebuilt or extended.
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
|
|
9
|
+
const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
10
|
+
const dir = path.join(root, 'fonts');
|
|
11
|
+
const UA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Safari/537.36';
|
|
12
|
+
|
|
13
|
+
export const FAMILIES = [
|
|
14
|
+
['Kalam', [400, 700]],
|
|
15
|
+
['Patrick Hand', [400]],
|
|
16
|
+
['Cabin Sketch', [400, 700]],
|
|
17
|
+
['Architects Daughter', [400]],
|
|
18
|
+
['Inter', [400, 600, 800]],
|
|
19
|
+
['Plus Jakarta Sans', [600, 800]],
|
|
20
|
+
['Space Grotesk', [500, 700]],
|
|
21
|
+
['JetBrains Mono', [400, 700]],
|
|
22
|
+
];
|
|
23
|
+
const SUBSETS = new Set(['latin', 'latin-ext']);
|
|
24
|
+
|
|
25
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
26
|
+
for (const f of fs.readdirSync(dir)) if (f.endsWith('.woff2')) fs.rmSync(path.join(dir, f));
|
|
27
|
+
const manifest = [];
|
|
28
|
+
const seen = new Map(); // variable fonts serve one file for several weights
|
|
29
|
+
for (const [family, weights] of FAMILIES) {
|
|
30
|
+
const url = `https://fonts.googleapis.com/css2?family=${encodeURIComponent(family)}:wght@${weights.join(';')}&display=block`;
|
|
31
|
+
const css = await (await fetch(url, { headers: { 'User-Agent': UA } })).text();
|
|
32
|
+
const blocks = css.split('/* ').slice(1);
|
|
33
|
+
for (const block of blocks) {
|
|
34
|
+
const subset = block.slice(0, block.indexOf(' */')).trim();
|
|
35
|
+
if (!SUBSETS.has(subset)) continue;
|
|
36
|
+
const weight = Number(/font-weight:\s*(\d+)/.exec(block)[1]);
|
|
37
|
+
const src = /src:\s*url\(([^)]+)\)/.exec(block)[1];
|
|
38
|
+
const range = /unicode-range:\s*([^;]+);/.exec(block)[1].trim();
|
|
39
|
+
if (seen.has(src)) {
|
|
40
|
+
manifest.push({ family, weight, subset, file: seen.get(src), unicodeRange: range });
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const file = `${family.replace(/\s+/g, '')}-${weight}-${subset}.woff2`;
|
|
44
|
+
const buf = Buffer.from(await (await fetch(src, { headers: { 'User-Agent': UA } })).arrayBuffer());
|
|
45
|
+
fs.writeFileSync(path.join(dir, file), buf);
|
|
46
|
+
seen.set(src, file);
|
|
47
|
+
manifest.push({ family, weight, subset, file, unicodeRange: range });
|
|
48
|
+
console.log(`${file} ${(buf.length / 1024).toFixed(1)} KB`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const [weight, style] of [[400, 'Regular'], [700, 'Bold']]) {
|
|
52
|
+
const file = `NotoSansDevanagari-${weight}.ttf`;
|
|
53
|
+
const res = await fetch(`https://raw.githubusercontent.com/notofonts/noto-fonts/main/hinted/ttf/NotoSansDevanagari/NotoSansDevanagari-${style}.ttf`);
|
|
54
|
+
if (!res.ok) throw new Error(`no ${file} (${res.status})`);
|
|
55
|
+
fs.writeFileSync(path.join(dir, file), Buffer.from(await res.arrayBuffer()));
|
|
56
|
+
manifest.push({ family: 'Noto Sans Devanagari', weight, subset: 'devanagari', file, unicodeRange: 'U+0900-097F,U+1CD0-1CFF,U+200C-200D,U+20B9,U+25CC,U+A8E0-A8FF' });
|
|
57
|
+
}
|
|
58
|
+
fs.writeFileSync(path.join(dir, 'fonts.json'), JSON.stringify(manifest, null, 2) + '\n');
|
|
59
|
+
|
|
60
|
+
// SIL Open Font License texts, one per family.
|
|
61
|
+
fs.mkdirSync(path.join(dir, 'licenses'), { recursive: true });
|
|
62
|
+
for (const [family] of [...FAMILIES, ['Noto Sans Devanagari']]) {
|
|
63
|
+
const slug = family.toLowerCase().replace(/\s+/g, '');
|
|
64
|
+
const res = await fetch(`https://raw.githubusercontent.com/google/fonts/main/ofl/${slug}/OFL.txt`);
|
|
65
|
+
if (!res.ok) throw new Error(`no OFL.txt for ${family} (${res.status})`);
|
|
66
|
+
fs.writeFileSync(path.join(dir, 'licenses', `${family.replace(/\s+/g, '')}-OFL.txt`), await res.text());
|
|
67
|
+
}
|
|
68
|
+
console.log(`${manifest.length} font files`);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: explainroo
|
|
3
|
+
description: Make an explainer video (MP4) with a voice-over using explainroo. The voice is made on this computer and no API key is needed. Use when the user asks for an explainer, how-to, walkthrough, launch or tutorial video, an animated diagram with a voice-over, or a short for YouTube, Reels or TikTok.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Make an explainer video with explainroo
|
|
7
|
+
|
|
8
|
+
explainroo makes an MP4 video from two files: a script with the words the
|
|
9
|
+
voice says, and a small JavaScript file that draws the pictures. A voice
|
|
10
|
+
model on this computer reads the script. explainroo writes down when each
|
|
11
|
+
word is spoken, Chrome draws the frames, and explainroo adds music and sound
|
|
12
|
+
effects.
|
|
13
|
+
|
|
14
|
+
## Find or install explainroo
|
|
15
|
+
|
|
16
|
+
Try these first:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
command -v explainroo
|
|
20
|
+
ls "${EXPLAINROO_HOME:-$HOME/.explainroo}/bin/explainroo.js"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
If neither exists, install it. It needs Node.js 20.11 or newer, ffmpeg, and
|
|
24
|
+
Chrome or Chromium.
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
git clone https://github.com/vincentsch/explainroo.git "$HOME/.explainroo"
|
|
28
|
+
cd "$HOME/.explainroo" && npm install
|
|
29
|
+
node bin/explainroo.js doctor --fetch
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
In the rest of this file, `explainroo` means whichever one you found: the
|
|
33
|
+
`explainroo` command, or `node "$HOME/.explainroo/bin/explainroo.js"`.
|
|
34
|
+
|
|
35
|
+
## Read AGENTS.md
|
|
36
|
+
|
|
37
|
+
`AGENTS.md` in the explainroo folder has the steps, the writing rules, the
|
|
38
|
+
settings and the Scene API, which is the list of everything `scenes.js` can
|
|
39
|
+
draw. Read it before you start.
|
|
40
|
+
|
|
41
|
+
## Make the video
|
|
42
|
+
|
|
43
|
+
Create the project where the user wants it, for example `./video`:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
explainroo init video --theme paper --title "..."
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Then follow the steps in AGENTS.md. When the video is done, tell the user
|
|
50
|
+
where `video/out/video.mp4` is, how long it is, and what
|
|
51
|
+
`explainroo verify video` said.
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
// Aligns the words we asked the voice model to say with the words Whisper
|
|
2
|
+
// heard, so every script word gets a start and end time.
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { normWord } from './script.js';
|
|
5
|
+
import { japaneseReadings } from './phonemes.js';
|
|
6
|
+
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
|
|
9
|
+
// Whisper often writes Mandarin in Traditional characters, even when the
|
|
10
|
+
// script is Simplified. Both sides are compared in Simplified.
|
|
11
|
+
let toSimplified;
|
|
12
|
+
function simplified(text) {
|
|
13
|
+
toSimplified ??= require('opencc-js/t2cn').Converter({ from: 'tw', to: 'cn' });
|
|
14
|
+
return toSimplified(text);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Whisper also often writes another character with the same sound, like 直
|
|
18
|
+
// for 值. Mandarin characters match when they share a reading without tones.
|
|
19
|
+
const readings = new Map();
|
|
20
|
+
function soundsAlike(a, b) {
|
|
21
|
+
const as = Array.from(a);
|
|
22
|
+
const bs = Array.from(b);
|
|
23
|
+
if (as.length !== bs.length) return false;
|
|
24
|
+
const { pinyin } = require('pinyin-pro');
|
|
25
|
+
const of = c => {
|
|
26
|
+
if (!readings.has(c)) readings.set(c, /\p{Script=Han}/u.test(c) ? pinyin(c, { multiple: true, toneType: 'none', type: 'array' }) : [c]);
|
|
27
|
+
return readings.get(c);
|
|
28
|
+
};
|
|
29
|
+
return as.every((c, i) => c === bs[i] || of(c).some(r => of(bs[i]).includes(r)));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const katakana = text => text.replace(/[\u3041-\u3096]/g, c => String.fromCharCode(c.charCodeAt(0) + 0x60));
|
|
33
|
+
|
|
34
|
+
// Gives each piece of text the reading of its characters. A reading that
|
|
35
|
+
// covers several characters is shared out between them.
|
|
36
|
+
export function spreadReadings(pieces, tokens) {
|
|
37
|
+
const kana = Array.from(pieces.join(''), () => '');
|
|
38
|
+
for (const [start, end, reading] of tokens) {
|
|
39
|
+
const r = Array.from(katakana(reading));
|
|
40
|
+
const n = end - start;
|
|
41
|
+
for (let k = 0; k < n && start + k < kana.length; k++) kana[start + k] = r.slice(Math.round(k * r.length / n), Math.round((k + 1) * r.length / n)).join('');
|
|
42
|
+
}
|
|
43
|
+
let at = 0;
|
|
44
|
+
return pieces.map(p => {
|
|
45
|
+
const length = Array.from(p).length;
|
|
46
|
+
at += length;
|
|
47
|
+
return kana.slice(at - length, at).join('');
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Japanese has many words that sound the same, and Whisper often writes a
|
|
52
|
+
// word in kana that the script writes in kanji. When Python with misaki is
|
|
53
|
+
// there, both sides are compared by their reading.
|
|
54
|
+
export async function readAlike(words, heard, language) {
|
|
55
|
+
if (language !== 'ja' || !words.length || !heard.length) return { words, heard };
|
|
56
|
+
try {
|
|
57
|
+
const spoken = words.map(w => String(w.spoken));
|
|
58
|
+
const pieces = heard.map(h => String(h.text).trim());
|
|
59
|
+
const shown = words.map((w, i) => w.display !== undefined && String(w.display) !== spoken[i] ? String(w.display) : null);
|
|
60
|
+
const extra = shown.filter(s => s !== null);
|
|
61
|
+
const [a, b, ...c] = await japaneseReadings([spoken.join(''), pieces.join(''), ...extra]);
|
|
62
|
+
const spokenKana = spreadReadings(spoken, a);
|
|
63
|
+
const heardKana = spreadReadings(pieces, b);
|
|
64
|
+
let k = 0;
|
|
65
|
+
const shownKana = shown.map(s => s === null ? null : spreadReadings([s], c[k++])[0]);
|
|
66
|
+
return {
|
|
67
|
+
words: words.map((w, i) => ({ ...w, spoken: spokenKana[i] || w.spoken, display: shown[i] === null ? undefined : shownKana[i] || w.display })),
|
|
68
|
+
heard: heard.map((h, i) => ({ ...h, text: heardKana[i] || h.text })),
|
|
69
|
+
};
|
|
70
|
+
} catch {
|
|
71
|
+
return { words, heard };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const NUMBER_WORDS = {
|
|
76
|
+
zero: '0', one: '1', two: '2', three: '3', four: '4', five: '5', six: '6', seven: '7', eight: '8', nine: '9',
|
|
77
|
+
ten: '10', eleven: '11', twelve: '12', thirteen: '13', fourteen: '14', fifteen: '15', sixteen: '16',
|
|
78
|
+
seventeen: '17', eighteen: '18', nineteen: '19', twenty: '20', thirty: '30', forty: '40', fifty: '50',
|
|
79
|
+
sixty: '60', seventy: '70', eighty: '80', ninety: '90', hundred: '100', thousand: '1000',
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
function canon(w) {
|
|
83
|
+
const n = normWord(w).replace(/,/g, '');
|
|
84
|
+
return NUMBER_WORDS[n] ?? n;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const ONES = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
|
|
88
|
+
const TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
|
|
89
|
+
const SCALES = [[1e9, 'billion'], [1e6, 'million'], [1e3, 'thousand']];
|
|
90
|
+
const CURRENCY = { $: 'dollars', '€': 'euros', '£': 'pounds' };
|
|
91
|
+
|
|
92
|
+
// 1102 -> one thousand one hundred two (without "and"; "and" is optional below).
|
|
93
|
+
export function intWords(n) {
|
|
94
|
+
if (!Number.isFinite(n) || n < 0 || n >= 1e12) return [String(n)];
|
|
95
|
+
if (n < 20) return [ONES[n]];
|
|
96
|
+
if (n < 100) return [TENS[Math.floor(n / 10)], ...(n % 10 ? [ONES[n % 10]] : [])];
|
|
97
|
+
if (n < 1000) return [ONES[Math.floor(n / 100)], 'hundred', ...(n % 100 ? intWords(n % 100) : [])];
|
|
98
|
+
for (const [size, name] of SCALES) {
|
|
99
|
+
if (n >= size) return [...intWords(Math.floor(n / size)), name, ...(n % size ? intWords(n % size) : [])];
|
|
100
|
+
}
|
|
101
|
+
return [String(n)];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Kokoro reads a lone four-digit number like a year: 1582 "fifteen eighty
|
|
105
|
+
// two", 1900 "nineteen hundred", 1905 "nineteen oh five". Numbers below 1100
|
|
106
|
+
// or with fewer than ten in the last three digits (2000, 2005) stay cardinal.
|
|
107
|
+
export function yearWords(n) {
|
|
108
|
+
if (n < 1100 || n > 9999 || n % 1000 < 10) return null;
|
|
109
|
+
const hi = Math.floor(n / 100);
|
|
110
|
+
const lo = n % 100;
|
|
111
|
+
if (n % 1000 >= 100 && lo === 0) return [...intWords(hi), 'hundred'];
|
|
112
|
+
if (n % 1000 >= 100 && lo < 10) return [...intWords(hi), 'oh', ...intWords(lo)];
|
|
113
|
+
return [...intWords(hi), ...intWords(lo)];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const ORDINALS = { one: 'first', two: 'second', three: 'third', five: 'fifth', eight: 'eighth', nine: 'ninth', twelve: 'twelfth' };
|
|
117
|
+
|
|
118
|
+
// 29th -> twenty ninth, 101st -> one hundred first.
|
|
119
|
+
function ordinalWords(n) {
|
|
120
|
+
const words = intWords(n);
|
|
121
|
+
const last = words.pop();
|
|
122
|
+
const ord = ORDINALS[last] ?? (last.endsWith('y') ? last.slice(0, -1) + 'ieth' : last + 'th');
|
|
123
|
+
return [...words.map(canon), ord];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Turns one written word into the tokens a voice would say, in canonical form.
|
|
127
|
+
// Whisper writes "$1,050" and "5%" where the voice said "one thousand and
|
|
128
|
+
// fifty dollars" and "five percent", and "example.com" or "1.1.1.1" where it
|
|
129
|
+
// said words joined by "dot". Both sides are expanded the same way.
|
|
130
|
+
export function expand(word, language = 'en') {
|
|
131
|
+
if (language !== 'en') {
|
|
132
|
+
const n = normWord(word).replace(/[०-९]/g, d => String(d.charCodeAt(0) - 0x0966));
|
|
133
|
+
const digits = /^[\p{N}%$€£+.]+$/u.test(n) ? n.replace(/[^\p{N}]/gu, '') : '';
|
|
134
|
+
return n ? [digits || n] : [];
|
|
135
|
+
}
|
|
136
|
+
const raw = String(word).trim().toLowerCase();
|
|
137
|
+
// Whisper splits "1.1.1.1" into "1", ".1", ".1", ".1": a leading dot is a spoken "dot".
|
|
138
|
+
const lead = /^\.[a-z0-9]/i.test(raw) ? ['dot'] : [];
|
|
139
|
+
const n = canon(word);
|
|
140
|
+
const ordinal = /^(\d+)(st|nd|rd|th)$/.exec(n);
|
|
141
|
+
if (ordinal && !/\d,\d/.test(raw)) return [...lead, ...ordinalWords(Number(ordinal[1]))];
|
|
142
|
+
const money = /^([$€£])?(\d+)(?:\.(\d+))?(%)?$/.exec(n);
|
|
143
|
+
if (money && /\d/.test(raw) && !/^\d+\.\d+\.\d+/.test(n)) {
|
|
144
|
+
const [, cur, int, dec, pct] = money;
|
|
145
|
+
const year = !cur && dec === undefined && !pct && /^\d{4}$/.test(int) && !/\d,\d/.test(raw) ? yearWords(Number(int)) : null;
|
|
146
|
+
if (year) return [...lead, ...year].map(canon);
|
|
147
|
+
const out = [...lead, ...intWords(Number(int))];
|
|
148
|
+
if (dec !== undefined) {
|
|
149
|
+
if (cur) out.push(CURRENCY[cur], ...intWords(Number(dec)), 'cents');
|
|
150
|
+
else out.push('point', ...dec.split('').map((d) => ONES[Number(d)]));
|
|
151
|
+
} else if (cur) out.push(CURRENCY[cur]);
|
|
152
|
+
if (pct) out.push('percent');
|
|
153
|
+
return out.map(canon);
|
|
154
|
+
}
|
|
155
|
+
if (!/[a-z0-9]\.[a-z0-9]/.test(n)) return n ? [...lead, n] : lead;
|
|
156
|
+
const parts = n.split('.').filter(Boolean).map((p) => NUMBER_WORDS[p] ?? p);
|
|
157
|
+
const out = [...lead];
|
|
158
|
+
parts.forEach((p, i) => {
|
|
159
|
+
if (i) out.push('dot');
|
|
160
|
+
out.push(p);
|
|
161
|
+
});
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const isNumberToken = (t) => /^\d+$/.test(t) || ['hundred', 'thousand', 'million', 'billion', '100', '1000'].includes(t);
|
|
166
|
+
|
|
167
|
+
function editDistance(a, b) {
|
|
168
|
+
if (a === b) return 0;
|
|
169
|
+
const m = a.length;
|
|
170
|
+
const n = b.length;
|
|
171
|
+
if (!m) return n;
|
|
172
|
+
if (!n) return m;
|
|
173
|
+
let prev = Array.from({ length: n + 1 }, (_, j) => j);
|
|
174
|
+
for (let i = 1; i <= m; i++) {
|
|
175
|
+
const cur = [i];
|
|
176
|
+
for (let j = 1; j <= n; j++) {
|
|
177
|
+
cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
178
|
+
}
|
|
179
|
+
prev = cur;
|
|
180
|
+
}
|
|
181
|
+
return prev[n];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function similar(a, b) {
|
|
185
|
+
if (!a || !b) return false;
|
|
186
|
+
if (a === b) return true;
|
|
187
|
+
const len = Math.max(a.length, b.length);
|
|
188
|
+
if (len <= 3) return false;
|
|
189
|
+
return editDistance(a, b) / len <= 0.34;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// words: [{ spoken, display? }] in order. heard: [{ text, start, end }] from Whisper.
|
|
193
|
+
// Returns per-word { start, end, matched } and the share of spoken tokens matched.
|
|
194
|
+
// A word also counts as heard when Whisper writes its shown form, so
|
|
195
|
+
// {SQL|sequel} or {async|a sink} is confirmed by "SQL" or "async".
|
|
196
|
+
export function alignWords(words, heard, duration, language = 'en') {
|
|
197
|
+
const cjk = ['ja', 'zh'].includes(language);
|
|
198
|
+
const chars = word => normWord(word).normalize('NFC');
|
|
199
|
+
const partsOf = word => cjk ? Array.from(language === 'zh' ? simplified(chars(word)) : chars(word)) : expand(word, language);
|
|
200
|
+
const same = (a, b) => a === b || (language === 'zh' && soundsAlike(a, b));
|
|
201
|
+
const matches = (a, b) => cjk ? same(a, b) : language !== 'en' && (/\p{N}/u.test(a) || /\p{N}/u.test(b)) ? a === b : similar(a, b);
|
|
202
|
+
const tokens = [];
|
|
203
|
+
words.forEach((w, wi) => {
|
|
204
|
+
const parts = String(w.spoken).split(/\s+/).flatMap(partsOf);
|
|
205
|
+
(parts.length ? parts : ['']).forEach((p, k) => {
|
|
206
|
+
// "and" inside a spoken number ("one thousand and fifty") is optional.
|
|
207
|
+
const prev = tokens[tokens.length - 1];
|
|
208
|
+
const optional = language === 'en' && p === 'and' && prev && isNumberToken(prev.norm);
|
|
209
|
+
tokens.push({ wi, norm: p, first: k === 0, last: k === parts.length - 1 || !parts.length, optional });
|
|
210
|
+
});
|
|
211
|
+
const shown = w.display === undefined ? '' : String(w.display).split(/\s+/).flatMap(partsOf).join('');
|
|
212
|
+
const first = tokens.findIndex((t) => t.wi === wi);
|
|
213
|
+
if (shown && shown !== parts.join('')) tokens[first].shown = shown;
|
|
214
|
+
});
|
|
215
|
+
// Whisper splits numbers into pieces ("$1", ",000" and "5", "%" and "1",
|
|
216
|
+
// ".1"). Join them back before comparing.
|
|
217
|
+
const joined = [];
|
|
218
|
+
for (const h of heard) {
|
|
219
|
+
const prev = joined[joined.length - 1];
|
|
220
|
+
const t = String(h.text).trim();
|
|
221
|
+
const glue = prev && ((/^[,.]\d/.test(t) && /\d$/.test(prev.text)) || (/^%/.test(t) && /\d$/.test(prev.text)) || (/^\d/.test(t) && /^[$€£]$/.test(prev.text)));
|
|
222
|
+
if (glue) {
|
|
223
|
+
prev.text += t;
|
|
224
|
+
prev.end = h.end;
|
|
225
|
+
} else {
|
|
226
|
+
joined.push({ ...h, text: t });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const H = [];
|
|
230
|
+
for (const h of joined) {
|
|
231
|
+
const parts = partsOf(h.text);
|
|
232
|
+
const step = (h.end - h.start) / Math.max(1, parts.length);
|
|
233
|
+
parts.forEach((p, k) => H.push({ text: p, norm: p, start: h.start + k * step, end: h.start + (k + 1) * step }));
|
|
234
|
+
}
|
|
235
|
+
const T = tokens.length;
|
|
236
|
+
const N = H.length;
|
|
237
|
+
|
|
238
|
+
// Dynamic programming over (token i, heard j). Moves:
|
|
239
|
+
// match 1:1, skip a token, skip a heard word, a whole multi-token word to one
|
|
240
|
+
// heard word ("D N S" vs "DNS"), and one token to two heard words.
|
|
241
|
+
const INF = 1e9;
|
|
242
|
+
const cost = Array.from({ length: T + 1 }, () => new Float64Array(N + 1).fill(INF));
|
|
243
|
+
const move = Array.from({ length: T + 1 }, () => new Int8Array(N + 1));
|
|
244
|
+
const shownLength = cjk ? Array.from({ length: T + 1 }, () => new Int32Array(N + 1)) : null;
|
|
245
|
+
cost[0][0] = 0;
|
|
246
|
+
const groupEnd = new Int32Array(T);
|
|
247
|
+
for (let i = 0; i < T; i++) {
|
|
248
|
+
let e = i;
|
|
249
|
+
while (e + 1 < T && tokens[e + 1].wi === tokens[i].wi) e++;
|
|
250
|
+
groupEnd[i] = e;
|
|
251
|
+
}
|
|
252
|
+
for (let i = 0; i <= T; i++) {
|
|
253
|
+
for (let j = 0; j <= N; j++) {
|
|
254
|
+
const c = cost[i][j];
|
|
255
|
+
if (c >= INF) continue;
|
|
256
|
+
if (i < T && c + (tokens[i].optional ? 0 : 1) < cost[i + 1][j]) { cost[i + 1][j] = c + (tokens[i].optional ? 0 : 1); move[i + 1][j] = 2; }
|
|
257
|
+
if (j < N && c + 1 < cost[i][j + 1]) { cost[i][j + 1] = c + 1; move[i][j + 1] = 3; }
|
|
258
|
+
if (i < T && j < N) {
|
|
259
|
+
const ok = matches(tokens[i].norm, H[j].norm);
|
|
260
|
+
const v = c + (ok ? 0 : 1.6);
|
|
261
|
+
if (v < cost[i + 1][j + 1]) { cost[i + 1][j + 1] = v; move[i + 1][j + 1] = ok ? 1 : 4; }
|
|
262
|
+
}
|
|
263
|
+
if (i < T && j < N && tokens[i].first && (groupEnd[i] > i || tokens[i].shown)) {
|
|
264
|
+
const e = groupEnd[i];
|
|
265
|
+
const joined = tokens.slice(i, e + 1).map((t) => t.norm).join('');
|
|
266
|
+
const ok = (groupEnd[i] > i && matches(joined, H[j].norm)) || (tokens[i].shown && matches(tokens[i].shown, H[j].norm));
|
|
267
|
+
if (ok && c < cost[e + 1][j + 1]) { cost[e + 1][j + 1] = c; move[e + 1][j + 1] = 5; }
|
|
268
|
+
}
|
|
269
|
+
if (cjk && i < T && tokens[i].first && tokens[i].shown) {
|
|
270
|
+
const length = Array.from(tokens[i].shown).length;
|
|
271
|
+
const e = groupEnd[i];
|
|
272
|
+
if (j + length <= N && same(tokens[i].shown, H.slice(j, j + length).map(h => h.norm).join('')) && c < cost[e + 1][j + length]) {
|
|
273
|
+
cost[e + 1][j + length] = c;
|
|
274
|
+
move[e + 1][j + length] = 7;
|
|
275
|
+
shownLength[e + 1][j + length] = length;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (i < T && j + 1 < N && matches(tokens[i].norm, H[j].norm + H[j + 1].norm) && c < cost[i + 1][j + 2]) {
|
|
279
|
+
cost[i + 1][j + 2] = c;
|
|
280
|
+
move[i + 1][j + 2] = 6;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const spans = words.map(() => null);
|
|
286
|
+
const hits = words.map(() => 0);
|
|
287
|
+
const need = words.map(() => 0);
|
|
288
|
+
for (const t of tokens) if (!t.optional) need[t.wi]++;
|
|
289
|
+
const add = (wi, s, e, n = 1) => {
|
|
290
|
+
hits[wi] += n;
|
|
291
|
+
const cur = spans[wi];
|
|
292
|
+
spans[wi] = cur ? { start: Math.min(cur.start, s), end: Math.max(cur.end, e) } : { start: s, end: e };
|
|
293
|
+
};
|
|
294
|
+
let matched = 0;
|
|
295
|
+
let i = T;
|
|
296
|
+
let j = N;
|
|
297
|
+
while (i > 0 || j > 0) {
|
|
298
|
+
const m = move[i][j];
|
|
299
|
+
if (m === 1) { add(tokens[i - 1].wi, H[j - 1].start, H[j - 1].end); matched++; i--; j--; }
|
|
300
|
+
else if (m === 4) { i--; j--; }
|
|
301
|
+
else if (m === 2) { i--; }
|
|
302
|
+
else if (m === 3) { j--; }
|
|
303
|
+
else if (m === 5) {
|
|
304
|
+
let s = i - 1;
|
|
305
|
+
while (s > 0 && tokens[s - 1].wi === tokens[i - 1].wi) s--;
|
|
306
|
+
add(tokens[i - 1].wi, H[j - 1].start, H[j - 1].end, i - s);
|
|
307
|
+
matched += i - s;
|
|
308
|
+
i = s;
|
|
309
|
+
j--;
|
|
310
|
+
} else if (m === 7) {
|
|
311
|
+
const length = shownLength[i][j];
|
|
312
|
+
let s = i - 1;
|
|
313
|
+
while (s > 0 && tokens[s - 1].wi === tokens[i - 1].wi) s--;
|
|
314
|
+
add(tokens[i - 1].wi, H[j - length].start, H[j - 1].end, i - s);
|
|
315
|
+
matched += i - s;
|
|
316
|
+
i = s;
|
|
317
|
+
j -= length;
|
|
318
|
+
} else if (m === 6) { add(tokens[i - 1].wi, H[j - 2].start, H[j - 1].end); matched++; i--; j -= 2; }
|
|
319
|
+
else break;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Interpolate words Whisper did not confirm, by character length, between
|
|
323
|
+
// the nearest confirmed neighbours.
|
|
324
|
+
// A word counts as confirmed only when every part of it was heard, so
|
|
325
|
+
// "example.com" read as "example comm" is reported.
|
|
326
|
+
const out = words.map((w, wi) => ({ start: 0, end: 0, matched: !!spans[wi] && hits[wi] >= need[wi] }));
|
|
327
|
+
const weight = words.map((w) => Math.max(1, String(w.spoken).length));
|
|
328
|
+
let k = 0;
|
|
329
|
+
while (k < words.length) {
|
|
330
|
+
if (spans[k]) {
|
|
331
|
+
out[k].start = spans[k].start;
|
|
332
|
+
out[k].end = Math.max(spans[k].end, spans[k].start + 0.05);
|
|
333
|
+
k++;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
let e = k;
|
|
337
|
+
while (e < words.length && !spans[e]) e++;
|
|
338
|
+
const from = k > 0 ? out[k - 1].end : 0;
|
|
339
|
+
const to = e < words.length ? spans[e].start : duration;
|
|
340
|
+
const total = weight.slice(k, e).reduce((a, b) => a + b, 0);
|
|
341
|
+
let t = from;
|
|
342
|
+
for (let q = k; q < e; q++) {
|
|
343
|
+
const d = ((to - from) * weight[q]) / total;
|
|
344
|
+
out[q].start = t;
|
|
345
|
+
out[q].end = t + d;
|
|
346
|
+
t += d;
|
|
347
|
+
}
|
|
348
|
+
k = e;
|
|
349
|
+
}
|
|
350
|
+
// Keep times monotonic.
|
|
351
|
+
for (let q = 1; q < out.length; q++) {
|
|
352
|
+
if (out[q].start < out[q - 1].start) out[q].start = out[q - 1].start;
|
|
353
|
+
if (out[q].end < out[q].start) out[q].end = out[q].start + 0.05;
|
|
354
|
+
}
|
|
355
|
+
const counted = tokens.filter((t) => !t.optional).length;
|
|
356
|
+
return { words: out, matchRate: counted ? Math.min(1, matched / counted) : 1 };
|
|
357
|
+
}
|