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.
Files changed (260) hide show
  1. package/README.md +10 -2
  2. package/dev/canvas-recorder.css +20 -6
  3. package/dev/canvas-recorder.ts +389 -54
  4. package/dev/color-picker.ts +13 -0
  5. package/dev/component-picker.ts +60 -11
  6. package/dev/delay-files.ts +3 -1
  7. package/dev/font-widgets.ts +2 -2
  8. package/dev/format-editor.ts +3 -1
  9. package/dev/fourier-panel.ts +7 -3
  10. package/dev/handwriting-panel.ts +7 -7
  11. package/dev/multi-text-dialog.ts +28 -15
  12. package/dev/player-body.ts +78 -33
  13. package/dev/schedule-helpers.ts +10 -3
  14. package/dev/static-image-panel.ts +4 -4
  15. package/dev/subtitles.ts +170 -0
  16. package/dev/synced-file.ts +34 -0
  17. package/dev/timeline-display.ts +1 -1
  18. package/dev/transcribe-core.ts +7 -4
  19. package/dev/transcript-align.ts +24 -18
  20. package/dev/transcript-editor.ts +1187 -0
  21. package/dev/transcript-export.ts +90 -0
  22. package/dev/transcript-view.ts +8 -5
  23. package/dev/transcripts.ts +140 -15
  24. package/dev/video-clip-panel.ts +4 -4
  25. package/dist/dev/canvas-recorder.css +20 -6
  26. package/dist/dev/canvas-recorder.js +314 -48
  27. package/dist/dev/canvas-recorder.js.map +1 -1
  28. package/dist/dev/color-picker.d.ts.map +1 -1
  29. package/dist/dev/color-picker.js +15 -0
  30. package/dist/dev/color-picker.js.map +1 -1
  31. package/dist/dev/component-picker.d.ts +2 -1
  32. package/dist/dev/component-picker.d.ts.map +1 -1
  33. package/dist/dev/component-picker.js +56 -11
  34. package/dist/dev/component-picker.js.map +1 -1
  35. package/dist/dev/delay-files.d.ts.map +1 -1
  36. package/dist/dev/delay-files.js +3 -1
  37. package/dist/dev/delay-files.js.map +1 -1
  38. package/dist/dev/font-widgets.d.ts +2 -2
  39. package/dist/dev/font-widgets.d.ts.map +1 -1
  40. package/dist/dev/font-widgets.js.map +1 -1
  41. package/dist/dev/format-editor.d.ts.map +1 -1
  42. package/dist/dev/format-editor.js +3 -1
  43. package/dist/dev/format-editor.js.map +1 -1
  44. package/dist/dev/fourier-panel.d.ts.map +1 -1
  45. package/dist/dev/fourier-panel.js +4 -3
  46. package/dist/dev/fourier-panel.js.map +1 -1
  47. package/dist/dev/handwriting-panel.d.ts +4 -4
  48. package/dist/dev/handwriting-panel.d.ts.map +1 -1
  49. package/dist/dev/handwriting-panel.js.map +1 -1
  50. package/dist/dev/multi-text-dialog.d.ts.map +1 -1
  51. package/dist/dev/multi-text-dialog.js +23 -12
  52. package/dist/dev/multi-text-dialog.js.map +1 -1
  53. package/dist/dev/player-body.d.ts +1 -1
  54. package/dist/dev/player-body.d.ts.map +1 -1
  55. package/dist/dev/player-body.js +78 -33
  56. package/dist/dev/player-body.js.map +1 -1
  57. package/dist/dev/schedule-helpers.d.ts.map +1 -1
  58. package/dist/dev/schedule-helpers.js +4 -2
  59. package/dist/dev/schedule-helpers.js.map +1 -1
  60. package/dist/dev/static-image-panel.d.ts +3 -3
  61. package/dist/dev/static-image-panel.d.ts.map +1 -1
  62. package/dist/dev/subtitles.d.ts +36 -0
  63. package/dist/dev/subtitles.d.ts.map +1 -0
  64. package/dist/dev/subtitles.js +135 -0
  65. package/dist/dev/subtitles.js.map +1 -0
  66. package/dist/dev/synced-file.d.ts +14 -0
  67. package/dist/dev/synced-file.d.ts.map +1 -1
  68. package/dist/dev/synced-file.js +22 -0
  69. package/dist/dev/synced-file.js.map +1 -1
  70. package/dist/dev/timeline-display.d.ts +5 -0
  71. package/dist/dev/timeline-display.d.ts.map +1 -1
  72. package/dist/dev/timeline-display.js +1 -1
  73. package/dist/dev/timeline-display.js.map +1 -1
  74. package/dist/dev/transcribe-core.d.ts.map +1 -1
  75. package/dist/dev/transcribe-core.js +7 -4
  76. package/dist/dev/transcribe-core.js.map +1 -1
  77. package/dist/dev/transcript-align.d.ts +6 -4
  78. package/dist/dev/transcript-align.d.ts.map +1 -1
  79. package/dist/dev/transcript-align.js +18 -13
  80. package/dist/dev/transcript-align.js.map +1 -1
  81. package/dist/dev/transcript-editor.d.ts +36 -0
  82. package/dist/dev/transcript-editor.d.ts.map +1 -0
  83. package/dist/dev/transcript-editor.js +948 -0
  84. package/dist/dev/transcript-editor.js.map +1 -0
  85. package/dist/dev/transcript-export.d.ts +21 -0
  86. package/dist/dev/transcript-export.d.ts.map +1 -1
  87. package/dist/dev/transcript-export.js +65 -0
  88. package/dist/dev/transcript-export.js.map +1 -1
  89. package/dist/dev/transcript-view.d.ts +2 -0
  90. package/dist/dev/transcript-view.d.ts.map +1 -1
  91. package/dist/dev/transcript-view.js +1 -1
  92. package/dist/dev/transcript-view.js.map +1 -1
  93. package/dist/dev/transcripts.d.ts +18 -1
  94. package/dist/dev/transcripts.d.ts.map +1 -1
  95. package/dist/dev/transcripts.js +102 -12
  96. package/dist/dev/transcripts.js.map +1 -1
  97. package/dist/dev/video-clip-panel.d.ts +3 -3
  98. package/dist/dev/video-clip-panel.d.ts.map +1 -1
  99. package/dist/lib/index.d.ts +1 -0
  100. package/dist/lib/index.d.ts.map +1 -1
  101. package/dist/lib/index.js +1 -0
  102. package/dist/lib/index.js.map +1 -1
  103. package/dist/src/glib/line-font.d.ts +6 -0
  104. package/dist/src/glib/line-font.d.ts.map +1 -1
  105. package/dist/src/glib/line-font.js +9 -0
  106. package/dist/src/glib/line-font.js.map +1 -1
  107. package/dist/src/glib/path-shape.d.ts +23 -0
  108. package/dist/src/glib/path-shape.d.ts.map +1 -1
  109. package/dist/src/glib/path-shape.js +66 -221
  110. package/dist/src/glib/path-shape.js.map +1 -1
  111. package/dist/src/glib/svg-arc.d.ts +41 -0
  112. package/dist/src/glib/svg-arc.d.ts.map +1 -0
  113. package/dist/src/glib/svg-arc.js +114 -0
  114. package/dist/src/glib/svg-arc.js.map +1 -0
  115. package/dist/src/glib/svg-path-parser.d.ts +60 -0
  116. package/dist/src/glib/svg-path-parser.d.ts.map +1 -0
  117. package/dist/src/glib/svg-path-parser.js +258 -0
  118. package/dist/src/glib/svg-path-parser.js.map +1 -0
  119. package/dist/src/glib/transforms.d.ts +9 -0
  120. package/dist/src/glib/transforms.d.ts.map +1 -1
  121. package/dist/src/glib/transforms.js +7 -4
  122. package/dist/src/glib/transforms.js.map +1 -1
  123. package/dist/src/interpolate.d.ts +17 -10
  124. package/dist/src/interpolate.d.ts.map +1 -1
  125. package/dist/src/interpolate.js +1 -17
  126. package/dist/src/interpolate.js.map +1 -1
  127. package/dist/src/morph-animation.js +1 -1
  128. package/dist/src/morph-animation.js.map +1 -1
  129. package/dist/src/peano-fourier/fourier-shared.d.ts.map +1 -1
  130. package/dist/src/peano-fourier/fourier-shared.js +4 -2
  131. package/dist/src/peano-fourier/fourier-shared.js.map +1 -1
  132. package/dist/src/schedule-helper.d.ts +50 -28
  133. package/dist/src/schedule-helper.d.ts.map +1 -1
  134. package/dist/src/schedule-helper.js +22 -0
  135. package/dist/src/schedule-helper.js.map +1 -1
  136. package/dist/src/showable.d.ts +57 -10
  137. package/dist/src/showable.d.ts.map +1 -1
  138. package/dist/src/showable.js +19 -0
  139. package/dist/src/showable.js.map +1 -1
  140. package/dist/src/slide-components/arrow.d.ts +4 -4
  141. package/dist/src/slide-components/arrow.d.ts.map +1 -1
  142. package/dist/src/slide-components/cross-fade-transition.d.ts +2 -1
  143. package/dist/src/slide-components/cross-fade-transition.d.ts.map +1 -1
  144. package/dist/src/slide-components/cross-fade-transition.js +2 -1
  145. package/dist/src/slide-components/cross-fade-transition.js.map +1 -1
  146. package/dist/src/slide-components/duration-agnostic.d.ts +3 -2
  147. package/dist/src/slide-components/duration-agnostic.d.ts.map +1 -1
  148. package/dist/src/slide-components/duration-agnostic.js +4 -2
  149. package/dist/src/slide-components/duration-agnostic.js.map +1 -1
  150. package/dist/src/slide-components/fixed-duration.d.ts +3 -1
  151. package/dist/src/slide-components/fixed-duration.d.ts.map +1 -1
  152. package/dist/src/slide-components/fixed-duration.js +4 -2
  153. package/dist/src/slide-components/fixed-duration.js.map +1 -1
  154. package/dist/src/slide-components/fourier.d.ts +19 -12
  155. package/dist/src/slide-components/fourier.d.ts.map +1 -1
  156. package/dist/src/slide-components/fourier.js +77 -16
  157. package/dist/src/slide-components/fourier.js.map +1 -1
  158. package/dist/src/slide-components/function-graph.d.ts +4 -3
  159. package/dist/src/slide-components/function-graph.d.ts.map +1 -1
  160. package/dist/src/slide-components/function-graph.js +6 -2
  161. package/dist/src/slide-components/function-graph.js.map +1 -1
  162. package/dist/src/slide-components/halftone-shadow.d.ts +8 -7
  163. package/dist/src/slide-components/halftone-shadow.d.ts.map +1 -1
  164. package/dist/src/slide-components/halftone-shadow.js +14 -10
  165. package/dist/src/slide-components/halftone-shadow.js.map +1 -1
  166. package/dist/src/slide-components/handwriting-effect.d.ts +8 -6
  167. package/dist/src/slide-components/handwriting-effect.d.ts.map +1 -1
  168. package/dist/src/slide-components/handwriting-effect.js +4 -2
  169. package/dist/src/slide-components/handwriting-effect.js.map +1 -1
  170. package/dist/src/slide-components/hold.d.ts +4 -2
  171. package/dist/src/slide-components/hold.d.ts.map +1 -1
  172. package/dist/src/slide-components/hold.js +4 -2
  173. package/dist/src/slide-components/hold.js.map +1 -1
  174. package/dist/src/slide-components/image.d.ts +7 -7
  175. package/dist/src/slide-components/image.d.ts.map +1 -1
  176. package/dist/src/slide-components/image.js +4 -2
  177. package/dist/src/slide-components/image.js.map +1 -1
  178. package/dist/src/slide-components/in-parallel.d.ts +6 -3
  179. package/dist/src/slide-components/in-parallel.d.ts.map +1 -1
  180. package/dist/src/slide-components/in-parallel.js +12 -2
  181. package/dist/src/slide-components/in-parallel.js.map +1 -1
  182. package/dist/src/slide-components/in-series.d.ts +2 -1
  183. package/dist/src/slide-components/in-series.d.ts.map +1 -1
  184. package/dist/src/slide-components/in-series.js +2 -1
  185. package/dist/src/slide-components/in-series.js.map +1 -1
  186. package/dist/src/slide-components/live-duration.d.ts +3 -1
  187. package/dist/src/slide-components/live-duration.d.ts.map +1 -1
  188. package/dist/src/slide-components/live-duration.js +4 -2
  189. package/dist/src/slide-components/live-duration.js.map +1 -1
  190. package/dist/src/slide-components/multi-text.d.ts +39 -26
  191. package/dist/src/slide-components/multi-text.d.ts.map +1 -1
  192. package/dist/src/slide-components/multi-text.js +44 -29
  193. package/dist/src/slide-components/multi-text.js.map +1 -1
  194. package/dist/src/slide-components/rectangle.d.ts +5 -4
  195. package/dist/src/slide-components/rectangle.d.ts.map +1 -1
  196. package/dist/src/slide-components/rectangle.js +2 -1
  197. package/dist/src/slide-components/rectangle.js.map +1 -1
  198. package/dist/src/slide-components/registry.d.ts +5 -0
  199. package/dist/src/slide-components/registry.d.ts.map +1 -1
  200. package/dist/src/slide-components/registry.js +28 -27
  201. package/dist/src/slide-components/registry.js.map +1 -1
  202. package/dist/src/slide-components/show-error.d.ts.map +1 -1
  203. package/dist/src/slide-components/show-error.js +12 -4
  204. package/dist/src/slide-components/show-error.js.map +1 -1
  205. package/dist/src/slide-components/simple-text.d.ts +11 -10
  206. package/dist/src/slide-components/simple-text.d.ts.map +1 -1
  207. package/dist/src/slide-components/simple-text.js +5 -3
  208. package/dist/src/slide-components/simple-text.js.map +1 -1
  209. package/dist/src/slide-components/slide-component.d.ts +14 -13
  210. package/dist/src/slide-components/slide-component.d.ts.map +1 -1
  211. package/dist/src/slide-components/slide-component.js +2 -1
  212. package/dist/src/slide-components/slide-component.js.map +1 -1
  213. package/dist/src/slide-components/slide-left.d.ts +2 -1
  214. package/dist/src/slide-components/slide-left.d.ts.map +1 -1
  215. package/dist/src/slide-components/slide-left.js +2 -1
  216. package/dist/src/slide-components/slide-left.js.map +1 -1
  217. package/dist/src/slide-components/text-effect.d.ts +9 -1
  218. package/dist/src/slide-components/text-effect.d.ts.map +1 -1
  219. package/dist/src/slide-components/text-effect.js.map +1 -1
  220. package/dist/src/slide-components/traditional-text.d.ts +8 -8
  221. package/dist/src/slide-components/traditional-text.d.ts.map +1 -1
  222. package/dist/src/slide-components/video-clip.d.ts +4 -3
  223. package/dist/src/slide-components/video-clip.d.ts.map +1 -1
  224. package/dist/src/slide-components/video-clip.js +2 -1
  225. package/dist/src/slide-components/video-clip.js.map +1 -1
  226. package/lib/index.ts +1 -0
  227. package/package.json +2 -2
  228. package/src/glib/line-font.ts +9 -0
  229. package/src/glib/path-shape.ts +101 -245
  230. package/src/glib/svg-arc.ts +141 -0
  231. package/src/glib/svg-path-parser.ts +308 -0
  232. package/src/glib/transforms.ts +7 -4
  233. package/src/interpolate.ts +18 -27
  234. package/src/morph-animation.ts +1 -1
  235. package/src/peano-fourier/fourier-shared.ts +4 -2
  236. package/src/schedule-helper.ts +50 -28
  237. package/src/showable.ts +79 -10
  238. package/src/slide-components/arrow.ts +4 -4
  239. package/src/slide-components/cross-fade-transition.ts +9 -2
  240. package/src/slide-components/duration-agnostic.ts +4 -3
  241. package/src/slide-components/fixed-duration.ts +4 -2
  242. package/src/slide-components/fourier.ts +98 -29
  243. package/src/slide-components/function-graph.ts +7 -3
  244. package/src/slide-components/halftone-shadow.ts +21 -16
  245. package/src/slide-components/handwriting-effect.ts +8 -6
  246. package/src/slide-components/hold.ts +18 -4
  247. package/src/slide-components/image.ts +9 -7
  248. package/src/slide-components/in-parallel.ts +16 -1
  249. package/src/slide-components/in-series.ts +3 -1
  250. package/src/slide-components/live-duration.ts +4 -2
  251. package/src/slide-components/multi-text.ts +71 -44
  252. package/src/slide-components/rectangle.ts +6 -4
  253. package/src/slide-components/registry.ts +33 -27
  254. package/src/slide-components/show-error.ts +13 -4
  255. package/src/slide-components/simple-text.ts +22 -12
  256. package/src/slide-components/slide-component.ts +15 -13
  257. package/src/slide-components/slide-left.ts +9 -2
  258. package/src/slide-components/text-effect.ts +9 -0
  259. package/src/slide-components/traditional-text.ts +8 -8
  260. package/src/slide-components/video-clip.ts +5 -3
