canvas-recorder-lib 0.7.1 → 0.7.2

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 (174) hide show
  1. package/README.md +38 -8
  2. package/dev/audio-builder.ts +5 -1
  3. package/dev/canvas-recorder.css +15 -4
  4. package/dev/canvas-recorder.ts +255 -82
  5. package/dev/delay-files.ts +1 -1
  6. package/dev/format-editor.ts +7 -2
  7. package/dev/fourier-panel.ts +11 -3
  8. package/dev/handwriting-panel.ts +14 -9
  9. package/dev/local-file-row.ts +12 -5
  10. package/dev/multi-text-dialog.ts +15 -6
  11. package/dev/multi-text-panel.ts +6 -2
  12. package/dev/player-body.ts +4 -0
  13. package/dev/schedule-helpers.ts +11 -4
  14. package/dev/sound-clips.ts +4 -1
  15. package/dev/soundtrack.ts +42 -11
  16. package/dev/split-dialog.ts +49 -19
  17. package/dev/static-image-panel.ts +17 -5
  18. package/dev/synced-file.ts +4 -5
  19. package/dev/timeline-display.ts +152 -50
  20. package/dev/transcribe-core.ts +48 -17
  21. package/dev/transcribe-worker.ts +20 -9
  22. package/dev/transcript-align.ts +95 -20
  23. package/dev/transcript-export.ts +18 -7
  24. package/dev/transcript-view.ts +78 -22
  25. package/dev/transcripts.ts +43 -23
  26. package/dev/video-clip-panel.ts +64 -18
  27. package/dev/voice/chirp.ts +21 -5
  28. package/dev/voice/folder.ts +22 -7
  29. package/dev/voice/voice-panel.ts +34 -10
  30. package/dev/voice/voice-recorder.ts +74 -18
  31. package/dist/dev/audio-builder.js.map +1 -1
  32. package/dist/dev/canvas-recorder.css +15 -4
  33. package/dist/dev/canvas-recorder.js +165 -42
  34. package/dist/dev/canvas-recorder.js.map +1 -1
  35. package/dist/dev/format-editor.d.ts.map +1 -1
  36. package/dist/dev/format-editor.js.map +1 -1
  37. package/dist/dev/fourier-panel.d.ts.map +1 -1
  38. package/dist/dev/fourier-panel.js.map +1 -1
  39. package/dist/dev/handwriting-panel.d.ts.map +1 -1
  40. package/dist/dev/handwriting-panel.js +10 -8
  41. package/dist/dev/handwriting-panel.js.map +1 -1
  42. package/dist/dev/local-file-row.d.ts.map +1 -1
  43. package/dist/dev/local-file-row.js +12 -4
  44. package/dist/dev/local-file-row.js.map +1 -1
  45. package/dist/dev/multi-text-dialog.d.ts.map +1 -1
  46. package/dist/dev/multi-text-dialog.js +4 -2
  47. package/dist/dev/multi-text-dialog.js.map +1 -1
  48. package/dist/dev/multi-text-panel.d.ts.map +1 -1
  49. package/dist/dev/multi-text-panel.js +2 -1
  50. package/dist/dev/multi-text-panel.js.map +1 -1
  51. package/dist/dev/player-body.d.ts +1 -1
  52. package/dist/dev/player-body.d.ts.map +1 -1
  53. package/dist/dev/player-body.js +4 -0
  54. package/dist/dev/player-body.js.map +1 -1
  55. package/dist/dev/schedule-helpers.d.ts.map +1 -1
  56. package/dist/dev/schedule-helpers.js +4 -1
  57. package/dist/dev/schedule-helpers.js.map +1 -1
  58. package/dist/dev/sound-clips.d.ts.map +1 -1
  59. package/dist/dev/sound-clips.js.map +1 -1
  60. package/dist/dev/soundtrack.d.ts +12 -0
  61. package/dist/dev/soundtrack.d.ts.map +1 -1
  62. package/dist/dev/soundtrack.js +33 -7
  63. package/dist/dev/soundtrack.js.map +1 -1
  64. package/dist/dev/split-dialog.d.ts.map +1 -1
  65. package/dist/dev/split-dialog.js +17 -10
  66. package/dist/dev/split-dialog.js.map +1 -1
  67. package/dist/dev/static-image-panel.d.ts.map +1 -1
  68. package/dist/dev/static-image-panel.js +7 -3
  69. package/dist/dev/static-image-panel.js.map +1 -1
  70. package/dist/dev/synced-file.d.ts.map +1 -1
  71. package/dist/dev/synced-file.js.map +1 -1
  72. package/dist/dev/timeline-display.d.ts.map +1 -1
  73. package/dist/dev/timeline-display.js +62 -17
  74. package/dist/dev/timeline-display.js.map +1 -1
  75. package/dist/dev/transcribe-core.d.ts.map +1 -1
  76. package/dist/dev/transcribe-core.js +13 -3
  77. package/dist/dev/transcribe-core.js.map +1 -1
  78. package/dist/dev/transcribe-worker.d.ts.map +1 -1
  79. package/dist/dev/transcribe-worker.js +9 -3
  80. package/dist/dev/transcribe-worker.js.map +1 -1
  81. package/dist/dev/transcript-align.d.ts.map +1 -1
  82. package/dist/dev/transcript-align.js +52 -9
  83. package/dist/dev/transcript-align.js.map +1 -1
  84. package/dist/dev/transcript-export.d.ts.map +1 -1
  85. package/dist/dev/transcript-export.js +10 -4
  86. package/dist/dev/transcript-export.js.map +1 -1
  87. package/dist/dev/transcript-view.d.ts +2 -0
  88. package/dist/dev/transcript-view.d.ts.map +1 -1
  89. package/dist/dev/transcript-view.js +32 -11
  90. package/dist/dev/transcript-view.js.map +1 -1
  91. package/dist/dev/transcripts.d.ts.map +1 -1
  92. package/dist/dev/transcripts.js +5 -3
  93. package/dist/dev/transcripts.js.map +1 -1
  94. package/dist/dev/video-clip-panel.d.ts.map +1 -1
  95. package/dist/dev/video-clip-panel.js +25 -8
  96. package/dist/dev/video-clip-panel.js.map +1 -1
  97. package/dist/dev/voice/chirp.d.ts.map +1 -1
  98. package/dist/dev/voice/chirp.js +5 -1
  99. package/dist/dev/voice/chirp.js.map +1 -1
  100. package/dist/dev/voice/folder.d.ts.map +1 -1
  101. package/dist/dev/voice/folder.js +1 -1
  102. package/dist/dev/voice/folder.js.map +1 -1
  103. package/dist/dev/voice/voice-panel.d.ts.map +1 -1
  104. package/dist/dev/voice/voice-panel.js +15 -6
  105. package/dist/dev/voice/voice-panel.js.map +1 -1
  106. package/dist/dev/voice/voice-recorder.d.ts.map +1 -1
  107. package/dist/dev/voice/voice-recorder.js +34 -9
  108. package/dist/dev/voice/voice-recorder.js.map +1 -1
  109. package/dist/lib/main-control.d.ts.map +1 -1
  110. package/dist/lib/main-control.js.map +1 -1
  111. package/dist/src/corner-rounder.d.ts.map +1 -1
  112. package/dist/src/corner-rounder.js +1 -1
  113. package/dist/src/corner-rounder.js.map +1 -1
  114. package/dist/src/local-files.d.ts.map +1 -1
  115. package/dist/src/local-files.js +5 -2
  116. package/dist/src/local-files.js.map +1 -1
  117. package/dist/src/peano-fourier/fourier-shared.d.ts +7 -0
  118. package/dist/src/peano-fourier/fourier-shared.d.ts.map +1 -1
  119. package/dist/src/peano-fourier/fourier-shared.js +8 -1
  120. package/dist/src/peano-fourier/fourier-shared.js.map +1 -1
  121. package/dist/src/slide-components/fourier.d.ts.map +1 -1
  122. package/dist/src/slide-components/fourier.js +12 -4
  123. package/dist/src/slide-components/fourier.js.map +1 -1
  124. package/dist/src/slide-components/image.d.ts.map +1 -1
  125. package/dist/src/slide-components/image.js +1 -1
  126. package/dist/src/slide-components/image.js.map +1 -1
  127. package/dist/src/slide-components/in-parallel.d.ts.map +1 -1
  128. package/dist/src/slide-components/in-parallel.js.map +1 -1
  129. package/dist/src/slide-components/multi-text.d.ts.map +1 -1
  130. package/dist/src/slide-components/multi-text.js +1 -1
  131. package/dist/src/slide-components/multi-text.js.map +1 -1
  132. package/dist/src/slide-components/rectangle.d.ts.map +1 -1
  133. package/dist/src/slide-components/rectangle.js +1 -1
  134. package/dist/src/slide-components/rectangle.js.map +1 -1
  135. package/dist/src/slide-components/registry.js.map +1 -1
  136. package/dist/src/slide-components/serialize.d.ts.map +1 -1
  137. package/dist/src/slide-components/serialize.js +2 -1
  138. package/dist/src/slide-components/serialize.js.map +1 -1
  139. package/dist/src/slide-components/split-video-clip.d.ts.map +1 -1
  140. package/dist/src/slide-components/split-video-clip.js +4 -1
  141. package/dist/src/slide-components/split-video-clip.js.map +1 -1
  142. package/dist/src/slide-components/video-clip.d.ts.map +1 -1
  143. package/dist/src/slide-components/video-clip.js +7 -4
  144. package/dist/src/slide-components/video-clip.js.map +1 -1
  145. package/dist/src/slide-components/video-info.d.ts.map +1 -1
  146. package/dist/src/slide-components/video-info.js.map +1 -1
  147. package/dist/src/slow-image-sources.d.ts.map +1 -1
  148. package/dist/src/slow-image-sources.js +1 -1
  149. package/dist/src/slow-image-sources.js.map +1 -1
  150. package/dist/src/snapshot.d.ts.map +1 -1
  151. package/dist/src/snapshot.js +1 -1
  152. package/dist/src/snapshot.js.map +1 -1
  153. package/dist/src/utility.d.ts.map +1 -1
  154. package/dist/src/utility.js +1 -3
  155. package/dist/src/utility.js.map +1 -1
  156. package/lib/main-control.ts +3 -1
  157. package/package.json +4 -1
  158. package/src/corner-rounder.ts +5 -12
  159. package/src/glib/letters-base.ts +1 -1
  160. package/src/local-files.ts +19 -6
  161. package/src/peano-fourier/fourier-shared.ts +8 -1
  162. package/src/slide-components/fourier.ts +32 -10
  163. package/src/slide-components/image.ts +4 -1
  164. package/src/slide-components/in-parallel.ts +1 -2
  165. package/src/slide-components/multi-text.ts +17 -4
  166. package/src/slide-components/rectangle.ts +4 -1
  167. package/src/slide-components/registry.ts +1 -1
  168. package/src/slide-components/serialize.ts +4 -1
  169. package/src/slide-components/split-video-clip.ts +4 -1
  170. package/src/slide-components/video-clip.ts +21 -8
  171. package/src/slide-components/video-info.ts +4 -1
  172. package/src/slow-image-sources.ts +5 -1
  173. package/src/snapshot.ts +13 -4
  174. package/src/utility.ts +70 -33
