@banou/media-player 0.10.30 → 0.10.32

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 (59) hide show
  1. package/README.md +23 -0
  2. package/dist/de-DphEdq0O.js +52 -0
  3. package/dist/engine/index.js +1 -1
  4. package/dist/engine/playback.d.ts +4 -3
  5. package/dist/engine/source-buffer.d.ts +0 -1
  6. package/dist/{engine-DeEY2qao.js → engine-BZDbaSfM.js} +211 -161
  7. package/dist/es-419-D6gsvnn8.js +52 -0
  8. package/dist/fr-BqAzAJX0.js +52 -0
  9. package/dist/index.js +942 -855
  10. package/dist/it-NTcUh3Gr.js +52 -0
  11. package/dist/ja-DSmX_bxj.js +52 -0
  12. package/dist/ko-DyPzWasj.js +52 -0
  13. package/dist/labels.d.ts +71 -0
  14. package/dist/pl-C6Ow4ra5.js +52 -0
  15. package/dist/pt-BR-wudK3giz.js +52 -0
  16. package/dist/react/labels.d.ts +28 -0
  17. package/dist/react/video-player.d.ts +9 -0
  18. package/dist/ru-D5kxBkik.js +52 -0
  19. package/dist/tr-CW7R7N9k.js +52 -0
  20. package/dist/utils/track-label.d.ts +9 -8
  21. package/dist/volume-normalizer-worklet.js +40 -26
  22. package/dist/zh-Hans-IhL1Pl78.js +52 -0
  23. package/dist/zh-Hant-C5YyFjEc.js +52 -0
  24. package/package.json +4 -2
  25. package/src/lib/audio/loudness-analysis.ts +3 -3
  26. package/src/lib/audio/playback-tap.ts +3 -3
  27. package/src/lib/audio/volume-normalizer-worklet.d.ts +1 -1
  28. package/src/lib/audio/volume-normalizer-worklet.js +40 -26
  29. package/src/lib/engine/playback.ts +21 -22
  30. package/src/lib/engine/source-buffer.ts +5 -7
  31. package/src/lib/labels.ts +75 -0
  32. package/src/lib/locales/de.json +49 -0
  33. package/src/lib/locales/es-419.json +49 -0
  34. package/src/lib/locales/fr.json +49 -0
  35. package/src/lib/locales/it.json +49 -0
  36. package/src/lib/locales/ja.json +49 -0
  37. package/src/lib/locales/ko.json +49 -0
  38. package/src/lib/locales/pl.json +49 -0
  39. package/src/lib/locales/pt-BR.json +49 -0
  40. package/src/lib/locales/ru.json +49 -0
  41. package/src/lib/locales/tr.json +49 -0
  42. package/src/lib/locales/zh-Hans.json +49 -0
  43. package/src/lib/locales/zh-Hant.json +49 -0
  44. package/src/lib/react/components/burn-in-hint.tsx +4 -1
  45. package/src/lib/react/components/chrome.tsx +9 -0
  46. package/src/lib/react/components/control-bar.tsx +15 -26
  47. package/src/lib/react/components/errors.tsx +11 -9
  48. package/src/lib/react/components/overlay.tsx +18 -5
  49. package/src/lib/react/components/playback-slider.tsx +4 -1
  50. package/src/lib/react/components/settings.tsx +23 -31
  51. package/src/lib/react/components/skip-chapter.tsx +5 -2
  52. package/src/lib/react/components/sound.tsx +5 -2
  53. package/src/lib/react/components/subtitles.tsx +7 -4
  54. package/src/lib/react/components/track-menu.tsx +8 -4
  55. package/src/lib/react/components/volume-slider.tsx +3 -1
  56. package/src/lib/react/hooks/use-playback.ts +12 -2
  57. package/src/lib/react/labels.tsx +125 -0
  58. package/src/lib/react/video-player.tsx +14 -1
  59. package/src/lib/utils/track-label.ts +16 -11
@@ -440,8 +440,9 @@ export const audioSamplesOf = (fragment, track) => {
440
440
  /**
441
441
  * The analysis: 1 ms bins of whatever audio it is given, the normal level learned from them, and the cut for the
442
442
  * blocks around the playhead. `toWorklet` takes `{ block, gain, finger }` (10 s of bins from `block * 10` s, NaN where
443
- * the cut or the audio is not known) and `{ drop }` for each block past the 60 sent nearest the playhead, which is all
444
- * the worklet keeps; `toPage` takes the coverage reports the page schedules reads on.
443
+ * the cut or the audio is not known), `{ drop }` for each block past the 60 sent nearest the playhead, which is all
444
+ * the worklet keeps, and `{ end }`, the bin the file's audio ends at, once a run reaches it; `toPage` takes the
445
+ * coverage reports the page schedules reads on.
445
446
  * Separate from the worker's messages so a test can drive it with decoded audio.
446
447
  */
447
448
  export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
@@ -556,8 +557,6 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
556
557
  finger[bin - start] = Math.log(chunk.finger[bin % BLOCK_BINS] + 1e-10)
557
558
  if (bin < known) gain[bin - start] = curve[bin - from]
558
559
  }