@@ -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, "&amp;")
168
+ .replace(/</g, "&lt;")
169
+ .replace(/>/g, "&gt;");
170
+ }
@@ -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
 
@@ -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;
@@ -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 logProbs = new Float32Array(0);
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
- logProbs = new Float32Array(frameCount * size);
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, logProbs, (base + k) * 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
- logProbs,
284
+ logProbabilities,
282
285
  vocabularySize,
283
286
  frameCount: written,
284
287
  frameMs: (ALIGNER_HOP_SAMPLES / SAMPLE_RATE) * 1000,
@@ -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
- * `logProbs[frame * vocabularySize + token]` is the log probability that
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 logProbs: Float32Array;
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; logProb: number }[] | undefined {
207
- const { logProbs, vocabularySize, blank } = emissions;
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
- logProbs[(firstFrame + frame) * vocabularySize + token];
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
- logProb: 0,
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.logProb += at(t, tokens[index]);
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.logProb = counts[index] ? span.logProb / counts[index] : NEG;
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 logProb =
371
- letters.reduce((sum, span) => sum + span.logProb, 0) / letters.length;
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(logProb),
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 { logProbs, vocabularySize, blank, wordSeparator } = emissions;
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 bestLogProb = -Infinity;
449
+ let bestLogProbability = -Infinity;
445
450
  for (let token = 0; token < vocabularySize; token++) {
446
- const logProb = logProbs[t * vocabularySize + token];
447
- if (logProb > bestLogProb) {
448
- bestLogProb = logProb;
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(bestLogProb);
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.