canvas-recorder-lib 0.8.3 → 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 +14 -0
- package/dev/canvas-recorder.ts +379 -45
- package/dev/color-picker.ts +13 -0
- package/dev/component-picker.ts +60 -11
- 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 +75 -30
- 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/transcript-align.ts +4 -1
- 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 +14 -0
- package/dist/dev/canvas-recorder.js +304 -39
- 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/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 +75 -30
- 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/transcript-align.d.ts +3 -1
- package/dist/dev/transcript-align.d.ts.map +1 -1
- package/dist/dev/transcript-align.js +2 -0
- 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 +16 -0
- package/dist/src/glib/path-shape.d.ts.map +1 -1
- package/dist/src/glib/path-shape.js +23 -0
- package/dist/src/glib/path-shape.js.map +1 -1
- 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 +70 -22
- 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 +2 -1
- 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 +3 -2
- 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 +35 -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 +89 -32
- package/src/slide-components/function-graph.ts +7 -3
- package/src/slide-components/halftone-shadow.ts +9 -7
- 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 +4 -2
- 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
|
@@ -0,0 +1,948 @@
|
|
|
1
|
+
import { WORD_FLAG_COLORS } from "./timeline-display.js";
|
|
2
|
+
import { wordFlag } from "./transcript-align.js";
|
|
3
|
+
// MARK: Sizes and colors
|
|
4
|
+
/** In CSS px. */
|
|
5
|
+
const WAVE_HEIGHT = 56;
|
|
6
|
+
const LANE_HEIGHT = 18;
|
|
7
|
+
const ROW_HEIGHT = WAVE_HEIGHT + 2 * LANE_HEIGHT;
|
|
8
|
+
const ROW_GAP = 8;
|
|
9
|
+
/** How close to an edge of the selected word a click grabs it, in CSS px. */
|
|
10
|
+
const EDGE_GRAB_PX = 6;
|
|
11
|
+
/** No word is shorter than this. */
|
|
12
|
+
const MIN_WORD_MS = 20;
|
|
13
|
+
/** A new word's length, when there's room. */
|
|
14
|
+
const NEW_WORD_MS = 400;
|
|
15
|
+
const FONT = "12px sans-serif";
|
|
16
|
+
const SELECTED_COLOR = "#1a73e8";
|
|
17
|
+
const EDITED_COLOR = "#2e7d32";
|
|
18
|
+
const PLAIN_COLOR = "#555";
|
|
19
|
+
// MARK: Words
|
|
20
|
+
/** How a word is labeled. Speech Whisper left out is in [brackets], as everywhere else. */
|
|
21
|
+
function labelOf(word) {
|
|
22
|
+
if (word.text.trim() === "")
|
|
23
|
+
return "␣";
|
|
24
|
+
return word.timing === "letters" ? `[${word.text}]` : word.text;
|
|
25
|
+
}
|
|
26
|
+
/** `word` with new times. Its cut points stay outside it. */
|
|
27
|
+
function retimed(word, startMs, endMs) {
|
|
28
|
+
return {
|
|
29
|
+
...word,
|
|
30
|
+
startMs,
|
|
31
|
+
endMs,
|
|
32
|
+
cutBeforeMs: Math.min(word.cutBeforeMs, startMs),
|
|
33
|
+
cutAfterMs: Math.max(word.cutAfterMs, endMs),
|
|
34
|
+
timing: "edited",
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** A word nobody heard: typed into a gap. */
|
|
38
|
+
function newWord(startMs, endMs) {
|
|
39
|
+
return {
|
|
40
|
+
text: "",
|
|
41
|
+
startMs,
|
|
42
|
+
endMs,
|
|
43
|
+
cutBeforeMs: startMs,
|
|
44
|
+
cutAfterMs: endMs,
|
|
45
|
+
timing: "edited",
|
|
46
|
+
confidence: 1,
|
|
47
|
+
heard: "",
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** One word into several, sharing its time in proportion to their lengths. */
|
|
51
|
+
function splitWord(word, parts) {
|
|
52
|
+
const total = parts.reduce((sum, part) => sum + part.length, 0);
|
|
53
|
+
const spanMs = word.endMs - word.startMs;
|
|
54
|
+
let startMs = word.startMs;
|
|
55
|
+
return parts.map((text, k) => {
|
|
56
|
+
const last = k === parts.length - 1;
|
|
57
|
+
const endMs = last
|
|
58
|
+
? word.endMs
|
|
59
|
+
: startMs + (spanMs * text.length) / Math.max(1, total);
|
|
60
|
+
const piece = {
|
|
61
|
+
...word,
|
|
62
|
+
text,
|
|
63
|
+
startMs,
|
|
64
|
+
endMs,
|
|
65
|
+
cutBeforeMs: k === 0 ? word.cutBeforeMs : startMs,
|
|
66
|
+
cutAfterMs: last ? word.cutAfterMs : endMs,
|
|
67
|
+
timing: "edited",
|
|
68
|
+
heard: k === 0 ? word.heard : "",
|
|
69
|
+
};
|
|
70
|
+
startMs = endMs;
|
|
71
|
+
return piece;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/** 83 456 ms → "1:23.5" */
|
|
75
|
+
function formatTime(ms) {
|
|
76
|
+
const tenths = Math.round(Math.max(0, ms) / 100);
|
|
77
|
+
const minutes = Math.floor(tenths / 600);
|
|
78
|
+
const seconds = (tenths % 600) / 10;
|
|
79
|
+
return `${minutes}:${seconds.toFixed(1).padStart(4, "0")}`;
|
|
80
|
+
}
|
|
81
|
+
/** `text`, cut short with "…" to fit in `width` px, or "" if not even that fits. */
|
|
82
|
+
function fitText(context, text, width) {
|
|
83
|
+
if (context.measureText(text).width <= width)
|
|
84
|
+
return text;
|
|
85
|
+
for (let length = text.length - 1; length > 0; length--) {
|
|
86
|
+
const shorter = text.slice(0, length) + "…";
|
|
87
|
+
if (context.measureText(shorter).width <= width)
|
|
88
|
+
return shorter;
|
|
89
|
+
}
|
|
90
|
+
return "";
|
|
91
|
+
}
|
|
92
|
+
// MARK: The dialog
|
|
93
|
+
/** Open the editor. Does nothing if the transcript isn't ready. */
|
|
94
|
+
export function openTranscriptEditor(hooks) {
|
|
95
|
+
const original = hooks.words();
|
|
96
|
+
if (!original)
|
|
97
|
+
return;
|
|
98
|
+
let words = [...original];
|
|
99
|
+
/** Index into {@link words}. */
|
|
100
|
+
let selected;
|
|
101
|
+
/** Where ▶ Play starts when no word is selected, in the file's time. */
|
|
102
|
+
let cursorMs = hooks.startAtMs;
|
|
103
|
+
/** How much of the file each row shows. */
|
|
104
|
+
let rowMs = 4000;
|
|
105
|
+
/** The text box changed since this word was selected, so it needs {@link commitText}. */
|
|
106
|
+
let textTouched = false;
|
|
107
|
+
const used = [...hooks.used].sort((a, b) => a.fromMs - b.fromMs);
|
|
108
|
+
const measure = document.createElement("canvas").getContext("2d");
|
|
109
|
+
measure.font = FONT;
|
|
110
|
+
const totalMs = () => {
|
|
111
|
+
const audio = hooks.audio();
|
|
112
|
+
return Math.max(audio ? audio.duration * 1000 : 0, words.at(-1)?.endMs ?? 0, 1000);
|
|
113
|
+
};
|
|
114
|
+
// MARK: Layout
|
|
115
|
+
const element = (tag, css = "", text = "") => {
|
|
116
|
+
const result = document.createElement(tag);
|
|
117
|
+
result.style.cssText = css;
|
|
118
|
+
result.textContent = text;
|
|
119
|
+
return result;
|
|
120
|
+
};
|
|
121
|
+
const button = (label, title, action) => {
|
|
122
|
+
const result = element("button", "", label);
|
|
123
|
+
result.type = "button";
|
|
124
|
+
result.title = title;
|
|
125
|
+
result.addEventListener("click", action);
|
|
126
|
+
return result;
|
|
127
|
+
};
|
|
128
|
+
const dialog = element("dialog", "width:min(96vw,1800px);max-width:none;height:90vh;max-height:none;padding:0;border:1px solid #999;display:flex;flex-direction:column;overflow:hidden");
|
|
129
|
+
const top = element("div", "padding:0.5em 0.8em;background:#f4f4f4;border-bottom:1px solid #ccc;flex-shrink:0");
|
|
130
|
+
const fileName = decodeURIComponent(hooks.source.split("/").pop() ?? hooks.source);
|
|
131
|
+
const heading = element("h3", "margin:0 0 0.2em 0", `✎ Transcript: ${fileName}`);
|
|
132
|
+
const hint = element("div", "font-size:0.85em;color:#555", "Click a word to hear it. Double-click it, or press Enter, to type a fix: a space makes two words, and an empty word is deleted. " +
|
|
133
|
+
"Drag the blue edges to change when it's said. Double-click an empty spot to add a word. " +
|
|
134
|
+
"↑ ↓ move between words, Enter in the text goes to the next one, Space or ⌥Space plays. The darker sound isn't used in this video.");
|
|
135
|
+
const toolbar = element("div", "display:flex;gap:0.5em;align-items:center;flex-wrap:wrap;margin-top:0.4em");
|
|
136
|
+
const rowSelect = element("select");
|
|
137
|
+
for (const seconds of [2, 4, 8, 16, 32]) {
|
|
138
|
+
const option = element("option", "", `${seconds} s`);
|
|
139
|
+
option.value = String(seconds * 1000);
|
|
140
|
+
rowSelect.append(option);
|
|
141
|
+
}
|
|
142
|
+
rowSelect.value = String(rowMs);
|
|
143
|
+
rowSelect.title =
|
|
144
|
+
"How much sound each row shows. Less gives words more room.";
|
|
145
|
+
const rowLabel = element("label", "", "Each row: ");
|
|
146
|
+
rowLabel.append(rowSelect);
|
|
147
|
+
const playBtn = button("▶ Play", "Play from the selected word, or from where you clicked, until you stop it. ⌥Space.", () => togglePlay());
|
|
148
|
+
const nextFlaggedBtn = button("Next flagged ⏭", "Go to the next red or orange word, and play it.", () => goToNextFlagged());
|
|
149
|
+
const counts = element("span", "font-size:0.85em;color:#555");
|
|
150
|
+
toolbar.append(rowLabel, playBtn, nextFlaggedBtn, counts);
|
|
151
|
+
const wordBar = element("div", "display:flex;gap:0.4em;align-items:center;flex-wrap:wrap;margin-top:0.4em");
|
|
152
|
+
const previousBtn = button("↑", "The previous word (↑).", () => step(-1));
|
|
153
|
+
const nextBtn = button("↓", "The next word (↓).", () => step(1));
|
|
154
|
+
const textInput = element("input", "width:16em");
|
|
155
|
+
textInput.type = "text";
|
|
156
|
+
textInput.placeholder = "type the word";
|
|
157
|
+
textInput.title =
|
|
158
|
+
"What was said. A space makes two words. Empty deletes the word.";
|
|
159
|
+
const secondsInput = (title) => {
|
|
160
|
+
const input = element("input", "width:6em");
|
|
161
|
+
input.type = "number";
|
|
162
|
+
input.step = "0.01";
|
|
163
|
+
input.min = "0";
|
|
164
|
+
input.title = title;
|
|
165
|
+
return input;
|
|
166
|
+
};
|
|
167
|
+
const startInput = secondsInput("When the word starts, in seconds from the start of the file.");
|
|
168
|
+
const endInput = secondsInput("When the word ends, in seconds from the start of the file.");
|
|
169
|
+
const playWordBtn = button("▶ Word", "Hear the selected word.", () => playWord());
|
|
170
|
+
const okBtn = button("✓ OK", "This word is right as it is: clear its flag, and go to the next flagged word.", () => markOk());
|
|
171
|
+
const addBtn = button("+ After", "Add a word after this one, in the gap if there is one, or else in the second half of this word.", () => addAfter());
|
|
172
|
+
const mergeBtn = button("Merge →", "Join this word and the next into one word, with no space. Then fix the text.", () => mergeWithNext());
|
|
173
|
+
const deleteBtn = button("🗑 Delete", "Delete this word.", () => deleteSelected());
|
|
174
|
+
wordBar.append(previousBtn, nextBtn, element("span", "", "Word:"), textInput, element("span", "", "Start:"), startInput, element("span", "", "End:"), endInput, playWordBtn, okBtn, addBtn, mergeBtn, deleteBtn);
|
|
175
|
+
const info = element("div", "font-size:0.85em;color:#a06000;min-height:1.2em;margin-top:0.3em");
|
|
176
|
+
top.append(heading, hint, toolbar, wordBar, info);
|
|
177
|
+
const list = element("div", "flex:1;overflow-y:auto;padding:0.5em 0.8em;background:#fff;outline:none");
|
|
178
|
+
// Focusable, so the keys work after a click on the words. See the keydown handler.
|
|
179
|
+
list.tabIndex = -1;
|
|
180
|
+
const bottom = element("div", "display:flex;gap:0.5em;align-items:center;padding:0.5em 0.8em;background:#f4f4f4;border-top:1px solid #ccc;flex-shrink:0");
|
|
181
|
+
const status = element("div", "flex:1;font-size:0.85em;color:#555", "Every change is made right away. The timeline, 📖 Text and the subtitles follow.");
|
|
182
|
+
const revertBtn = button("Revert", "Put back every word the way it was when this opened.", () => revert());
|
|
183
|
+
revertBtn.disabled = true;
|
|
184
|
+
const doneBtn = button("Done", "Close. Every change has already been made.", () => dialog.close());
|
|
185
|
+
bottom.append(status, revertBtn, doneBtn);
|
|
186
|
+
dialog.append(top, list, bottom);
|
|
187
|
+
/** A line that follows the sound while it plays. */
|
|
188
|
+
const playhead = element("div", `position:absolute;top:0;width:2px;height:${ROW_HEIGHT}px;background:#e00;pointer-events:none;z-index:1`);
|
|
189
|
+
let holders = [];
|
|
190
|
+
const live = new Map();
|
|
191
|
+
let observer;
|
|
192
|
+
const newCanvas = (width, height) => {
|
|
193
|
+
const canvas = element("canvas", `position:absolute;left:0;top:0;width:${width}px;height:${height}px`);
|
|
194
|
+
const dpr = devicePixelRatio || 1;
|
|
195
|
+
canvas.width = Math.round(width * dpr);
|
|
196
|
+
canvas.height = Math.round(height * dpr);
|
|
197
|
+
return canvas;
|
|
198
|
+
};
|
|
199
|
+
const context = (canvas, width) => {
|
|
200
|
+
const result = canvas.getContext("2d");
|
|
201
|
+
const scale = canvas.width / width;
|
|
202
|
+
result.setTransform(scale, 0, 0, scale, 0, 0);
|
|
203
|
+
return result;
|
|
204
|
+
};
|
|
205
|
+
function buildRows() {
|
|
206
|
+
observer?.disconnect();
|
|
207
|
+
live.clear();
|
|
208
|
+
holders = [];
|
|
209
|
+
const count = Math.max(1, Math.ceil(totalMs() / rowMs));
|
|
210
|
+
const rows = [];
|
|
211
|
+
for (let r = 0; r < count; r++) {
|
|
212
|
+
const row = element("div", `display:flex;gap:0.5em;height:${ROW_HEIGHT}px;margin-bottom:${ROW_GAP}px`);
|
|
213
|
+
const label = element("div", "width:3.5em;flex-shrink:0;font:12px monospace;color:#555;text-align:right", formatTime(r * rowMs));
|
|
214
|
+
const holder = element("div", `position:relative;flex:1;min-width:0;height:${ROW_HEIGHT}px;background:#eee`);
|
|
215
|
+
holder.dataset.row = String(r);
|
|
216
|
+
row.append(label, holder);
|
|
217
|
+
rows.push(row);
|
|
218
|
+
holders.push(holder);
|
|
219
|
+
}
|
|
220
|
+
list.replaceChildren(...rows);
|
|
221
|
+
observer = new IntersectionObserver((entries) => {
|
|
222
|
+
for (const entry of entries) {
|
|
223
|
+
const r = Number(entry.target.dataset.row);
|
|
224
|
+
if (entry.isIntersecting)
|
|
225
|
+
mount(r);
|
|
226
|
+
else
|
|
227
|
+
unmount(r);
|
|
228
|
+
}
|
|
229
|
+
}, { root: list, rootMargin: "400px 0px" });
|
|
230
|
+
for (const holder of holders)
|
|
231
|
+
observer.observe(holder);
|
|
232
|
+
}
|
|
233
|
+
function mount(r) {
|
|
234
|
+
const holder = holders[r];
|
|
235
|
+
if (!holder || live.has(r))
|
|
236
|
+
return;
|
|
237
|
+
const width = holder.clientWidth;
|
|
238
|
+
if (width <= 0)
|
|
239
|
+
return;
|
|
240
|
+
const wave = newCanvas(width, WAVE_HEIGHT);
|
|
241
|
+
const overlay = newCanvas(width, ROW_HEIGHT);
|
|
242
|
+
overlay.addEventListener("pointerdown", (event) => onPointerDown(event, r));
|
|
243
|
+
overlay.addEventListener("pointermove", (event) => onPointerMove(event, r));
|
|
244
|
+
overlay.addEventListener("pointerup", endDrag);
|
|
245
|
+
overlay.addEventListener("pointercancel", endDrag);
|
|
246
|
+
overlay.addEventListener("dblclick", (event) => onDoubleClick(event, r));
|
|
247
|
+
holder.append(wave, overlay);
|
|
248
|
+
live.set(r, { wave, overlay, width });
|
|
249
|
+
drawWave(r);
|
|
250
|
+
drawOverlay(r);
|
|
251
|
+
}
|
|
252
|
+
function unmount(r) {
|
|
253
|
+
const row = live.get(r);
|
|
254
|
+
if (!row)
|
|
255
|
+
return;
|
|
256
|
+
row.wave.remove();
|
|
257
|
+
row.overlay.remove();
|
|
258
|
+
live.delete(r);
|
|
259
|
+
}
|
|
260
|
+
/** Min / max bars, one per pixel column, like the timeline. */
|
|
261
|
+
function drawWave(r) {
|
|
262
|
+
const row = live.get(r);
|
|
263
|
+
if (!row)
|
|
264
|
+
return;
|
|
265
|
+
const { wave, width } = row;
|
|
266
|
+
const ctx = context(wave, width);
|
|
267
|
+
ctx.fillStyle = "#3aab3a";
|
|
268
|
+
ctx.fillRect(0, 0, width, WAVE_HEIGHT);
|
|
269
|
+
const audio = hooks.audio();
|
|
270
|
+
if (!audio) {
|
|
271
|
+
ctx.font = `italic ${FONT}`;
|
|
272
|
+
ctx.fillStyle = "#fff";
|
|
273
|
+
ctx.textBaseline = "middle";
|
|
274
|
+
ctx.fillText("The sound isn't loaded, so there's nothing to show or play.", 6, WAVE_HEIGHT / 2);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const data = audio.getChannelData(0);
|
|
278
|
+
const samplesPerMs = audio.sampleRate / 1000;
|
|
279
|
+
const fromMs = r * rowMs;
|
|
280
|
+
const middle = WAVE_HEIGHT / 2;
|
|
281
|
+
const half = WAVE_HEIGHT / 2 - 1;
|
|
282
|
+
ctx.fillStyle = "rgba(255, 255, 255, 0.8)";
|
|
283
|
+
for (let px = 0; px < width; px++) {
|
|
284
|
+
const from = Math.floor((fromMs + (px / width) * rowMs) * samplesPerMs);
|
|
285
|
+
if (from >= data.length)
|
|
286
|
+
break;
|
|
287
|
+
const to = Math.min(data.length, Math.max(from + 1, Math.floor((fromMs + ((px + 1) / width) * rowMs) * samplesPerMs)));
|
|
288
|
+
let low = data[from];
|
|
289
|
+
let high = low;
|
|
290
|
+
for (let i = from + 1; i < to; i++) {
|
|
291
|
+
const v = data[i];
|
|
292
|
+
if (v < low)
|
|
293
|
+
low = v;
|
|
294
|
+
else if (v > high)
|
|
295
|
+
high = v;
|
|
296
|
+
}
|
|
297
|
+
ctx.fillRect(px, middle - high * half, 1, Math.max(1, (high - low) * half));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/** Everything over the waveform: the shading, the words, the selection. */
|
|
301
|
+
function drawOverlay(r) {
|
|
302
|
+
const row = live.get(r);
|
|
303
|
+
if (!row)
|
|
304
|
+
return;
|
|
305
|
+
const { overlay, width } = row;
|
|
306
|
+
const ctx = context(overlay, width);
|
|
307
|
+
ctx.clearRect(0, 0, width, ROW_HEIGHT);
|
|
308
|
+
const fromMs = r * rowMs;
|
|
309
|
+
const toMs = fromMs + rowMs;
|
|
310
|
+
const x = (ms) => ((ms - fromMs) / rowMs) * width;
|
|
311
|
+
// The parts of the file this video doesn't play.
|
|
312
|
+
ctx.fillStyle = "rgba(0, 0, 0, 0.45)";
|
|
313
|
+
let shadeFrom = fromMs;
|
|
314
|
+
for (const range of used) {
|
|
315
|
+
if (range.toMs <= shadeFrom)
|
|
316
|
+
continue;
|
|
317
|
+
if (range.fromMs >= toMs)
|
|
318
|
+
break;
|
|
319
|
+
if (range.fromMs > shadeFrom) {
|
|
320
|
+
ctx.fillRect(x(shadeFrom), 0, x(range.fromMs) - x(shadeFrom), WAVE_HEIGHT);
|
|
321
|
+
}
|
|
322
|
+
shadeFrom = range.toMs;
|
|
323
|
+
}
|
|
324
|
+
if (shadeFrom < toMs) {
|
|
325
|
+
ctx.fillRect(x(shadeFrom), 0, width - x(shadeFrom), WAVE_HEIGHT);
|
|
326
|
+
}
|
|
327
|
+
ctx.fillStyle = "#2a2a2a";
|
|
328
|
+
ctx.fillRect(0, WAVE_HEIGHT, width, 2 * LANE_HEIGHT);
|
|
329
|
+
ctx.font = FONT;
|
|
330
|
+
ctx.textBaseline = "middle";
|
|
331
|
+
for (let i = firstWordEndingAfter(fromMs); i < words.length; i++) {
|
|
332
|
+
const word = words[i];
|
|
333
|
+
if (word.startMs >= toMs)
|
|
334
|
+
break;
|
|
335
|
+
const x0 = x(word.startMs);
|
|
336
|
+
const x1 = x(word.endMs);
|
|
337
|
+
const top = WAVE_HEIGHT + (i % 2) * LANE_HEIGHT;
|
|
338
|
+
const flag = wordFlag(word);
|
|
339
|
+
// Where each word starts, on the sound.
|
|
340
|
+
ctx.fillStyle = "rgba(255, 255, 255, 0.6)";
|
|
341
|
+
ctx.fillRect(Math.round(x0), 0, 1, WAVE_HEIGHT);
|
|
342
|
+
ctx.fillStyle =
|
|
343
|
+
i === selected
|
|
344
|
+
? SELECTED_COLOR
|
|
345
|
+
: flag
|
|
346
|
+
? WORD_FLAG_COLORS[flag]
|
|
347
|
+
: word.timing === "edited"
|
|
348
|
+
? EDITED_COLOR
|
|
349
|
+
: PLAIN_COLOR;
|
|
350
|
+
ctx.fillRect(x0, top + 1, Math.max(2, x1 - x0), LANE_HEIGHT - 2);
|
|
351
|
+
// The label can run past its word, up to the next word in the same lane.
|
|
352
|
+
const nextInLane = words[i + 2];
|
|
353
|
+
const limit = Math.min(width, nextInLane ? x(nextInLane.startMs) : width);
|
|
354
|
+
const textX = Math.max(x0, 0) + 3;
|
|
355
|
+
const text = fitText(ctx, labelOf(word), limit - 3 - textX);
|
|
356
|
+
if (text) {
|
|
357
|
+
ctx.fillStyle = "#fff";
|
|
358
|
+
ctx.fillText(text, textX, top + LANE_HEIGHT / 2);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
const chosen = selected === undefined ? undefined : words[selected];
|
|
362
|
+
if (chosen && chosen.endMs > fromMs && chosen.startMs < toMs) {
|
|
363
|
+
const x0 = x(chosen.startMs);
|
|
364
|
+
const x1 = x(chosen.endMs);
|
|
365
|
+
ctx.fillStyle = "rgba(26, 115, 232, 0.3)";
|
|
366
|
+
ctx.fillRect(x0, 0, x1 - x0, WAVE_HEIGHT);
|
|
367
|
+
// The edges, with handles to drag.
|
|
368
|
+
ctx.fillStyle = SELECTED_COLOR;
|
|
369
|
+
for (const edge of [x0, x1]) {
|
|
370
|
+
ctx.fillRect(edge - 1, 0, 2, ROW_HEIGHT);
|
|
371
|
+
ctx.fillRect(edge - 4, WAVE_HEIGHT / 2 - 9, 8, 18);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
else if (selected === undefined &&
|
|
375
|
+
cursorMs >= fromMs &&
|
|
376
|
+
cursorMs < toMs) {
|
|
377
|
+
ctx.fillStyle = "#ffd400";
|
|
378
|
+
ctx.fillRect(x(cursorMs) - 1, 0, 2, ROW_HEIGHT);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
function drawAllOverlays() {
|
|
382
|
+
for (const r of live.keys())
|
|
383
|
+
drawOverlay(r);
|
|
384
|
+
}
|
|
385
|
+
/** The first word that ends after `ms`, or words.length. */
|
|
386
|
+
function firstWordEndingAfter(ms) {
|
|
387
|
+
let low = 0;
|
|
388
|
+
let high = words.length;
|
|
389
|
+
while (low < high) {
|
|
390
|
+
const middle = (low + high) >> 1;
|
|
391
|
+
if (words[middle].endMs > ms)
|
|
392
|
+
high = middle;
|
|
393
|
+
else
|
|
394
|
+
low = middle + 1;
|
|
395
|
+
}
|
|
396
|
+
return low;
|
|
397
|
+
}
|
|
398
|
+
/** Bring the row holding `ms` into view. */
|
|
399
|
+
function reveal(ms, block = "nearest") {
|
|
400
|
+
const holder = holders[Math.floor(ms / rowMs)];
|
|
401
|
+
holder?.parentElement?.scrollIntoView({ block });
|
|
402
|
+
}
|
|
403
|
+
// MARK: Changes
|
|
404
|
+
/** Make a change, everywhere at once. */
|
|
405
|
+
function apply(newWords) {
|
|
406
|
+
words = newWords;
|
|
407
|
+
hooks.setWords(words);
|
|
408
|
+
revertBtn.disabled = false;
|
|
409
|
+
drawAllOverlays();
|
|
410
|
+
showCounts();
|
|
411
|
+
}
|
|
412
|
+
function replaceWord(index, word) {
|
|
413
|
+
apply(words.toSpliced(index, 1, word));
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Finish typing in the text box: a space makes several words, and nothing deletes the
|
|
417
|
+
* word. Call before anything that moves the selection.
|
|
418
|
+
* @returns The index of the word after the one that was typed in.
|
|
419
|
+
*/
|
|
420
|
+
function commitText() {
|
|
421
|
+
if (selected === undefined)
|
|
422
|
+
return undefined;
|
|
423
|
+
const index = selected;
|
|
424
|
+
if (!textTouched)
|
|
425
|
+
return index + 1;
|
|
426
|
+
textTouched = false;
|
|
427
|
+
const word = words[index];
|
|
428
|
+
if (!word)
|
|
429
|
+
return undefined;
|
|
430
|
+
const parts = word.text.trim().split(/\s+/).filter(Boolean);
|
|
431
|
+
if (parts.length === 0) {
|
|
432
|
+
selected = undefined;
|
|
433
|
+
apply(words.toSpliced(index, 1));
|
|
434
|
+
return index;
|
|
435
|
+
}
|
|
436
|
+
if (parts.length === 1) {
|
|
437
|
+
if (parts[0] !== word.text)
|
|
438
|
+
replaceWord(index, { ...word, text: parts[0] });
|
|
439
|
+
return index + 1;
|
|
440
|
+
}
|
|
441
|
+
apply(words.toSpliced(index, 1, ...splitWord(word, parts)));
|
|
442
|
+
return index + parts.length;
|
|
443
|
+
}
|
|
444
|
+
function select(index, focusText = false) {
|
|
445
|
+
selected =
|
|
446
|
+
index === undefined || words.length === 0
|
|
447
|
+
? undefined
|
|
448
|
+
: Math.max(0, Math.min(words.length - 1, index));
|
|
449
|
+
textTouched = false;
|
|
450
|
+
showSelected();
|
|
451
|
+
drawAllOverlays();
|
|
452
|
+
if (selected !== undefined) {
|
|
453
|
+
reveal(words[selected].startMs);
|
|
454
|
+
if (focusText) {
|
|
455
|
+
textInput.focus();
|
|
456
|
+
textInput.select();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
/** Move the selection by `delta` words, and play the new one. */
|
|
461
|
+
function step(delta, focusText = true) {
|
|
462
|
+
const after = commitText();
|
|
463
|
+
if (selected === undefined && after === undefined) {
|
|
464
|
+
select(firstWordEndingAfter(cursorMs), focusText);
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
select(delta > 0 ? after : (selected ?? after ?? 0) - 1, focusText);
|
|
468
|
+
}
|
|
469
|
+
playWord();
|
|
470
|
+
}
|
|
471
|
+
function retimeSelected(startMs, endMs) {
|
|
472
|
+
if (selected === undefined)
|
|
473
|
+
return;
|
|
474
|
+
const word = words[selected];
|
|
475
|
+
const previous = words[selected - 1];
|
|
476
|
+
const next = words[selected + 1];
|
|
477
|
+
// Never past a neighbor, unless it was already there.
|
|
478
|
+
const earliest = previous ? Math.min(previous.endMs, word.startMs) : 0;
|
|
479
|
+
const latest = next
|
|
480
|
+
? Math.max(next.startMs, word.endMs)
|
|
481
|
+
: Math.max(totalMs(), word.endMs);
|
|
482
|
+
const start = Math.round(Math.max(earliest, Math.min(startMs, endMs - MIN_WORD_MS)));
|
|
483
|
+
const end = Math.round(Math.min(latest, Math.max(endMs, start + MIN_WORD_MS)));
|
|
484
|
+
replaceWord(selected, retimed(word, start, end));
|
|
485
|
+
}
|
|
486
|
+
function markOk() {
|
|
487
|
+
const after = commitText();
|
|
488
|
+
if (selected !== undefined) {
|
|
489
|
+
replaceWord(selected, { ...words[selected], timing: "edited" });
|
|
490
|
+
}
|
|
491
|
+
goToNextFlagged(after);
|
|
492
|
+
}
|
|
493
|
+
function deleteSelected() {
|
|
494
|
+
if (selected === undefined)
|
|
495
|
+
return;
|
|
496
|
+
const index = selected;
|
|
497
|
+
selected = undefined;
|
|
498
|
+
apply(words.toSpliced(index, 1));
|
|
499
|
+
select(Math.min(index, words.length - 1));
|
|
500
|
+
}
|
|
501
|
+
function addAfter() {
|
|
502
|
+
const after = commitText();
|
|
503
|
+
if (selected === undefined || after === undefined)
|
|
504
|
+
return;
|
|
505
|
+
const index = after - 1;
|
|
506
|
+
const word = words[index];
|
|
507
|
+
const next = words[index + 1];
|
|
508
|
+
const gapEnd = next ? next.startMs : totalMs();
|
|
509
|
+
if (gapEnd - word.endMs >= 100) {
|
|
510
|
+
const added = newWord(word.endMs, Math.min(gapEnd, word.endMs + NEW_WORD_MS));
|
|
511
|
+
apply(words.toSpliced(index + 1, 0, added));
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
// No room: take the second half of this word.
|
|
515
|
+
const middle = Math.round((word.startMs + word.endMs) / 2);
|
|
516
|
+
const added = newWord(middle, word.endMs);
|
|
517
|
+
apply(words.toSpliced(index, 1, retimed(word, word.startMs, middle), added));
|
|
518
|
+
}
|
|
519
|
+
select(index + 1, true);
|
|
520
|
+
}
|
|
521
|
+
/** A new word where someone double-clicked, if there's room. */
|
|
522
|
+
function addAt(ms) {
|
|
523
|
+
commitText();
|
|
524
|
+
const index = firstWordEndingAfter(ms);
|
|
525
|
+
const previous = words[index - 1];
|
|
526
|
+
const next = words[index];
|
|
527
|
+
if (next && next.startMs <= ms)
|
|
528
|
+
return; // On a word, not in a gap.
|
|
529
|
+
const startMs = Math.round(Math.max(ms, previous?.endMs ?? 0));
|
|
530
|
+
const endMs = Math.round(Math.min(startMs + NEW_WORD_MS, next ? next.startMs : totalMs()));
|
|
531
|
+
if (endMs - startMs < MIN_WORD_MS)
|
|
532
|
+
return;
|
|
533
|
+
apply(words.toSpliced(index, 0, newWord(startMs, endMs)));
|
|
534
|
+
select(index, true);
|
|
535
|
+
}
|
|
536
|
+
function mergeWithNext() {
|
|
537
|
+
commitText();
|
|
538
|
+
if (selected === undefined)
|
|
539
|
+
return;
|
|
540
|
+
const index = selected;
|
|
541
|
+
const word = words[index];
|
|
542
|
+
const next = words[index + 1];
|
|
543
|
+
if (!next)
|
|
544
|
+
return;
|
|
545
|
+
const merged = {
|
|
546
|
+
...word,
|
|
547
|
+
text: word.text.trim() + next.text.trim(),
|
|
548
|
+
endMs: Math.max(word.endMs, next.endMs),
|
|
549
|
+
cutAfterMs: Math.max(word.cutAfterMs, next.cutAfterMs),
|
|
550
|
+
timing: "edited",
|
|
551
|
+
confidence: Math.min(word.confidence, next.confidence),
|
|
552
|
+
heard: [word.heard, next.heard].filter(Boolean).join(" "),
|
|
553
|
+
};
|
|
554
|
+
apply(words.toSpliced(index, 2, merged));
|
|
555
|
+
select(index, true);
|
|
556
|
+
}
|
|
557
|
+
function goToNextFlagged(from) {
|
|
558
|
+
const start = from ?? (selected ?? firstWordEndingAfter(cursorMs) - 1) + 1;
|
|
559
|
+
for (let k = 0; k < words.length; k++) {
|
|
560
|
+
const index = (start + k) % words.length;
|
|
561
|
+
if (wordFlag(words[index])) {
|
|
562
|
+
select(index, true);
|
|
563
|
+
reveal(words[index].startMs, "center");
|
|
564
|
+
playWord();
|
|
565
|
+
info.textContent =
|
|
566
|
+
(index < start ? "Back at the start. " : "") + info.textContent;
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
info.textContent = "No flagged words left.";
|
|
571
|
+
}
|
|
572
|
+
function revert() {
|
|
573
|
+
stop();
|
|
574
|
+
selected = undefined;
|
|
575
|
+
textTouched = false;
|
|
576
|
+
apply([...original]);
|
|
577
|
+
revertBtn.disabled = true;
|
|
578
|
+
showSelected();
|
|
579
|
+
}
|
|
580
|
+
// MARK: The selected word
|
|
581
|
+
const seconds = (ms) => String(Math.round(ms) / 1000);
|
|
582
|
+
function showSelected() {
|
|
583
|
+
const word = selected === undefined ? undefined : words[selected];
|
|
584
|
+
for (const control of [
|
|
585
|
+
textInput,
|
|
586
|
+
startInput,
|
|
587
|
+
endInput,
|
|
588
|
+
playWordBtn,
|
|
589
|
+
okBtn,
|
|
590
|
+
addBtn,
|
|
591
|
+
mergeBtn,
|
|
592
|
+
deleteBtn,
|
|
593
|
+
]) {
|
|
594
|
+
control.disabled = !word;
|
|
595
|
+
}
|
|
596
|
+
mergeBtn.disabled = !word || selected === words.length - 1;
|
|
597
|
+
if (!word) {
|
|
598
|
+
textInput.value = "";
|
|
599
|
+
startInput.value = "";
|
|
600
|
+
endInput.value = "";
|
|
601
|
+
info.textContent = words.length
|
|
602
|
+
? "Click a word to select it."
|
|
603
|
+
: "No words yet. Double-click an empty spot to add one.";
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
textInput.value = word.text;
|
|
607
|
+
startInput.value = seconds(word.startMs);
|
|
608
|
+
endInput.value = seconds(word.endMs);
|
|
609
|
+
const flag = wordFlag(word);
|
|
610
|
+
const notes = [];
|
|
611
|
+
if (flag === "left out") {
|
|
612
|
+
notes.push("Speech Whisper left out: probably a repeat, a false start or an um. " +
|
|
613
|
+
"It's not in the subtitles unless you fix it or press ✓ OK. Delete it if it's just noise.");
|
|
614
|
+
}
|
|
615
|
+
else if (flag === "doubtful") {
|
|
616
|
+
notes.push(word.heard
|
|
617
|
+
? `The letter model heard “${word.heard}”. Worth a listen.`
|
|
618
|
+
: "The letter model heard nothing here. Worth a listen.");
|
|
619
|
+
}
|
|
620
|
+
else if (word.timing === "edited") {
|
|
621
|
+
notes.push("Edited.");
|
|
622
|
+
}
|
|
623
|
+
if (word.timing !== "edited") {
|
|
624
|
+
notes.push(`${Math.round(word.confidence * 100)}% sure.`);
|
|
625
|
+
}
|
|
626
|
+
info.textContent = notes.join(" ");
|
|
627
|
+
}
|
|
628
|
+
function showCounts() {
|
|
629
|
+
const flagged = words.filter((word) => wordFlag(word)).length;
|
|
630
|
+
const edited = words.filter((word) => word.timing === "edited").length;
|
|
631
|
+
counts.textContent = `${words.length} words · ${flagged} flagged · ${edited} edited`;
|
|
632
|
+
}
|
|
633
|
+
textInput.addEventListener("input", () => {
|
|
634
|
+
if (selected === undefined)
|
|
635
|
+
return;
|
|
636
|
+
textTouched = true;
|
|
637
|
+
replaceWord(selected, {
|
|
638
|
+
...words[selected],
|
|
639
|
+
text: textInput.value,
|
|
640
|
+
timing: "edited",
|
|
641
|
+
});
|
|
642
|
+
});
|
|
643
|
+
// Clicking elsewhere finishes the word, so a space there makes two words right away.
|
|
644
|
+
textInput.addEventListener("blur", () => {
|
|
645
|
+
if (!textTouched)
|
|
646
|
+
return;
|
|
647
|
+
commitText();
|
|
648
|
+
showSelected();
|
|
649
|
+
});
|
|
650
|
+
const onSecondsInput = () => {
|
|
651
|
+
if (selected === undefined)
|
|
652
|
+
return;
|
|
653
|
+
const word = words[selected];
|
|
654
|
+
const start = startInput.valueAsNumber * 1000;
|
|
655
|
+
const end = endInput.valueAsNumber * 1000;
|
|
656
|
+
if (!Number.isFinite(start) || !Number.isFinite(end))
|
|
657
|
+
return;
|
|
658
|
+
if (start === word.startMs && end === word.endMs)
|
|
659
|
+
return;
|
|
660
|
+
retimeSelected(start, end);
|
|
661
|
+
};
|
|
662
|
+
startInput.addEventListener("input", onSecondsInput);
|
|
663
|
+
endInput.addEventListener("input", onSecondsInput);
|
|
664
|
+
// While typing, a number can be out of range for a moment. Afterwards, show what was used.
|
|
665
|
+
for (const input of [startInput, endInput]) {
|
|
666
|
+
input.addEventListener("blur", () => showSelected());
|
|
667
|
+
}
|
|
668
|
+
// MARK: Mouse
|
|
669
|
+
let drag;
|
|
670
|
+
const where = (event, r) => {
|
|
671
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
672
|
+
const x = event.clientX - rect.left;
|
|
673
|
+
return {
|
|
674
|
+
x,
|
|
675
|
+
y: event.clientY - rect.top,
|
|
676
|
+
width: rect.width,
|
|
677
|
+
ms: r * rowMs + (x / rect.width) * rowMs,
|
|
678
|
+
};
|
|
679
|
+
};
|
|
680
|
+
/** The edge of the selected word under `x`, if any: the closer one. */
|
|
681
|
+
function edgeAt(r, x, width) {
|
|
682
|
+
const word = selected === undefined ? undefined : words[selected];
|
|
683
|
+
if (!word)
|
|
684
|
+
return undefined;
|
|
685
|
+
const toX = (ms) => ((ms - r * rowMs) / rowMs) * width;
|
|
686
|
+
const start = Math.abs(toX(word.startMs) - x);
|
|
687
|
+
const end = Math.abs(toX(word.endMs) - x);
|
|
688
|
+
if (Math.min(start, end) > EDGE_GRAB_PX)
|
|
689
|
+
return undefined;
|
|
690
|
+
return start <= end ? "start" : "end";
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* The word under the mouse. Over the sound, the word being said. Over the words, the word
|
|
694
|
+
* or its label.
|
|
695
|
+
*/
|
|
696
|
+
function wordAt(r, x, y, width, ms) {
|
|
697
|
+
if (y < WAVE_HEIGHT) {
|
|
698
|
+
const index = firstWordEndingAfter(ms);
|
|
699
|
+
const word = words[index];
|
|
700
|
+
return word && word.startMs <= ms ? index : undefined;
|
|
701
|
+
}
|
|
702
|
+
const lane = y < WAVE_HEIGHT + LANE_HEIGHT ? 0 : 1;
|
|
703
|
+
const toX = (t) => ((t - r * rowMs) / rowMs) * width;
|
|
704
|
+
// The last word in this lane that starts before the mouse.
|
|
705
|
+
let index = firstWordEndingAfter(ms);
|
|
706
|
+
while (index >= 0 &&
|
|
707
|
+
(index % 2 !== lane || (words[index]?.startMs ?? Infinity) > ms)) {
|
|
708
|
+
index--;
|
|
709
|
+
}
|
|
710
|
+
const word = words[index];
|
|
711
|
+
if (!word)
|
|
712
|
+
return undefined;
|
|
713
|
+
const right = Math.max(toX(word.endMs), Math.max(toX(word.startMs), 0) +
|
|
714
|
+
3 +
|
|
715
|
+
measure.measureText(labelOf(word)).width);
|
|
716
|
+
return x <= right ? index : undefined;
|
|
717
|
+
}
|
|
718
|
+
function onPointerDown(event, r) {
|
|
719
|
+
if (event.button !== 0)
|
|
720
|
+
return;
|
|
721
|
+
event.preventDefault();
|
|
722
|
+
// Out of the text box, which finishes the word there, so Space plays and Enter edits.
|
|
723
|
+
list.focus({ preventScroll: true });
|
|
724
|
+
const { x, y, width } = where(event, r);
|
|
725
|
+
const edge = edgeAt(r, x, width);
|
|
726
|
+
if (edge) {
|
|
727
|
+
const overlay = event.currentTarget;
|
|
728
|
+
overlay.setPointerCapture(event.pointerId);
|
|
729
|
+
drag = { edge, overlay };
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
commitText();
|
|
733
|
+
const { ms } = where(event, r);
|
|
734
|
+
const index = wordAt(r, x, y, width, ms);
|
|
735
|
+
if (index === undefined) {
|
|
736
|
+
stop();
|
|
737
|
+
cursorMs = ms;
|
|
738
|
+
select(undefined);
|
|
739
|
+
}
|
|
740
|
+
else {
|
|
741
|
+
select(index);
|
|
742
|
+
playWord();
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
function onPointerMove(event, r) {
|
|
746
|
+
const { x, y, width, ms } = where(event, r);
|
|
747
|
+
const overlay = event.currentTarget;
|
|
748
|
+
if (!drag) {
|
|
749
|
+
overlay.style.cursor = edgeAt(r, x, width)
|
|
750
|
+
? "ew-resize"
|
|
751
|
+
: wordAt(r, x, y, width, ms) === undefined
|
|
752
|
+
? "default"
|
|
753
|
+
: "pointer";
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
if (selected === undefined)
|
|
757
|
+
return;
|
|
758
|
+
const word = words[selected];
|
|
759
|
+
if (drag.edge === "start")
|
|
760
|
+
retimeSelected(ms, word.endMs);
|
|
761
|
+
else
|
|
762
|
+
retimeSelected(word.startMs, ms);
|
|
763
|
+
showSelected();
|
|
764
|
+
}
|
|
765
|
+
function endDrag(event) {
|
|
766
|
+
if (!drag)
|
|
767
|
+
return;
|
|
768
|
+
drag.overlay.releasePointerCapture(event.pointerId);
|
|
769
|
+
drag = undefined;
|
|
770
|
+
playWord();
|
|
771
|
+
}
|
|
772
|
+
function onDoubleClick(event, r) {
|
|
773
|
+
const { x, y, width, ms } = where(event, r);
|
|
774
|
+
if (edgeAt(r, x, width))
|
|
775
|
+
return;
|
|
776
|
+
if (wordAt(r, x, y, width, ms) === undefined) {
|
|
777
|
+
addAt(ms);
|
|
778
|
+
}
|
|
779
|
+
else {
|
|
780
|
+
textInput.focus();
|
|
781
|
+
textInput.select();
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
// MARK: Sound
|
|
785
|
+
let audioContext;
|
|
786
|
+
let playing;
|
|
787
|
+
function play(fromMs, toMs, follow) {
|
|
788
|
+
stop();
|
|
789
|
+
const audio = hooks.audio();
|
|
790
|
+
if (!audio)
|
|
791
|
+
return;
|
|
792
|
+
hooks.pausePlayer();
|
|
793
|
+
audioContext ??= new AudioContext();
|
|
794
|
+
void audioContext.resume();
|
|
795
|
+
const node = audioContext.createBufferSource();
|
|
796
|
+
node.buffer = audio;
|
|
797
|
+
node.connect(audioContext.destination);
|
|
798
|
+
const from = Math.max(0, fromMs);
|
|
799
|
+
if (Number.isFinite(toMs)) {
|
|
800
|
+
node.start(0, from / 1000, Math.max(0.01, (toMs - from) / 1000));
|
|
801
|
+
}
|
|
802
|
+
else {
|
|
803
|
+
node.start(0, from / 1000);
|
|
804
|
+
}
|
|
805
|
+
const current = {
|
|
806
|
+
node,
|
|
807
|
+
fromMs: from,
|
|
808
|
+
startedAt: audioContext.currentTime,
|
|
809
|
+
follow,
|
|
810
|
+
};
|
|
811
|
+
playing = current;
|
|
812
|
+
node.addEventListener("ended", () => {
|
|
813
|
+
if (playing === current)
|
|
814
|
+
stop();
|
|
815
|
+
});
|
|
816
|
+
playBtn.textContent = "■ Stop";
|
|
817
|
+
requestAnimationFrame(movePlayhead);
|
|
818
|
+
}
|
|
819
|
+
function stop() {
|
|
820
|
+
if (!playing)
|
|
821
|
+
return;
|
|
822
|
+
const { node } = playing;
|
|
823
|
+
playing = undefined;
|
|
824
|
+
try {
|
|
825
|
+
node.stop();
|
|
826
|
+
}
|
|
827
|
+
catch {
|
|
828
|
+
// It already ended.
|
|
829
|
+
}
|
|
830
|
+
playhead.remove();
|
|
831
|
+
playBtn.textContent = "▶ Play";
|
|
832
|
+
}
|
|
833
|
+
function movePlayhead() {
|
|
834
|
+
if (!playing || !audioContext)
|
|
835
|
+
return;
|
|
836
|
+
const ms = playing.fromMs + (audioContext.currentTime - playing.startedAt) * 1000;
|
|
837
|
+
const r = Math.floor(ms / rowMs);
|
|
838
|
+
const holder = holders[r];
|
|
839
|
+
if (holder) {
|
|
840
|
+
if (playhead.parentElement !== holder) {
|
|
841
|
+
holder.append(playhead);
|
|
842
|
+
if (playing.follow)
|
|
843
|
+
reveal(ms);
|
|
844
|
+
}
|
|
845
|
+
playhead.style.left = `${((ms - r * rowMs) / rowMs) * holder.clientWidth - 1}px`;
|
|
846
|
+
}
|
|
847
|
+
requestAnimationFrame(movePlayhead);
|
|
848
|
+
}
|
|
849
|
+
function playWord() {
|
|
850
|
+
const word = selected === undefined ? undefined : words[selected];
|
|
851
|
+
if (word)
|
|
852
|
+
play(word.cutBeforeMs, word.cutAfterMs, false);
|
|
853
|
+
}
|
|
854
|
+
function togglePlay() {
|
|
855
|
+
if (playing) {
|
|
856
|
+
stop();
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
const word = selected === undefined ? undefined : words[selected];
|
|
860
|
+
play(word ? word.cutBeforeMs : cursorMs, Infinity, true);
|
|
861
|
+
}
|
|
862
|
+
// MARK: Keys
|
|
863
|
+
dialog.addEventListener("keydown", (event) => {
|
|
864
|
+
// The player's own hot keys (⌥Space, ⌥←, Cmd+Z…) mustn't act behind the dialog.
|
|
865
|
+
event.stopPropagation();
|
|
866
|
+
const target = event.target;
|
|
867
|
+
const inText = target === textInput;
|
|
868
|
+
const inField = target instanceof HTMLInputElement || target instanceof HTMLSelectElement;
|
|
869
|
+
/**
|
|
870
|
+
* The word is selected in the text box, as it is after ↑, ↓ or Enter, and nothing typed
|
|
871
|
+
* yet. Then Space plays, rather than replacing the word with a space.
|
|
872
|
+
*/
|
|
873
|
+
const untouchedText = inText &&
|
|
874
|
+
textInput.value !== "" &&
|
|
875
|
+
textInput.selectionStart === 0 &&
|
|
876
|
+
textInput.selectionEnd === textInput.value.length;
|
|
877
|
+
if (event.code === "Space" && (event.altKey || !inField || untouchedText)) {
|
|
878
|
+
event.preventDefault();
|
|
879
|
+
togglePlay();
|
|
880
|
+
}
|
|
881
|
+
else if (event.key === "Enter" &&
|
|
882
|
+
!inField &&
|
|
883
|
+
!(target instanceof HTMLButtonElement) &&
|
|
884
|
+
selected !== undefined) {
|
|
885
|
+
event.preventDefault();
|
|
886
|
+
textInput.focus();
|
|
887
|
+
textInput.select();
|
|
888
|
+
}
|
|
889
|
+
else if ((event.key === "ArrowDown" || event.key === "ArrowUp") &&
|
|
890
|
+
(inText || !inField)) {
|
|
891
|
+
event.preventDefault();
|
|
892
|
+
step(event.key === "ArrowDown" ? 1 : -1);
|
|
893
|
+
}
|
|
894
|
+
else if (event.key === "Enter" && inText) {
|
|
895
|
+
event.preventDefault();
|
|
896
|
+
step(1);
|
|
897
|
+
}
|
|
898
|
+
else if ((event.key === "Delete" || event.key === "Backspace") &&
|
|
899
|
+
!inField) {
|
|
900
|
+
event.preventDefault();
|
|
901
|
+
deleteSelected();
|
|
902
|
+
}
|
|
903
|
+
});
|
|
904
|
+
dialog.addEventListener("keypress", (event) => event.stopPropagation());
|
|
905
|
+
// MARK: Open and close
|
|
906
|
+
rowSelect.addEventListener("input", () => {
|
|
907
|
+
const keep = selected === undefined ? cursorMs : words[selected].startMs;
|
|
908
|
+
rowMs = Number(rowSelect.value);
|
|
909
|
+
stop();
|
|
910
|
+
buildRows();
|
|
911
|
+
reveal(keep, "center");
|
|
912
|
+
});
|
|
913
|
+
// The rows' canvases are as wide as the dialog was. Start over when that changes.
|
|
914
|
+
let lastWidth = 0;
|
|
915
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
916
|
+
const width = list.clientWidth;
|
|
917
|
+
if (width === lastWidth)
|
|
918
|
+
return;
|
|
919
|
+
lastWidth = width;
|
|
920
|
+
for (const r of [...live.keys()])
|
|
921
|
+
unmount(r);
|
|
922
|
+
observer?.disconnect();
|
|
923
|
+
for (const holder of holders)
|
|
924
|
+
observer?.observe(holder);
|
|
925
|
+
});
|
|
926
|
+
// Escape, as well as Done. Every change is already made, so closing is always safe.
|
|
927
|
+
dialog.addEventListener("close", () => {
|
|
928
|
+
commitText();
|
|
929
|
+
stop();
|
|
930
|
+
void audioContext?.close();
|
|
931
|
+
observer?.disconnect();
|
|
932
|
+
resizeObserver.disconnect();
|
|
933
|
+
dialog.remove();
|
|
934
|
+
hooks.closed();
|
|
935
|
+
});
|
|
936
|
+
document.body.append(dialog);
|
|
937
|
+
dialog.showModal();
|
|
938
|
+
// Not the first control, which showModal() picks, so the keys work right away.
|
|
939
|
+
list.focus({ preventScroll: true });
|
|
940
|
+
buildRows();
|
|
941
|
+
lastWidth = list.clientWidth;
|
|
942
|
+
resizeObserver.observe(list);
|
|
943
|
+
showCounts();
|
|
944
|
+
const first = firstWordEndingAfter(hooks.startAtMs);
|
|
945
|
+
select(first < words.length ? first : undefined);
|
|
946
|
+
reveal(words[selected ?? -1]?.startMs ?? cursorMs, "center");
|
|
947
|
+
}
|
|
948
|
+
//# sourceMappingURL=transcript-editor.js.map
|