canvas-recorder-lib 0.5.0 → 0.7.0

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 (347) hide show
  1. package/BREAKING-CHANGES.md +24 -0
  2. package/README.md +131 -25
  3. package/dev/audio-builder.ts +69 -161
  4. package/dev/canvas-recorder.css +59 -0
  5. package/dev/canvas-recorder.ts +1006 -212
  6. package/dev/delay-files.ts +1 -1
  7. package/dev/font-utils.ts +8 -1
  8. package/dev/format-editor.ts +734 -0
  9. package/dev/format-string.ts +110 -0
  10. package/dev/local-file-row.ts +107 -0
  11. package/dev/multi-text-dialog.ts +360 -0
  12. package/dev/multi-text-panel.ts +11 -0
  13. package/dev/player-body.ts +94 -6
  14. package/dev/rect-snap.ts +9 -9
  15. package/dev/slide-panel.ts +1 -1
  16. package/dev/sound-clips.ts +3 -2
  17. package/dev/soundtrack.ts +434 -0
  18. package/dev/split-dialog.ts +13 -2
  19. package/dev/static-image-panel.ts +35 -5
  20. package/dev/timeline-display.ts +50 -5
  21. package/dev/transcripts.ts +5 -3
  22. package/dev/video-clip-panel.ts +202 -16
  23. package/dev/voice/chirp.ts +129 -0
  24. package/dev/voice/folder.ts +84 -0
  25. package/dev/voice/voice-panel.ts +513 -0
  26. package/dev/voice/voice-recorder.ts +603 -0
  27. package/dist/dev/audio-builder.d.ts +14 -31
  28. package/dist/dev/audio-builder.d.ts.map +1 -1
  29. package/dist/dev/audio-builder.js +62 -134
  30. package/dist/dev/audio-builder.js.map +1 -1
  31. package/dist/dev/canvas-recorder.css +59 -0
  32. package/dist/dev/canvas-recorder.js +926 -198
  33. package/dist/dev/canvas-recorder.js.map +1 -1
  34. package/dist/dev/delay-files.js +1 -1
  35. package/dist/dev/delay-files.js.map +1 -1
  36. package/dist/dev/font-utils.d.ts.map +1 -1
  37. package/dist/dev/font-utils.js +9 -1
  38. package/dist/dev/font-utils.js.map +1 -1
  39. package/dist/dev/format-editor.d.ts +75 -0
  40. package/dist/dev/format-editor.d.ts.map +1 -0
  41. package/dist/dev/format-editor.js +642 -0
  42. package/dist/dev/format-editor.js.map +1 -0
  43. package/dist/dev/format-string.d.ts +62 -0
  44. package/dist/dev/format-string.d.ts.map +1 -0
  45. package/dist/dev/format-string.js +86 -0
  46. package/dist/dev/format-string.js.map +1 -0
  47. package/dist/dev/local-file-row.d.ts +22 -0
  48. package/dist/dev/local-file-row.d.ts.map +1 -0
  49. package/dist/dev/local-file-row.js +84 -0
  50. package/dist/dev/local-file-row.js.map +1 -0
  51. package/dist/dev/multi-text-dialog.d.ts +30 -0
  52. package/dist/dev/multi-text-dialog.d.ts.map +1 -0
  53. package/dist/dev/multi-text-dialog.js +297 -0
  54. package/dist/dev/multi-text-dialog.js.map +1 -0
  55. package/dist/dev/multi-text-panel.d.ts +2 -0
  56. package/dist/dev/multi-text-panel.d.ts.map +1 -1
  57. package/dist/dev/multi-text-panel.js +4 -0
  58. package/dist/dev/multi-text-panel.js.map +1 -1
  59. package/dist/dev/player-body.d.ts +1 -1
  60. package/dist/dev/player-body.d.ts.map +1 -1
  61. package/dist/dev/player-body.js +94 -6
  62. package/dist/dev/player-body.js.map +1 -1
  63. package/dist/dev/rect-snap.d.ts +1 -1
  64. package/dist/dev/rect-snap.js +7 -7
  65. package/dist/dev/rect-snap.js.map +1 -1
  66. package/dist/dev/slide-panel.d.ts +1 -1
  67. package/dist/dev/slide-panel.d.ts.map +1 -1
  68. package/dist/dev/slide-panel.js +1 -1
  69. package/dist/dev/slide-panel.js.map +1 -1
  70. package/dist/dev/sound-clips.d.ts +3 -2
  71. package/dist/dev/sound-clips.d.ts.map +1 -1
  72. package/dist/dev/sound-clips.js +3 -2
  73. package/dist/dev/sound-clips.js.map +1 -1
  74. package/dist/dev/soundtrack.d.ts +115 -0
  75. package/dist/dev/soundtrack.d.ts.map +1 -0
  76. package/dist/dev/soundtrack.js +369 -0
  77. package/dist/dev/soundtrack.js.map +1 -0
  78. package/dist/dev/split-dialog.d.ts.map +1 -1
  79. package/dist/dev/split-dialog.js +14 -2
  80. package/dist/dev/split-dialog.js.map +1 -1
  81. package/dist/dev/static-image-panel.d.ts +2 -0
  82. package/dist/dev/static-image-panel.d.ts.map +1 -1
  83. package/dist/dev/static-image-panel.js +29 -5
  84. package/dist/dev/static-image-panel.js.map +1 -1
  85. package/dist/dev/timeline-display.d.ts +23 -2
  86. package/dist/dev/timeline-display.d.ts.map +1 -1
  87. package/dist/dev/timeline-display.js +47 -5
  88. package/dist/dev/timeline-display.js.map +1 -1
  89. package/dist/dev/transcripts.d.ts.map +1 -1
  90. package/dist/dev/transcripts.js +5 -3
  91. package/dist/dev/transcripts.js.map +1 -1
  92. package/dist/dev/video-clip-panel.d.ts +8 -0
  93. package/dist/dev/video-clip-panel.d.ts.map +1 -1
  94. package/dist/dev/video-clip-panel.js +176 -10
  95. package/dist/dev/video-clip-panel.js.map +1 -1
  96. package/dist/dev/voice/chirp.d.ts +27 -0
  97. package/dist/dev/voice/chirp.d.ts.map +1 -0
  98. package/dist/dev/voice/chirp.js +107 -0
  99. package/dist/dev/voice/chirp.js.map +1 -0
  100. package/dist/dev/voice/folder.d.ts +18 -0
  101. package/dist/dev/voice/folder.d.ts.map +1 -0
  102. package/dist/dev/voice/folder.js +68 -0
  103. package/dist/dev/voice/folder.js.map +1 -0
  104. package/dist/dev/voice/voice-panel.d.ts +44 -0
  105. package/dist/dev/voice/voice-panel.d.ts.map +1 -0
  106. package/dist/dev/voice/voice-panel.js +418 -0
  107. package/dist/dev/voice/voice-panel.js.map +1 -0
  108. package/dist/dev/voice/voice-recorder.d.ts +141 -0
  109. package/dist/dev/voice/voice-recorder.d.ts.map +1 -0
  110. package/dist/dev/voice/voice-recorder.js +506 -0
  111. package/dist/dev/voice/voice-recorder.js.map +1 -0
  112. package/dist/lib/index.d.ts +3 -0
  113. package/dist/lib/index.d.ts.map +1 -1
  114. package/dist/lib/index.js +3 -0
  115. package/dist/lib/index.js.map +1 -1
  116. package/dist/lib/main-control.d.ts +50 -4
  117. package/dist/lib/main-control.d.ts.map +1 -1
  118. package/dist/lib/main-control.js +94 -12
  119. package/dist/lib/main-control.js.map +1 -1
  120. package/dist/src/binary-search.js +1 -1
  121. package/dist/src/binary-search.js.map +1 -1
  122. package/dist/src/corner-rounder.d.ts +17 -0
  123. package/dist/src/corner-rounder.d.ts.map +1 -0
  124. package/dist/src/corner-rounder.js +89 -0
  125. package/dist/src/corner-rounder.js.map +1 -0
  126. package/dist/src/cross-fade.d.ts +1 -1
  127. package/dist/src/cross-fade.d.ts.map +1 -1
  128. package/dist/src/cross-fade.js +3 -3
  129. package/dist/src/cross-fade.js.map +1 -1
  130. package/dist/src/fancy-text.d.ts +116 -0
  131. package/dist/src/fancy-text.d.ts.map +1 -0
  132. package/dist/src/fancy-text.js +211 -0
  133. package/dist/src/fancy-text.js.map +1 -0
  134. package/dist/src/glib/letters-base.d.ts +1 -1
  135. package/dist/src/glib/letters-base.d.ts.map +1 -1
  136. package/dist/src/glib/letters-base.js +4 -3
  137. package/dist/src/glib/letters-base.js.map +1 -1
  138. package/dist/src/glib/line-font.d.ts +2 -2
  139. package/dist/src/glib/line-font.d.ts.map +1 -1
  140. package/dist/src/glib/line-font.js +2 -2
  141. package/dist/src/glib/line-font.js.map +1 -1
  142. package/dist/src/glib/paragraph-layout.d.ts +2 -2
  143. package/dist/src/glib/paragraph-layout.d.ts.map +1 -1
  144. package/dist/src/glib/paragraph-layout.js +1 -1
  145. package/dist/src/glib/paragraph-layout.js.map +1 -1
  146. package/dist/src/glib/path-shape-splitter.d.ts +1 -1
  147. package/dist/src/glib/path-shape-splitter.d.ts.map +1 -1
  148. package/dist/src/glib/path-shape-splitter.js +1 -1
  149. package/dist/src/glib/path-shape-splitter.js.map +1 -1
  150. package/dist/src/glib/path-shape.js +2 -2
  151. package/dist/src/glib/path-shape.js.map +1 -1
  152. package/dist/src/interpolate.d.ts +2 -2
  153. package/dist/src/interpolate.d.ts.map +1 -1
  154. package/dist/src/interpolate.js +1 -1
  155. package/dist/src/interpolate.js.map +1 -1
  156. package/dist/src/lattice.d.ts +1 -1
  157. package/dist/src/lattice.d.ts.map +1 -1
  158. package/dist/src/local-files.d.ts +129 -0
  159. package/dist/src/local-files.d.ts.map +1 -0
  160. package/dist/src/local-files.js +327 -0
  161. package/dist/src/local-files.js.map +1 -0
  162. package/dist/src/morph-animation.d.ts +1 -1
  163. package/dist/src/morph-animation.d.ts.map +1 -1
  164. package/dist/src/morph-animation.js +2 -2
  165. package/dist/src/morph-animation.js.map +1 -1
  166. package/dist/src/peano-fourier/fourier-shared.d.ts +1 -1
  167. package/dist/src/peano-fourier/fourier-shared.d.ts.map +1 -1
  168. package/dist/src/peano-fourier/fourier-shared.js +17 -4
  169. package/dist/src/peano-fourier/fourier-shared.js.map +1 -1
  170. package/dist/src/schedule-helper.d.ts +4 -4
  171. package/dist/src/schedule-helper.d.ts.map +1 -1
  172. package/dist/src/schedule-helper.js +2 -2
  173. package/dist/src/schedule-helper.js.map +1 -1
  174. package/dist/src/showable.d.ts +45 -4
  175. package/dist/src/showable.d.ts.map +1 -1
  176. package/dist/src/showable.js +37 -1
  177. package/dist/src/showable.js.map +1 -1
  178. package/dist/src/slide-components/arrow.d.ts +4 -4
  179. package/dist/src/slide-components/arrow.d.ts.map +1 -1
  180. package/dist/src/slide-components/arrow.js +2 -2
  181. package/dist/src/slide-components/arrow.js.map +1 -1
  182. package/dist/src/slide-components/cross-fade-transition.d.ts +3 -3
  183. package/dist/src/slide-components/cross-fade-transition.d.ts.map +1 -1
  184. package/dist/src/slide-components/cross-fade-transition.js +3 -3
  185. package/dist/src/slide-components/cross-fade-transition.js.map +1 -1
  186. package/dist/src/slide-components/duration-agnostic.d.ts +3 -3
  187. package/dist/src/slide-components/duration-agnostic.d.ts.map +1 -1
  188. package/dist/src/slide-components/duration-agnostic.js +1 -1
  189. package/dist/src/slide-components/duration-agnostic.js.map +1 -1
  190. package/dist/src/slide-components/fixed-duration.d.ts +1 -1
  191. package/dist/src/slide-components/fixed-duration.d.ts.map +1 -1
  192. package/dist/src/slide-components/fixed-duration.js +1 -1
  193. package/dist/src/slide-components/fixed-duration.js.map +1 -1
  194. package/dist/src/slide-components/fourier.d.ts +6 -6
  195. package/dist/src/slide-components/fourier.d.ts.map +1 -1
  196. package/dist/src/slide-components/fourier.js +6 -6
  197. package/dist/src/slide-components/fourier.js.map +1 -1
  198. package/dist/src/slide-components/frame-counter.d.ts +2 -2
  199. package/dist/src/slide-components/frame-counter.d.ts.map +1 -1
  200. package/dist/src/slide-components/frame-counter.js +1 -1
  201. package/dist/src/slide-components/frame-counter.js.map +1 -1
  202. package/dist/src/slide-components/function-graph.d.ts +2 -2
  203. package/dist/src/slide-components/function-graph.d.ts.map +1 -1
  204. package/dist/src/slide-components/function-graph.js +3 -3
  205. package/dist/src/slide-components/function-graph.js.map +1 -1
  206. package/dist/src/slide-components/halftone-shadow.d.ts +4 -4
  207. package/dist/src/slide-components/halftone-shadow.d.ts.map +1 -1
  208. package/dist/src/slide-components/halftone-shadow.js +2 -2
  209. package/dist/src/slide-components/halftone-shadow.js.map +1 -1
  210. package/dist/src/slide-components/handwriting-effect.d.ts +5 -5
  211. package/dist/src/slide-components/handwriting-effect.d.ts.map +1 -1
  212. package/dist/src/slide-components/handwriting-effect.js +5 -5
  213. package/dist/src/slide-components/handwriting-effect.js.map +1 -1
  214. package/dist/src/slide-components/hold.d.ts +3 -3
  215. package/dist/src/slide-components/hold.d.ts.map +1 -1
  216. package/dist/src/slide-components/hold.js +3 -3
  217. package/dist/src/slide-components/hold.js.map +1 -1
  218. package/dist/src/slide-components/image.d.ts +3 -3
  219. package/dist/src/slide-components/image.d.ts.map +1 -1
  220. package/dist/src/slide-components/image.js +3 -3
  221. package/dist/src/slide-components/image.js.map +1 -1
  222. package/dist/src/slide-components/in-parallel.d.ts +3 -3
  223. package/dist/src/slide-components/in-parallel.d.ts.map +1 -1
  224. package/dist/src/slide-components/in-parallel.js +2 -2
  225. package/dist/src/slide-components/in-parallel.js.map +1 -1
  226. package/dist/src/slide-components/in-series.d.ts +4 -4
  227. package/dist/src/slide-components/in-series.d.ts.map +1 -1
  228. package/dist/src/slide-components/in-series.js +5 -1
  229. package/dist/src/slide-components/in-series.js.map +1 -1
  230. package/dist/src/slide-components/live-duration.d.ts +1 -1
  231. package/dist/src/slide-components/live-duration.d.ts.map +1 -1
  232. package/dist/src/slide-components/live-duration.js +1 -1
  233. package/dist/src/slide-components/live-duration.js.map +1 -1
  234. package/dist/src/slide-components/multi-text.d.ts +76 -15
  235. package/dist/src/slide-components/multi-text.d.ts.map +1 -1
  236. package/dist/src/slide-components/multi-text.js +170 -25
  237. package/dist/src/slide-components/multi-text.js.map +1 -1
  238. package/dist/src/slide-components/rectangle.d.ts +3 -3
  239. package/dist/src/slide-components/rectangle.d.ts.map +1 -1
  240. package/dist/src/slide-components/rectangle.js +1 -1
  241. package/dist/src/slide-components/rectangle.js.map +1 -1
  242. package/dist/src/slide-components/registry.d.ts +1 -1
  243. package/dist/src/slide-components/registry.d.ts.map +1 -1
  244. package/dist/src/slide-components/registry.js +18 -18
  245. package/dist/src/slide-components/registry.js.map +1 -1
  246. package/dist/src/slide-components/serialize.d.ts +1 -1
  247. package/dist/src/slide-components/serialize.d.ts.map +1 -1
  248. package/dist/src/slide-components/serialize.js +2 -2
  249. package/dist/src/slide-components/serialize.js.map +1 -1
  250. package/dist/src/slide-components/show-error.js +2 -2
  251. package/dist/src/slide-components/show-error.js.map +1 -1
  252. package/dist/src/slide-components/simple-text.d.ts +4 -4
  253. package/dist/src/slide-components/simple-text.d.ts.map +1 -1
  254. package/dist/src/slide-components/simple-text.js +4 -4
  255. package/dist/src/slide-components/simple-text.js.map +1 -1
  256. package/dist/src/slide-components/slide-component.d.ts +5 -5
  257. package/dist/src/slide-components/slide-component.d.ts.map +1 -1
  258. package/dist/src/slide-components/slide-component.js +4 -4
  259. package/dist/src/slide-components/slide-component.js.map +1 -1
  260. package/dist/src/slide-components/slide-left.d.ts +3 -3
  261. package/dist/src/slide-components/slide-left.d.ts.map +1 -1
  262. package/dist/src/slide-components/slide-left.js +3 -3
  263. package/dist/src/slide-components/slide-left.js.map +1 -1
  264. package/dist/src/slide-components/split-video-clip.d.ts +2 -2
  265. package/dist/src/slide-components/split-video-clip.d.ts.map +1 -1
  266. package/dist/src/slide-components/split-video-clip.js +3 -3
  267. package/dist/src/slide-components/split-video-clip.js.map +1 -1
  268. package/dist/src/slide-components/text-effect.d.ts +2 -2
  269. package/dist/src/slide-components/text-effect.d.ts.map +1 -1
  270. package/dist/src/slide-components/traditional-text.d.ts +4 -4
  271. package/dist/src/slide-components/traditional-text.d.ts.map +1 -1
  272. package/dist/src/slide-components/traditional-text.js +3 -3
  273. package/dist/src/slide-components/traditional-text.js.map +1 -1
  274. package/dist/src/slide-components/video-clip.d.ts +17 -5
  275. package/dist/src/slide-components/video-clip.d.ts.map +1 -1
  276. package/dist/src/slide-components/video-clip.js +52 -11
  277. package/dist/src/slide-components/video-clip.js.map +1 -1
  278. package/dist/src/slide-components/video-info.d.ts +11 -11
  279. package/dist/src/slide-components/video-info.d.ts.map +1 -1
  280. package/dist/src/slide-components/video-info.js +32 -21
  281. package/dist/src/slide-components/video-info.js.map +1 -1
  282. package/dist/src/slow-image-sources.d.ts +7 -1
  283. package/dist/src/slow-image-sources.d.ts.map +1 -1
  284. package/dist/src/slow-image-sources.js +36 -5
  285. package/dist/src/slow-image-sources.js.map +1 -1
  286. package/dist/src/snapshot.d.ts +2 -2
  287. package/dist/src/snapshot.d.ts.map +1 -1
  288. package/dist/src/snapshot.js +31 -15
  289. package/dist/src/snapshot.js.map +1 -1
  290. package/dist/src/stroke-colors.d.ts +1 -1
  291. package/dist/src/stroke-colors.d.ts.map +1 -1
  292. package/dist/src/stroke-colors.js +4 -4
  293. package/dist/src/stroke-colors.js.map +1 -1
  294. package/dist/src/utility.d.ts +3 -4
  295. package/dist/src/utility.d.ts.map +1 -1
  296. package/dist/src/utility.js +4 -4
  297. package/dist/src/utility.js.map +1 -1
  298. package/lib/index.ts +3 -0
  299. package/lib/main-control.ts +119 -12
  300. package/package.json +7 -5
  301. package/src/binary-search.ts +2 -2
  302. package/src/corner-rounder.ts +128 -0
  303. package/src/cross-fade.ts +3 -3
  304. package/src/fancy-text.ts +255 -0
  305. package/src/glib/letters-base.ts +4 -3
  306. package/src/glib/line-font.ts +2 -2
  307. package/src/glib/paragraph-layout.ts +2 -2
  308. package/src/glib/path-shape-splitter.ts +1 -1
  309. package/src/glib/path-shape.ts +2 -2
  310. package/src/interpolate.ts +3 -3
  311. package/src/lattice.ts +1 -1
  312. package/src/local-files.ts +389 -0
  313. package/src/morph-animation.ts +2 -2
  314. package/src/peano-fourier/fourier-shared.ts +17 -4
  315. package/src/schedule-helper.ts +4 -4
  316. package/src/showable.ts +63 -4
  317. package/src/slide-components/arrow.ts +4 -4
  318. package/src/slide-components/cross-fade-transition.ts +4 -4
  319. package/src/slide-components/duration-agnostic.ts +3 -3
  320. package/src/slide-components/fixed-duration.ts +1 -1
  321. package/src/slide-components/fourier.ts +9 -9
  322. package/src/slide-components/frame-counter.ts +2 -2
  323. package/src/slide-components/function-graph.ts +4 -4
  324. package/src/slide-components/halftone-shadow.ts +4 -4
  325. package/src/slide-components/handwriting-effect.ts +7 -7
  326. package/src/slide-components/hold.ts +4 -4
  327. package/src/slide-components/image.ts +5 -5
  328. package/src/slide-components/in-parallel.ts +4 -4
  329. package/src/slide-components/in-series.ts +7 -4
  330. package/src/slide-components/live-duration.ts +1 -1
  331. package/src/slide-components/multi-text.ts +195 -30
  332. package/src/slide-components/rectangle.ts +3 -3
  333. package/src/slide-components/registry.ts +19 -19
  334. package/src/slide-components/serialize.ts +2 -2
  335. package/src/slide-components/show-error.ts +2 -2
  336. package/src/slide-components/simple-text.ts +7 -7
  337. package/src/slide-components/slide-component.ts +7 -7
  338. package/src/slide-components/slide-left.ts +4 -4
  339. package/src/slide-components/split-video-clip.ts +4 -4
  340. package/src/slide-components/text-effect.ts +2 -2
  341. package/src/slide-components/traditional-text.ts +5 -5
  342. package/src/slide-components/video-clip.ts +64 -13
  343. package/src/slide-components/video-info.ts +33 -21
  344. package/src/slow-image-sources.ts +38 -6
  345. package/src/snapshot.ts +34 -19
  346. package/src/stroke-colors.ts +4 -4
  347. package/src/utility.ts +5 -5
