@kenzuya/mediabunny 1.26.0 → 1.28.5

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 (237) hide show
  1. package/README.md +1 -1
  2. package/dist/bundles/{mediabunny.mjs → mediabunny.js} +21963 -21388
  3. package/dist/bundles/mediabunny.min.js +490 -0
  4. package/dist/modules/shared/mp3-misc.d.ts.map +1 -1
  5. package/dist/modules/src/adts/adts-demuxer.d.ts +6 -6
  6. package/dist/modules/src/adts/adts-demuxer.d.ts.map +1 -1
  7. package/dist/modules/src/adts/adts-muxer.d.ts +4 -4
  8. package/dist/modules/src/adts/adts-muxer.d.ts.map +1 -1
  9. package/dist/modules/src/adts/adts-reader.d.ts +1 -1
  10. package/dist/modules/src/adts/adts-reader.d.ts.map +1 -1
  11. package/dist/modules/src/avi/avi-demuxer.d.ts +44 -0
  12. package/dist/modules/src/avi/avi-demuxer.d.ts.map +1 -0
  13. package/dist/modules/src/avi/avi-misc.d.ts +88 -0
  14. package/dist/modules/src/avi/avi-misc.d.ts.map +1 -0
  15. package/dist/modules/src/avi/avi-muxer.d.ts +45 -0
  16. package/dist/modules/src/avi/avi-muxer.d.ts.map +1 -0
  17. package/dist/modules/src/avi/riff-writer.d.ts +26 -0
  18. package/dist/modules/src/avi/riff-writer.d.ts.map +1 -0
  19. package/dist/modules/src/codec-data.d.ts +8 -3
  20. package/dist/modules/src/codec-data.d.ts.map +1 -1
  21. package/dist/modules/src/codec.d.ts +10 -10
  22. package/dist/modules/src/codec.d.ts.map +1 -1
  23. package/dist/modules/src/conversion.d.ts +33 -16
  24. package/dist/modules/src/conversion.d.ts.map +1 -1
  25. package/dist/modules/src/custom-coder.d.ts +8 -8
  26. package/dist/modules/src/custom-coder.d.ts.map +1 -1
  27. package/dist/modules/src/demuxer.d.ts +3 -3
  28. package/dist/modules/src/demuxer.d.ts.map +1 -1
  29. package/dist/modules/src/encode.d.ts +8 -8
  30. package/dist/modules/src/encode.d.ts.map +1 -1
  31. package/dist/modules/src/flac/flac-demuxer.d.ts +7 -7
  32. package/dist/modules/src/flac/flac-demuxer.d.ts.map +1 -1
  33. package/dist/modules/src/flac/flac-misc.d.ts +3 -3
  34. package/dist/modules/src/flac/flac-misc.d.ts.map +1 -1
  35. package/dist/modules/src/flac/flac-muxer.d.ts +5 -5
  36. package/dist/modules/src/flac/flac-muxer.d.ts.map +1 -1
  37. package/dist/modules/src/id3.d.ts +3 -3
  38. package/dist/modules/src/id3.d.ts.map +1 -1
  39. package/dist/modules/src/index.d.ts +20 -20
  40. package/dist/modules/src/index.d.ts.map +1 -1
  41. package/dist/modules/src/input-format.d.ts +22 -0
  42. package/dist/modules/src/input-format.d.ts.map +1 -1
  43. package/dist/modules/src/input-track.d.ts +8 -8
  44. package/dist/modules/src/input-track.d.ts.map +1 -1
  45. package/dist/modules/src/input.d.ts +12 -12
  46. package/dist/modules/src/isobmff/isobmff-boxes.d.ts +2 -2
  47. package/dist/modules/src/isobmff/isobmff-boxes.d.ts.map +1 -1
  48. package/dist/modules/src/isobmff/isobmff-demuxer.d.ts +12 -12
  49. package/dist/modules/src/isobmff/isobmff-demuxer.d.ts.map +1 -1
  50. package/dist/modules/src/isobmff/isobmff-misc.d.ts.map +1 -1
  51. package/dist/modules/src/isobmff/isobmff-muxer.d.ts +11 -11
  52. package/dist/modules/src/isobmff/isobmff-muxer.d.ts.map +1 -1
  53. package/dist/modules/src/isobmff/isobmff-reader.d.ts +2 -2
  54. package/dist/modules/src/isobmff/isobmff-reader.d.ts.map +1 -1
  55. package/dist/modules/src/matroska/ebml.d.ts +3 -3
  56. package/dist/modules/src/matroska/ebml.d.ts.map +1 -1
  57. package/dist/modules/src/matroska/matroska-demuxer.d.ts +13 -13
  58. package/dist/modules/src/matroska/matroska-demuxer.d.ts.map +1 -1
  59. package/dist/modules/src/matroska/matroska-input.d.ts +33 -0
  60. package/dist/modules/src/matroska/matroska-input.d.ts.map +1 -0
  61. package/dist/modules/src/matroska/matroska-misc.d.ts.map +1 -1
  62. package/dist/modules/src/matroska/matroska-muxer.d.ts +5 -5
  63. package/dist/modules/src/matroska/matroska-muxer.d.ts.map +1 -1
  64. package/dist/modules/src/media-sink.d.ts +5 -5
  65. package/dist/modules/src/media-sink.d.ts.map +1 -1
  66. package/dist/modules/src/media-source.d.ts +22 -4
  67. package/dist/modules/src/media-source.d.ts.map +1 -1
  68. package/dist/modules/src/metadata.d.ts +2 -2
  69. package/dist/modules/src/metadata.d.ts.map +1 -1
  70. package/dist/modules/src/misc.d.ts +5 -4
  71. package/dist/modules/src/misc.d.ts.map +1 -1
  72. package/dist/modules/src/mp3/mp3-demuxer.d.ts +7 -7
  73. package/dist/modules/src/mp3/mp3-demuxer.d.ts.map +1 -1
  74. package/dist/modules/src/mp3/mp3-muxer.d.ts +4 -4
  75. package/dist/modules/src/mp3/mp3-muxer.d.ts.map +1 -1
  76. package/dist/modules/src/mp3/mp3-reader.d.ts +2 -2
  77. package/dist/modules/src/mp3/mp3-reader.d.ts.map +1 -1
  78. package/dist/modules/src/mp3/mp3-writer.d.ts +1 -1
  79. package/dist/modules/src/mp3/mp3-writer.d.ts.map +1 -1
  80. package/dist/modules/src/muxer.d.ts +4 -4
  81. package/dist/modules/src/muxer.d.ts.map +1 -1
  82. package/dist/modules/src/node.d.ts +1 -1
  83. package/dist/modules/src/ogg/ogg-demuxer.d.ts +7 -7
  84. package/dist/modules/src/ogg/ogg-demuxer.d.ts.map +1 -1
  85. package/dist/modules/src/ogg/ogg-misc.d.ts +1 -1
  86. package/dist/modules/src/ogg/ogg-misc.d.ts.map +1 -1
  87. package/dist/modules/src/ogg/ogg-muxer.d.ts +5 -5
  88. package/dist/modules/src/ogg/ogg-muxer.d.ts.map +1 -1
  89. package/dist/modules/src/ogg/ogg-reader.d.ts +1 -1
  90. package/dist/modules/src/ogg/ogg-reader.d.ts.map +1 -1
  91. package/dist/modules/src/output-format.d.ts +51 -6
  92. package/dist/modules/src/output-format.d.ts.map +1 -1
  93. package/dist/modules/src/output.d.ts +13 -13
  94. package/dist/modules/src/output.d.ts.map +1 -1
  95. package/dist/modules/src/packet.d.ts +1 -1
  96. package/dist/modules/src/packet.d.ts.map +1 -1
  97. package/dist/modules/src/pcm.d.ts.map +1 -1
  98. package/dist/modules/src/reader.d.ts +2 -2
  99. package/dist/modules/src/reader.d.ts.map +1 -1
  100. package/dist/modules/src/sample.d.ts +57 -15
  101. package/dist/modules/src/sample.d.ts.map +1 -1
  102. package/dist/modules/src/source.d.ts +3 -3
  103. package/dist/modules/src/source.d.ts.map +1 -1
  104. package/dist/modules/src/subtitles.d.ts +1 -1
  105. package/dist/modules/src/subtitles.d.ts.map +1 -1
  106. package/dist/modules/src/target.d.ts +2 -2
  107. package/dist/modules/src/target.d.ts.map +1 -1
  108. package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
  109. package/dist/modules/src/wave/riff-writer.d.ts +1 -1
  110. package/dist/modules/src/wave/riff-writer.d.ts.map +1 -1
  111. package/dist/modules/src/wave/wave-demuxer.d.ts +6 -6
  112. package/dist/modules/src/wave/wave-demuxer.d.ts.map +1 -1
  113. package/dist/modules/src/wave/wave-muxer.d.ts +4 -4
  114. package/dist/modules/src/wave/wave-muxer.d.ts.map +1 -1
  115. package/dist/modules/src/writer.d.ts +1 -1
  116. package/dist/modules/src/writer.d.ts.map +1 -1
  117. package/dist/packages/eac3/eac3.wasm +0 -0
  118. package/dist/packages/eac3/mediabunny-eac3.js +1058 -0
  119. package/dist/packages/eac3/mediabunny-eac3.min.js +44 -0
  120. package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.js +694 -0
  121. package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.min.js +58 -0
  122. package/dist/packages/mpeg4/mediabunny-mpeg4.js +1198 -0
  123. package/dist/packages/mpeg4/mediabunny-mpeg4.min.js +44 -0
  124. package/dist/packages/mpeg4/xvid.wasm +0 -0
  125. package/package.json +18 -57
  126. package/dist/bundles/mediabunny.cjs +0 -26140
  127. package/dist/bundles/mediabunny.min.cjs +0 -147
  128. package/dist/bundles/mediabunny.min.mjs +0 -146
  129. package/dist/mediabunny.d.ts +0 -3319
  130. package/dist/modules/shared/mp3-misc.js +0 -147
  131. package/dist/modules/src/adts/adts-demuxer.js +0 -239
  132. package/dist/modules/src/adts/adts-muxer.js +0 -80
  133. package/dist/modules/src/adts/adts-reader.js +0 -63
  134. package/dist/modules/src/codec-data.js +0 -1730
  135. package/dist/modules/src/codec.js +0 -869
  136. package/dist/modules/src/conversion.js +0 -1459
  137. package/dist/modules/src/custom-coder.js +0 -117
  138. package/dist/modules/src/demuxer.js +0 -12
  139. package/dist/modules/src/encode.js +0 -442
  140. package/dist/modules/src/flac/flac-demuxer.js +0 -504
  141. package/dist/modules/src/flac/flac-misc.js +0 -135
  142. package/dist/modules/src/flac/flac-muxer.js +0 -222
  143. package/dist/modules/src/id3.js +0 -848
  144. package/dist/modules/src/index.js +0 -28
  145. package/dist/modules/src/input-format.js +0 -480
  146. package/dist/modules/src/input-track.js +0 -372
  147. package/dist/modules/src/input.js +0 -188
  148. package/dist/modules/src/isobmff/isobmff-boxes.js +0 -1480
  149. package/dist/modules/src/isobmff/isobmff-demuxer.js +0 -2618
  150. package/dist/modules/src/isobmff/isobmff-misc.js +0 -20
  151. package/dist/modules/src/isobmff/isobmff-muxer.js +0 -966
  152. package/dist/modules/src/isobmff/isobmff-reader.js +0 -72
  153. package/dist/modules/src/matroska/ebml.js +0 -653
  154. package/dist/modules/src/matroska/matroska-demuxer.js +0 -2133
  155. package/dist/modules/src/matroska/matroska-misc.js +0 -20
  156. package/dist/modules/src/matroska/matroska-muxer.js +0 -1017
  157. package/dist/modules/src/media-sink.js +0 -1736
  158. package/dist/modules/src/media-source.js +0 -1825
  159. package/dist/modules/src/metadata.js +0 -193
  160. package/dist/modules/src/misc.js +0 -623
  161. package/dist/modules/src/mp3/mp3-demuxer.js +0 -285
  162. package/dist/modules/src/mp3/mp3-muxer.js +0 -123
  163. package/dist/modules/src/mp3/mp3-reader.js +0 -26
  164. package/dist/modules/src/mp3/mp3-writer.js +0 -78
  165. package/dist/modules/src/muxer.js +0 -50
  166. package/dist/modules/src/node.js +0 -9
  167. package/dist/modules/src/ogg/ogg-demuxer.js +0 -763
  168. package/dist/modules/src/ogg/ogg-misc.js +0 -78
  169. package/dist/modules/src/ogg/ogg-muxer.js +0 -353
  170. package/dist/modules/src/ogg/ogg-reader.js +0 -65
  171. package/dist/modules/src/output-format.js +0 -527
  172. package/dist/modules/src/output.js +0 -300
  173. package/dist/modules/src/packet.js +0 -182
  174. package/dist/modules/src/pcm.js +0 -85
  175. package/dist/modules/src/reader.js +0 -236
  176. package/dist/modules/src/sample.js +0 -1056
  177. package/dist/modules/src/source.js +0 -1182
  178. package/dist/modules/src/subtitles.js +0 -575
  179. package/dist/modules/src/target.js +0 -140
  180. package/dist/modules/src/wave/riff-writer.js +0 -30
  181. package/dist/modules/src/wave/wave-demuxer.js +0 -447
  182. package/dist/modules/src/wave/wave-muxer.js +0 -318
  183. package/dist/modules/src/writer.js +0 -370
  184. package/src/adts/adts-demuxer.ts +0 -331
  185. package/src/adts/adts-muxer.ts +0 -111
  186. package/src/adts/adts-reader.ts +0 -85
  187. package/src/codec-data.ts +0 -2078
  188. package/src/codec.ts +0 -1092
  189. package/src/conversion.ts +0 -2112
  190. package/src/custom-coder.ts +0 -197
  191. package/src/demuxer.ts +0 -24
  192. package/src/encode.ts +0 -739
  193. package/src/flac/flac-demuxer.ts +0 -730
  194. package/src/flac/flac-misc.ts +0 -164
  195. package/src/flac/flac-muxer.ts +0 -320
  196. package/src/id3.ts +0 -925
  197. package/src/index.ts +0 -221
  198. package/src/input-format.ts +0 -541
  199. package/src/input-track.ts +0 -529
  200. package/src/input.ts +0 -235
  201. package/src/isobmff/isobmff-boxes.ts +0 -1719
  202. package/src/isobmff/isobmff-demuxer.ts +0 -3190
  203. package/src/isobmff/isobmff-misc.ts +0 -29
  204. package/src/isobmff/isobmff-muxer.ts +0 -1348
  205. package/src/isobmff/isobmff-reader.ts +0 -91
  206. package/src/matroska/ebml.ts +0 -730
  207. package/src/matroska/matroska-demuxer.ts +0 -2481
  208. package/src/matroska/matroska-misc.ts +0 -29
  209. package/src/matroska/matroska-muxer.ts +0 -1276
  210. package/src/media-sink.ts +0 -2179
  211. package/src/media-source.ts +0 -2243
  212. package/src/metadata.ts +0 -320
  213. package/src/misc.ts +0 -798
  214. package/src/mp3/mp3-demuxer.ts +0 -383
  215. package/src/mp3/mp3-muxer.ts +0 -166
  216. package/src/mp3/mp3-reader.ts +0 -34
  217. package/src/mp3/mp3-writer.ts +0 -120
  218. package/src/muxer.ts +0 -88
  219. package/src/node.ts +0 -11
  220. package/src/ogg/ogg-demuxer.ts +0 -1053
  221. package/src/ogg/ogg-misc.ts +0 -116
  222. package/src/ogg/ogg-muxer.ts +0 -497
  223. package/src/ogg/ogg-reader.ts +0 -93
  224. package/src/output-format.ts +0 -945
  225. package/src/output.ts +0 -488
  226. package/src/packet.ts +0 -263
  227. package/src/pcm.ts +0 -112
  228. package/src/reader.ts +0 -323
  229. package/src/sample.ts +0 -1461
  230. package/src/source.ts +0 -1688
  231. package/src/subtitles.ts +0 -711
  232. package/src/target.ts +0 -204
  233. package/src/tsconfig.json +0 -16
  234. package/src/wave/riff-writer.ts +0 -36
  235. package/src/wave/wave-demuxer.ts +0 -529
  236. package/src/wave/wave-muxer.ts +0 -371
  237. package/src/writer.ts +0 -490
