canvas-recorder-lib 0.8.1 → 0.8.4
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/README.md +10 -2
- package/dev/canvas-recorder.css +20 -6
- package/dev/canvas-recorder.ts +389 -54
- package/dev/color-picker.ts +13 -0
- package/dev/component-picker.ts +60 -11
- package/dev/delay-files.ts +3 -1
- package/dev/font-widgets.ts +2 -2
- package/dev/format-editor.ts +3 -1
- package/dev/fourier-panel.ts +7 -3
- package/dev/handwriting-panel.ts +7 -7
- package/dev/multi-text-dialog.ts +28 -15
- package/dev/player-body.ts +78 -33
- package/dev/schedule-helpers.ts +10 -3
- package/dev/static-image-panel.ts +4 -4
- package/dev/subtitles.ts +170 -0
- package/dev/synced-file.ts +34 -0
- package/dev/timeline-display.ts +1 -1
- package/dev/transcribe-core.ts +7 -4
- package/dev/transcript-align.ts +24 -18
- package/dev/transcript-editor.ts +1187 -0
- package/dev/transcript-export.ts +90 -0
- package/dev/transcript-view.ts +8 -5
- package/dev/transcripts.ts +140 -15
- package/dev/video-clip-panel.ts +4 -4
- package/dist/dev/canvas-recorder.css +20 -6
- package/dist/dev/canvas-recorder.js +314 -48
- package/dist/dev/canvas-recorder.js.map +1 -1
- package/dist/dev/color-picker.d.ts.map +1 -1
- package/dist/dev/color-picker.js +15 -0
- package/dist/dev/color-picker.js.map +1 -1
- package/dist/dev/component-picker.d.ts +2 -1
- package/dist/dev/component-picker.d.ts.map +1 -1
- package/dist/dev/component-picker.js +56 -11
- package/dist/dev/component-picker.js.map +1 -1
- package/dist/dev/delay-files.d.ts.map +1 -1
- package/dist/dev/delay-files.js +3 -1
- package/dist/dev/delay-files.js.map +1 -1
- package/dist/dev/font-widgets.d.ts +2 -2
- package/dist/dev/font-widgets.d.ts.map +1 -1
- package/dist/dev/font-widgets.js.map +1 -1
- package/dist/dev/format-editor.d.ts.map +1 -1
- package/dist/dev/format-editor.js +3 -1
- package/dist/dev/format-editor.js.map +1 -1
- package/dist/dev/fourier-panel.d.ts.map +1 -1
- package/dist/dev/fourier-panel.js +4 -3
- package/dist/dev/fourier-panel.js.map +1 -1
- package/dist/dev/handwriting-panel.d.ts +4 -4
- package/dist/dev/handwriting-panel.d.ts.map +1 -1
- package/dist/dev/handwriting-panel.js.map +1 -1
- package/dist/dev/multi-text-dialog.d.ts.map +1 -1
- package/dist/dev/multi-text-dialog.js +23 -12
- package/dist/dev/multi-text-dialog.js.map +1 -1
- package/dist/dev/player-body.d.ts +1 -1
- package/dist/dev/player-body.d.ts.map +1 -1
- package/dist/dev/player-body.js +78 -33
- package/dist/dev/player-body.js.map +1 -1
- package/dist/dev/schedule-helpers.d.ts.map +1 -1
- package/dist/dev/schedule-helpers.js +4 -2
- package/dist/dev/schedule-helpers.js.map +1 -1
- package/dist/dev/static-image-panel.d.ts +3 -3
- package/dist/dev/static-image-panel.d.ts.map +1 -1
- package/dist/dev/subtitles.d.ts +36 -0
- package/dist/dev/subtitles.d.ts.map +1 -0
- package/dist/dev/subtitles.js +135 -0
- package/dist/dev/subtitles.js.map +1 -0
- package/dist/dev/synced-file.d.ts +14 -0
- package/dist/dev/synced-file.d.ts.map +1 -1
- package/dist/dev/synced-file.js +22 -0
- package/dist/dev/synced-file.js.map +1 -1
- package/dist/dev/timeline-display.d.ts +5 -0
- package/dist/dev/timeline-display.d.ts.map +1 -1
- package/dist/dev/timeline-display.js +1 -1
- package/dist/dev/timeline-display.js.map +1 -1
- package/dist/dev/transcribe-core.d.ts.map +1 -1
- package/dist/dev/transcribe-core.js +7 -4
- package/dist/dev/transcribe-core.js.map +1 -1
- package/dist/dev/transcript-align.d.ts +6 -4
- package/dist/dev/transcript-align.d.ts.map +1 -1
- package/dist/dev/transcript-align.js +18 -13
- package/dist/dev/transcript-align.js.map +1 -1
- package/dist/dev/transcript-editor.d.ts +36 -0
- package/dist/dev/transcript-editor.d.ts.map +1 -0
- package/dist/dev/transcript-editor.js +948 -0
- package/dist/dev/transcript-editor.js.map +1 -0
- package/dist/dev/transcript-export.d.ts +21 -0
- package/dist/dev/transcript-export.d.ts.map +1 -1
- package/dist/dev/transcript-export.js +65 -0
- package/dist/dev/transcript-export.js.map +1 -1
- package/dist/dev/transcript-view.d.ts +2 -0
- package/dist/dev/transcript-view.d.ts.map +1 -1
- package/dist/dev/transcript-view.js +1 -1
- package/dist/dev/transcript-view.js.map +1 -1
- package/dist/dev/transcripts.d.ts +18 -1
- package/dist/dev/transcripts.d.ts.map +1 -1
- package/dist/dev/transcripts.js +102 -12
- package/dist/dev/transcripts.js.map +1 -1
- package/dist/dev/video-clip-panel.d.ts +3 -3
- package/dist/dev/video-clip-panel.d.ts.map +1 -1
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +1 -0
- package/dist/lib/index.js.map +1 -1
- package/dist/src/glib/line-font.d.ts +6 -0
- package/dist/src/glib/line-font.d.ts.map +1 -1
- package/dist/src/glib/line-font.js +9 -0
- package/dist/src/glib/line-font.js.map +1 -1
- package/dist/src/glib/path-shape.d.ts +23 -0
- package/dist/src/glib/path-shape.d.ts.map +1 -1
- package/dist/src/glib/path-shape.js +66 -221
- package/dist/src/glib/path-shape.js.map +1 -1
- package/dist/src/glib/svg-arc.d.ts +41 -0
- package/dist/src/glib/svg-arc.d.ts.map +1 -0
- package/dist/src/glib/svg-arc.js +114 -0
- package/dist/src/glib/svg-arc.js.map +1 -0
- package/dist/src/glib/svg-path-parser.d.ts +60 -0
- package/dist/src/glib/svg-path-parser.d.ts.map +1 -0
- package/dist/src/glib/svg-path-parser.js +258 -0
- package/dist/src/glib/svg-path-parser.js.map +1 -0
- package/dist/src/glib/transforms.d.ts +9 -0
- package/dist/src/glib/transforms.d.ts.map +1 -1
- package/dist/src/glib/transforms.js +7 -4
- package/dist/src/glib/transforms.js.map +1 -1
- package/dist/src/interpolate.d.ts +17 -10
- package/dist/src/interpolate.d.ts.map +1 -1
- package/dist/src/interpolate.js +1 -17
- package/dist/src/interpolate.js.map +1 -1
- package/dist/src/morph-animation.js +1 -1
- package/dist/src/morph-animation.js.map +1 -1
- package/dist/src/peano-fourier/fourier-shared.d.ts.map +1 -1
- package/dist/src/peano-fourier/fourier-shared.js +4 -2
- package/dist/src/peano-fourier/fourier-shared.js.map +1 -1
- package/dist/src/schedule-helper.d.ts +50 -28
- package/dist/src/schedule-helper.d.ts.map +1 -1
- package/dist/src/schedule-helper.js +22 -0
- package/dist/src/schedule-helper.js.map +1 -1
- package/dist/src/showable.d.ts +57 -10
- package/dist/src/showable.d.ts.map +1 -1
- package/dist/src/showable.js +19 -0
- package/dist/src/showable.js.map +1 -1
- package/dist/src/slide-components/arrow.d.ts +4 -4
- package/dist/src/slide-components/arrow.d.ts.map +1 -1
- package/dist/src/slide-components/cross-fade-transition.d.ts +2 -1
- package/dist/src/slide-components/cross-fade-transition.d.ts.map +1 -1
- package/dist/src/slide-components/cross-fade-transition.js +2 -1
- package/dist/src/slide-components/cross-fade-transition.js.map +1 -1
- package/dist/src/slide-components/duration-agnostic.d.ts +3 -2
- package/dist/src/slide-components/duration-agnostic.d.ts.map +1 -1
- package/dist/src/slide-components/duration-agnostic.js +4 -2
- package/dist/src/slide-components/duration-agnostic.js.map +1 -1
- package/dist/src/slide-components/fixed-duration.d.ts +3 -1
- package/dist/src/slide-components/fixed-duration.d.ts.map +1 -1
- package/dist/src/slide-components/fixed-duration.js +4 -2
- package/dist/src/slide-components/fixed-duration.js.map +1 -1
- package/dist/src/slide-components/fourier.d.ts +19 -12
- package/dist/src/slide-components/fourier.d.ts.map +1 -1
- package/dist/src/slide-components/fourier.js +77 -16
- package/dist/src/slide-components/fourier.js.map +1 -1
- package/dist/src/slide-components/function-graph.d.ts +4 -3
- package/dist/src/slide-components/function-graph.d.ts.map +1 -1
- package/dist/src/slide-components/function-graph.js +6 -2
- package/dist/src/slide-components/function-graph.js.map +1 -1
- package/dist/src/slide-components/halftone-shadow.d.ts +8 -7
- package/dist/src/slide-components/halftone-shadow.d.ts.map +1 -1
- package/dist/src/slide-components/halftone-shadow.js +14 -10
- package/dist/src/slide-components/halftone-shadow.js.map +1 -1
- package/dist/src/slide-components/handwriting-effect.d.ts +8 -6
- package/dist/src/slide-components/handwriting-effect.d.ts.map +1 -1
- package/dist/src/slide-components/handwriting-effect.js +4 -2
- package/dist/src/slide-components/handwriting-effect.js.map +1 -1
- package/dist/src/slide-components/hold.d.ts +4 -2
- package/dist/src/slide-components/hold.d.ts.map +1 -1
- package/dist/src/slide-components/hold.js +4 -2
- package/dist/src/slide-components/hold.js.map +1 -1
- package/dist/src/slide-components/image.d.ts +7 -7
- package/dist/src/slide-components/image.d.ts.map +1 -1
- package/dist/src/slide-components/image.js +4 -2
- package/dist/src/slide-components/image.js.map +1 -1
- package/dist/src/slide-components/in-parallel.d.ts +6 -3
- package/dist/src/slide-components/in-parallel.d.ts.map +1 -1
- package/dist/src/slide-components/in-parallel.js +12 -2
- package/dist/src/slide-components/in-parallel.js.map +1 -1
- package/dist/src/slide-components/in-series.d.ts +2 -1
- package/dist/src/slide-components/in-series.d.ts.map +1 -1
- package/dist/src/slide-components/in-series.js +2 -1
- package/dist/src/slide-components/in-series.js.map +1 -1
- package/dist/src/slide-components/live-duration.d.ts +3 -1
- package/dist/src/slide-components/live-duration.d.ts.map +1 -1
- package/dist/src/slide-components/live-duration.js +4 -2
- package/dist/src/slide-components/live-duration.js.map +1 -1
- package/dist/src/slide-components/multi-text.d.ts +39 -26
- package/dist/src/slide-components/multi-text.d.ts.map +1 -1
- package/dist/src/slide-components/multi-text.js +44 -29
- package/dist/src/slide-components/multi-text.js.map +1 -1
- package/dist/src/slide-components/rectangle.d.ts +5 -4
- package/dist/src/slide-components/rectangle.d.ts.map +1 -1
- package/dist/src/slide-components/rectangle.js +2 -1
- package/dist/src/slide-components/rectangle.js.map +1 -1
- package/dist/src/slide-components/registry.d.ts +5 -0
- package/dist/src/slide-components/registry.d.ts.map +1 -1
- package/dist/src/slide-components/registry.js +28 -27
- package/dist/src/slide-components/registry.js.map +1 -1
- package/dist/src/slide-components/show-error.d.ts.map +1 -1
- package/dist/src/slide-components/show-error.js +12 -4
- package/dist/src/slide-components/show-error.js.map +1 -1
- package/dist/src/slide-components/simple-text.d.ts +11 -10
- package/dist/src/slide-components/simple-text.d.ts.map +1 -1
- package/dist/src/slide-components/simple-text.js +5 -3
- package/dist/src/slide-components/simple-text.js.map +1 -1
- package/dist/src/slide-components/slide-component.d.ts +14 -13
- package/dist/src/slide-components/slide-component.d.ts.map +1 -1
- package/dist/src/slide-components/slide-component.js +2 -1
- package/dist/src/slide-components/slide-component.js.map +1 -1
- package/dist/src/slide-components/slide-left.d.ts +2 -1
- package/dist/src/slide-components/slide-left.d.ts.map +1 -1
- package/dist/src/slide-components/slide-left.js +2 -1
- package/dist/src/slide-components/slide-left.js.map +1 -1
- package/dist/src/slide-components/text-effect.d.ts +9 -1
- package/dist/src/slide-components/text-effect.d.ts.map +1 -1
- package/dist/src/slide-components/text-effect.js.map +1 -1
- package/dist/src/slide-components/traditional-text.d.ts +8 -8
- package/dist/src/slide-components/traditional-text.d.ts.map +1 -1
- package/dist/src/slide-components/video-clip.d.ts +4 -3
- package/dist/src/slide-components/video-clip.d.ts.map +1 -1
- package/dist/src/slide-components/video-clip.js +2 -1
- package/dist/src/slide-components/video-clip.js.map +1 -1
- package/lib/index.ts +1 -0
- package/package.json +2 -2
- package/src/glib/line-font.ts +9 -0
- package/src/glib/path-shape.ts +101 -245
- package/src/glib/svg-arc.ts +141 -0
- package/src/glib/svg-path-parser.ts +308 -0
- package/src/glib/transforms.ts +7 -4
- package/src/interpolate.ts +18 -27
- package/src/morph-animation.ts +1 -1
- package/src/peano-fourier/fourier-shared.ts +4 -2
- package/src/schedule-helper.ts +50 -28
- package/src/showable.ts +79 -10
- package/src/slide-components/arrow.ts +4 -4
- package/src/slide-components/cross-fade-transition.ts +9 -2
- package/src/slide-components/duration-agnostic.ts +4 -3
- package/src/slide-components/fixed-duration.ts +4 -2
- package/src/slide-components/fourier.ts +98 -29
- package/src/slide-components/function-graph.ts +7 -3
- package/src/slide-components/halftone-shadow.ts +21 -16
- package/src/slide-components/handwriting-effect.ts +8 -6
- package/src/slide-components/hold.ts +18 -4
- package/src/slide-components/image.ts +9 -7
- package/src/slide-components/in-parallel.ts +16 -1
- package/src/slide-components/in-series.ts +3 -1
- package/src/slide-components/live-duration.ts +4 -2
- package/src/slide-components/multi-text.ts +71 -44
- package/src/slide-components/rectangle.ts +6 -4
- package/src/slide-components/registry.ts +33 -27
- package/src/slide-components/show-error.ts +13 -4
- package/src/slide-components/simple-text.ts +22 -12
- package/src/slide-components/slide-component.ts +15 -13
- package/src/slide-components/slide-left.ts +9 -2
- package/src/slide-components/text-effect.ts +9 -0
- package/src/slide-components/traditional-text.ts +8 -8
- package/src/slide-components/video-clip.ts +5 -3
package/dev/subtitles.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { PlacedSoundClip } from "./soundtrack.ts";
|
|
2
|
+
import type { TranscriptWord } from "./transcript-align.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Subtitles for a saved video, made from the transcripts of its sound clips.
|
|
6
|
+
*
|
|
7
|
+
* Word by word, the way YouTube shows its own automatic captions: each word appears as it's
|
|
8
|
+
* spoken, and the line before stays on screen above the one being spoken. Every change is its
|
|
9
|
+
* own cue, so any player shows it this way, with no styling and no timestamps inside a cue.
|
|
10
|
+
*
|
|
11
|
+
* See "Transcript Editor" in development-plans/saturday-afternoon.md.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** One word, in the video's time. */
|
|
15
|
+
export type SubtitleWord = {
|
|
16
|
+
readonly text: string;
|
|
17
|
+
readonly startMs: number;
|
|
18
|
+
readonly endMs: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** A line breaks before it gets longer than this. About as long as YouTube's own lines. */
|
|
22
|
+
const MAX_LINE_CHARACTERS = 42;
|
|
23
|
+
/** A silence this long starts a new line. */
|
|
24
|
+
const PAUSE_MS = 1000;
|
|
25
|
+
/** How long the last line stays up after its last word, unless the next line comes sooner. */
|
|
26
|
+
const HOLD_MS = 1500;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Every word the video says, in the video's time, in order.
|
|
30
|
+
*
|
|
31
|
+
* A word belongs to the clip that plays its start. Speech Whisper left out (timing `letters`)
|
|
32
|
+
* isn't included: it's nearly always a repeat, a false start or an um. Editing one in the
|
|
33
|
+
* Transcript Editor makes it a real word.
|
|
34
|
+
*
|
|
35
|
+
* @param clips Every sound clip in the video.
|
|
36
|
+
* @param wordsOf A file's words, or undefined if its transcript isn't ready.
|
|
37
|
+
*/
|
|
38
|
+
export function subtitleWords(
|
|
39
|
+
clips: readonly PlacedSoundClip[],
|
|
40
|
+
wordsOf: (source: string) => readonly TranscriptWord[] | undefined,
|
|
41
|
+
): SubtitleWord[] {
|
|
42
|
+
const result: SubtitleWord[] = [];
|
|
43
|
+
for (const clip of clips) {
|
|
44
|
+
const words = wordsOf(clip.source);
|
|
45
|
+
if (!words) continue;
|
|
46
|
+
const fromMs = clip.trimMs;
|
|
47
|
+
const toMs = clip.trimMs + clip.lengthMs;
|
|
48
|
+
/** File time to video time. */
|
|
49
|
+
const shift = clip.startMs - clip.trimMs;
|
|
50
|
+
for (const word of words) {
|
|
51
|
+
if (word.startMs < fromMs || word.startMs >= toMs) continue;
|
|
52
|
+
if (word.timing === "letters") continue;
|
|
53
|
+
const text = word.text.trim();
|
|
54
|
+
if (text === "") continue;
|
|
55
|
+
result.push({
|
|
56
|
+
text,
|
|
57
|
+
startMs: word.startMs + shift,
|
|
58
|
+
endMs: Math.max(word.startMs, Math.min(word.endMs, toMs)) + shift,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return result.sort((a, b) => a.startMs - b.startMs);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Group the words into lines, breaking at the length limit, a pause, or the end of a sentence. */
|
|
66
|
+
function toLines(words: readonly SubtitleWord[]): SubtitleWord[][] {
|
|
67
|
+
const lines: SubtitleWord[][] = [];
|
|
68
|
+
let line: SubtitleWord[] = [];
|
|
69
|
+
let length = 0;
|
|
70
|
+
for (const word of words) {
|
|
71
|
+
const previous = line.at(-1);
|
|
72
|
+
if (
|
|
73
|
+
previous &&
|
|
74
|
+
(length + 1 + word.text.length > MAX_LINE_CHARACTERS ||
|
|
75
|
+
word.startMs - previous.endMs > PAUSE_MS ||
|
|
76
|
+
/[.?!]["'”’)]*$/.test(previous.text))
|
|
77
|
+
) {
|
|
78
|
+
lines.push(line);
|
|
79
|
+
line = [];
|
|
80
|
+
length = 0;
|
|
81
|
+
}
|
|
82
|
+
length += (line.length ? 1 : 0) + word.text.length;
|
|
83
|
+
line.push(word);
|
|
84
|
+
}
|
|
85
|
+
if (line.length) lines.push(line);
|
|
86
|
+
return lines;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type Cue = {
|
|
90
|
+
readonly startMs: number;
|
|
91
|
+
readonly endMs: number;
|
|
92
|
+
readonly text: string;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* One cue for each word: the line so far, under the previous line when there was no gap.
|
|
97
|
+
* The cues are in order and never overlap.
|
|
98
|
+
*/
|
|
99
|
+
function toCues(words: readonly SubtitleWord[]): Cue[] {
|
|
100
|
+
const lines = toLines(words);
|
|
101
|
+
const result: Cue[] = [];
|
|
102
|
+
/** The previous line, while it's still on screen. */
|
|
103
|
+
let above: string | undefined;
|
|
104
|
+
for (let l = 0; l < lines.length; l++) {
|
|
105
|
+
const line = lines[l]!;
|
|
106
|
+
const nextStart = lines[l + 1]?.[0]!.startMs ?? Infinity;
|
|
107
|
+
const lineEnd = Math.min(nextStart, line.at(-1)!.endMs + HOLD_MS);
|
|
108
|
+
for (let i = 0; i < line.length; i++) {
|
|
109
|
+
const startMs = line[i]!.startMs;
|
|
110
|
+
const endMs = i + 1 < line.length ? line[i + 1]!.startMs : lineEnd;
|
|
111
|
+
// Two words at the same moment: the next cue shows both.
|
|
112
|
+
if (endMs <= startMs) continue;
|
|
113
|
+
const spoken = line
|
|
114
|
+
.slice(0, i + 1)
|
|
115
|
+
.map((word) => word.text)
|
|
116
|
+
.join(" ");
|
|
117
|
+
result.push({
|
|
118
|
+
startMs,
|
|
119
|
+
endMs,
|
|
120
|
+
text: above === undefined ? spoken : `${above}\n${spoken}`,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
// The next line keeps this one above it only if they touch.
|
|
124
|
+
above =
|
|
125
|
+
lineEnd >= nextStart
|
|
126
|
+
? line.map((word) => word.text).join(" ")
|
|
127
|
+
: undefined;
|
|
128
|
+
}
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A WebVTT file for part of the video.
|
|
134
|
+
* @param fromMs Where the saved file starts, in the video's time. It becomes 0.
|
|
135
|
+
* @param toMs Where the saved file ends.
|
|
136
|
+
* @returns Undefined if no words are spoken in that time.
|
|
137
|
+
*/
|
|
138
|
+
export function webVtt(
|
|
139
|
+
words: readonly SubtitleWord[],
|
|
140
|
+
fromMs: number,
|
|
141
|
+
toMs: number,
|
|
142
|
+
): string | undefined {
|
|
143
|
+
const cues: string[] = [];
|
|
144
|
+
for (const cue of toCues(words)) {
|
|
145
|
+
const startMs = Math.round(Math.max(cue.startMs, fromMs) - fromMs);
|
|
146
|
+
const endMs = Math.round(Math.min(cue.endMs, toMs) - fromMs);
|
|
147
|
+
if (endMs <= startMs) continue;
|
|
148
|
+
cues.push(
|
|
149
|
+
`${timestamp(startMs)} --> ${timestamp(endMs)}\n${escapeCueText(cue.text)}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return cues.length ? `WEBVTT\n\n${cues.join("\n\n")}\n` : undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** hh:mm:ss.ttt, as WebVTT wants. */
|
|
156
|
+
function timestamp(ms: number): string {
|
|
157
|
+
const pad = (n: number, width = 2) => String(n).padStart(width, "0");
|
|
158
|
+
const hours = Math.floor(ms / 3_600_000);
|
|
159
|
+
const minutes = Math.floor(ms / 60_000) % 60;
|
|
160
|
+
const seconds = Math.floor(ms / 1000) % 60;
|
|
161
|
+
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}.${pad(ms % 1000, 3)}`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** WebVTT cue text is a little like HTML. This also rules out "-->". */
|
|
165
|
+
function escapeCueText(text: string): string {
|
|
166
|
+
return text
|
|
167
|
+
.replace(/&/g, "&")
|
|
168
|
+
.replace(/</g, "<")
|
|
169
|
+
.replace(/>/g, ">");
|
|
170
|
+
}
|
package/dev/synced-file.ts
CHANGED
|
@@ -55,6 +55,22 @@ export type SyncedFileOptions = {
|
|
|
55
55
|
readonly showPending?: boolean;
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* A synced file's state in one word, for a button that stands for several files.
|
|
60
|
+
* * `off`: the checkbox is off.
|
|
61
|
+
* * `ok`: the file matches, or will in a moment with nothing for anyone to do.
|
|
62
|
+
* * `pending`: memory is ahead of the file, waiting for the next save.
|
|
63
|
+
* * `action`: someone has to click something: permission, a conflict or an error.
|
|
64
|
+
*/
|
|
65
|
+
export type SyncSummary = "off" | "ok" | "pending" | "action";
|
|
66
|
+
|
|
67
|
+
const summaryListeners = new Set<() => void>();
|
|
68
|
+
|
|
69
|
+
/** Called whenever any synced file's status is redrawn. See {@link SyncedFile.summary}. */
|
|
70
|
+
export function onSyncStatusChange(listener: () => void): void {
|
|
71
|
+
summaryListeners.add(listener);
|
|
72
|
+
}
|
|
73
|
+
|
|
58
74
|
type State =
|
|
59
75
|
| { kind: "starting" }
|
|
60
76
|
| { kind: "off" }
|
|
@@ -73,6 +89,7 @@ export class SyncedFile {
|
|
|
73
89
|
#again = false;
|
|
74
90
|
/** The next sync may overwrite a file someone else changed: the user said so. */
|
|
75
91
|
#forceNext = false;
|
|
92
|
+
#summary: SyncSummary = "off";
|
|
76
93
|
|
|
77
94
|
constructor(options: SyncedFileOptions) {
|
|
78
95
|
this.#options = options;
|
|
@@ -135,6 +152,16 @@ export class SyncedFile {
|
|
|
135
152
|
})();
|
|
136
153
|
}
|
|
137
154
|
|
|
155
|
+
/** The state in one word, as of the last redraw. */
|
|
156
|
+
get summary(): SyncSummary {
|
|
157
|
+
return this.#summary;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** What the status shows, as plain text, for a tooltip. */
|
|
161
|
+
get statusText(): string {
|
|
162
|
+
return this.#options.status.textContent ?? "";
|
|
163
|
+
}
|
|
164
|
+
|
|
138
165
|
/** Redraw, e.g. to show or clear the pending " *" after an edit. Cheap; does no I/O. */
|
|
139
166
|
refreshStatus(): void {
|
|
140
167
|
this.#draw();
|
|
@@ -288,14 +315,17 @@ export class SyncedFile {
|
|
|
288
315
|
case "starting":
|
|
289
316
|
text("…");
|
|
290
317
|
status.style.color = "gray";
|
|
318
|
+
this.#summary = "ok";
|
|
291
319
|
break;
|
|
292
320
|
case "off":
|
|
293
321
|
text("off");
|
|
294
322
|
status.style.color = "gray";
|
|
323
|
+
this.#summary = "off";
|
|
295
324
|
break;
|
|
296
325
|
case "synced": {
|
|
297
326
|
const pending =
|
|
298
327
|
showPending && render() !== this.#record?.lastWrittenBody;
|
|
328
|
+
this.#summary = pending ? "pending" : "ok";
|
|
299
329
|
text(`${name} ${pending ? "*" : "✓"}`);
|
|
300
330
|
status.style.color = pending ? "darkorange" : "";
|
|
301
331
|
status.title = pending
|
|
@@ -311,12 +341,14 @@ export class SyncedFile {
|
|
|
311
341
|
"The browser needs your permission again to write this file. " +
|
|
312
342
|
'Choosing "Allow on every visit" stops it asking.';
|
|
313
343
|
status.onclick = () => void this.#allow();
|
|
344
|
+
this.#summary = "action";
|
|
314
345
|
break;
|
|
315
346
|
case "conflict":
|
|
316
347
|
text(`${name} ⚠ changed on disk`);
|
|
317
348
|
status.style.color = "darkorange";
|
|
318
349
|
status.title =
|
|
319
350
|
"Something else changed this file since it was last synced, so it hasn't been overwritten.";
|
|
351
|
+
this.#summary = "action";
|
|
320
352
|
button("Overwrite", `Replace ${name} with the current state`, () =>
|
|
321
353
|
this.request(true),
|
|
322
354
|
);
|
|
@@ -334,8 +366,10 @@ export class SyncedFile {
|
|
|
334
366
|
status.style.cursor = "pointer";
|
|
335
367
|
status.title = this.#state.message;
|
|
336
368
|
status.onclick = () => this.request();
|
|
369
|
+
this.#summary = "action";
|
|
337
370
|
break;
|
|
338
371
|
}
|
|
372
|
+
for (const listener of summaryListeners) listener();
|
|
339
373
|
}
|
|
340
374
|
}
|
|
341
375
|
|
package/dev/timeline-display.ts
CHANGED
|
@@ -78,7 +78,7 @@ export type TimelineWord = {
|
|
|
78
78
|
};
|
|
79
79
|
|
|
80
80
|
/** Background colors for flagged words. The 📖 Text view uses lighter versions of the same colors. */
|
|
81
|
-
const WORD_FLAG_COLORS = {
|
|
81
|
+
export const WORD_FLAG_COLORS = {
|
|
82
82
|
"left out": "rgba(225, 30, 30, 0.9)",
|
|
83
83
|
doubtful: "rgba(255, 140, 0, 0.8)",
|
|
84
84
|
} as const;
|
package/dev/transcribe-core.ts
CHANGED
|
@@ -244,18 +244,21 @@ export class Transcriber {
|
|
|
244
244
|
const step =
|
|
245
245
|
ALIGNER_WINDOW_SAMPLES - 2 * ALIGNER_OVERLAP_FRAMES * ALIGNER_HOP_SAMPLES;
|
|
246
246
|
let vocabularySize = 0;
|
|
247
|
-
let
|
|
247
|
+
let logProbabilities = new Float32Array(0);
|
|
248
248
|
/** Frames filled in so far, from the start. */
|
|
249
249
|
let written = 0;
|
|
250
250
|
for (let offset = 0; written < frameCount; offset += step) {
|
|
251
251
|
onProgress?.({ stage: "aligning", fraction: offset / samples.length });
|
|
252
252
|
const window = samples.subarray(offset, offset + ALIGNER_WINDOW_SAMPLES);
|
|
253
253
|
const isLast = offset + ALIGNER_WINDOW_SAMPLES >= samples.length;
|
|
254
|
+
// "Logits" is the machine-learning word for a model's raw scores: any
|
|
255
|
+
// number, higher is more likely. logSoftmax() turns them into log
|
|
256
|
+
// probabilities. The name comes from transformers.js, so it stays.
|
|
254
257
|
const { logits } = await model(await processor(window));
|
|
255
258
|
const [, frames, size] = logits.dims as number[];
|
|
256
259
|
if (vocabularySize === 0) {
|
|
257
260
|
vocabularySize = size;
|
|
258
|
-
|
|
261
|
+
logProbabilities = new Float32Array(frameCount * size);
|
|
259
262
|
}
|
|
260
263
|
const data = logits.data as Float32Array;
|
|
261
264
|
const base = offset / ALIGNER_HOP_SAMPLES;
|
|
@@ -266,7 +269,7 @@ export class Transcriber {
|
|
|
266
269
|
);
|
|
267
270
|
const to = isLast ? frames : frames - ALIGNER_OVERLAP_FRAMES;
|
|
268
271
|
for (let k = from; k < to && base + k < frameCount; k++) {
|
|
269
|
-
logSoftmax(data, k * size, size,
|
|
272
|
+
logSoftmax(data, k * size, size, logProbabilities, (base + k) * size);
|
|
270
273
|
written = base + k + 1;
|
|
271
274
|
}
|
|
272
275
|
if (isLast) break;
|
|
@@ -278,7 +281,7 @@ export class Transcriber {
|
|
|
278
281
|
}
|
|
279
282
|
const letterOf = (token: number) => letters.get(token);
|
|
280
283
|
return {
|
|
281
|
-
|
|
284
|
+
logProbabilities,
|
|
282
285
|
vocabularySize,
|
|
283
286
|
frameCount: written,
|
|
284
287
|
frameMs: (ALIGNER_HOP_SAMPLES / SAMPLE_RATE) * 1000,
|
package/dev/transcript-align.ts
CHANGED
|
@@ -41,8 +41,10 @@ export type TranscriptWord = {
|
|
|
41
41
|
* * `letters`: speech Whisper **left out**, such as a repeated word, a false
|
|
42
42
|
* start or an "um". Whisper tidies what it hears; the letter model doesn't.
|
|
43
43
|
* See {@link recoverSkippedSpeech}.
|
|
44
|
+
* * `edited`: a person typed, timed or checked this word in the Transcript
|
|
45
|
+
* Editor. Never flagged, and always in the subtitles.
|
|
44
46
|
*/
|
|
45
|
-
readonly timing: "aligned" | "whisper" | "letters";
|
|
47
|
+
readonly timing: "aligned" | "whisper" | "letters" | "edited";
|
|
46
48
|
/**
|
|
47
49
|
* How sure the letter model was, 0 to 1: the average probability it gave
|
|
48
50
|
* the letters it was made to hear. Low in noise and mumbling. Only
|
|
@@ -170,11 +172,11 @@ export function spellForAlignment(word: string): string {
|
|
|
170
172
|
/**
|
|
171
173
|
* Letter probabilities from a CTC model like wav2vec2.
|
|
172
174
|
*
|
|
173
|
-
* `
|
|
175
|
+
* `logProbabilities[frame * vocabularySize + token]` is the log probability that
|
|
174
176
|
* `token` is being spoken in `frame`.
|
|
175
177
|
*/
|
|
176
178
|
export type CtcEmissions = {
|
|
177
|
-
readonly
|
|
179
|
+
readonly logProbabilities: Float32Array;
|
|
178
180
|
readonly vocabularySize: number;
|
|
179
181
|
readonly frameCount: number;
|
|
180
182
|
/** Length of one frame. 20 ms for wav2vec2. */
|
|
@@ -203,8 +205,8 @@ export function forcedAlign(
|
|
|
203
205
|
tokens: readonly number[],
|
|
204
206
|
firstFrame: number,
|
|
205
207
|
endFrame: number,
|
|
206
|
-
): { first: number; last: number;
|
|
207
|
-
const {
|
|
208
|
+
): { first: number; last: number; logProbability: number }[] | undefined {
|
|
209
|
+
const { logProbabilities, vocabularySize, blank } = emissions;
|
|
208
210
|
const frames = endFrame - firstFrame;
|
|
209
211
|
// The extended sequence: blank, t0, blank, t1, …, blank.
|
|
210
212
|
const states = 2 * tokens.length + 1;
|
|
@@ -217,7 +219,7 @@ export function forcedAlign(
|
|
|
217
219
|
/** How each cell was reached: 0 = stayed, 1 = from s − 1, 2 = from s − 2. */
|
|
218
220
|
const moves = new Uint8Array(frames * states);
|
|
219
221
|
const at = (frame: number, token: number) =>
|
|
220
|
-
|
|
222
|
+
logProbabilities[(firstFrame + frame) * vocabularySize + token];
|
|
221
223
|
|
|
222
224
|
previous[0] = at(0, blank);
|
|
223
225
|
previous[1] = at(0, tokens[0]);
|
|
@@ -258,7 +260,7 @@ export function forcedAlign(
|
|
|
258
260
|
const result = tokens.map(() => ({
|
|
259
261
|
first: Infinity,
|
|
260
262
|
last: -Infinity,
|
|
261
|
-
|
|
263
|
+
logProbability: 0,
|
|
262
264
|
}));
|
|
263
265
|
const counts = new Array<number>(tokens.length).fill(0);
|
|
264
266
|
for (let t = frames - 1; t >= 0; t--) {
|
|
@@ -267,13 +269,15 @@ export function forcedAlign(
|
|
|
267
269
|
const span = result[index];
|
|
268
270
|
span.first = firstFrame + t;
|
|
269
271
|
span.last = Math.max(span.last, firstFrame + t);
|
|
270
|
-
span.
|
|
272
|
+
span.logProbability += at(t, tokens[index]);
|
|
271
273
|
counts[index]++;
|
|
272
274
|
}
|
|
273
275
|
s -= moves[t * states + s];
|
|
274
276
|
}
|
|
275
277
|
for (const [index, span] of result.entries()) {
|
|
276
|
-
span.
|
|
278
|
+
span.logProbability = counts[index]
|
|
279
|
+
? span.logProbability / counts[index]
|
|
280
|
+
: NEG;
|
|
277
281
|
}
|
|
278
282
|
return result;
|
|
279
283
|
}
|
|
@@ -367,13 +371,14 @@ export function alignWords(
|
|
|
367
371
|
const range = letterRanges[i - from];
|
|
368
372
|
if (!range) continue;
|
|
369
373
|
const letters = spans.slice(range[0], range[1]);
|
|
370
|
-
const
|
|
371
|
-
letters.reduce((sum, span) => sum + span.
|
|
374
|
+
const logProbability =
|
|
375
|
+
letters.reduce((sum, span) => sum + span.logProbability, 0) /
|
|
376
|
+
letters.length;
|
|
372
377
|
result[i] = {
|
|
373
378
|
startMs: letters[0].first * emissions.frameMs,
|
|
374
379
|
endMs: (letters[letters.length - 1].last + 1) * emissions.frameMs,
|
|
375
380
|
timing: "aligned",
|
|
376
|
-
confidence: Math.exp(
|
|
381
|
+
confidence: Math.exp(logProbability),
|
|
377
382
|
};
|
|
378
383
|
}
|
|
379
384
|
}
|
|
@@ -414,7 +419,7 @@ export function lettersHeard(
|
|
|
414
419
|
firstFrame: number,
|
|
415
420
|
endFrame: number,
|
|
416
421
|
): HeardWord[] {
|
|
417
|
-
const {
|
|
422
|
+
const { logProbabilities, vocabularySize, blank, wordSeparator } = emissions;
|
|
418
423
|
const result: HeardWord[] = [];
|
|
419
424
|
let letters = "";
|
|
420
425
|
let start = -1;
|
|
@@ -441,11 +446,11 @@ export function lettersHeard(
|
|
|
441
446
|
t++
|
|
442
447
|
) {
|
|
443
448
|
let best = 0;
|
|
444
|
-
let
|
|
449
|
+
let bestLogProbability = -Infinity;
|
|
445
450
|
for (let token = 0; token < vocabularySize; token++) {
|
|
446
|
-
const
|
|
447
|
-
if (
|
|
448
|
-
|
|
451
|
+
const logProbability = logProbabilities[t * vocabularySize + token];
|
|
452
|
+
if (logProbability > bestLogProbability) {
|
|
453
|
+
bestLogProbability = logProbability;
|
|
449
454
|
best = token;
|
|
450
455
|
}
|
|
451
456
|
}
|
|
@@ -460,7 +465,7 @@ export function lettersHeard(
|
|
|
460
465
|
// The same letter in neighboring frames is one letter, held.
|
|
461
466
|
if (best !== previous) letters += letter;
|
|
462
467
|
last = t;
|
|
463
|
-
probabilitySum += Math.exp(
|
|
468
|
+
probabilitySum += Math.exp(bestLogProbability);
|
|
464
469
|
frames++;
|
|
465
470
|
}
|
|
466
471
|
}
|
|
@@ -585,6 +590,7 @@ const squash = (s: string) => s.toLowerCase().replace(/[^a-z']/g, "");
|
|
|
585
590
|
export function wordFlag(
|
|
586
591
|
word: TranscriptWord,
|
|
587
592
|
): "left out" | "doubtful" | undefined {
|
|
593
|
+
if (word.timing === "edited") return undefined;
|
|
588
594
|
if (word.timing === "letters") return "left out";
|
|
589
595
|
const text = squash(word.text);
|
|
590
596
|
// `heard` is missing from transcripts made before it existed.
|