@@ -5,16 +5,20 @@ import {
5
5
  centeredOnCanvas,
6
6
  maxFit,
7
7
  minCover,
8
- PIXELS_PER_UNIT,
8
+ pixelsPerUnit,
9
9
  preservePixels,
10
10
  sameCenter,
11
11
  shrinkToAspect,
12
12
  } from "../src/slide-components/video-info.ts";
13
13
  import { SingleImage } from "../src/slow-image-sources.ts";
14
14
  import { ALIGN_HORIZONTALLY, ALIGN_VERTICALLY, buildSnapRow } from "./rect-snap.ts";
15
+ import { buildLocalFileRow } from "./local-file-row.ts";
16
+ import { IMAGE_FILE_TYPES, onLocalFilesChange } from "../src/local-files.ts";
15
17
 
16
18
  /** What the panel needs from the Visual Editor. It never reaches into canvas-recorder.ts directly. */
17
19
  export type StaticImagePanelHooks = {
20
+ /** The URL was changed by a button here. Save, and rebuild the schedule editor so the URL field shows it. */
21
+ readonly urlChanged: () => void;
18
22
  /** A Dest Rect keyframe's value was replaced. Sync its number fields and on-canvas handles, and save. */
19
23
  readonly rectKeyframeChanged: (keyframe: Keyframe<ReadOnlyRect>) => void;
20
24
  /** The Dest Rect keyframe in ✎ edit mode, if there is one. */
@@ -51,6 +55,23 @@ export function buildStaticImagePanel(
51
55
  legend.textContent = "Image File";
52
56
  panel.append(legend);
53
57
 
58
+ panel.append(
59
+ buildLocalFileRow({
60
+ types: IMAGE_FILE_TYPES,
61
+ pickerId: "images",
62
+ startIn: "pictures",
63
+ currentUrl: () => component.urlSchedule.at(0),
64
+ choose(url) {
65
+ if (component.urlSchedule.schedule.length > 1) {
66
+ alert("URL has several keyframes. Type the file's URL into the one you want.\n\n" + url);
67
+ return;
68
+ }
69
+ component.urlSchedule.set(url);
70
+ hooks.urlChanged();
71
+ },
72
+ }),
73
+ );
74
+
54
75
  const line = (parent: HTMLElement, cssText = "") => {
55
76
  const div = document.createElement("div");
56
77
  div.style.cssText = cssText;
@@ -75,7 +96,7 @@ export function buildStaticImagePanel(
75
96
  const rectButtons = [
76
97
  {
77
98
  text: "Fit canvas",
78
- title: "As large as possible while the whole image stays on the 16×9 canvas. Centered.",
99
+ title: "As large as possible while the whole image stays on the canvas. Centered.",
79
100
  make: (size: Size) => centeredOnCanvas(maxFit(size.width, size.height)),
80
101
  },
81
102
  {
@@ -85,7 +106,7 @@ export function buildStaticImagePanel(
85
106
  },
86
107
  {
87
108
  text: "Native pixels",
88
- title: `One image pixel per output pixel at ${PIXELS_PER_UNIT} px/unit — pixel perfect. Keeps the rectangle's center.`,
109
+ title: `One image pixel per output pixel at ${pixelsPerUnit()} px/unit — pixel perfect. Keeps the rectangle's center.`,
89
110
  make: (size: Size, rect: ReadOnlyRect) =>
90
111
  sameCenter(preservePixels(size.width, size.height), rect),
91
112
  },
@@ -156,7 +177,7 @@ export function buildStaticImagePanel(
156
177
  // native resolution. Above 1 the image is being upscaled.
157
178
  const ratios = component.destRectSchedule.schedule.map(({ value }) => {
158
179
  const drawn = shrinkToAspect(value, size.width / size.height);
159
- return (drawn.width * PIXELS_PER_UNIT) / size.width;
180
+ return (drawn.width * pixelsPerUnit()) / size.width;
160
181
  });
161
182
  const lo = Math.min(...ratios);
162
183
  const hi = Math.max(...ratios);
@@ -196,7 +217,7 @@ export function buildStaticImagePanel(
196
217
  const url = component.urlSchedule.at(
197
218
  typeof target === "string" ? 0 : target.time,
198
219
  );
199
- if (url === (image?.url ?? "")) {
220
+ if (url === (image?.url ?? "") && !image?.worthRetrying) {
200
221
  // Same file. Only the Dest Rect may have changed.
201
222
  draw();
202
223
  return;
@@ -242,6 +263,15 @@ export function buildStaticImagePanel(
242
263
  });
243
264
  }
244
265
 
266
+ // Permission granted, or a file picked again: a file that failed may open now.
267
+ const stopListening = onLocalFilesChange(() => {
268
+ if (!panel.isConnected) {
269
+ stopListening();
270
+ return;
271
+ }
272
+ refresh();
273
+ });
274
+
245
275
  refresh();
246
276
  return {
247
277
  element: panel,
@@ -138,11 +138,33 @@ export class TimelineDisplay {
138
138
 
139
139
  // ── public API ─────────────────────────────────────────────────────────────
140
140
 
141
- /** Call when the chapter changes; resets zoom/pan to show the full chapter. */
142
- setChapterDuration(durationMs: number): void {
143
- this._durationMs = Math.max(durationMs, 1);
144
- this._viewStartMs = 0;
145
- this._viewEndMs = this._durationMs;
141
+ /** The chapter last passed to {@link setChapter}. */
142
+ private _chapter: object | undefined;
143
+
144
+ /**
145
+ * Call whenever the chapter, or its length, may have changed.
146
+ *
147
+ * A different chapter shows all of itself.
148
+ * The same chapter keeps the user's zoom and pan, so an edit like ✂ Split doesn't undo them.
149
+ * The exception: a view that showed the whole chapter keeps showing the whole chapter.
150
+ * @param chapter The chapter, by identity.
151
+ * @param durationMs Its length now.
152
+ */
153
+ setChapter(chapter: object, durationMs: number): void {
154
+ const newDurationMs = Math.max(durationMs, 1);
155
+ const showingAll =
156
+ this._viewStartMs <= 0 && this._viewEndMs >= this._durationMs;
157
+ if (chapter !== this._chapter || showingAll) {
158
+ this._viewStartMs = 0;
159
+ this._viewEndMs = newDurationMs;
160
+ } else if (this._viewStartMs >= newDurationMs) {
161
+ // The chapter got shorter than where the view starts. Keep the zoom; show its end.
162
+ const span = this._viewEndMs - this._viewStartMs;
163
+ this._viewStartMs = Math.max(0, newDurationMs - span);
164
+ this._viewEndMs = this._viewStartMs + span;
165
+ }
166
+ this._chapter = chapter;
167
+ this._durationMs = newDurationMs;
146
168
  this._draw();
147
169
  }
148
170
 
@@ -180,6 +202,24 @@ export class TimelineDisplay {
180
202
  this._draw();
181
203
  }
182
204
 
205
+ /**
206
+ * An edit just moved the play head, like ✂ Split keeping only the second piece.
207
+ *
208
+ * If that took it off screen, bring it back the same way playback does: jump so it's at the
209
+ * left edge, keeping the zoom. If it was already off screen, the user put it there, so the
210
+ * view stays where it is.
211
+ * @param localMs The play head's new time, from the start of the chapter.
212
+ */
213
+ movePlayMs(localMs: number): void {
214
+ const wasVisible = this._playheadVisible();
215
+ this._playLocalMs = localMs;
216
+ if (wasVisible && !this._interacting && !this._playheadVisible()) {
217
+ this._jumpToPlayhead();
218
+ }
219
+ this._wasPlayheadVisible = this._playheadVisible();
220
+ this._draw();
221
+ }
222
+
183
223
  /**
184
224
  * The user just panned or zoomed. If that took the play head off screen,
185
225
  * that was their choice: don't follow it until it comes back into view.
@@ -210,6 +250,11 @@ export class TimelineDisplay {
210
250
  if (this._interacting || this._playheadVisible()) return;
211
251
  const justStarted = !this._wasFollowing;
212
252
  if (!this._wasPlayheadVisible && !justStarted) return;
253
+ this._jumpToPlayhead();
254
+ }
255
+
256
+ /** Put the play head at the left edge, keeping the zoom. Near the end, show the end instead. */
257
+ private _jumpToPlayhead(): void {
213
258
  const span = this._viewEndMs - this._viewStartMs;
214
259
  const start = Math.max(
215
260
  0,
@@ -1,4 +1,5 @@
1
1
  import { debugLog } from "../src/debug-log.ts";
2
+ import { fetchMedia } from "../src/local-files.ts";
2
3
  import {
3
4
  Device,
4
5
  MODELS,
@@ -221,7 +222,7 @@ export async function makeTranscript(
221
222
 
222
223
  /** The file at `url` as 16 kHz mono, the way the models want it. */
223
224
  async function decode(url: string): Promise<Float32Array> {
224
- const response = await fetch(url);
225
+ const response = await fetchMedia(url);
225
226
  if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
226
227
  // decodeAudioData resamples to its context's rate, which does the 16 kHz part.
227
228
  const context = new OfflineAudioContext(1, 1, SAMPLE_RATE);
@@ -305,13 +306,14 @@ type CacheRecord = Transcript & { readonly key: string; readonly url: string };
305
306
  async function cacheKey(url: string): Promise<string> {
306
307
  const absolute = new URL(url, location.href).href;
307
308
  const prefix = `${PIPELINE_VERSION}|${MODELS.whisper}|${absolute}|`;
308
- const head = await fetch(url, { method: "HEAD", cache: "no-cache" });
309
+ // For a local file, fetchMedia() answers with its size and modification time.
310
+ const head = await fetchMedia(url, { method: "HEAD", cache: "no-cache" });
309
311
  const etag = head.headers.get("etag");
310
312
  const modified = head.headers.get("last-modified");
311
313
  const length = head.headers.get("content-length");
312
314
  if (etag) return prefix + etag;
313
315
  if (modified && length) return `${prefix}${modified} ${length}`;
314
- const bytes = await (await fetch(url)).arrayBuffer();
316
+ const bytes = await (await fetchMedia(url)).arrayBuffer();
315
317
  const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
316
318
  return prefix + Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("");
317
319
  }
@@ -1,18 +1,25 @@
1
1
  import { ReadOnlyRect } from "phil-lib/misc";
2
2
  import { Keyframe } from "../src/interpolate.ts";
3
3
  import { SoundClip } from "../src/showable.ts";
4
+ import { canvasRecorder, type VideoSize } from "../lib/main-control.ts";
4
5
  import { VideoClipComponent } from "../src/slide-components/video-clip.ts";
5
6
  import {
6
7
  centeredOnCanvas,
7
8
  maxFit,
8
9
  minCover,
9
- PIXELS_PER_UNIT,
10
+ pixelsPerUnit,
10
11
  preservePixels,
11
12
  sameCenter,
12
13
  shrinkToAspect,
13
14
  VideoInfo,
14
15
  } from "../src/slide-components/video-info.ts";
15
16
  import { ALIGN_HORIZONTALLY, ALIGN_VERTICALLY, buildSnapRow } from "./rect-snap.ts";
17
+ import { buildLocalFileRow } from "./local-file-row.ts";
18
+ import {
19
+ LocalFileError,
20
+ onLocalFilesChange,
21
+ VIDEO_FILE_TYPES,
22
+ } from "../src/local-files.ts";
16
23
 
17
24
  /**
18
25
  * The `notes` on a sound clip that this panel created. This is how it
@@ -23,6 +30,13 @@ const IMPORTED_AUDIO_NOTE = "imported from video";
23
30
 
24
31
  /** What the panel needs from the Visual Editor. It never reaches into canvas-recorder.ts directly. */
25
32
  export type VideoClipPanelHooks = {
33
+ /**
34
+ * Change the whole video's size, and remember it, as if the user had typed it in the size fields.
35
+ * @returns False if the size is locked, or not usable.
36
+ */
37
+ readonly setVideoSize: (size: VideoSize) => boolean;
38
+ /** The URL was changed by a button here. Save, and rebuild the schedule editor so the URL field shows it. */
39
+ readonly urlChanged: () => void;
26
40
  /**
27
41
  * Start / End were changed by a button. Save, and rebuild the schedule
28
42
  * editor so the number fields show the new values.
@@ -68,6 +82,8 @@ function formatSpeed(speed: number): string {
68
82
  * every keystroke names a file that doesn't exist, and that is not a problem.
69
83
  */
70
84
  function quietError(error: unknown): string {
85
+ // These are written for the user already, and say what to do.
86
+ if (error instanceof LocalFileError) return error.message;
71
87
  const text = String(error);
72
88
  if (/404|not found|Failed to fetch/i.test(text)) return "Not found";
73
89
  if (/unsupported|unrecognized|format/i.test(text)) return "Not a video file this can read";
@@ -89,13 +105,21 @@ function playbackSpeed(clip: VideoClipComponent): number {
89
105
 
90
106
  const isRealTime = (speed: number) => Math.abs(speed - 1) < 0.0005;
91
107
 
108
+ /**
109
+ * The largest even number of pixels that fits in `pixels`.
110
+ * HEVC stores color at half resolution (4:2:0), so the saved file's sides must be even.
111
+ */
112
+ function evenSize(pixels: number): number {
113
+ return Math.max(2, Math.floor(pixels / 2) * 2);
114
+ }
115
+
92
116
  /**
93
117
  * Output pixels per source pixel, for one Dest Rect keyframe. 1 is native
94
118
  * resolution. Above 1 the video is being upscaled and will look soft.
95
119
  */
96
120
  function nativeRatio(rect: ReadOnlyRect, info: VideoInfo): number {
97
121
  const drawn = shrinkToAspect(rect, info.video.width / info.video.height);
98
- return (drawn.width * PIXELS_PER_UNIT) / info.video.width;
122
+ return (drawn.width * pixelsPerUnit()) / info.video.width;
99
123
  }
100
124
 
101
125
  /** The sound clip that would line this video's audio up with its picture, right now. */
@@ -122,6 +146,18 @@ function expectedSoundClip(
122
146
  };
123
147
  }
124
148
 
149
+ /**
150
+ * How far `sound` has been slid away from the picture, compared with a fresh import.
151
+ * Trimming either end doesn't change this; only moving the sound does.
152
+ * @returns Milliseconds. Negative means the sound plays later than the matching picture.
153
+ */
154
+ function syncOffsetMs(sound: SoundClip, expected: SoundClip): number {
155
+ // Which moment of the file plays at the clip's own time 0, for each.
156
+ const fileAtZero = (clip: SoundClip) =>
157
+ (clip.startMsIntoClip ?? 0) - clip.startMsIntoScene;
158
+ return fileAtZero(sound) - fileAtZero(expected);
159
+ }
160
+
125
161
  function sameSoundClip(a: SoundClip, b: SoundClip): boolean {
126
162
  const close = (x: number | undefined, y: number | undefined) =>
127
163
  x === y || (x !== undefined && y !== undefined && Math.abs(x - y) < 0.5);
@@ -152,6 +188,45 @@ export function buildVideoClipPanel(
152
188
  legend.textContent = "Video File";
153
189
  panel.append(legend);
154
190
 
191
+ panel.append(
192
+ buildLocalFileRow({
193
+ types: VIDEO_FILE_TYPES,
194
+ pickerId: "video-clips",
195
+ startIn: "videos",
196
+ currentUrl: () => clip.urlScalar.value,
197
+ choose(url) {
198
+ // Start and End still at a brand new clip's 0 and 1000 means nobody has chosen what to show.
199
+ // Then show the whole file at 1×, and import its audio, so it plays like the file itself.
200
+ const untouched =
201
+ clip.startMsIntoClipScalar.value === 0 &&
202
+ clip.endMsIntoClipScalar.value === 1000;
203
+ clip.urlScalar.value = url;
204
+ hooks.urlChanged();
205
+ if (!untouched) return;
206
+ clip.videoInfo()?.then(
207
+ (info) => {
208
+ // The user may have picked another file while this one opened.
209
+ if (clip.urlScalar.value !== url) return;
210
+ clip.endMsIntoClipScalar.value = info.durationMs;
211
+ // Bubbles up through scheduleHasChanged(), like the Whole file button.
212
+ clip.setDuration(info.durationMs);
213
+ if (info.audio) {
214
+ // The same as Import audio, now that the clip plays at 1×.
215
+ const kept = clip.soundClips.filter(
216
+ (sc) => sc.notes !== IMPORTED_AUDIO_NOTE,
217
+ );
218
+ clip.soundClips = [...kept, expectedSoundClip(clip, info.audio)];
219
+ clip.scheduleHasChanged();
220
+ }
221
+ hooks.scalarsChanged();
222
+ },
223
+ // The panel shows why the file didn't open.
224
+ () => {},
225
+ );
226
+ },
227
+ }),
228
+ );
229
+
155
230
  const line = (parent: HTMLElement, cssText = "") => {
156
231
  const div = document.createElement("div");
157
232
  div.style.cssText = cssText;
@@ -173,11 +248,9 @@ export function buildVideoClipPanel(
173
248
  const derivedEl = line(panel, "font-size:0.9em;line-height:1.4;margin-top:0.3em");
174
249
 
175
250
  const timingRow = line(panel, "margin-top:0.5em");
176
- const wholeFileBtn = button(
177
- timingRow,
178
- "Whole file",
179
- "Start = 0, End = the end of the file, and Duration to match, so it plays in real time.",
180
- );
251
+ const WHOLE_FILE_TITLE =
252
+ "Start = 0, End = the end of the file, and Duration to match, so it plays in real time.";
253
+ const wholeFileBtn = button(timingRow, "Whole file", WHOLE_FILE_TITLE);
181
254
  const realTimeBtn = button(
182
255
  timingRow,
183
256
  "Play at 1×",
@@ -190,10 +263,14 @@ export function buildVideoClipPanel(
190
263
  splitBtn.addEventListener("click", () => hooks.split());
191
264
 
192
265
  const rectRow = line(panel, "margin-top:0.4em");
266
+ const SIZE_VIDEO_TITLE =
267
+ "Make the whole video exactly this clip's size, in pixels, and fill it with this clip, " +
268
+ "one video pixel per output pixel. The units stay as wide as they are now.";
269
+ const sizeVideoBtn = button(rectRow, "Make video this size", SIZE_VIDEO_TITLE);
193
270
  const rectButtons = [
194
271
  {
195
272
  text: "Fit canvas",
196
- title: "As large as possible while the whole video stays on the 16×9 canvas. Centered.",
273
+ title: "As large as possible while the whole video stays on the canvas. Centered.",
197
274
  make: (info: VideoInfo) =>
198
275
  centeredOnCanvas(maxFit(info.video.width, info.video.height)),
199
276
  },
@@ -205,7 +282,7 @@ export function buildVideoClipPanel(
205
282
  },
206
283
  {
207
284
  text: "Native pixels",
208
- title: `One video pixel per output pixel at ${PIXELS_PER_UNIT} px/unit — pixel perfect. Keeps the rectangle's center.`,
285
+ title: `One video pixel per output pixel at ${pixelsPerUnit()} px/unit — pixel perfect. Keeps the rectangle's center.`,
209
286
  make: (info: VideoInfo, rect: ReadOnlyRect) =>
210
287
  sameCenter(preservePixels(info.video.width, info.video.height), rect),
211
288
  },
@@ -260,7 +337,7 @@ export function buildVideoClipPanel(
260
337
  function draw(): void {
261
338
  switch (state) {
262
339
  case "empty":
263
- statusEl.textContent = "No URL set";
340
+ statusEl.textContent = "No file yet: open one, or type a URL below.";
264
341
  statusEl.style.color = "#888";
265
342
  break;
266
343
  case "loading":
@@ -361,6 +438,24 @@ export function buildVideoClipPanel(
361
438
  realTimeBtn.hidden = Number.isNaN(speed) || isRealTime(speed);
362
439
  realTimeBtn.disabled = !(end > start);
363
440
  for (const { btn } of rectButtons) btn.disabled = !ready;
441
+ const alreadyWhole =
442
+ ready &&
443
+ start === 0 &&
444
+ Math.abs(end - info!.durationMs) < 0.5 &&
445
+ Math.abs(clip.duration - info!.durationMs) < 0.5;
446
+ wholeFileBtn.disabled = !ready || alreadyWhole;
447
+ wholeFileBtn.title = alreadyWhole
448
+ ? `${WHOLE_FILE_TITLE}\n\nIt already is.`
449
+ : WHOLE_FILE_TITLE;
450
+ const alreadySized = ready && videoMatchesClip(info!);
451
+ sizeVideoBtn.disabled = !ready || canvasRecorder.sizeLocked || alreadySized;
452
+ sizeVideoBtn.title = canvasRecorder.sizeLocked
453
+ ? `${SIZE_VIDEO_TITLE}\n\nThis program sets the video's size, so it can't be changed here.`
454
+ : alreadySized
455
+ ? `${SIZE_VIDEO_TITLE}\n\nIt already is.`
456
+ : ready
457
+ ? `${SIZE_VIDEO_TITLE}\n\n${evenSize(info!.video.width)} × ${evenSize(info!.video.height)} pixels.`
458
+ : SIZE_VIDEO_TITLE;
364
459
  alignRow.draw();
365
460
  drawAudio(speed);
366
461
  }
@@ -390,7 +485,7 @@ export function buildVideoClipPanel(
390
485
  return;
391
486
  }
392
487
  if (!isRealTime(speed)) {
393
- // AudioBuilder can't change the speed of a sound, so anything but 1×
488
+ // The soundtrack can't change the speed of a sound, so anything but 1×
394
489
  // would drift out of sync.
395
490
  setStatus(`Audio needs 1× — currently ${Number.isNaN(speed) ? "—" : formatSpeed(speed)}`, "#a06000");
396
491
  importBtn.disabled = true;
@@ -401,15 +496,27 @@ export function buildVideoClipPanel(
401
496
  importBtn.title =
402
497
  "Add this file's audio as a sound clip on this Video Clip, lined up with the picture. " +
403
498
  "It lives on the clip, so it moves with the clip on the timeline.";
499
+ const expected = expectedSoundClip(clip, audio);
404
500
  if (imported.length === 0) {
405
501
  setStatus("");
406
- } else if (
407
- imported.length === 1 &&
408
- sameSoundClip(imported[0], expectedSoundClip(clip, audio))
409
- ) {
502
+ } else if (imported.length > 1) {
503
+ setStatus("Audio imported, then cut into pieces");
504
+ importBtn.title += "\n\nRe-import replaces the pieces with one sound clip, lined up with the picture.";
505
+ } else if (sameSoundClip(imported[0], expected)) {
410
506
  setStatus("Audio imported ✓", "#070");
411
507
  } else {
412
- setStatus("Audio imported, but out of date", "#a06000");
508
+ const offsetMs = syncOffsetMs(imported[0], expected);
509
+ if (Math.abs(offsetMs) < 0.5) {
510
+ setStatus("Audio trimmed, still in sync ✓", "#070");
511
+ } else {
512
+ const seconds = (Math.abs(offsetMs) / 1000).toFixed(2);
513
+ setStatus(
514
+ `Audio moved ${seconds} s ${offsetMs < 0 ? "later" : "earlier"} than the picture`,
515
+ "#555",
516
+ );
517
+ }
518
+ importBtn.title +=
519
+ "\n\nRe-import undoes your changes to this sound clip: its start, its end, and any move.";
413
520
  }
414
521
  }
415
522
 
@@ -469,6 +576,67 @@ export function buildVideoClipPanel(
469
576
  return "Dest Rect has several keyframes. Click ✎ on the one you want to change.";
470
577
  }
471
578
 
579
+ /**
580
+ * What "Make video this size" does: the video's new size, and the Dest Rect that fills it.
581
+ *
582
+ * The pixels are the clip's, rounded down to even numbers. The units stay as wide as they
583
+ * are now, and get as tall as the clip's shape needs. The rectangle puts one clip pixel on
584
+ * one output pixel, from the top left corner. If a side was odd, its last column or row is
585
+ * off the edge, rather than resampling the whole picture.
586
+ */
587
+ function clipSize(info: VideoInfo): { size: VideoSize; rect: ReadOnlyRect } {
588
+ const { width: videoWidth, height: videoHeight } = info.video;
589
+ const pixelWidth = evenSize(videoWidth);
590
+ const pixelHeight = evenSize(videoHeight);
591
+ const width = canvasRecorder.width;
592
+ const height = (width * pixelHeight) / pixelWidth;
593
+ const unitsPerPixel = width / pixelWidth;
594
+ return {
595
+ size: { pixelWidth, pixelHeight, width, height },
596
+ rect: { x: 0, y: 0, width: videoWidth * unitsPerPixel, height: videoHeight * unitsPerPixel },
597
+ };
598
+ }
599
+
600
+ /** True when "Make video this size" would change nothing. */
601
+ function videoMatchesClip(info: VideoInfo): boolean {
602
+ const target = targetKeyframe();
603
+ // Several keyframes and none chosen: clicking explains that, so keep the button enabled.
604
+ if (typeof target === "string") return false;
605
+ const { size, rect } = clipSize(info);
606
+ const close = (a: number, b: number) => Math.abs(a - b) <= 1e-6 * Math.max(1, Math.abs(b));
607
+ const current = canvasRecorder.size;
608
+ return (
609
+ current.pixelWidth === size.pixelWidth &&
610
+ current.pixelHeight === size.pixelHeight &&
611
+ close(current.height, size.height) &&
612
+ close(target.value.x, rect.x) &&
613
+ close(target.value.y, rect.y) &&
614
+ close(target.value.width, rect.width) &&
615
+ close(target.value.height, rect.height)
616
+ );
617
+ }
618
+
619
+ sizeVideoBtn.addEventListener("click", () => {
620
+ if (!info) return;
621
+ const target = targetKeyframe();
622
+ if (typeof target === "string") {
623
+ rectHint.textContent = target;
624
+ return;
625
+ }
626
+ const { width: videoWidth, height: videoHeight } = info.video;
627
+ const { size, rect } = clipSize(info);
628
+ const { pixelWidth, pixelHeight } = size;
629
+ if (!hooks.setVideoSize(size)) return;
630
+ target.value = rect;
631
+ hooks.rectKeyframeChanged(target);
632
+ rectHint.textContent =
633
+ pixelWidth === videoWidth && pixelHeight === videoHeight
634
+ ? ""
635
+ : `The clip is ${videoWidth} × ${videoHeight}. The video is ${pixelWidth} × ${pixelHeight}, ` +
636
+ "because HEVC needs even sizes, so the clip's last column or row is cut off.";
637
+ drawDerived();
638
+ });
639
+
472
640
  for (const { btn, make } of rectButtons) {
473
641
  btn.addEventListener("click", () => {
474
642
  if (!info) return;
@@ -505,6 +673,24 @@ export function buildVideoClipPanel(
505
673
  hooks.soundClipsChanged();
506
674
  });
507
675
 
676
+ // The size fields changed, so "Make video this size" may have something to do now, or not.
677
+ const stopWatchingSize = canvasRecorder.onSizeChange(() => {
678
+ if (!panel.isConnected) {
679
+ stopWatchingSize();
680
+ return;
681
+ }
682
+ drawDerived();
683
+ });
684
+
685
+ // Permission granted, or a file picked again: a file that failed may open now.
686
+ const stopListening = onLocalFilesChange(() => {
687
+ if (!panel.isConnected) {
688
+ stopListening();
689
+ return;
690
+ }
691
+ if (state === "error") refresh();
692
+ });
693
+
508
694
  refresh();
509
695
  return {
510
696
  element: panel,
@@ -0,0 +1,129 @@
1
+ /**
2
+ * The chirp that starts a voice recording, and the code that finds it in the recording.
3
+ *
4
+ * The chirp is a sweep: a tone that slides smoothly from one pitch to another. Lining a sweep up
5
+ * with its recording only works at one exact position, so it can be found to a small fraction of a
6
+ * millisecond, even with a train going by. A plain beep's start is fuzzy.
7
+ *
8
+ * Copied from riemann-surfaces-3d's src/recording/calibration-sounds.ts (9bbe2f9), with only the
9
+ * Start sound, and quieter. See development-plans/voice-recording.md.
10
+ * @module
11
+ */
12
+
13
+ /** One sweep, from one frequency to another, in Hz. */
14
+ type Sweep = { from: number; to: number; seconds: number };
15
+
16
+ /** Riemann's Start sound: a rising sweep. */
17
+ const CHIRP: Sweep = { from: 400, to: 4000, seconds: 0.2 };
18
+
19
+ /**
20
+ * About −28 dBFS. Riemann's was 0.3, about −10 dBFS, and it reached a MacBook Air's mic at almost 5
21
+ * times full scale. It was found easily (matches 0.37–0.75), so 17 dB quieter should still work.
22
+ */
23
+ const AMPLITUDE = 0.04;
24
+ /** Fade in and out over this long, so there are no clicks. */
25
+ const FADE_SECONDS = 0.006;
26
+
27
+ /** The chirp's samples, at this sample rate. */
28
+ export function chirpSamples(sampleRate: number): Float32Array<ArrayBuffer> {
29
+ return sweepSamples(CHIRP, sampleRate);
30
+ }
31
+
32
+ /** An exponential sweep: every octave gets the same amount of time, so it sounds even. */
33
+ function sweepSamples({ from, to, seconds }: Sweep, sampleRate: number) {
34
+ const count = Math.round(seconds * sampleRate);
35
+ const samples = new Float32Array(count);
36
+ const k = Math.log(to / from);
37
+ for (let i = 0; i < count; i++) {
38
+ const t = i / sampleRate;
39
+ const phase = (2 * Math.PI * from * seconds * (Math.exp((t * k) / seconds) - 1)) / k;
40
+ samples[i] = AMPLITUDE * Math.sin(phase);
41
+ }
42
+ const fade = Math.min(count / 2, Math.round(FADE_SECONDS * sampleRate));
43
+ for (let i = 0; i < fade; i++) {
44
+ const gain = 0.5 - 0.5 * Math.cos((Math.PI * i) / fade);
45
+ samples[i] *= gain;
46
+ samples[count - 1 - i] *= gain;
47
+ }
48
+ return samples;
49
+ }
50
+
51
+ export type Found = {
52
+ /** Where the sound starts in the recording, in samples. Not a whole number: it's interpolated. */
53
+ position: number;
54
+ /** How well it matched, 0 to 1. Above about 0.1 means it really is there. Noise alone scores about 0.03. */
55
+ match: number;
56
+ /** The loudest sample in the match, as dBFS. */
57
+ peakDb: number;
58
+ };
59
+
60
+ /**
61
+ * Find a sound in a recording, starting somewhere from `from` to `to` (sample indexes). It searches
62
+ * coarsely first, at a quarter of the sample rate, then exactly around the best spot.
63
+ */
64
+ export function findSound(
65
+ recording: Float32Array,
66
+ sound: Float32Array,
67
+ from: number,
68
+ to: number,
69
+ ): Found {
70
+ const first = Math.max(0, Math.floor(from));
71
+ const last = Math.min(recording.length - sound.length, Math.ceil(to));
72
+ if (last < first) {
73
+ return { position: NaN, match: 0, peakDb: -Infinity };
74
+ }
75
+ const soundEnergy = energy(sound, 0, sound.length, 1);
76
+ const score = (lag: number, step: number) => {
77
+ let sum = 0;
78
+ for (let i = 0; i < sound.length; i += step) {
79
+ sum += sound[i] * recording[lag + i];
80
+ }
81
+ const norm = Math.sqrt(energy(recording, lag, sound.length, step) * (soundEnergy / step));
82
+ return norm > 0 ? sum / norm : 0;
83
+ };
84
+ // Coarse: every 4th lag, every 4th sample.
85
+ let best = first;
86
+ let bestScore = -Infinity;
87
+ for (let lag = first; lag <= last; lag += 4) {
88
+ const s = score(lag, 4);
89
+ if (s > bestScore) {
90
+ bestScore = s;
91
+ best = lag;
92
+ }
93
+ }
94
+ // Exact: every lag near the best one, every sample.
95
+ const exact = new Map<number, number>();
96
+ for (let lag = Math.max(first, best - 6); lag <= Math.min(last, best + 6); lag++) {
97
+ exact.set(lag, score(lag, 1));
98
+ }
99
+ for (const [lag, s] of exact) {
100
+ if (s > (exact.get(best) ?? -Infinity)) {
101
+ best = lag;
102
+ }
103
+ }
104
+ // Between samples: fit a parabola through the best score and its neighbors.
105
+ const here = exact.get(best) ?? 0;
106
+ const before = exact.get(best - 1);
107
+ const after = exact.get(best + 1);
108
+ let position = best;
109
+ if (before !== undefined && after !== undefined) {
110
+ const curve = before - 2 * here + after;
111
+ if (curve < 0) {
112
+ position += (0.5 * (before - after)) / curve;
113
+ }
114
+ }
115
+ let peak = 0;
116
+ for (let i = best; i < best + sound.length; i++) {
117
+ peak = Math.max(peak, Math.abs(recording[i]));
118
+ }
119
+ return { position, match: Math.max(0, here), peakDb: 20 * Math.log10(peak || 1e-12) };
120
+ }
121
+
122
+ function energy(samples: Float32Array, start: number, length: number, step: number) {
123
+ let sum = 0;
124
+ for (let i = 0; i < length; i += step) {
125
+ const v = samples[start + i];
126
+ sum += v * v;
127
+ }
128
+ return sum;
129
+ }