559
- // the last cut holds past the end of the file, where playback's audio can run on by its offset
560
- if (b >= endBin && b > start && b < end) gain.fill(gain[b - 1 - start], b - start)
561
560
  }
562
561
  return { gain, finger }
563
562
  }
@@ -616,7 +615,10 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
616
615
  endRun: (finished) => {
617
616
  if (!run) return
618
617
  run.binner?.flush()
619
- if (finished && run.last >= 0) endBin = run.last + 1
618
+ if (finished && run.last >= 0) {
619
+ endBin = run.last + 1
620
+ toWorklet({ end: endBin })
621
+ }
620
622
  run = null
621
623
  schedule()
622
624
  },
@@ -644,10 +646,11 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
644
646
  *
645
647
  * { type: 'port', port } the worklet's end of a MessageChannel, for the blocks
646
648
  * { type: 'init', data, codec } the init segment and the codec string MediaSource was given
647
- * { type: 'run', run, pts, start } a run of fragments starts, `pts` added to their times as the SourceBuffer's
648
- * timestampOffset adds it for playback; `start` for the one from the start,
649
- * and `join: true` for one whose first fragments went by unseen, which starts
650
- * at its first sample
649
+ * { type: 'run', run, pts, from, start }
650
+ * a run of fragments starts at `from` s, `pts` added to their times as the
651
+ * SourceBuffer's timestampOffset adds it for playback; `start` for the one
652
+ * from the start, and `join: true` for one whose first fragments went by
653
+ * unseen, which starts at its first sample
651
654
  * { type: 'fragment', run, data } answered { type: 'ack', run, spans, end, normal } once its audio is queued
652
655
  * for decoding: the analysed seconds, the file's end once known, and the
653
656
  * level learned so far in LUFS
@@ -682,10 +685,10 @@ const serve = (scope) => {
682
685
  })
683
686
  /*
684
687
  * Each output is placed by its own chunk's timestamp, never the one AudioData carries: Chrome 155 stamps outputs by
685
- * counting samples from the first chunk, and libav's first audio sample in a run lasts until the video's first frame
686
- * is due (62 ms more than its own length in Matroska and mp4 with B-frames, 82 ms in FLAC), which MediaSource plays
687
- * as a gap; counted, what follows came out that much early, too far for the first match to find (HOR-498 build,
688
- * 2026-10-17).
688
+ * counting samples from the first chunk, so past a gap in the timestamps, which MediaSource plays as a gap, what
689
+ * follows comes out early by the gap. Before 0.9.0 libav-wasm's first audio sample of a run lasted until the video's
690
+ * first frame, 62 ms more than its own length in Matroska and mp4 with B-frames and 82 ms in FLAC, too far for the
691
+ * first match to find (HOR-498 build, 2026-10-17).
689
692
  */
690
693
  let stamps = []
