canvas-recorder-lib 0.7.0 → 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 (175) 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 +276 -88
  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 +52 -13
  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 +97 -26
  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 +61 -27
  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 +185 -48
  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 +42 -9
  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 +5 -0
  92. package/dist/dev/transcripts.d.ts.map +1 -1
  93. package/dist/dev/transcripts.js +54 -6
  94. package/dist/dev/transcripts.js.map +1 -1
  95. package/dist/dev/video-clip-panel.d.ts.map +1 -1
  96. package/dist/dev/video-clip-panel.js +25 -8
  97. package/dist/dev/video-clip-panel.js.map +1 -1
  98. package/dist/dev/voice/chirp.d.ts.map +1 -1
  99. package/dist/dev/voice/chirp.js +5 -1
  100. package/dist/dev/voice/chirp.js.map +1 -1
  101. package/dist/dev/voice/folder.d.ts.map +1 -1
  102. package/dist/dev/voice/folder.js +1 -1
  103. package/dist/dev/voice/folder.js.map +1 -1
  104. package/dist/dev/voice/voice-panel.d.ts.map +1 -1
  105. package/dist/dev/voice/voice-panel.js +42 -23
  106. package/dist/dev/voice/voice-panel.js.map +1 -1
  107. package/dist/dev/voice/voice-recorder.d.ts.map +1 -1
  108. package/dist/dev/voice/voice-recorder.js +34 -9
  109. package/dist/dev/voice/voice-recorder.js.map +1 -1
  110. package/dist/lib/main-control.d.ts.map +1 -1
  111. package/dist/lib/main-control.js.map +1 -1
  112. package/dist/src/corner-rounder.d.ts.map +1 -1
  113. package/dist/src/corner-rounder.js +1 -1
  114. package/dist/src/corner-rounder.js.map +1 -1
  115. package/dist/src/local-files.d.ts.map +1 -1
  116. package/dist/src/local-files.js +5 -2
  117. package/dist/src/local-files.js.map +1 -1
  118. package/dist/src/peano-fourier/fourier-shared.d.ts +7 -0
  119. package/dist/src/peano-fourier/fourier-shared.d.ts.map +1 -1
  120. package/dist/src/peano-fourier/fourier-shared.js +8 -1
  121. package/dist/src/peano-fourier/fourier-shared.js.map +1 -1
  122. package/dist/src/slide-components/fourier.d.ts.map +1 -1
  123. package/dist/src/slide-components/fourier.js +12 -4
  124. package/dist/src/slide-components/fourier.js.map +1 -1
  125. package/dist/src/slide-components/image.d.ts.map +1 -1
  126. package/dist/src/slide-components/image.js +1 -1
  127. package/dist/src/slide-components/image.js.map +1 -1
  128. package/dist/src/slide-components/in-parallel.d.ts.map +1 -1
  129. package/dist/src/slide-components/in-parallel.js.map +1 -1
  130. package/dist/src/slide-components/multi-text.d.ts.map +1 -1
  131. package/dist/src/slide-components/multi-text.js +1 -1
  132. package/dist/src/slide-components/multi-text.js.map +1 -1
  133. package/dist/src/slide-components/rectangle.d.ts.map +1 -1
  134. package/dist/src/slide-components/rectangle.js +1 -1
  135. package/dist/src/slide-components/rectangle.js.map +1 -1
  136. package/dist/src/slide-components/registry.js.map +1 -1
  137. package/dist/src/slide-components/serialize.d.ts.map +1 -1
  138. package/dist/src/slide-components/serialize.js +2 -1
  139. package/dist/src/slide-components/serialize.js.map +1 -1
  140. package/dist/src/slide-components/split-video-clip.d.ts.map +1 -1
  141. package/dist/src/slide-components/split-video-clip.js +4 -1
  142. package/dist/src/slide-components/split-video-clip.js.map +1 -1
  143. package/dist/src/slide-components/video-clip.d.ts.map +1 -1
  144. package/dist/src/slide-components/video-clip.js +7 -4
  145. package/dist/src/slide-components/video-clip.js.map +1 -1
  146. package/dist/src/slide-components/video-info.d.ts.map +1 -1
  147. package/dist/src/slide-components/video-info.js.map +1 -1
  148. package/dist/src/slow-image-sources.d.ts.map +1 -1
  149. package/dist/src/slow-image-sources.js +1 -1
  150. package/dist/src/slow-image-sources.js.map +1 -1
  151. package/dist/src/snapshot.d.ts.map +1 -1
  152. package/dist/src/snapshot.js +1 -1
  153. package/dist/src/snapshot.js.map +1 -1
  154. package/dist/src/utility.d.ts.map +1 -1
  155. package/dist/src/utility.js +1 -3
  156. package/dist/src/utility.js.map +1 -1
  157. package/lib/main-control.ts +3 -1
  158. package/package.json +4 -1
  159. package/src/corner-rounder.ts +5 -12
  160. package/src/glib/letters-base.ts +1 -1
  161. package/src/local-files.ts +19 -6
  162. package/src/peano-fourier/fourier-shared.ts +8 -1
  163. package/src/slide-components/fourier.ts +32 -10
  164. package/src/slide-components/image.ts +4 -1
  165. package/src/slide-components/in-parallel.ts +1 -2
  166. package/src/slide-components/multi-text.ts +17 -4
  167. package/src/slide-components/rectangle.ts +4 -1
  168. package/src/slide-components/registry.ts +1 -1
  169. package/src/slide-components/serialize.ts +4 -1
  170. package/src/slide-components/split-video-clip.ts +4 -1
  171. package/src/slide-components/video-clip.ts +21 -8
  172. package/src/slide-components/video-info.ts +4 -1
  173. package/src/slow-image-sources.ts +5 -1
  174. package/src/snapshot.ts +13 -4
  175. package/src/utility.ts +70 -33