package/src/packet.ts DELETED
@@ -1,263 +0,0 @@
1
- /*!
2
- * Copyright (c) 2025-present, Vanilagy and contributors
3
- *
4
- * This Source Code Form is subject to the terms of the Mozilla Public
5
- * License, v. 2.0. If a copy of the MPL was not distributed with this
6
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
- */
8
-
9
- import { SECOND_TO_MICROSECOND_FACTOR } from './misc';
10
-
11
- export const PLACEHOLDER_DATA = /* #__PURE__ */ new Uint8Array(0);
12
-
13
- /**
14
- * The type of a packet. Key packets can be decoded without previous packets, while delta packets depend on previous
15
- * packets.
16
- * @group Packets
17
- * @public
18
- */
19
- export type PacketType = 'key' | 'delta';
20
-
21
- /**
22
- * Holds additional data accompanying an {@link EncodedPacket}.
23
- * @group Packets
24
- * @public
25
- */
26
- export type EncodedPacketSideData = {
27
- /**
28
- * An encoded alpha frame, encoded with the same codec as the packet. Typically used for transparent videos, where
29
- * the alpha information is stored separately from the color information.
30
- */
31
- alpha?: Uint8Array;
32
- /**
33
- * The actual byte length of the alpha data. This field is useful for metadata-only packets where the
34
- * `alpha` field contains no bytes.
35
- */
36
- alphaByteLength?: number;
37
- };
38
-
39
- /**
40
- * Represents an encoded chunk of media. Mainly used as an expressive wrapper around WebCodecs API's
41
- * [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) and
42
- * [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk), but can also be used
43
- * standalone.
44
- * @group Packets
45
- * @public
46
- */
47
- export class EncodedPacket {
48
- /**
49
- * The actual byte length of the data in this packet. This field is useful for metadata-only packets where the
50
- * `data` field contains no bytes.
51
- */
52
- readonly byteLength: number;
53
-
54
- /** Additional data carried with this packet. */
55
- readonly sideData: EncodedPacketSideData;
56
-
57
- /** Creates a new {@link EncodedPacket} from raw bytes and timing information. */
58
- constructor(
59
- /** The encoded data of this packet. */
60
- public readonly data: Uint8Array,
61
- /** The type of this packet. */
62
- public readonly type: PacketType,
63
- /**
64
- * The presentation timestamp of this packet in seconds. May be negative. Samples with negative end timestamps
65
- * should not be presented.
66
- */
67
- public readonly timestamp: number,
68
- /** The duration of this packet in seconds. */
69
- public readonly duration: number,
70
- /**
71
- * The sequence number indicates the decode order of the packets. Packet A must be decoded before packet B if A
72
- * has a lower sequence number than B. If two packets have the same sequence number, they are the same packet.
73
- * Otherwise, sequence numbers are arbitrary and are not guaranteed to have any meaning besides their relative
74
- * ordering. Negative sequence numbers mean the sequence number is undefined.
75
- */
76
- public readonly sequenceNumber = -1,
77
- byteLength?: number,
78
- sideData?: EncodedPacketSideData,
79
- ) {
80
- if (data === PLACEHOLDER_DATA && byteLength === undefined) {
81
- throw new Error(
82
- 'Internal error: byteLength must be explicitly provided when constructing metadata-only packets.',
83
- );
84
- }
85
-
86
- if (byteLength === undefined) {
87
- byteLength = data.byteLength;
88
- }
89
-
90
- if (!(data instanceof Uint8Array)) {
91
- throw new TypeError('data must be a Uint8Array.');
92
- }
93
- if (type !== 'key' && type !== 'delta') {
94
- throw new TypeError('type must be either "key" or "delta".');
95
- }
96
- if (!Number.isFinite(timestamp)) {
97
- throw new TypeError('timestamp must be a number.');
98
- }
99
- if (!Number.isFinite(duration) || duration < 0) {
100
- throw new TypeError('duration must be a non-negative number.');
101
- }
102
- if (!Number.isFinite(sequenceNumber)) {
103
- throw new TypeError('sequenceNumber must be a number.');
104
- }
105
- if (!Number.isInteger(byteLength) || byteLength < 0) {
106
- throw new TypeError('byteLength must be a non-negative integer.');
107
- }
108
- if (sideData !== undefined && (typeof sideData !== 'object' || !sideData)) {
109
- throw new TypeError('sideData, when provided, must be an object.');
110
- }
111
- if (sideData?.alpha !== undefined && !(sideData.alpha instanceof Uint8Array)) {
112
- throw new TypeError('sideData.alpha, when provided, must be a Uint8Array.');
113
- }
114
- if (
115
- sideData?.alphaByteLength !== undefined
116
- && (!Number.isInteger(sideData.alphaByteLength) || sideData.alphaByteLength < 0)
117
- ) {
118
- throw new TypeError('sideData.alphaByteLength, when provided, must be a non-negative integer.');
119
- }
120
-
121
- this.byteLength = byteLength;
122
- this.sideData = sideData ?? {};
123
-
124
- if (this.sideData.alpha && this.sideData.alphaByteLength === undefined) {
125
- this.sideData.alphaByteLength = this.sideData.alpha.byteLength;
126
- }
127
- }
128
-
129
- /**
130
- * If this packet is a metadata-only packet. Metadata-only packets don't contain their packet data. They are the
131
- * result of retrieving packets with {@link PacketRetrievalOptions.metadataOnly} set to `true`.
132
- */
133
- get isMetadataOnly() {
134
- return this.data === PLACEHOLDER_DATA;
135
- }
136
-
137
- /** The timestamp of this packet in microseconds. */
138
- get microsecondTimestamp() {
139
- return Math.trunc(SECOND_TO_MICROSECOND_FACTOR * this.timestamp);
140
- }
141
-
142
- /** The duration of this packet in microseconds. */
143
- get microsecondDuration() {
144
- return Math.trunc(SECOND_TO_MICROSECOND_FACTOR * this.duration);
145
- }
146
-
147
- /** Converts this packet to an
148
- * [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) for use with the
149
- * WebCodecs API. */
150
- toEncodedVideoChunk() {
151
- if (this.isMetadataOnly) {
152
- throw new TypeError('Metadata-only packets cannot be converted to a video chunk.');
153
- }
154
- if (typeof EncodedVideoChunk === 'undefined') {
155
- throw new Error('Your browser does not support EncodedVideoChunk.');
156
- }
157
-
158
- return new EncodedVideoChunk({
159
- data: this.data,
160
- type: this.type,
161
- timestamp: this.microsecondTimestamp,
162
- duration: this.microsecondDuration,
163
- });
164
- }
165
-
166
- /**
167
- * Converts this packet to an
168
- * [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) for use with the
169
- * WebCodecs API, using the alpha side data instead of the color data. Throws if no alpha side data is defined.
170
- */
171
- alphaToEncodedVideoChunk(type = this.type) {
172
- if (!this.sideData.alpha) {
173
- throw new TypeError('This packet does not contain alpha side data.');
174
- }
175
- if (this.isMetadataOnly) {
176
- throw new TypeError('Metadata-only packets cannot be converted to a video chunk.');
177
- }
178
- if (typeof EncodedVideoChunk === 'undefined') {
179
- throw new Error('Your browser does not support EncodedVideoChunk.');
180
- }
181
-
182
- return new EncodedVideoChunk({
183
- data: this.sideData.alpha,
184
- type,
185
- timestamp: this.microsecondTimestamp,
186
- duration: this.microsecondDuration,
187
- });
188
- }
189
-
190
- /** Converts this packet to an
191
- * [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk) for use with the
192
- * WebCodecs API. */
193
- toEncodedAudioChunk() {
194
- if (this.isMetadataOnly) {
195
- throw new TypeError('Metadata-only packets cannot be converted to an audio chunk.');
196
- }
197
- if (typeof EncodedAudioChunk === 'undefined') {
198
- throw new Error('Your browser does not support EncodedAudioChunk.');
199
- }
200
-
201
- return new EncodedAudioChunk({
202
- data: this.data,
203
- type: this.type,
204
- timestamp: this.microsecondTimestamp,
205
- duration: this.microsecondDuration,
206
- });
207
- }
208
-
209
- /**
210
- * Creates an {@link EncodedPacket} from an
211
- * [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) or
212
- * [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk). This method is useful
213
- * for converting chunks from the WebCodecs API to `EncodedPacket` instances.
214
- */
215
- static fromEncodedChunk(
216
- chunk: EncodedVideoChunk | EncodedAudioChunk,
217
- sideData?: EncodedPacketSideData,
218
- ): EncodedPacket {
219
- if (!(chunk instanceof EncodedVideoChunk || chunk instanceof EncodedAudioChunk)) {
220
- throw new TypeError('chunk must be an EncodedVideoChunk or EncodedAudioChunk.');
221
- }
222
-
223
- const data = new Uint8Array(chunk.byteLength);
224
- chunk.copyTo(data);
225
-
226
- return new EncodedPacket(
227
- data,
228
- chunk.type as PacketType,
229
- chunk.timestamp / 1e6,
230
- (chunk.duration ?? 0) / 1e6,
231
- undefined,
232
- undefined,
233
- sideData,
234
- );
235
- }
236
-
237
- /** Clones this packet while optionally updating timing information. */
238
- clone(options?: {
239
- /** The timestamp of the cloned packet in seconds. */
240
- timestamp?: number;
241
- /** The duration of the cloned packet in seconds. */
242
- duration?: number;
243
- }): EncodedPacket {
244
- if (options !== undefined && (typeof options !== 'object' || options === null)) {
245
- throw new TypeError('options, when provided, must be an object.');
246
- }
247
- if (options?.timestamp !== undefined && !Number.isFinite(options.timestamp)) {
248
- throw new TypeError('options.timestamp, when provided, must be a number.');
249
- }
250
- if (options?.duration !== undefined && !Number.isFinite(options.duration)) {
251
- throw new TypeError('options.duration, when provided, must be a number.');
252
- }
253
-
254
- return new EncodedPacket(
255
- this.data,
256
- this.type,
257
- options?.timestamp ?? this.timestamp,
258
- options?.duration ?? this.duration,
259
- this.sequenceNumber,
260
- this.byteLength,
261
- );
262
- }
263
- }
package/src/pcm.ts DELETED
@@ -1,112 +0,0 @@
1
- /*!
2
- * Copyright (c) 2025-present, Vanilagy and contributors
3
- *
4
- * This Source Code Form is subject to the terms of the Mozilla Public
5
- * License, v. 2.0. If a copy of the MPL was not distributed with this
6
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
- */
8
-
9
- // https://github.com/dystopiancode/pcm-g711/blob/master/pcm-g711/g711.c
10
-
11
- export const toUlaw = (s16: number) => {
12
- const MULAW_MAX = 0x1FFF;
13
- const MULAW_BIAS = 33;
14
-
15
- let number = s16;
16
- let mask = 0x1000;
17
- let sign = 0;
18
- let position = 12;
19
- let lsb = 0;
20
-
21
- if (number < 0) {
22
- number = -number;
23
- sign = 0x80;
24
- }
25
-
26
- number += MULAW_BIAS;
27
-
28
- if (number > MULAW_MAX) {
29
- number = MULAW_MAX;
30
- }
31
-
32
- while ((number & mask) !== mask && position >= 5) {
33
- mask >>= 1;
34
- position--;
35
- }
36
-
37
- lsb = (number >> (position - 4)) & 0x0f;
38
-
39
- return ~(sign | ((position - 5) << 4) | lsb) & 0xFF;
40
- };
41
-
42
- export const fromUlaw = (u8: number) => {
43
- const MULAW_BIAS = 33;
44
- let sign = 0;
45
- let position = 0;
46
-
47
- let number = ~u8;
48
-
49
- if (number & 0x80) {
50
- number &= ~(1 << 7);
51
- sign = -1;
52
- }
53
-
54
- position = ((number & 0xF0) >> 4) + 5;
55
-
56
- const decoded = ((1 << position) | ((number & 0x0F) << (position - 4))
57
- | (1 << (position - 5))) - MULAW_BIAS;
58
-
59
- return (sign === 0) ? decoded : -decoded;
60
- };
61
-
62
- export const toAlaw = (s16: number) => {
63
- const ALAW_MAX = 0xFFF;
64
- let mask = 0x800;
65
- let sign = 0;
66
- let position = 11;
67
- let lsb = 0;
68
-
69
- let number = s16;
70
-
71
- if (number < 0) {
72
- number = -number;
73
- sign = 0x80;
74
- }
75
-
76
- if (number > ALAW_MAX) {
77
- number = ALAW_MAX;
78
- }
79
-
80
- while ((number & mask) !== mask && position >= 5) {
81
- mask >>= 1;
82
- position--;
83
- }
84
-
85
- lsb = (number >> ((position === 4) ? 1 : (position - 4))) & 0x0f;
86
-
87
- return (sign | ((position - 4) << 4) | lsb) ^ 0x55;
88
- };
89
-
90
- export const fromAlaw = (u8: number) => {
91
- let sign = 0x00;
92
- let position = 0;
93
-
94
- let number = u8 ^ 0x55;
95
-
96
- if (number & 0x80) {
97
- number &= ~(1 << 7);
98
- sign = -1;
99
- }
100
-
101
- position = ((number & 0xF0) >> 4) + 4;
102
-
103
- let decoded = 0;
104
- if (position !== 4) {
105
- decoded = ((1 << position) | ((number & 0x0F) << (position - 4))
106
- | (1 << (position - 5)));
107
- } else {
108
- decoded = (number << 1) | 1;
109
- }
110
-
111
- return (sign === 0) ? decoded : -decoded;
112
- };
package/src/reader.ts DELETED
@@ -1,323 +0,0 @@
1
- /*!
2
- * Copyright (c) 2025-present, Vanilagy and contributors
3
- *
4
- * This Source Code Form is subject to the terms of the Mozilla Public
5
- * License, v. 2.0. If a copy of the MPL was not distributed with this
6
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
- */
8
-
9
- import { InputDisposedError } from './input';
10
- import { assert, clamp, getUint24, MaybePromise, toDataView } from './misc';
11
- import { Source } from './source';
12
-
13
- export class Reader {
14
- fileSize!: number | null;
15
-
16
- constructor(public source: Source) {}
17
-
18
- requestSlice(start: number, length: number): MaybePromise<FileSlice | null> {
19
- if (this.source._disposed) {
20
- throw new InputDisposedError();
21
- }
22
-
23
- if (this.fileSize !== null && start + length > this.fileSize) {
24
- return null;
25
- }
26
-
27
- const end = start + length;
28
- const result = this.source._read(start, end);
29
-
30
- if (result instanceof Promise) {
31
- return result.then((x) => {
32
- if (!x) {
33
- return null;
34
- }
35
-
36
- return new FileSlice(x.bytes, x.view, x.offset, start, end);
37
- });
38
- } else {
39
- if (!result) {
40
- return null;
41
- }
42
-
43
- return new FileSlice(result.bytes, result.view, result.offset, start, end);
44
- }
45
- }
46
-
47
- requestSliceRange(start: number, minLength: number, maxLength: number): MaybePromise<FileSlice | null> {
48
- if (this.source._disposed) {
49
- throw new InputDisposedError();
50
- }
51
-
52
- if (this.fileSize !== null) {
53
- return this.requestSlice(
54
- start,
55
- clamp(this.fileSize - start, minLength, maxLength),
56
- );
57
- } else {
58
- const promisedAttempt = this.requestSlice(start, maxLength);
59
-
60
- const handleAttempt = (attempt: FileSlice | null) => {
61
- if (attempt) {
62
- return attempt;
63
- }
64
-
65
- const handleFileSize = (fileSize: number | null) => {
66
- assert(fileSize !== null); // The slice couldn't fit, meaning we must know the file size now
67
-
68
- return this.requestSlice(
69
- start,
70
- clamp(fileSize - start, minLength, maxLength),
71
- );
72
- };
73
-
74
- const promisedFileSize = this.source._retrieveSize();
75
- if (promisedFileSize instanceof Promise) {
76
- return promisedFileSize.then(handleFileSize);
77
- } else {
78
- return handleFileSize(promisedFileSize);
79
- }
80
- };
81
-
82
- if (promisedAttempt instanceof Promise) {
83
- return promisedAttempt.then(handleAttempt);
84
- } else {
85
- return handleAttempt(promisedAttempt);
86
- }
87
- }
88
- }
89
- }
90
-
91
- export class FileSlice {
92
- /** The current position in the backing buffer. Do not modify directly, prefer `.skip()` instead. */
93
- bufferPos: number;
94
-
95
- constructor(
96
- /** The underlying bytes backing this slice. Avoid using this directly and prefer reader functions instead. */
97
- public readonly bytes: Uint8Array,
98
- /** A view into the bytes backing this slice. Avoid using this directly and prefer reader functions instead. */
99
- public readonly view: DataView,
100
- /** The offset in "file bytes" at which `bytes` begins in the file. */
101
- private readonly offset: number,
102
- /** The offset in "file bytes" where this slice begins. */
103
- public readonly start: number,
104
- /** The offset in "file bytes" where this slice ends (exclusive). */
105
- public readonly end: number,
106
- ) {
107
- this.bufferPos = start - offset;
108
- }
109
-
110
- static tempFromBytes(bytes: Uint8Array) {
111
- return new FileSlice(
112
- bytes,
113
- toDataView(bytes),
114
- 0,
115
- 0,
116
- bytes.length,
117
- );
118
- }
119
-
120
- get length() {
121
- return this.end - this.start;
122
- }
123
-
124
- get filePos() {
125
- return this.offset + this.bufferPos;
126
- }
127
-
128
- set filePos(value: number) {
129
- this.bufferPos = value - this.offset;
130
- }
131
-
132
- /** The number of bytes left from the current pos to the end of the slice. */
133
- get remainingLength() {
134
- return Math.max(this.end - this.filePos, 0);
135
- }
136
-
137
- skip(byteCount: number) {
138
- this.bufferPos += byteCount;
139
- }
140
-
141
- /** Creates a new subslice of this slice whose byte range must be contained within this slice. */
142
- slice(filePos: number, length = this.end - filePos) {
143
- if (filePos < this.start || filePos + length > this.end) {
144
- throw new RangeError('Slicing outside of original slice.');
145
- }
146
-
147
- return new FileSlice(
148
- this.bytes,
149
- this.view,
150
- this.offset,
151
- filePos,
152
- filePos + length,
153
- );
154
- }
155
- }
156
-
157
- const checkIsInRange = (slice: FileSlice, bytesToRead: number) => {
158
- if (slice.filePos < slice.start || slice.filePos + bytesToRead > slice.end) {
159
- throw new RangeError(
160
- `Tried reading [${slice.filePos}, ${slice.filePos + bytesToRead}), but slice is`
161
- + ` [${slice.start}, ${slice.end}). This is likely an internal error, please report it alongside the file`
162
- + ` that caused it.`,
163
- );
164
- }
165
- };
166
-
167
- export const readBytes = (slice: FileSlice, length: number) => {
168
- checkIsInRange(slice, length);
169
-
170
- const bytes = slice.bytes.subarray(slice.bufferPos, slice.bufferPos + length);
171
- slice.bufferPos += length;
172
-
173
- return bytes;
174
- };
175
-
176
- export const readU8 = (slice: FileSlice) => {
177
- checkIsInRange(slice, 1);
178
- return slice.view.getUint8(slice.bufferPos++);
179
- };
180
-
181
- export const readU16 = (slice: FileSlice, littleEndian: boolean) => {
182
- checkIsInRange(slice, 2);
183
-
184
- const value = slice.view.getUint16(slice.bufferPos, littleEndian);
185
- slice.bufferPos += 2;
186
-
187
- return value;
188
- };
189
-
190
- export const readU16Be = (slice: FileSlice) => {
191
- checkIsInRange(slice, 2);
192
-
193
- const value = slice.view.getUint16(slice.bufferPos, false);
194
- slice.bufferPos += 2;
195
-
196
- return value;
197
- };
198
-
199
- export const readU24Be = (slice: FileSlice) => {
200
- checkIsInRange(slice, 3);
201
-
202
- const value = getUint24(slice.view, slice.bufferPos, false);
203
- slice.bufferPos += 3;
204
-
205
- return value;
206
- };
207
-
208
- export const readI16Be = (slice: FileSlice) => {
209
- checkIsInRange(slice, 2);
210
-
211
- const value = slice.view.getInt16(slice.bufferPos, false);
212
- slice.bufferPos += 2;
213
-
214
- return value;
215
- };
216
-
217
- export const readU32 = (slice: FileSlice, littleEndian: boolean) => {
218
- checkIsInRange(slice, 4);
219
-
220
- const value = slice.view.getUint32(slice.bufferPos, littleEndian);
221
- slice.bufferPos += 4;
222
-
223
- return value;
224
- };
225
-
226
- export const readU32Be = (slice: FileSlice) => {
227
- checkIsInRange(slice, 4);
228
-
229
- const value = slice.view.getUint32(slice.bufferPos, false);
230
- slice.bufferPos += 4;
231
-
232
- return value;
233
- };
234
-
235
- export const readU32Le = (slice: FileSlice) => {
236
- checkIsInRange(slice, 4);
237
-
238
- const value = slice.view.getUint32(slice.bufferPos, true);
239
- slice.bufferPos += 4;
240
-
241
- return value;
242
- };
243
-
244
- export const readI32Be = (slice: FileSlice) => {
245
- checkIsInRange(slice, 4);
246
-
247
- const value = slice.view.getInt32(slice.bufferPos, false);
248
- slice.bufferPos += 4;
249
-
250
- return value;
251
- };
252
-
253
- export const readI32Le = (slice: FileSlice) => {
254
- checkIsInRange(slice, 4);
255
-
256
- const value = slice.view.getInt32(slice.bufferPos, true);
257
- slice.bufferPos += 4;
258
-
259
- return value;
260
- };
261
-
262
- export const readU64 = (slice: FileSlice, littleEndian: boolean) => {
263
- let low: number;
264
- let high: number;
265
-
266
- if (littleEndian) {
267
- low = readU32(slice, true);
268
- high = readU32(slice, true);
269
- } else {
270
- high = readU32(slice, false);
271
- low = readU32(slice, false);
272
- }
273
-
274
- return high * 0x100000000 + low;
275
- };
276
-
277
- export const readU64Be = (slice: FileSlice) => {
278
- const high = readU32Be(slice);
279
- const low = readU32Be(slice);
280
- return high * 0x100000000 + low;
281
- };
282
-
283
- export const readI64Be = (slice: FileSlice) => {
284
- const high = readI32Be(slice);
285
- const low = readU32Be(slice);
286
- return high * 0x100000000 + low;
287
- };
288
-
289
- export const readI64Le = (slice: FileSlice) => {
290
- const low = readU32Le(slice);
291
- const high = readI32Le(slice);
292
- return high * 0x100000000 + low;
293
- };
294
-
295
- export const readF32Be = (slice: FileSlice) => {
296
- checkIsInRange(slice, 4);
297
-
298
- const value = slice.view.getFloat32(slice.bufferPos, false);
299
- slice.bufferPos += 4;
300
-
301
- return value;
302
- };
303
-
304
- export const readF64Be = (slice: FileSlice) => {
305
- checkIsInRange(slice, 8);
306
-
307
- const value = slice.view.getFloat64(slice.bufferPos, false);
308
- slice.bufferPos += 8;
309
-
310
- return value;
311
- };
312
-
313
- export const readAscii = (slice: FileSlice, length: number) => {
314
- checkIsInRange(slice, length);
315
-
316
- let str = '';
317
-
318
- for (let i = 0; i < length; i++) {
319
- str += String.fromCharCode(slice.bytes[slice.bufferPos++]!);
320
- }
321
-
322
- return str;
323
- };