@@ -78,11 +78,39 @@ export type RoughWord = {
78
78
  // MARK: Words to letters
79
79
 
80
80
  const ONES = [
81
- "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE",
82
- "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN",
83
- "SEVENTEEN", "EIGHTEEN", "NINETEEN",
81
+ "ZERO",
82
+ "ONE",
83
+ "TWO",
84
+ "THREE",
85
+ "FOUR",
86
+ "FIVE",
87
+ "SIX",
88
+ "SEVEN",
89
+ "EIGHT",
90
+ "NINE",
91
+ "TEN",
92
+ "ELEVEN",
93
+ "TWELVE",
94
+ "THIRTEEN",
95
+ "FOURTEEN",
96
+ "FIFTEEN",
97
+ "SIXTEEN",
98
+ "SEVENTEEN",
99
+ "EIGHTEEN",
100
+ "NINETEEN",
101
+ ];
102
+ const TENS = [
103
+ "",
104
+ "",
105
+ "TWENTY",
106
+ "THIRTY",
107
+ "FORTY",
108
+ "FIFTY",
109
+ "SIXTY",
110
+ "SEVENTY",
111
+ "EIGHTY",
112
+ "NINETY",
84
113
  ];
85
- const TENS = ["", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"];
86
114
 
87
115
  /**
88
116
  * A whole number as English words, the way it's usually said: 1981 is
@@ -92,7 +120,8 @@ const TENS = ["", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY",
92
120
  export function spellNumber(n: number): string {
93
121
  if (!Number.isInteger(n) || n < 0) return "";
94
122
  if (n < 20) return ONES[n];
95
- if (n < 100) return TENS[Math.floor(n / 10)] + (n % 10 ? " " + ONES[n % 10] : "");
123
+ if (n < 100)
124
+ return TENS[Math.floor(n / 10)] + (n % 10 ? " " + ONES[n % 10] : "");
96
125
  if (n < 1000) {
97
126
  return `${ONES[Math.floor(n / 100)]} HUNDRED${n % 100 ? " " + spellNumber(n % 100) : ""}`;
98
127
  }
@@ -206,7 +235,12 @@ export function forcedAlign(
206
235
  move = 1;
207
236
  }
208
237
  // Skip a blank, unless that would merge two identical letters.
209
- if (s >= 2 && s % 2 === 1 && stateToken(s) !== stateToken(s - 2) && previous[s - 2] > best) {
238
+ if (
239
+ s >= 2 &&
240
+ s % 2 === 1 &&
241
+ stateToken(s) !== stateToken(s - 2) &&
242
+ previous[s - 2] > best
243
+ ) {
210
244
  best = previous[s - 2];
211
245
  move = 2;
212
246
  }
@@ -218,9 +252,14 @@ export function forcedAlign(
218
252
  }
219
253
 
220
254
  // End on the last token or the blank after it.
221
- let s = previous[states - 1] >= previous[states - 2] ? states - 1 : states - 2;
255
+ let s =
256
+ previous[states - 1] >= previous[states - 2] ? states - 1 : states - 2;
222
257
  if (previous[s] === NEG) return undefined;
223
- const result = tokens.map(() => ({ first: Infinity, last: -Infinity, logProb: 0 }));
258
+ const result = tokens.map(() => ({
259
+ first: Infinity,
260
+ last: -Infinity,
261
+ logProb: 0,
262
+ }));
224
263
  const counts = new Array<number>(tokens.length).fill(0);
225
264
  for (let t = frames - 1; t >= 0; t--) {
226
265
  if (s % 2 === 1) {
@@ -272,7 +311,8 @@ export function alignWords(
272
311
  let groupStart = 0;
273
312
  for (let i = 1; i <= words.length; i++) {
274
313
  const tooLong =
275
- i < words.length && words[i].endMs - words[groupStart].startMs > MAX_WINDOW_MS;
314
+ i < words.length &&
315
+ words[i].endMs - words[groupStart].startMs > MAX_WINDOW_MS;
276
316
  if (i === words.length || tooLong) {
277
317
  if (tooLong) {
278
318
  // Split at the biggest gap in this group, so no word is cut in two.
@@ -395,7 +435,11 @@ export function lettersHeard(
395
435
  frames = 0;
396
436
  };
397
437
  let previous = blank;
398
- for (let t = Math.max(0, firstFrame); t < Math.min(endFrame, emissions.frameCount); t++) {
438
+ for (
439
+ let t = Math.max(0, firstFrame);
440
+ t < Math.min(endFrame, emissions.frameCount);
441
+ t++
442
+ ) {
399
443
  let best = 0;
400
444
  let bestLogProb = -Infinity;
401
445
  for (let token = 0; token < vocabularySize; token++) {
@@ -430,7 +474,11 @@ export function lettersHeard(
430
474
  * What the letter model heard from `fromMs` to `toMs`, as one string: the
431
475
  * words separated by spaces, or "" for nothing.
432
476
  */
433
- export function heardBetween(emissions: CtcEmissions, fromMs: number, toMs: number): string {
477
+ export function heardBetween(
478
+ emissions: CtcEmissions,
479
+ fromMs: number,
480
+ toMs: number,
481
+ ): string {
434
482
  return lettersHeard(
435
483
  emissions,
436
484
  Math.floor(fromMs / emissions.frameMs),
@@ -477,8 +525,14 @@ export function recoverSkippedSpeech(
477
525
  const { frameMs, frameCount } = emissions;
478
526
  const covered = new Uint8Array(frameCount);
479
527
  for (const { startMs, endMs } of times) {
480
- const from = Math.max(0, Math.floor(startMs / frameMs) - RECOVER_CLEARANCE_FRAMES);
481
- const to = Math.min(frameCount, Math.ceil(endMs / frameMs) + RECOVER_CLEARANCE_FRAMES);
528
+ const from = Math.max(
529
+ 0,
530
+ Math.floor(startMs / frameMs) - RECOVER_CLEARANCE_FRAMES,
531
+ );
532
+ const to = Math.min(
533
+ frameCount,
534
+ Math.ceil(endMs / frameMs) + RECOVER_CLEARANCE_FRAMES,
535
+ );
482
536
  covered.fill(1, from, to);
483
537
  }
484
538
  const result: { word: RoughWord; times: AlignedTimes }[] = [];
@@ -492,13 +546,20 @@ export function recoverSkippedSpeech(
492
546
  while (t < frameCount && !covered[t]) t++;
493
547
  for (const heard of lettersHeard(emissions, gapStart, t)) {
494
548
  const needed =
495
- heard.text.length === 1 ? RECOVER_MIN_PROBABILITY_ONE_LETTER : RECOVER_MIN_PROBABILITY;
549
+ heard.text.length === 1
550
+ ? RECOVER_MIN_PROBABILITY_ONE_LETTER
551
+ : RECOVER_MIN_PROBABILITY;
496
552
  if (heard.probability < needed) continue;
497
553
  const startMs = heard.firstFrame * frameMs;
498
554
  const endMs = (heard.lastFrame + 1) * frameMs;
499
555
  result.push({
500
556
  word: { text: heard.text, startMs, endMs },
501
- times: { startMs, endMs, timing: "letters", confidence: heard.probability },
557
+ times: {
558
+ startMs,
559
+ endMs,
560
+ timing: "letters",
561
+ confidence: heard.probability,
562
+ },
502
563
  });
503
564
  }
504
565
  }
@@ -521,7 +582,9 @@ const squash = (s: string) => s.toLowerCase().replace(/[^a-z']/g, "");
521
582
  * or something that doesn't even start the same way. Rough, but in testing
522
583
  * it found real misreadings ("and pairs" for "in pairs").
523
584
  */
524
- export function wordFlag(word: TranscriptWord): "left out" | "doubtful" | undefined {
585
+ export function wordFlag(
586
+ word: TranscriptWord,
587
+ ): "left out" | "doubtful" | undefined {
525
588
  if (word.timing === "letters") return "left out";
526
589
  const text = squash(word.text);
527
590
  // `heard` is missing from transcripts made before it existed.
@@ -565,13 +628,17 @@ export function quietestMoment(
565
628
  const windowLength = Math.max(1, Math.round(CUT_WINDOW_MS * perMs));
566
629
  const step = Math.max(1, Math.round(windowLength / 2));
567
630
  const first = Math.max(0, Math.floor(fromMs * perMs));
568
- const last = Math.min(samples.length - windowLength, Math.ceil(toMs * perMs) - windowLength);
631
+ const last = Math.min(
632
+ samples.length - windowLength,
633
+ Math.ceil(toMs * perMs) - windowLength,
634
+ );
569
635
  if (last < first) return (fromMs + toMs) / 2;
570
636
  let bestStart = first;
571
637
  let bestEnergy = Infinity;
572
638
  for (let start = first; start <= last; start += step) {
573
639
  let energy = 0;
574
- for (let i = start; i < start + windowLength; i++) energy += samples[i] * samples[i];
640
+ for (let i = start; i < start + windowLength; i++)
641
+ energy += samples[i] * samples[i];
575
642
  if (energy < bestEnergy) {
576
643
  bestEnergy = energy;
577
644
  bestStart = start;
@@ -601,7 +668,12 @@ export function withCutPoints(
601
668
  ): TranscriptWord[] {
602
669
  const fileMs = (samples.length / sampleRate) * 1000;
603
670
  const quietest = (from: number, to: number) =>
604
- quietestMoment(samples, sampleRate, Math.max(0, from), Math.min(fileMs, to));
671
+ quietestMoment(
672
+ samples,
673
+ sampleRate,
674
+ Math.max(0, from),
675
+ Math.min(fileMs, to),
676
+ );
605
677
  const cutBefore: number[] = [];
606
678
  const cutAfter: number[] = [];
607
679
  for (let i = 0; i <= times.length; i++) {
@@ -623,7 +695,10 @@ export function withCutPoints(
623
695
  } else if (gap <= 2 * CUT_REACH_MS) {
624
696
  cutAfter[i - 1] = cutBefore[i] = quietest(previous.endMs, next.startMs);
625
697
  } else {
626
- cutAfter[i - 1] = quietest(previous.endMs, previous.endMs + CUT_REACH_MS);
698
+ cutAfter[i - 1] = quietest(
699
+ previous.endMs,
700
+ previous.endMs + CUT_REACH_MS,
701
+ );
627
702
  cutBefore[i] = quietest(next.startMs - CUT_REACH_MS, next.startMs);
628
703
  }
629
704
  }
@@ -28,21 +28,29 @@ const ms = (n: number) => Math.round(n * 10) / 10;
28
28
 
29
29
  /** Whisper's words as they are; speech it left out in [brackets]. */
30
30
  function asText(words: readonly TranscriptWord[]): string {
31
- return words.map((w) => (w.timing === "letters" ? `[${w.text}]` : w.text)).join(" ");
31
+ return words
32
+ .map((w) => (w.timing === "letters" ? `[${w.text}]` : w.text))
33
+ .join(" ");
32
34
  }
33
35
 
34
36
  /**
35
37
  * The JSON text to save. Each word is on its own line, so the file reads
36
38
  * well and diffs and greps by word.
37
39
  */
38
- export function transcriptJson(transcript: Transcript, context: ClipContext): string {
40
+ export function transcriptJson(
41
+ transcript: Transcript,
42
+ context: ClipContext,
43
+ ): string {
39
44
  const { clip } = context;
40
45
  const fromMs = clip.startMsIntoClip ?? 0;
41
- const toMs = context.lengthMs === undefined ? undefined : fromMs + context.lengthMs;
46
+ const toMs =
47
+ context.lengthMs === undefined ? undefined : fromMs + context.lengthMs;
42
48
  const inClip = (w: TranscriptWord) =>
43
49
  w.endMs > fromMs && (toMs === undefined || w.startMs < toMs);
44
50
  const clipWords = transcript.words.filter(inClip);
45
- const first = clipWords.length ? transcript.words.indexOf(clipWords[0]) : undefined;
51
+ const first = clipWords.length
52
+ ? transcript.words.indexOf(clipWords[0])
53
+ : undefined;
46
54
  const last = clipWords.length
47
55
  ? transcript.words.indexOf(clipWords[clipWords.length - 1])
48
56
  : undefined;
@@ -79,8 +87,10 @@ export function transcriptJson(transcript: Transcript, context: ClipContext): st
79
87
  pipelineVersion: PIPELINE_VERSION,
80
88
  models: MODELS,
81
89
  wordCount: transcript.words.length,
82
- recoveredCount: transcript.words.filter((w) => w.timing === "letters").length,
83
- doubtfulCount: transcript.words.filter((w) => wordFlag(w) === "doubtful").length,
90
+ recoveredCount: transcript.words.filter((w) => w.timing === "letters")
91
+ .length,
92
+ doubtfulCount: transcript.words.filter((w) => wordFlag(w) === "doubtful")
93
+ .length,
84
94
  text: asText(transcript.words),
85
95
  },
86
96
  words: "@@WORDS@@",
@@ -109,6 +119,7 @@ export function transcriptJson(transcript: Transcript, context: ClipContext): st
109
119
 
110
120
  /** A file name for the saved transcript: the sound file's name, with `.transcript.json`. */
111
121
  export function transcriptFileName(source: string): string {
112
- const name = decodeURIComponent(source.split("/").pop() ?? "sound") || "sound";
122
+ const name =
123
+ decodeURIComponent(source.split("/").pop() ?? "sound") || "sound";
113
124
  return name.replace(/\.[^.]*$/, "") + ".transcript.json";
114
125
  }
@@ -23,7 +23,12 @@ export type TranscriptViewHooks = {
23
23
  */
24
24
  transcript(
25
25
  source: string,
26
- ): { readonly words: readonly TranscriptWord[]; readonly madeAt: number } | string | undefined;
26
+ ):
27
+ | { readonly words: readonly TranscriptWord[]; readonly madeAt: number }
28
+ | string
29
+ | undefined;
30
+ /** Why this clip's file can't be played, if it can't. Shown above its words. */
31
+ problem(clip: SoundClip): string | undefined;
27
32
  /** Put the playhead here, in the root's time. */
28
33
  seek(rootMs: number): void;
29
34
  /** Select this clip, as clicking it on the timeline does. */
@@ -34,7 +39,12 @@ export type TranscriptViewHooks = {
34
39
  * Cut `fromMs`–`toMs` (the owner's time) out of `clip`. See
35
40
  * `cutOutOfSoundClip()`.
36
41
  */
37
- cutOut(target: TranscriptViewClip, fromMs: number, toMs: number, closeGap: boolean): void;
42
+ cutOut(
43
+ target: TranscriptViewClip,
44
+ fromMs: number,
45
+ toMs: number,
46
+ closeGap: boolean,
47
+ ): void;
38
48
  };
39
49
 
40
50
  /** One word on screen. */
@@ -92,7 +102,8 @@ function describe(view: Omit<ViewWord, "element">): string {
92
102
  : word.timing === "whisper"
93
103
  ? "Whisper's word and Whisper's rough times"
94
104
  : "Whisper's word, timed by the letter model";
95
- const owner = target.owner.userEditableDescription ?? target.owner.description;
105
+ const owner =
106
+ target.owner.userEditableDescription ?? target.owner.description;
96
107
  const flag = wordFlag(word);
97
108
  return [
98
109
  `“${word.text}” ${played ? "" : "(not played: trimmed or cut out)"}`.trim(),
@@ -267,10 +278,14 @@ export class TranscriptView {
267
278
  this.#drawSelectedClip();
268
279
  const { anchor, side } = this.#wordAt(playMs);
269
280
  const className = side && PLAYHEAD_CLASSES[side];
270
- if (anchor !== this.#marked?.view || className !== this.#marked?.className) {
281
+ if (
282
+ anchor !== this.#marked?.view ||
283
+ className !== this.#marked?.className
284
+ ) {
271
285
  this.#marked?.view.element.classList.remove(this.#marked.className);
272
286
  if (anchor && className) anchor.element.classList.add(className);
273
- this.#marked = anchor && className ? { view: anchor, className } : undefined;
287
+ this.#marked =
288
+ anchor && className ? { view: anchor, className } : undefined;
274
289
  }
275
290
  this.#current = anchor;
276
291
  if (!anchor) {
@@ -295,7 +310,10 @@ export class TranscriptView {
295
310
 
296
311
  #rebuildIfNeeded(): void {
297
312
  const clips = this.hooks.clips();
298
- const transcripts = clips.map(({ clip }) => this.hooks.transcript(clip.source));
313
+ const transcripts = clips.map(({ clip }) =>
314
+ this.hooks.transcript(clip.source),
315
+ );
316
+ const problems = clips.map(({ clip }) => this.hooks.problem(clip));
299
317
  const key = JSON.stringify([
300
318
  this.#showCut.checked,
301
319
  clips.map(({ clip, startMs, lengthMs }, i) => {
@@ -307,17 +325,19 @@ export class TranscriptView {
307
325
  lengthMs ?? null,
308
326
  clip.notes ?? "",
309
327
  typeof t === "object" ? t.madeAt : (t ?? null),
328
+ problems[i] ?? null,
310
329
  ];
311
330
  }),
312
331
  ]);
313
332
  if (key === this.#key) return;
314
333
  this.#key = key;
315
- this.#build(clips, transcripts);
334
+ this.#build(clips, transcripts, problems);
316
335
  }
317
336
 
318
337
  #build(
319
338
  clips: readonly TranscriptViewClip[],
320
339
  transcripts: ReturnType<TranscriptViewHooks["transcript"]>[],
340
+ problems: readonly (string | undefined)[],
321
341
  ): void {
322
342
  this.#scroll.replaceChildren();
323
343
  this.#played = [];
@@ -340,7 +360,10 @@ export class TranscriptView {
340
360
  /** The file range each clip plays. */
341
361
  const range = (target: TranscriptViewClip) => {
342
362
  const from = target.clip.startMsIntoClip ?? 0;
343
- return { from, to: target.lengthMs === undefined ? Infinity : from + target.lengthMs };
363
+ return {
364
+ from,
365
+ to: target.lengthMs === undefined ? Infinity : from + target.lengthMs,
366
+ };
344
367
  };
345
368
  const plays = (target: TranscriptViewClip, word: TranscriptWord) => {
346
369
  const { from, to } = range(target);
@@ -351,7 +374,10 @@ export class TranscriptView {
351
374
  // words of its file that no clip plays. Each of those is shown once:
352
375
  // just before the first clip (in the file) that starts after it, or else
353
376
  // after the clip that ends last before it.
354
- const wordsFor = new Map<TranscriptViewClip, { word: TranscriptWord; played: boolean }[]>();
377
+ const wordsFor = new Map<
378
+ TranscriptViewClip,
379
+ { word: TranscriptWord; played: boolean }[]
380
+ >();
355
381
  for (const target of clips) wordsFor.set(target, []);
356
382
  const sources = new Set(clips.map(({ clip }) => clip.source));
357
383
  for (const source of sources) {
@@ -361,7 +387,8 @@ export class TranscriptView {
361
387
  const ofFile = clips.filter(({ clip }) => clip.source === source);
362
388
  for (const word of transcript.words) {
363
389
  const playing = ofFile.filter((target) => plays(target, word));
364
- for (const target of playing) wordsFor.get(target)!.push({ word, played: true });
390
+ for (const target of playing)
391
+ wordsFor.get(target)!.push({ word, played: true });
365
392
  if (playing.length > 0 || !this.#showCut.checked) continue;
366
393
  const after = ofFile
367
394
  .filter((target) => range(target).from >= word.endMs)
@@ -379,14 +406,25 @@ export class TranscriptView {
379
406
  section.className = "tv-clip";
380
407
  const head = document.createElement("div");
381
408
  head.className = "tv-clip-head";
382
- const owner = target.owner.userEditableDescription ?? target.owner.description;
383
- const name = target.clip.notes || decodeURIComponent(target.clip.source.split("/").pop() ?? "");
409
+ const owner =
410
+ target.owner.userEditableDescription ?? target.owner.description;
411
+ const name =
412
+ target.clip.notes ||
413
+ decodeURIComponent(target.clip.source.split("/").pop() ?? "");
384
414
  head.textContent = `${seconds(target.startMs)} s · ${owner} · ${name}`;
385
415
  head.title = "Click to select this clip.";
386
416
  head.addEventListener("click", () => this.hooks.selectClip(target.clip));
387
417
  section.append(head);
388
418
  this.#clipElements.set(target.clip, section);
389
419
 
420
+ const problem = problems[i];
421
+ if (problem !== undefined) {
422
+ const warning = document.createElement("div");
423
+ warning.className = "tv-problem";
424
+ warning.textContent = `⚠ ${problem}`;
425
+ section.append(warning);
426
+ }
427
+
390
428
  const transcript = transcripts[i];
391
429
  if (typeof transcript !== "object") {
392
430
  const note = document.createElement("div");
@@ -396,7 +434,9 @@ export class TranscriptView {
396
434
  this.#scroll.append(section);
397
435
  return;
398
436
  }
399
- const words = wordsFor.get(target)!.sort((a, b) => a.word.startMs - b.word.startMs);
437
+ const words = wordsFor
438
+ .get(target)!
439
+ .sort((a, b) => a.word.startMs - b.word.startMs);
400
440
  if (words.length === 0) {
401
441
  const note = document.createElement("div");
402
442
  note.className = "tv-note";
@@ -421,7 +461,8 @@ export class TranscriptView {
421
461
  if (flag === "left out") element.classList.add("recovered");
422
462
  if (flag === "doubtful") element.classList.add("doubtful");
423
463
  if (!played) element.classList.add("cut");
424
- element.textContent = word.timing === "letters" ? `[${word.text}]` : word.text;
464
+ element.textContent =
465
+ word.timing === "letters" ? `[${word.text}]` : word.text;
425
466
  const view = {
426
467
  word,
427
468
  target,
@@ -461,7 +502,10 @@ export class TranscriptView {
461
502
  * * `side`: whether `ms` is inside the anchor, in the pause after it, or
462
503
  * before it (only before the very first word).
463
504
  */
464
- #wordAt(ms: number): { anchor?: ViewWord; side?: keyof typeof PLAYHEAD_CLASSES } {
505
+ #wordAt(ms: number): {
506
+ anchor?: ViewWord;
507
+ side?: keyof typeof PLAYHEAD_CLASSES;
508
+ } {
465
509
  const words = this.#played;
466
510
  let low = 0;
467
511
  let high = words.length - 1;
@@ -486,7 +530,10 @@ export class TranscriptView {
486
530
  }
487
531
 
488
532
  #isAbove(view: ViewWord): boolean {
489
- return view.element.getBoundingClientRect().top < this.#scroll.getBoundingClientRect().top;
533
+ return (
534
+ view.element.getBoundingClientRect().top <
535
+ this.#scroll.getBoundingClientRect().top
536
+ );
490
537
  }
491
538
 
492
539
  /** Bring a word into sight, a third of the way down, so there's room to read ahead. */
@@ -513,10 +560,13 @@ export class TranscriptView {
513
560
  /** The played words the selection touches. */
514
561
  #selectedWords(): ViewWord[] {
515
562
  const selection = document.getSelection();
516
- if (!selection || selection.isCollapsed || selection.rangeCount === 0) return [];
563
+ if (!selection || selection.isCollapsed || selection.rangeCount === 0)
564
+ return [];
517
565
  const range = selection.getRangeAt(0);
518
566
  if (!this.#scroll.contains(range.commonAncestorContainer)) return [];
519
- return this.#all.filter((view) => view.played && range.intersectsNode(view.element));
567
+ return this.#all.filter(
568
+ (view) => view.played && range.intersectsNode(view.element),
569
+ );
520
570
  }
521
571
 
522
572
  #drawCutButton(): void {
@@ -525,10 +575,12 @@ export class TranscriptView {
525
575
  const button = this.#cutButton;
526
576
  if (words.length === 0) {
527
577
  button.disabled = true;
528
- button.title = "Select some words, then click here to cut them out of their clip.";
578
+ button.title =
579
+ "Select some words, then click here to cut them out of their clip.";
529
580
  } else if (clips.size > 1) {
530
581
  button.disabled = true;
531
- button.title = "The selection covers more than one clip. Cut one clip at a time.";
582
+ button.title =
583
+ "The selection covers more than one clip. Cut one clip at a time.";
532
584
  } else {
533
585
  button.disabled = false;
534
586
  button.title = `Cut ${words.length} word${words.length === 1 ? "" : "s"} out of this clip, quiet moment to quiet moment.`;
@@ -544,7 +596,8 @@ export class TranscriptView {
544
596
  const last = words[words.length - 1];
545
597
  const { clip } = target;
546
598
  // Owner time: where the clip starts in its owner, plus how far into the clip.
547
- const toOwner = (fileMs: number) => clip.startMsIntoScene + (fileMs - (clip.startMsIntoClip ?? 0));
599
+ const toOwner = (fileMs: number) =>
600
+ clip.startMsIntoScene + (fileMs - (clip.startMsIntoClip ?? 0));
548
601
  const fromMs = toOwner(first.word.cutBeforeMs);
549
602
  const toMs = toOwner(last.word.cutAfterMs);
550
603
  const closeGap = await askHowToCut(
@@ -563,7 +616,10 @@ export class TranscriptView {
563
616
  * Confirm a cut, and ask whether to close the gap.
564
617
  * @returns true to close the gap, false to leave it, undefined to cancel.
565
618
  */
566
- function askHowToCut(text: string, lengthMs: number): Promise<boolean | undefined> {
619
+ function askHowToCut(
620
+ text: string,
621
+ lengthMs: number,
622
+ ): Promise<boolean | undefined> {
567
623
  const dialog = document.createElement("dialog");
568
624
  dialog.style.cssText = "min-width:26em;max-width:40em;padding:1em";
569
625
  const heading = document.createElement("h3");
@@ -8,7 +8,10 @@ import {
8
8
  TranscribeProgress,
9
9
  } from "./transcribe-core.ts";
10
10
  import type { TranscriptWord } from "./transcript-align.ts";
11
- import type { TranscribeReply, TranscribeRequest } from "./transcribe-worker.ts";
11
+ import type {
12
+ TranscribeReply,
13
+ TranscribeRequest,
14
+ } from "./transcribe-worker.ts";
12
15
 
13
16
  /**
14
17
  * Word-by-word transcripts of sound files, for the timeline.
@@ -154,7 +157,9 @@ export function transcriptsSummary(): string {
154
157
  : "reading the file";
155
158
  return `${name}: ${progress}${more}`;
156
159
  }
157
- const failed = [...statuses.values()].filter((s) => s.kind === "failed").length;
160
+ const failed = [...statuses.values()].filter(
161
+ (s) => s.kind === "failed",
162
+ ).length;
158
163
  return failed ? `${failed} failed (see the Debug Log)` : "";
159
164
  }
160
165
 
@@ -251,7 +256,8 @@ export async function makeTranscript(
251
256
  /** The file at `url` as 16 kHz mono, the way the models want it. */
252
257
  async function decode(url: string): Promise<Float32Array> {
253
258
  const response = await fetchMedia(url);
254
- if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
259
+ if (!response.ok)
260
+ throw new Error(`${response.status} ${response.statusText}`);
255
261
  // decodeAudioData resamples to its context's rate, which does the 16 kHz part.
256
262
  const context = new OfflineAudioContext(1, 1, SAMPLE_RATE);
257
263
  const audio = await context.decodeAudioData(await response.arrayBuffer());
@@ -259,7 +265,8 @@ async function decode(url: string): Promise<Float32Array> {
259
265
  const mono = new Float32Array(audio.length);
260
266
  for (let channel = 0; channel < audio.numberOfChannels; channel++) {
261
267
  const data = audio.getChannelData(channel);
262
- for (let i = 0; i < mono.length; i++) mono[i] += data[i] / audio.numberOfChannels;
268
+ for (let i = 0; i < mono.length; i++)
269
+ mono[i] += data[i] / audio.numberOfChannels;
263
270
  }
264
271
  return mono;
265
272
  }
@@ -288,20 +295,23 @@ function runWorker(
288
295
  worker = new Worker(new URL("./transcribe-worker.js", import.meta.url), {
289
296
  type: "module",
290
297
  });
291
- worker.addEventListener("message", (event: MessageEvent<TranscribeReply>) => {
292
- const reply = event.data;
293
- const job = pending.get(reply.id);
294
- if (!job) return;
295
- if (reply.kind === "progress") {
296
- job.onProgress?.(reply.progress);
297
- } else if (reply.kind === "done") {
298
- pending.delete(reply.id);
299
- job.resolve(reply);
300
- } else {
301
- pending.delete(reply.id);
302
- job.reject(new Error(reply.message));
303
- }
304
- });
298
+ worker.addEventListener(
299
+ "message",
300
+ (event: MessageEvent<TranscribeReply>) => {
301
+ const reply = event.data;
302
+ const job = pending.get(reply.id);
303
+ if (!job) return;
304
+ if (reply.kind === "progress") {
305
+ job.onProgress?.(reply.progress);
306
+ } else if (reply.kind === "done") {
307
+ pending.delete(reply.id);
308
+ job.resolve(reply);
309
+ } else {
310
+ pending.delete(reply.id);
311
+ job.reject(new Error(reply.message));
312
+ }
313
+ },
314
+ );
305
315
  worker.addEventListener("error", (event) => {
306
316
  for (const job of pending.values()) job.reject(new Error(event.message));
307
317
  pending.clear();
@@ -343,7 +353,9 @@ async function cacheKey(url: string): Promise<string> {
343
353
  if (modified && length) return `${prefix}${modified} ${length}`;
344
354
  const bytes = await (await fetchMedia(url)).arrayBuffer();
345
355
  const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
346
- return prefix + Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("");
356
+ return (
357
+ prefix + Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("")
358
+ );
347
359
  }
348
360
 
349
361
  /** Every {@link cacheKey} of this URL starts with this: everything but the file's fingerprint. */
@@ -364,7 +376,8 @@ async function newestCached(url: string): Promise<Transcript | undefined> {
364
376
  request.onerror = () => reject(request.error);
365
377
  });
366
378
  return records.reduce<CacheRecord | undefined>(
367
- (newest, record) => (newest && newest.madeAt >= record.madeAt ? newest : record),
379
+ (newest, record) =>
380
+ newest && newest.madeAt >= record.madeAt ? newest : record,
368
381
  undefined,
369
382
  );
370
383
  }
@@ -386,16 +399,23 @@ async function readCache(key: string): Promise<Transcript | undefined> {
386
399
  const db = await openDb();
387
400
  return new Promise((resolve, reject) => {
388
401
  const request = db.transaction(STORE).objectStore(STORE).get(key);
389
- request.onsuccess = () => resolve(request.result as CacheRecord | undefined);
402
+ request.onsuccess = () =>
403
+ resolve(request.result as CacheRecord | undefined);
390
404
  request.onerror = () => reject(request.error);
391
405
  });
392
406
  }
393
407
 
394
- async function writeCache(key: string, url: string, transcript: Transcript): Promise<void> {
408
+ async function writeCache(
409
+ key: string,
410
+ url: string,
411
+ transcript: Transcript,
412
+ ): Promise<void> {
395
413
  const db = await openDb();
396
414
  return new Promise((resolve, reject) => {
397
415
  const transaction = db.transaction(STORE, "readwrite");
398
- transaction.objectStore(STORE).put({ ...transcript, key, url } satisfies CacheRecord);
416
+ transaction
417
+ .objectStore(STORE)
418
+ .put({ ...transcript, key, url } satisfies CacheRecord);
399
419
  transaction.oncomplete = () => resolve();
400
420
  transaction.onerror = () => reject(transaction.error);
401
421
  });