@@ -22,7 +22,7 @@ import { ownerOf, parseSoundClips, rehomeSoundClip, soundOwners, startWithin, ti
22
22
  import { askHowToSplit, askHowToSplitSound } from "./split-dialog.js";
23
23
  import { SyncedFile } from "./synced-file.js";
24
24
  import { TimelineDisplay, } from "./timeline-display.js";
25
- import { forgetTranscripts, makeTranscript, onTranscriptsChange, readyTranscript, setTranscriptsEnabled, statusText, transcriptsEnabled, transcriptStatus, transcriptsSummary, } from "./transcripts.js";
25
+ import { forgetTranscriptFailures, forgetTranscripts, makeTranscript, onTranscriptsChange, readyTranscript, setTranscriptsEnabled, statusText, transcriptsEnabled, transcriptStatus, transcriptsSummary, } from "./transcripts.js";
26
26
  import { transcriptFileName, transcriptJson } from "./transcript-export.js";
27
27
  import { wordFlag } from "./transcript-align.js";
28
28
  import { canvasRecorder } from "../lib/main-control.js";
@@ -136,7 +136,7 @@ function applyZoom(zoom, px = panX, py = panY) {
136
136
  canvas.height = physH;
137
137
  canvas.style.width = `${cssW}px`;
138
138
  canvas.style.height = `${cssH}px`;
139
- showFrame(playPositionSeconds.valueAsNumber * 1000, true);
139
+ showFrame(playheadInVideoMs(), true);
140
140
  }
141
141
  [panX, panY] = clampPan(cssW, cssH, px, py);
142
142
  canvas.style.left = `${panX}px`;
@@ -166,7 +166,7 @@ function zoomToFit() {
166
166
  canvas.style.left = `${Math.round((vpW - cssW) / 2)}px`;
167
167
  canvas.style.top = `${Math.round((vpH - cssH) / 2)}px`;
168
168
  // Immediately redraw so the canvas clear doesn't show as a gray flash.
169
- showFrame(playPositionSeconds.valueAsNumber * 1000, true);
169
+ showFrame(playheadInVideoMs(), true);
170
170
  }
171
171
  zoomSelect.addEventListener("change", () => {
172
172
  if (zoomSelect.value === "fit") {
@@ -261,9 +261,10 @@ function showVideoSize(size) {
261
261
  videoSizeWarning.title = stretched
262
262
  ? `The pixels are ${pixelAspect.toFixed(3)} : 1 and the units are ${unitAspect.toFixed(3)} : 1, so the picture is stretched to fit.`
263
263
  : "";
264
- getById("videoSizeControls", HTMLSpanElement).title = canvasRecorder.sizeLocked
265
- ? "The size of the video. This program sets it, so it can't be changed here."
266
- : "The size of the video. Pixels: the saved file. Units: the coordinates everything is drawn in. Remembered for this video.";
264
+ getById("videoSizeControls", HTMLSpanElement).title =
265
+ canvasRecorder.sizeLocked
266
+ ? "The size of the video. This program sets it, so it can't be changed here."
267
+ : "The size of the video. Pixels: the saved file. Units: the coordinates everything is drawn in. Remembered for this video.";
267
268
  }
268
269
  showVideoSize(canvasRecorder.size);
269
270
  /**
@@ -326,9 +327,12 @@ function updateLocalFilesButton() {
326
327
  const names = localFilesNeedingPermission();
327
328
  localFilesBtn.hidden = names.length === 0;
328
329
  localFilesBtn.textContent =
329
- names.length === 1 ? "🔓 Allow local file" : `🔓 Allow ${names.length} local files`;
330
+ names.length === 1
331
+ ? "🔓 Allow local file"
332
+ : `🔓 Allow ${names.length} local files`;
330
333
  localFilesBtn.title =
331
- "The browser needs your OK before it reads these files again:\n" + names.join("\n");
334
+ "The browser needs your OK before it reads these files again:\n" +
335
+ names.join("\n");
332
336
  }
333
337
  onLocalFilesChange(updateLocalFilesButton);
334
338
  /** Ask about every file that needs it. Then the sounds can load. */
@@ -336,6 +340,7 @@ async function allowLocalFiles() {
336
340
  await requestLocalFileAccess();
337
341
  // The images and video clips try again by themselves.
338
342
  soundtrack.forgetFailures();
343
+ forgetTranscriptFailures();
339
344
  }
340
345
  localFilesBtn.addEventListener("click", () => void allowLocalFiles());
341
346
  /**
@@ -460,29 +465,65 @@ const playPositionRange = getById("playPositionRange", HTMLInputElement);
460
465
  * This can be a number outside of the normal range of the current section.
461
466
  * This, like all user visible number, is measured in seconds. Internal
462
467
  * things measure time in milliseconds.
468
+ *
469
+ * It's measured from the start of the Visual Editor's root when
470
+ * {@link playPositionLocal} is checked, otherwise from the start of the video.
471
+ * Read it with {@link playheadInVideoMs} and write it with
472
+ * {@link loadPlayPositionSeconds}, which take care of that.
463
473
  */
464
474
  const playPositionSeconds = getById("playPositionSeconds", HTMLInputElement);
475
+ /**
476
+ * Checked: {@link playPositionSeconds} is measured from the start of the
477
+ * Visual Editor's root. Unchecked: from the start of the video.
478
+ */
479
+ const playPositionLocal = getById("playPositionLocal", HTMLInputElement);
465
480
  /**
466
481
  * When we get to the end of the section, automatically jump back to the
467
482
  * beginning of the section.
468
483
  */
469
484
  const repeatRadioButton = querySelector('input[name="onSectionEnd"][value="repeat"]', HTMLInputElement);
485
+ /**
486
+ * The playhead, measured from the start of the whole video.
487
+ * {@link playPositionSeconds} is the source of truth.
488
+ */
489
+ function playheadInVideoMs() {
490
+ const shownMs = playPositionSeconds.valueAsNumber * 1000;
491
+ return playPositionLocal.checked ? shownMs + rootStartMs : shownMs;
492
+ }
470
493
  /**
471
494
  * The playhead, measured from the start of the Visual Editor's root: the
472
495
  * time its keyframes use.
473
496
  */
474
497
  function playheadInRootMs() {
475
- return playPositionSeconds.valueAsNumber * 1000 - rootStartMs;
498
+ return playheadInVideoMs() - rootStartMs;
476
499
  }
477
500
  /**
478
501
  * Update the input that shows the time as a number.
479
- * @param timeInMs The time to display in the number control.
502
+ * @param timeInMs The time to display in the number control, measured from the start of the
503
+ * video. It's shown from the start of the root instead if {@link playPositionLocal} says so.
480
504
  * This is *automatically* converted to seconds and rounded to the nearest ⅒ of a millisecond.
481
505
  * We only use milliseconds, not seconds, inside the code.
482
506
  */
483
507
  function loadPlayPositionSeconds(timeInMs) {
484
- playPositionSeconds.value = (timeInMs / 1000).toFixed(4);
508
+ const shownMs = playPositionLocal.checked ? timeInMs - rootStartMs : timeInMs;
509
+ playPositionSeconds.value = (shownMs / 1000).toFixed(4);
485
510
  }
511
+ /**
512
+ * Move the Visual Editor's root. The playhead stays where it is in the video, so a local
513
+ * {@link playPositionSeconds} changes to match.
514
+ */
515
+ function setRootStartMs(ms) {
516
+ if (ms === rootStartMs)
517
+ return;
518
+ const playheadMs = playheadInVideoMs();
519
+ rootStartMs = ms;
520
+ loadPlayPositionSeconds(playheadMs);
521
+ }
522
+ playPositionLocal.addEventListener("input", () => {
523
+ // The same time, shown the other way. The number still means what the checkbox used to say.
524
+ const shownMs = playPositionSeconds.valueAsNumber * 1000;
525
+ loadPlayPositionSeconds(playPositionLocal.checked ? shownMs : shownMs + rootStartMs);
526
+ });
486
527
  /**
487
528
  * Update the range input to show the same time as the number input.
488
529
  *
@@ -492,7 +533,7 @@ function loadPlayPositionSeconds(timeInMs) {
492
533
  * If the user changes the range input, and the value is immediately copied to the number input for use in the program.
493
534
  */
494
535
  function loadPlayPositionRange() {
495
- playPositionRange.valueAsNumber = playPositionSeconds.valueAsNumber * 1000;
536
+ playPositionRange.valueAsNumber = playheadInVideoMs();
496
537
  }
497
538
  // The same rate as the decoded files and the voice recorder, whatever the hardware uses.
498
539
  const audioContext = new AudioContext({ sampleRate: SAMPLE_RATE });
@@ -509,7 +550,9 @@ const soundtrack = new Soundtrack({
509
550
  root: () => toShow,
510
551
  // Sound clips on the timeline can now show their waveforms, and the ones
511
552
  // that play to the end of their file now know how long that is.
512
- onDecoded: () => timelineDisplay.redraw(),
553
+ onDecoded: soundFileLoaded,
554
+ // Marked ⚠, on the timeline and on its card. See soundFileProblem().
555
+ onFailed: soundFileLoaded,
513
556
  // The new file may be a local one that needs 🔓.
514
557
  onNewSource: () => checkVideosLocalFiles(),
515
558
  });
@@ -690,7 +733,7 @@ const animationLoop = new AnimationLoop((_rAFTimeInMs) => {
690
733
  if (voiceRecording && !voiceChirping)
691
734
  stopVoiceRecording();
692
735
  playPositionSeconds.disabled = false;
693
- showFrame(playPositionSeconds.valueAsNumber * 1000, true);
736
+ showFrame(playheadInVideoMs(), true);
694
737
  return;
695
738
  }
696
739
  // Playing.
@@ -698,7 +741,7 @@ const animationLoop = new AnimationLoop((_rAFTimeInMs) => {
698
741
  if (!audioPlaying && !rafFallbackPlaying) {
699
742
  // Not running: first play, after pause/seek, or after repeat.
700
743
  // Clips whose file isn't decoded yet join in when it is, so nothing waits for them.
701
- let startMs = playPositionSeconds.valueAsNumber * 1000;
744
+ let startMs = playheadInVideoMs();
702
745
  if (startMs >= sectionEndTime - 0.05) {
703
746
  // At the end — jump to the beginning.
704
747
  startMs = sectionStartTime;
@@ -794,7 +837,7 @@ addEventListener("keypress", (event) => {
794
837
  }
795
838
  });
796
839
  getById("back5s", HTMLButtonElement).addEventListener("click", () => {
797
- const backMs = Math.max(sectionStartTime, playPositionSeconds.valueAsNumber * 1000 - 5000);
840
+ const backMs = Math.max(sectionStartTime, playheadInVideoMs() - 5000);
798
841
  loadPlayPositionSeconds(backMs);
799
842
  loadPlayPositionRange();
800
843
  stopAudio();
@@ -842,7 +885,7 @@ function framesBetween(startMs, endMs) {
842
885
  * It stays inside the play limits.
843
886
  */
844
887
  function stepOneFrame(direction) {
845
- const position = framePosition(playPositionSeconds.valueAsNumber * 1000);
888
+ const position = framePosition(playheadInVideoMs());
846
889
  let frame = direction > 0
847
890
  ? Math.floor(position + FRAME_TOLERANCE) + 1
848
891
  : Math.ceil(position - FRAME_TOLERANCE) - 1;
@@ -1093,6 +1136,12 @@ async function startRecording(saveStartMs = 0, saveEndMs = toShow.duration, pixe
1093
1136
  },
1094
1137
  ],
1095
1138
  });
1139
+ // The newest soundtrack, with every file in it decoded. Right after a reload or an
1140
+ // edit, that can take a moment. (After the file picker, which needs the click.)
1141
+ // Before anything is written: a sound that can't be read stops the save here.
1142
+ infoDiv.innerHTML = "Waiting for the sound…";
1143
+ await soundtrack.ready();
1144
+ assertSoundFilesLoaded();
1096
1145
  const writableStream = await fileHandle.createWritable();
1097
1146
  // Set up Mediabunny output (streaming to file)
1098
1147
  output = new Output({
@@ -1111,10 +1160,6 @@ async function startRecording(saveStartMs = 0, saveEndMs = toShow.duration, pixe
1111
1160
  const firstFrame = firstFrameAtOrAfter(saveStartMs);
1112
1161
  // The file starts where its first frame starts, so the sound starts there too.
1113
1162
  const audioStartMs = (firstFrame * 1000) / FPS;
1114
- // The newest soundtrack, with every file in it decoded. Right after a reload or an
1115
- // edit, that can take a moment. (After the file picker, which needs the click.)
1116
- infoDiv.innerHTML = "Waiting for the sound…";
1117
- await soundtrack.ready();
1118
1163
  const audioBuffer = soundtrack
1119
1164
  .mix(audioStartMs, Math.min(saveEndMs, toShow.duration))
1120
1165
  .getAudioBuffer();
@@ -1267,7 +1312,10 @@ const saveOkBtn = getById("saveOkBtn", HTMLButtonElement);
1267
1312
  * HEVC stores color at half resolution (4:2:0), so both numbers must be even.
1268
1313
  */
1269
1314
  function saveSizeProblem(width, height) {
1270
- if (!(Number.isInteger(width) && width > 0 && Number.isInteger(height) && height > 0)) {
1315
+ if (!(Number.isInteger(width) &&
1316
+ width > 0 &&
1317
+ Number.isInteger(height) &&
1318
+ height > 0)) {
1271
1319
  return "The size must be two whole numbers of pixels.";
1272
1320
  }
1273
1321
  if (width % 2 || height % 2) {
@@ -1290,7 +1338,7 @@ function updateSaveSizeNote() {
1290
1338
  saveSizeNote.style.color = "#555";
1291
1339
  }
1292
1340
  else {
1293
- const stretched = Math.abs((width / height) / (pixelWidth / pixelHeight) - 1) > 0.001;
1341
+ const stretched = Math.abs(width / height / (pixelWidth / pixelHeight) - 1) > 0.001;
1294
1342
  saveSizeNote.textContent = stretched
1295
1343
  ? `Stretched: the video is ${pixelWidth} × ${pixelHeight}.`
1296
1344
  : `${Math.round((width / pixelWidth) * 100)}% of the video's ${pixelWidth} × ${pixelHeight}.`;
@@ -1414,7 +1462,9 @@ async function saveDialogLocalFilesReady() {
1414
1462
  saveLocalFilesRow.hidden = problems.length === 0;
1415
1463
  const needPermission = problems.some((problem) => problem.reason === "permission");
1416
1464
  saveLocalFilesBtn.hidden = !needPermission;
1417
- saveLocalFilesNote.textContent = problems.map((problem) => problem.message).join(" ");
1465
+ saveLocalFilesNote.textContent = problems
1466
+ .map((problem) => problem.message)
1467
+ .join(" ");
1418
1468
  return problems.length === 0;
1419
1469
  }
1420
1470
  saveLocalFilesBtn.addEventListener("click", async () => {
@@ -1843,7 +1893,7 @@ function applyLimits() {
1843
1893
  */
1844
1894
  function updateFromSelect() {
1845
1895
  const info = chapterList[select.selectedIndex];
1846
- rootStartMs = info.start;
1896
+ setRootStartMs(info.start);
1847
1897
  if (keepLimitsCheckbox.checked && limitsShowable) {
1848
1898
  // Only the "Play this one" button changes. Playback carries on.
1849
1899
  applyLimits();
@@ -1889,7 +1939,7 @@ playRootBtn.addEventListener("click", () => {
1889
1939
  */
1890
1940
  function refreshChapters() {
1891
1941
  if (initChapters()) {
1892
- rootStartMs = chapterList[select.selectedIndex].start;
1942
+ setRootStartMs(chapterList[select.selectedIndex].start);
1893
1943
  applyLimits();
1894
1944
  // The range clamped itself to the new bounds; show the real position again. The
1895
1945
  // number input stays the source of truth, so a playhead past a shortened end is left
@@ -1944,8 +1994,11 @@ transcriptView = new TranscriptView({
1944
1994
  const status = transcriptStatus(source);
1945
1995
  if (!status)
1946
1996
  return undefined;
1947
- return status.kind === "ready" ? status.transcript : (statusText(status) ?? "");
1997
+ return status.kind === "ready"
1998
+ ? status.transcript
1999
+ : (statusText(status) ?? "");
1948
2000
  },
2001
+ problem: (clip) => soundFileProblem(clip),
1949
2002
  seek: (rootMs) => timelineDisplay.onSeek?.(rootMs),
1950
2003
  selectClip: (clip) => timelineDisplay.onBlockClick?.(clip),
1951
2004
  selectedClip: () => selectedSoundClip,
@@ -2046,6 +2099,35 @@ function _onClipValueChanged() {
2046
2099
  // MARK: Sound clips
2047
2100
  /** Shortest a sound clip can be trimmed to by dragging, in ms. */
2048
2101
  const MIN_SOUND_CLIP_MS = 50;
2102
+ /** A sound file was decoded, or failed: update the timeline and the Sound Clips cards. */
2103
+ function soundFileLoaded() {
2104
+ timelineDisplay.redraw();
2105
+ for (const cb of soundClipSyncCallbacks)
2106
+ cb();
2107
+ }
2108
+ /**
2109
+ * Why `clip`'s file can't be played, or undefined if nothing is wrong, as far as we know yet.
2110
+ * Shown on the timeline, on the clip's card and in the 📖 Text view. A missing file must not
2111
+ * look like a file full of silence, even when its old transcript still shows.
2112
+ */
2113
+ function soundFileProblem(clip) {
2114
+ const reason = soundtrack.failure(clip.source);
2115
+ if (reason === undefined)
2116
+ return undefined;
2117
+ return reason instanceof Error ? reason.message : String(reason);
2118
+ }
2119
+ /** Stop a save, if any sound in it can't be read. Otherwise the saved sound would have holes. */
2120
+ function assertSoundFilesLoaded() {
2121
+ const failed = soundtrack.failures();
2122
+ if (failed.length === 0)
2123
+ return;
2124
+ const { url, reason } = failed[0];
2125
+ const why = (reason instanceof Error ? reason.message : String(reason)).replace(/([^.!?)])$/, "$1.");
2126
+ const which = failed.length === 1
2127
+ ? ""
2128
+ : `${failed.length} sound files couldn't be read. The first: `;
2129
+ throw new Error(`${which}Can't play "${url}": ${why} Each clip that uses ${failed.length === 1 ? "it" : "one"} is marked ⚠ on the timeline.`);
2130
+ }
2049
2131
  /** How long the file `clip` plays from is, once it has been decoded. */
2050
2132
  function soundFileMs(clip) {
2051
2133
  const buffer = soundtrack.getDecodedBuffer(clip.source);
@@ -2124,11 +2206,16 @@ function soundClipBlock(clip, ownerStart) {
2124
2206
  return {
2125
2207
  id: clip,
2126
2208
  get label() {
2209
+ const problem = soundFileProblem(clip);
2210
+ if (problem !== undefined)
2211
+ return `⚠ ${problem}`;
2127
2212
  // Notes if there are any, or else the first few words, so clips can be
2128
2213
  // told apart even zoomed out.
2129
2214
  return clip.notes || clipText(clip, 6) || clip.source || "—";
2130
2215
  },
2131
- color: "#3aab3a",
2216
+ get color() {
2217
+ return soundFileProblem(clip) === undefined ? "#3aab3a" : "#9a3412";
2218
+ },
2132
2219
  lane: "sound",
2133
2220
  transcript: () => {
2134
2221
  // Asking is what queues the file for transcribing.
@@ -2330,7 +2417,10 @@ function saveState() {
2330
2417
  sessionStorage.setItem("index", select.selectedIndex.toString());
2331
2418
  sessionStorage.setItem("keepLimits", keepLimitsCheckbox.checked ? "1" : "0");
2332
2419
  sessionStorage.setItem("limitsIndex", chapterList.findIndex((d) => d.selectable === limitsShowable).toString());
2333
- sessionStorage.setItem("timeInSeconds", playPositionSeconds.value);
2420
+ // From the start of the video, however the field shows it. "" for an empty field.
2421
+ const playheadMs = playheadInVideoMs();
2422
+ sessionStorage.setItem("timeInSeconds", isFinite(playheadMs) ? (playheadMs / 1000).toString() : "");
2423
+ sessionStorage.setItem("playPositionLocal", playPositionLocal.checked ? "1" : "0");
2334
2424
  sessionStorage.setItem("state", querySelector('input[name="onSectionEnd"]:checked', HTMLInputElement).value);
2335
2425
  sessionStorage.setItem("wasPlaying", playCheckBox.checked ? "1" : "0");
2336
2426
  sessionStorage.setItem("zoomSelect", zoomSelect.value);
@@ -2552,7 +2642,9 @@ const jsonSync = new SyncedFile({
2552
2642
  if (enabled) {
2553
2643
  // Only one file per video is synced (and so "active") at a time.
2554
2644
  for (const r of getAllReq.result) {
2555
- if (r.videoKey === toShowKey && r.filename !== filename && r.isActive)
2645
+ if (r.videoKey === toShowKey &&
2646
+ r.filename !== filename &&
2647
+ r.isActive)
2556
2648
  store.put({ ...r, isActive: false, syncEnabled: false });
2557
2649
  }
2558
2650
  }
@@ -2773,7 +2865,8 @@ function buildDiffText() {
2773
2865
  out.push(`${indent}${varName}.setDuration(${comp.duration});`);
2774
2866
  }
2775
2867
  if ((comp.soundClips?.length || def.soundClips?.length) &&
2776
- JSON.stringify(comp.soundClips ?? []) !== JSON.stringify(def.soundClips ?? [])) {
2868
+ JSON.stringify(comp.soundClips ?? []) !==
2869
+ JSON.stringify(def.soundClips ?? [])) {
2777
2870
  out.push(`${indent}${varName}.soundClips = ${JSON.stringify(comp.soundClips ?? [])};`);
2778
2871
  }
2779
2872
  out.push(`${indent}${parentExpr}.replaceableComponents!.push(${varName});`);
@@ -2905,7 +2998,10 @@ function buildDiffText() {
2905
2998
  diffReplaceable(d.components ?? [], comp, childPath);
2906
2999
  }
2907
3000
  else {
2908
- printPathIfNeeded([...path, d.userEditableDescription ?? d.registryKey]);
3001
+ printPathIfNeeded([
3002
+ ...path,
3003
+ d.userEditableDescription ?? d.registryKey,
3004
+ ]);
2909
3005
  lines.push(` [deleted]`);
2910
3006
  }
2911
3007
  }
@@ -3229,10 +3325,6 @@ function currentSaveTarget() {
3229
3325
  return selectable.schedules?.length ? selectable : null;
3230
3326
  }
3231
3327
  function saveOnUnload() {
3232
- // Start closing any voice recording's files. They may not finish in time.
3233
- // TODO: test this, and add the clip being recorded before the save below.
3234
- // See "Risks" in development-plans/voice-recording.md.
3235
- voicePanel?.recorder.flush();
3236
3328
  // When the history dialog is open the live tree may hold a transient preview from
3237
3329
  // _dialogSelectItem. Back up the pre-dialog state instead, so a preview is never
3238
3330
  // persisted as if the user had accepted it.
@@ -3695,7 +3787,7 @@ function buildScheduleSection(info, selectable) {
3695
3787
  markDirty();
3696
3788
  }
3697
3789
  function currentTimeMs() {
3698
- return playPositionSeconds.valueAsNumber * 1000;
3790
+ return playheadInVideoMs();
3699
3791
  }
3700
3792
  /**
3701
3793
  * Where the component that owns this schedule starts, measured from the start of the Visual
@@ -3991,8 +4083,10 @@ function buildScheduleSection(info, selectable) {
3991
4083
  else {
3992
4084
  const none = document.createElement("span");
3993
4085
  none.textContent = "—";
3994
- none.title = "The last keyframe has no duration: nothing comes after it.";
3995
- none.style.cssText = "display:inline-block;width:6em;text-align:center;color:#888";
4086
+ none.title =
4087
+ "The last keyframe has no duration: nothing comes after it.";
4088
+ none.style.cssText =
4089
+ "display:inline-block;width:6em;text-align:center;color:#888";
3996
4090
  cell.append(none);
3997
4091
  }
3998
4092
  const atLabel = document.createElement("span");
@@ -4869,12 +4963,14 @@ function buildSoundClipSection(selectable) {
4869
4963
  const header = document.createElement("div");
4870
4964
  header.style.cssText =
4871
4965
  "display:flex;align-items:center;gap:0.4em;margin-bottom:0.3em;font-size:0.85em;color:#555";
4872
- header.textContent = "Times are in ms, measured from the start of this component.";
4873
- const playheadInOwner = () => tidyMs(playPositionSeconds.valueAsNumber * 1000 - selfStartMs);
4966
+ header.textContent =
4967
+ "Times are in ms, measured from the start of this component.";
4968
+ const playheadInOwner = () => tidyMs(playheadInVideoMs() - selfStartMs);
4874
4969
  const addBtn = document.createElement("button");
4875
4970
  addBtn.type = "button";
4876
4971
  addBtn.textContent = "+ Add";
4877
- addBtn.title = "Add an empty sound clip at the playhead. Then fill in its source.";
4972
+ addBtn.title =
4973
+ "Add an empty sound clip at the playhead. Then fill in its source.";
4878
4974
  addBtn.style.marginLeft = "auto";
4879
4975
  addBtn.addEventListener("click", () => {
4880
4976
  const clip = { source: "", startMsIntoScene: playheadInOwner() };
@@ -5066,6 +5162,17 @@ function buildSoundClipSection(selectable) {
5066
5162
  _onClipValueChanged();
5067
5163
  });
5068
5164
  row2.append(srcLabel, sourceInput);
5165
+ // Why the file can't be played, if it can't.
5166
+ const problemLine = document.createElement("div");
5167
+ problemLine.style.cssText =
5168
+ "margin-top:0.2em;font-size:0.85em;color:#b00020";
5169
+ const showProblem = () => {
5170
+ const problem = soundFileProblem(clip);
5171
+ problemLine.hidden = problem === undefined;
5172
+ problemLine.textContent = `⚠ ${problem}`;
5173
+ };
5174
+ showProblem();
5175
+ soundClipSyncCallbacks.push(showProblem);
5069
5176
  // Row 3: numeric time fields
5070
5177
  const row3 = document.createElement("div");
5071
5178
  row3.style.cssText =
@@ -5116,7 +5223,8 @@ function buildSoundClipSection(selectable) {
5116
5223
  row4.style.cssText =
5117
5224
  "display:flex;gap:0.4em;align-items:center;margin-top:0.2em;flex-wrap:wrap;font-size:0.85em";
5118
5225
  const ownerLabel = document.createElement("label");
5119
- ownerLabel.style.cssText = "display:flex;align-items:center;gap:0.2em;min-width:0;flex:1";
5226
+ ownerLabel.style.cssText =
5227
+ "display:flex;align-items:center;gap:0.2em;min-width:0;flex:1";
5120
5228
  ownerLabel.textContent = "owner:";
5121
5229
  ownerLabel.title =
5122
5230
  "Which component this sound belongs to. It moves when its owner moves. " +
@@ -5168,7 +5276,7 @@ function buildSoundClipSection(selectable) {
5168
5276
  drawPlaysAt();
5169
5277
  soundClipSyncCallbacks.push(drawPlaysAt);
5170
5278
  row4.append(ownerLabel, playsAt);
5171
- card.append(row1, row2, row3, row4);
5279
+ card.append(row1, row2, problemLine, row3, row4);
5172
5280
  section.append(card);
5173
5281
  if (clip === selectedSoundClip) {
5174
5282
  requestAnimationFrame(() => card.scrollIntoView({ block: "nearest" }));
@@ -5237,7 +5345,8 @@ function updateScheduleEditor(selectable) {
5237
5345
  soundClipSyncCallbacks.length = 0;
5238
5346
  rectAspectLock = null;
5239
5347
  // A highlighted sound clip stays highlighted only while its owner is selected.
5240
- if (selectedSoundClip && !selectable.soundClips?.includes(selectedSoundClip)) {
5348
+ if (selectedSoundClip &&
5349
+ !selectable.soundClips?.includes(selectedSoundClip)) {
5241
5350
  selectedSoundClip = undefined;
5242
5351
  }
5243
5352
  timelineDisplay.setSelectedSoundId(selectedSoundClip);
@@ -5661,8 +5770,7 @@ function rectHandlePositions(r) {
5661
5770
  * level, so local == logical and no conversion is needed).
5662
5771
  */
5663
5772
  function getMarkerRelTf() {
5664
- const component = selectedSlideChild ??
5665
- currentRoot();
5773
+ const component = selectedSlideChild ?? currentRoot();
5666
5774
  if (!component)
5667
5775
  return null;
5668
5776
  const compTf = componentTransforms.get(component);
@@ -6419,6 +6527,11 @@ function applyMarkerDrag(localX, localY, shiftKey = false) {
6419
6527
  // So when you configure the web page, and you hit refresh, or the page automatically
6420
6528
  // refreshes, you don't lose your settings.
6421
6529
  try {
6530
+ if (sessionStorage.getItem("playPositionLocal") === "0") {
6531
+ const playheadMs = playheadInVideoMs();
6532
+ playPositionLocal.checked = false;
6533
+ loadPlayPositionSeconds(playheadMs);
6534
+ }
6422
6535
  const index = sessionStorage.getItem("index");
6423
6536
  const timeInSeconds = sessionStorage.getItem("timeInSeconds");
6424
6537
  const state = sessionStorage.getItem("state");
@@ -6672,6 +6785,13 @@ getById("recordJustSound", HTMLButtonElement).addEventListener("click", async ()
6672
6785
  const before = infoDiv.innerHTML;
6673
6786
  infoDiv.innerHTML = "Waiting for the sound…";
6674
6787
  await soundtrack.ready();
6788
+ try {
6789
+ assertSoundFilesLoaded();
6790
+ }
6791
+ catch (error) {
6792
+ infoDiv.innerHTML = `<strong style="color:red">Not saved:</strong> ${escapeHtml(String(error instanceof Error ? error.message : error))}`;
6793
+ return;
6794
+ }
6675
6795
  infoDiv.innerHTML = before;
6676
6796
  downloadBlob("sound_file.wav", soundtrack.mix(0, toShow.duration).toBlob());
6677
6797
  });
@@ -6990,11 +7110,28 @@ if (import.meta.hot) {
6990
7110
  // Changing a typescript file invokes this.
6991
7111
  // Changing a css file would cause vite:beforeUpdate, instead.
6992
7112
  import.meta.hot.on("vite:beforeFullReload", (_data) => {
7113
+ finishVoiceOnUnload();
6993
7114
  saveState();
6994
7115
  saveOnUnload();
6995
7116
  });
6996
7117
  }
6997
- window.addEventListener("beforeunload", saveOnUnload);
7118
+ /**
7119
+ * Start closing any voice recording's files. They may not finish in time.
7120
+ *
7121
+ * Only when the page is really going away. Not on visibilitychange: a page is hidden by
7122
+ * another tab, another app or another desktop, often to read a script, and the recording
7123
+ * should keep going.
7124
+ *
7125
+ * TODO: test this, and add the clip being recorded before {@link saveOnUnload}'s backup.
7126
+ * See "Risks" in development-plans/voice-recording.md.
7127
+ */
7128
+ function finishVoiceOnUnload() {
7129
+ voicePanel?.recorder.flush();
7130
+ }
7131
+ window.addEventListener("beforeunload", () => {
7132
+ finishVoiceOnUnload();
7133
+ saveOnUnload();
7134
+ });
6998
7135
  document.addEventListener("visibilitychange", () => {
6999
7136
  if (document.visibilityState === "hidden")
7000
7137
  saveOnUnload();