691
694
  const open = (id) => {
@@ -722,7 +725,7 @@ const serve = (scope) => {
722
725
  pts = data.pts
723
726
  open(runId)
724
727
  joining = !!data.join
725
- if (!joining) analysis.startRun(runId, pts, data.start)
728
+ if (!joining) analysis.startRun(runId, data.from, data.start)
726
729
  } else if (data.type === 'fragment') {
727
730
  if (data.run !== runId || decoder.state !== 'configured') return
728
731
  const samples = audioSamplesOf(data.data, track)
@@ -781,13 +784,14 @@ const HANDOVER_SECONDS = 0.05
781
784
  const CROSSFADE_SECONDS = 0.05
782
785
  /*
783
786
  * How far either way the live audio is searched for in the analysed audio, in ms: first, and after a seek, against
784
- * where the last match put it. Played from the start, the fixtures matched 21 to 60 ms off, and FLAC past 60 ms (HOR-498
785
- * build, 2026-10-17), as long as libav's first audio sample stretches to the video's first frame, which B-frames set.
787
+ * where the last match put it. Played from the start, the fixtures matched 21 to 60 ms off, and FLAC past 60 ms, while
788
+ * libav-wasm stretched each run's first audio sample to the video's first frame (HOR-498 build, 2026-10-17); with each
789
+ * track at its own time (0.9.0), within 3.4 ms (2026-10-30).
786
790
  */
787
791
  const LOCK_RANGE_MS = 250
788
792
  const RELOCK_RANGE_MS = 30
789
793
  // how far from the last match a seek may move the audio, which the deepest cut in doubt covers, in ms: FLAC moved 82 ms
790
- // across the first remuxer seek, from the stream's start, where MediaSource plays libav's first sample short
794
+ // across the first remuxer seek while libav-wasm stretched a run's first audio sample, and at most 3 ms since 0.9.0
791
795
  const DOUBT_MS = 100
792
796
  // how far from the best offset another must come close for the match to be in question, in ms
793
797
  const CLEAR_MS = 60
@@ -805,14 +809,15 @@ const SCORES_PER_QUANTUM = 40_000
805
809
  * context.currentTime read in one task names the media time of the audio reaching a worklet that same render frame,
806
810
  * to the sample for a 48 kHz file and within about 2 ms for a 44.1 kHz one, except for the odd pair off by up to
807
811
  * 13 ms; so a jump of more than 25 ms re-anchors the timeline, and the median of the next five pairs settles it. Where
808
- * playback's audio sits against the analysed audio changes with each run of the remuxer behind it: -18 to +44 ms on
809
- * the fixtures, the same file 44 ms apart before and after a seek (HOR-498 build, 2026-10-17). So the offset is found
810
- * from the audio itself: the live fingerprint against the analysed one, every 250 ms at normal speed, to a fraction of
811
- * a millisecond. The same match catches the rare stall (once, 60 ms of silence came in mid-file and the audio after it
812
- * ran 11 ms ahead of the clock). Nothing is cut ahead until the first match, which also proves the blocks belong to
813
- * this audio; after a seek, until the next one, each quantum gets the deepest cut within the doubt, never less than
814
- * the cut asks, as it does away from normal speed (Chrome's time stretching moves the audio up to about 30 ms either
815
- * way).
812
+ * playback's audio sits against the analysed audio changed with each run of the remuxer behind it while libav-wasm
813
+ * moved each run by its own amount: -18 to +44 ms on the fixtures, the same file 44 ms apart before and after a seek
814
+ * (HOR-498 build, 2026-10-17); with each track at its own time, -3.4 to +1.2 ms (0.9.0, 2026-10-30). So the offset
815
+ * is found from the audio itself: the live fingerprint against the analysed one, every 250 ms at normal speed, to a
816
+ * fraction of a millisecond. The same match catches the rare stall (once, 60 ms of silence came in mid-file and the
817
+ * audio after it ran 11 ms ahead of the clock). Nothing is cut ahead until the first match, which also proves the
818
+ * blocks belong to this audio; after a seek, until the next one, each quantum gets the deepest cut within the doubt,
819
+ * never less than the cut asks, as it does away from normal speed (Chrome's time stretching moves the audio up to
820
+ * about 30 ms either way).
816
821
  */
817
822
  const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcessor {
818
823
  constructor({ processorOptions = {} } = {}) {
@@ -867,6 +872,8 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
867
872
  this.shortTime = 0
868
873
 
869
874
  this.blocks = new Map()
875
+ // the bin the file's audio ends at; the last cut holds past it, where playback's audio can run on
876
+ this.end = Infinity
870
877
  this.analysis = null
871
878
  this.fingerFilter = fingerprintFilter(sampleRate)
872
879
  this.fingerState = []
@@ -909,6 +916,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
909
916
  if (data?.analysis !== undefined) {
910
917
  this.analysis?.close()
911
918
  this.blocks.clear()
919
+ this.end = Infinity
912
920
  this.search = null
913
921
  this.analysis = data.analysis
914
922
  if (this.analysis) this.analysis.onmessage = ({ data: block }) => this.receive(block)
@@ -930,6 +938,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
930
938
  receive(message) {
931
939
  if (typeof message?.drop === 'number') this.blocks.delete(message.drop)
932
940
  else if (typeof message?.block === 'number') this.blocks.set(message.block, message)
941
+ else if (typeof message?.end === 'number') this.end = message.end
933
942
  this.lastBlock = undefined
934
943
  }
935
944
 
@@ -937,7 +946,11 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
937
946
  const frame = ctx * sampleRate
938
947
  const model = this.model
939
948
  if (paused) {
940
- this.model = { paused: true, media: model && !model.paused ? this.mediaAt(frame) : media }
949
+ // More than 25 ms from where the clock had it, the media is where the pair says: a seek moved it, its audio
950
+ // already playing (10 to 27 ms after the move in Chrome 155, where the main thread once took 52 ms to say so,
951
+ // 2026-10-30), or a pause was told late. The seek's reset, posted on the same event, is what puts it in doubt.
952
+ const held = model && !model.paused ? this.mediaAt(frame) : media
953
+ this.model = { paused: true, media: Math.abs(media - held) <= 0.025 ? held : media }
941
954
  this.liveCount = 0
942
955
  return
943
956
  }
@@ -973,6 +986,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
973
986
  }
974
987
 
975
988
  gainBin(i) {
989
+ if (i >= this.end) i = this.end - 1
976
990
  const block = this.blockOf(i)
977
991
  return block.gain ? block.gain[i - block.block * BLOCK_BINS] : NaN
978
992
  }
@@ -0,0 +1,52 @@
1
+ //#region src/lib/locales/zh-Hans.json
2
+ var e = "音频", t = "已复制", n = "复制", r = "关闭", i = "静音", a = "暂停", o = "播放", s = "画质", c = "重新播放", l = "设置", u = "字幕", d = "不可用", f = "已恢复", p = {
3
+ "(Default)": "(正常)",
4
+ Audio: e,
5
+ Copied: t,
6
+ Copy: n,
7
+ "Copy the errors": "复制错误信息",
8
+ "Could not switch. Try again.": "无法切换,请重试。",
9
+ Disable: r,
10
+ "Exit full screen (f)": "退出全屏 (f)",
11
+ "Exit picture in picture": "退出画中画",
12
+ "Full screen": "全屏",
13
+ "Full screen (f)": "全屏 (f)",
14
+ Mute: i,
15
+ "Mute (m)": "静音 (m)",
16
+ "Normalize volume": "音量均衡",
17
+ "Now click your browser's pop out button": "现在点击浏览器的画中画按钮",
18
+ Pause: a,
19
+ "Pause (k)": "暂停 (k)",
20
+ "Picture in picture": "画中画",
21
+ Play: o,
22
+ "Play (k)": "播放 (k)",
23
+ "Playback errors ({count})": "播放错误 ({count})",
24
+ "Playback failed": "播放失败",
25
+ "Playback speed": "播放速度",
26
+ "Put the subtitles in the picture": "将字幕嵌入画面",
27
+ "Put the subtitles in the video": "将字幕嵌入视频",
28
+ Quality: s,
29
+ "Reading the video file failed": "读取视频文件失败",
30
+ Replay: c,
31
+ "Replay (k)": "重新播放 (k)",
32
+ Settings: l,
33
+ "Skip Ending": "跳过片尾",
34
+ "Skip Opening": "跳过片头",
35
+ Subtitles: u,
36
+ "Subtitles are in the picture": "字幕已嵌入画面",
37
+ "The MediaSource closed before a SourceBuffer could be created": "MediaSource 在创建 SourceBuffer 之前已关闭",
38
+ "The MediaSource never opened": "MediaSource 始终未打开",
39
+ "The browser rejected a media segment": "浏览器拒绝了一个媒体片段",
40
+ "The video codec in this file could not be identified": "无法识别此文件的视频编解码器",
41
+ "This browser cannot play the codecs in this file: {codecs}": "此浏览器无法播放此文件的编解码器:{codecs}",
42
+ "This file has no subtitles": "此文件没有字幕",
43
+ "Track {number}": "轨道 {number}",
44
+ Unavailable: d,
45
+ "Unmute (m)": "取消静音 (m)",
46
+ "at {time}": "位于 {time}",
47
+ recovered: f,
48
+ "{rate}x": "{rate}x",
49
+ "{start} to {end}": "{start} 至 {end}"
50
+ };
51
+ //#endregion
52
+ export { e as Audio, t as Copied, n as Copy, r as Disable, i as Mute, a as Pause, o as Play, s as Quality, c as Replay, l as Settings, u as Subtitles, d as Unavailable, p as default, f as recovered };
@@ -0,0 +1,52 @@
1
+ //#region src/lib/locales/zh-Hant.json
2
+ var e = "音訊", t = "已複製", n = "複製", r = "關閉", i = "靜音", a = "暫停", o = "播放", s = "畫質", c = "重新播放", l = "設定", u = "字幕", d = "無法使用", f = "已恢復", p = {
3
+ "(Default)": "(正常)",
4
+ Audio: e,
5
+ Copied: t,
6
+ Copy: n,
7
+ "Copy the errors": "複製錯誤訊息",
8
+ "Could not switch. Try again.": "無法切換,請再試一次。",
9
+ Disable: r,
10
+ "Exit full screen (f)": "結束全螢幕 (f)",
11
+ "Exit picture in picture": "結束子母畫面",
12
+ "Full screen": "全螢幕",
13
+ "Full screen (f)": "全螢幕 (f)",
14
+ Mute: i,
15
+ "Mute (m)": "靜音 (m)",
16
+ "Normalize volume": "音量平衡",
17
+ "Now click your browser's pop out button": "現在請點選瀏覽器的子母畫面按鈕",
18
+ Pause: a,
19
+ "Pause (k)": "暫停 (k)",
20
+ "Picture in picture": "子母畫面",
21
+ Play: o,
22
+ "Play (k)": "播放 (k)",
23
+ "Playback errors ({count})": "播放錯誤 ({count})",
24
+ "Playback failed": "播放失敗",
25
+ "Playback speed": "播放速度",
26
+ "Put the subtitles in the picture": "將字幕嵌入畫面",
27
+ "Put the subtitles in the video": "將字幕嵌入影片",
28
+ Quality: s,
29
+ "Reading the video file failed": "無法讀取影片檔案",
30
+ Replay: c,
31
+ "Replay (k)": "重新播放 (k)",
32
+ Settings: l,
33
+ "Skip Ending": "略過片尾",
34
+ "Skip Opening": "略過片頭",
35
+ Subtitles: u,
36
+ "Subtitles are in the picture": "字幕已嵌入畫面",
37
+ "The MediaSource closed before a SourceBuffer could be created": "MediaSource 在建立 SourceBuffer 之前已關閉",
38
+ "The MediaSource never opened": "MediaSource 始終未開啟",
39
+ "The browser rejected a media segment": "瀏覽器拒絕了一個媒體片段",
40
+ "The video codec in this file could not be identified": "無法辨識這個檔案的影片編解碼器",
41
+ "This browser cannot play the codecs in this file: {codecs}": "這個瀏覽器無法播放這個檔案的編解碼器:{codecs}",
42
+ "This file has no subtitles": "這個檔案沒有字幕",
43
+ "Track {number}": "軌道 {number}",
44
+ Unavailable: d,
45
+ "Unmute (m)": "取消靜音 (m)",
46
+ "at {time}": "位於 {time}",
47
+ recovered: f,
48
+ "{rate}x": "{rate}x",
49
+ "{start} to {end}": "{start} 至 {end}"
50
+ };
51
+ //#endregion
52
+ export { e as Audio, t as Copied, n as Copy, r as Disable, i as Mute, a as Pause, o as Play, s as Quality, c as Replay, l as Settings, u as Subtitles, d as Unavailable, p as default, f as recovered };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@banou/media-player",
3
- "version": "0.10.30",
3
+ "version": "0.10.32",
4
4
  "description": "A video player for containers and codecs the browser cannot play natively, remuxed on the fly",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -49,11 +49,12 @@
49
49
  "author": "Banou26",
50
50
  "dependencies": {
51
51
  "@banou/ponyfill": "^0.0.12",
52
+ "@formatjs/intl": "^6.1.4",
52
53
  "@videojs/core": "10.0.0-beta.26",
53
54
  "@videojs/react": "10.0.0-beta.26",
54
55
  "ass-compiler": "^0.1.16",
55
56
  "jassub": "^2.5.23",
56
- "libav-wasm": "^0.8.12",
57
+ "libav-wasm": "^0.8.14",
57
58
  "lucide-react": "^0.536.0",
58
59
  "osra": "^0.6.12",
59
60
  "react-tooltip": "^5.28.0"
@@ -64,6 +65,7 @@
64
65
  },
65
66
  "devDependencies": {
66
67
  "@emotion/react": "^11.14.0",
68
+ "@formatjs/cli": "^6.16.35",
67
69
  "@types/node": "^22.20.1",
68
70
  "@types/react": "^19.0.0",
69
71
  "@types/react-dom": "^19.0.0",
@@ -202,7 +202,7 @@ export const startLoudnessAnalysis = ({ node, element, scriptUrl, source }: {
202
202
  worker.postMessage({ type: 'init', data: init.data, codec: init.info.output.audioMimeType }, [init.data])
203
203
  active = true
204
204
  started = true
205
- worker.postMessage({ type: 'run', run, pts: 0, start: true })
205
+ worker.postMessage({ type: 'run', run, pts: init.timestampOffset, from: 0, start: true })
206
206
  while (!stopped) {
207
207
  const next = decide()
208
208
  if (next === 'done') break
@@ -229,7 +229,7 @@ export const startLoudnessAnalysis = ({ node, element, scriptUrl, source }: {
229
229
  run++
230
230
  active = true
231
231
  runStart = result.pts
232
- worker.postMessage({ type: 'run', run, pts: result.pts, start: result.pts <= 0 })
232
+ worker.postMessage({ type: 'run', run, pts: init.timestampOffset, from: result.pts, start: result.pts <= 0 })
233
233
  await send(result)
234
234
  if (result.finished) await endRun(true)
235
235
  }
@@ -260,7 +260,7 @@ export const startLoudnessAnalysis = ({ node, element, scriptUrl, source }: {
260
260
  } else if (output.type === 'run') {
261
261
  end(false)
262
262
  open = true
263
- worker.postMessage({ type: 'run', run: ++run, pts: output.pts, start: !join && output.pts <= 0, join })
263
+ worker.postMessage({ type: 'run', run: ++run, pts: output.pts, from: output.from, start: !join && output.from <= 0, join })
264
264
  } else if (output.type === 'end') end(true)
265
265
  else if (open) {
266
266
  const data = output.data.slice(0)
@@ -12,7 +12,7 @@ export type PlaybackTap = {
12
12
 
13
13
  export const createPlaybackTap = (): PlaybackTap => {
14
14
  let init: Extract<PlaybackOutput, { type: 'init' }> | undefined
15
- let run: { pts: number, begun: boolean } | undefined
15
+ let run: { output: Extract<PlaybackOutput, { type: 'run' }>, begun: boolean } | undefined
16
16
  let listener: { audioStreamIndex: number, take: (output: PlaybackOutput, join?: boolean) => void } | undefined
17
17
  const live = () => listener && init?.audioStreamIndex === listener.audioStreamIndex ? listener : undefined
18
18
  return {
@@ -20,7 +20,7 @@ export const createPlaybackTap = (): PlaybackTap => {
20
20
  if (output.type === 'init') {
21
21
  init = output
22
22
  run = undefined
23
- } else if (output.type === 'run') run = { pts: output.pts, begun: false }
23
+ } else if (output.type === 'run') run = { output, begun: false }
24
24
  else if (output.type === 'fragment' && run) run.begun = true
25
25
  live()?.take(output)
26
26
  },
@@ -29,7 +29,7 @@ export const createPlaybackTap = (): PlaybackTap => {
29
29
  listener = mine
30
30
  if (live() && init) {
31
31
  take(init)
32
- if (run) take({ type: 'run', pts: run.pts }, run.begun)
32
+ if (run) take(run.output, run.begun)
33
33
  }
34
34
  return () => { if (listener === mine) listener = undefined }
35
35
  },
@@ -32,7 +32,7 @@ export type AudioTrack = {
32
32
  export const audioTrackOf: (init: ArrayBuffer) => AudioTrack | undefined
33
33
  export const audioSamplesOf: (fragment: ArrayBuffer, track: AudioTrack) => { time: number, duration: number, data: Uint8Array }[]
34
34
 
35
- export type BlockMessage = { block: number, gain: Float32Array, finger: Float32Array } | { drop: number }
35
+ export type BlockMessage = { block: number, gain: Float32Array, finger: Float32Array } | { drop: number } | { end: number }
36
36
  export type Analysis = {
37
37
  startRun: (id: number, time: number, start?: boolean) => void
38
38
  push: (planes: Float32Array[], frames: number, rate: number, time: number) => void
@@ -440,8 +440,9 @@ export const audioSamplesOf = (fragment, track) => {
440
440
  /**
441
441
  * The analysis: 1 ms bins of whatever audio it is given, the normal level learned from them, and the cut for the
442
442
  * blocks around the playhead. `toWorklet` takes `{ block, gain, finger }` (10 s of bins from `block * 10` s, NaN where
443
- * the cut or the audio is not known) and `{ drop }` for each block past the 60 sent nearest the playhead, which is all
444
- * the worklet keeps; `toPage` takes the coverage reports the page schedules reads on.
443
+ * the cut or the audio is not known), `{ drop }` for each block past the 60 sent nearest the playhead, which is all
444
+ * the worklet keeps, and `{ end }`, the bin the file's audio ends at, once a run reaches it; `toPage` takes the
445
+ * coverage reports the page schedules reads on.
445
446
  * Separate from the worker's messages so a test can drive it with decoded audio.
446
447
  */
447
448
  export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
@@ -556,8 +557,6 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
556
557
  finger[bin - start] = Math.log(chunk.finger[bin % BLOCK_BINS] + 1e-10)
557
558
  if (bin < known) gain[bin - start] = curve[bin - from]
558
559
  }
559
- // the last cut holds past the end of the file, where playback's audio can run on by its offset
560
- if (b >= endBin && b > start && b < end) gain.fill(gain[b - 1 - start], b - start)
561
560
  }
562
561
  return { gain, finger }
563
562
  }
@@ -616,7 +615,10 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
616
615
  endRun: (finished) => {
617
616
  if (!run) return
618
617
  run.binner?.flush()
619
- if (finished && run.last >= 0) endBin = run.last + 1
618
+ if (finished && run.last >= 0) {
619
+ endBin = run.last + 1
620
+ toWorklet({ end: endBin })
621
+ }
620
622
  run = null
621
623
  schedule()
622
624
  },
@@ -644,10 +646,11 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
644
646
  *
645
647
  * { type: 'port', port } the worklet's end of a MessageChannel, for the blocks
646
648
  * { type: 'init', data, codec } the init segment and the codec string MediaSource was given
647
- * { type: 'run', run, pts, start } a run of fragments starts, `pts` added to their times as the SourceBuffer's
648
- * timestampOffset adds it for playback; `start` for the one from the start,
649
- * and `join: true` for one whose first fragments went by unseen, which starts
650
- * at its first sample
649
+ * { type: 'run', run, pts, from, start }
650
+ * a run of fragments starts at `from` s, `pts` added to their times as the
651
+ * SourceBuffer's timestampOffset adds it for playback; `start` for the one
652
+ * from the start, and `join: true` for one whose first fragments went by
653
+ * unseen, which starts at its first sample
651
654
  * { type: 'fragment', run, data } answered { type: 'ack', run, spans, end, normal } once its audio is queued
652
655
  * for decoding: the analysed seconds, the file's end once known, and the
653
656
  * level learned so far in LUFS
@@ -682,10 +685,10 @@ const serve = (scope) => {
682
685
  })
683
686
  /*
684
687
  * Each output is placed by its own chunk's timestamp, never the one AudioData carries: Chrome 155 stamps outputs by
685
- * counting samples from the first chunk, and libav's first audio sample in a run lasts until the video's first frame
686
- * is due (62 ms more than its own length in Matroska and mp4 with B-frames, 82 ms in FLAC), which MediaSource plays
687
- * as a gap; counted, what follows came out that much early, too far for the first match to find (HOR-498 build,
688
- * 2026-10-17).
688
+ * counting samples from the first chunk, so past a gap in the timestamps, which MediaSource plays as a gap, what
689
+ * follows comes out early by the gap. Before 0.9.0 libav-wasm's first audio sample of a run lasted until the video's
690
+ * first frame, 62 ms more than its own length in Matroska and mp4 with B-frames and 82 ms in FLAC, too far for the
691
+ * first match to find (HOR-498 build, 2026-10-17).
689
692
  */
690
693
  let stamps = []
691
694
  const open = (id) => {
@@ -722,7 +725,7 @@ const serve = (scope) => {
722
725
  pts = data.pts
723
726
  open(runId)
724
727
  joining = !!data.join
725
- if (!joining) analysis.startRun(runId, pts, data.start)
728
+ if (!joining) analysis.startRun(runId, data.from, data.start)
726
729
  } else if (data.type === 'fragment') {
727
730
  if (data.run !== runId || decoder.state !== 'configured') return
728
731
  const samples = audioSamplesOf(data.data, track)
@@ -781,13 +784,14 @@ const HANDOVER_SECONDS = 0.05
781
784
  const CROSSFADE_SECONDS = 0.05
782
785
  /*
783
786
  * How far either way the live audio is searched for in the analysed audio, in ms: first, and after a seek, against
784
- * where the last match put it. Played from the start, the fixtures matched 21 to 60 ms off, and FLAC past 60 ms (HOR-498
785
- * build, 2026-10-17), as long as libav's first audio sample stretches to the video's first frame, which B-frames set.
787
+ * where the last match put it. Played from the start, the fixtures matched 21 to 60 ms off, and FLAC past 60 ms, while
788
+ * libav-wasm stretched each run's first audio sample to the video's first frame (HOR-498 build, 2026-10-17); with each
789
+ * track at its own time (0.9.0), within 3.4 ms (2026-10-30).
786
790
  */
787
791
  const LOCK_RANGE_MS = 250
788
792
  const RELOCK_RANGE_MS = 30
789
793
  // how far from the last match a seek may move the audio, which the deepest cut in doubt covers, in ms: FLAC moved 82 ms
790
- // across the first remuxer seek, from the stream's start, where MediaSource plays libav's first sample short
794
+ // across the first remuxer seek while libav-wasm stretched a run's first audio sample, and at most 3 ms since 0.9.0
791
795
  const DOUBT_MS = 100
792
796
  // how far from the best offset another must come close for the match to be in question, in ms
793
797
  const CLEAR_MS = 60
@@ -805,14 +809,15 @@ const SCORES_PER_QUANTUM = 40_000
805
809
  * context.currentTime read in one task names the media time of the audio reaching a worklet that same render frame,
806
810
  * to the sample for a 48 kHz file and within about 2 ms for a 44.1 kHz one, except for the odd pair off by up to
807
811
  * 13 ms; so a jump of more than 25 ms re-anchors the timeline, and the median of the next five pairs settles it. Where
808
- * playback's audio sits against the analysed audio changes with each run of the remuxer behind it: -18 to +44 ms on
809
- * the fixtures, the same file 44 ms apart before and after a seek (HOR-498 build, 2026-10-17). So the offset is found
810
- * from the audio itself: the live fingerprint against the analysed one, every 250 ms at normal speed, to a fraction of
811
- * a millisecond. The same match catches the rare stall (once, 60 ms of silence came in mid-file and the audio after it
812
- * ran 11 ms ahead of the clock). Nothing is cut ahead until the first match, which also proves the blocks belong to
813
- * this audio; after a seek, until the next one, each quantum gets the deepest cut within the doubt, never less than
814
- * the cut asks, as it does away from normal speed (Chrome's time stretching moves the audio up to about 30 ms either
815
- * way).
812
+ * playback's audio sits against the analysed audio changed with each run of the remuxer behind it while libav-wasm
813
+ * moved each run by its own amount: -18 to +44 ms on the fixtures, the same file 44 ms apart before and after a seek
814
+ * (HOR-498 build, 2026-10-17); with each track at its own time, -3.4 to +1.2 ms (0.9.0, 2026-10-30). So the offset
815
+ * is found from the audio itself: the live fingerprint against the analysed one, every 250 ms at normal speed, to a
816
+ * fraction of a millisecond. The same match catches the rare stall (once, 60 ms of silence came in mid-file and the
817
+ * audio after it ran 11 ms ahead of the clock). Nothing is cut ahead until the first match, which also proves the
818
+ * blocks belong to this audio; after a seek, until the next one, each quantum gets the deepest cut within the doubt,
819
+ * never less than the cut asks, as it does away from normal speed (Chrome's time stretching moves the audio up to
820
+ * about 30 ms either way).
816
821
  */
817
822
  const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcessor {
818
823
  constructor({ processorOptions = {} } = {}) {
@@ -867,6 +872,8 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
867
872
  this.shortTime = 0
868
873
 
869
874
  this.blocks = new Map()
875
+ // the bin the file's audio ends at; the last cut holds past it, where playback's audio can run on
876
+ this.end = Infinity
870
877
  this.analysis = null
871
878
  this.fingerFilter = fingerprintFilter(sampleRate)
872
879
  this.fingerState = []
@@ -909,6 +916,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
909
916
  if (data?.analysis !== undefined) {
910
917
  this.analysis?.close()
911
918
  this.blocks.clear()
919
+ this.end = Infinity
912
920
  this.search = null
913
921
  this.analysis = data.analysis
914
922
  if (this.analysis) this.analysis.onmessage = ({ data: block }) => this.receive(block)
@@ -930,6 +938,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
930
938
  receive(message) {
931
939
  if (typeof message?.drop === 'number') this.blocks.delete(message.drop)
932
940
  else if (typeof message?.block === 'number') this.blocks.set(message.block, message)
941
+ else if (typeof message?.end === 'number') this.end = message.end
933
942
  this.lastBlock = undefined
934
943
  }
935
944
 
@@ -937,7 +946,11 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
937
946
  const frame = ctx * sampleRate
938
947
  const model = this.model
939
948
  if (paused) {
940
- this.model = { paused: true, media: model && !model.paused ? this.mediaAt(frame) : media }
949
+ // More than 25 ms from where the clock had it, the media is where the pair says: a seek moved it, its audio
950
+ // already playing (10 to 27 ms after the move in Chrome 155, where the main thread once took 52 ms to say so,
951
+ // 2026-10-30), or a pause was told late. The seek's reset, posted on the same event, is what puts it in doubt.
952
+ const held = model && !model.paused ? this.mediaAt(frame) : media
953
+ this.model = { paused: true, media: Math.abs(media - held) <= 0.025 ? held : media }
941
954
  this.liveCount = 0
942
955
  return
943
956
  }
@@ -973,6 +986,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
973
986
  }
974
987
 
975
988
  gainBin(i) {
989
+ if (i >= this.end) i = this.end - 1
976
990
  const block = this.blockOf(i)
977
991
  return block.gain ? block.gain[i - block.block * BLOCK_BINS] : NaN
978
992
  }