@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
@@ -1,371 +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 { Muxer } from '../muxer';
10
- import { Output, OutputAudioTrack } from '../output';
11
- import { parsePcmCodec, PcmAudioCodec, validateAudioChunkMetadata } from '../codec';
12
- import { WaveFormat } from './wave-demuxer';
13
- import { RiffWriter } from './riff-writer';
14
- import { Writer } from '../writer';
15
- import { EncodedPacket } from '../packet';
16
- import { WavOutputFormat } from '../output-format';
17
- import { assert, assertNever, isIso88591Compatible, keyValueIterator } from '../misc';
18
- import { MetadataTags, metadataTagsAreEmpty } from '../metadata';
19
- import { Id3V2Writer } from '../id3';
20
-
21
- export class WaveMuxer extends Muxer {
22
- private format: WavOutputFormat;
23
- private isRf64: boolean;
24
- private writer: Writer;
25
- private riffWriter: RiffWriter;
26
- private headerWritten = false;
27
- private dataSize = 0;
28
- private sampleRate: number | null = null;
29
- private sampleCount = 0;
30
-
31
- private riffSizePos: number | null = null;
32
- private dataSizePos: number | null = null;
33
- private ds64RiffSizePos: number | null = null;
34
- private ds64DataSizePos: number | null = null;
35
- private ds64SampleCountPos: number | null = null;
36
-
37
- constructor(output: Output, format: WavOutputFormat) {
38
- super(output);
39
-
40
- this.format = format;
41
- this.writer = output._writer;
42
- this.riffWriter = new RiffWriter(output._writer);
43
- this.isRf64 = !!format._options.large;
44
- }
45
-
46
- async start() {
47
- // Nothing needed here - we'll write the header with the first sample
48
- }
49
-
50
- async getMimeType() {
51
- return 'audio/wav';
52
- }
53
-
54
- async addEncodedVideoPacket() {
55
- throw new Error('WAVE does not support video.');
56
- }
57
-
58
- async addEncodedAudioPacket(
59
- track: OutputAudioTrack,
60
- packet: EncodedPacket,
61
- meta?: EncodedAudioChunkMetadata,
62
- ) {
63
- const release = await this.mutex.acquire();
64
-
65
- try {
66
- if (!this.headerWritten) {
67
- validateAudioChunkMetadata(meta);
68
-
69
- assert(meta);
70
- assert(meta.decoderConfig);
71
-
72
- this.writeHeader(track, meta.decoderConfig);
73
- this.sampleRate = meta.decoderConfig.sampleRate;
74
- this.headerWritten = true;
75
- }
76
-
77
- this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === 'key');
78
-
79
- if (!this.isRf64 && this.writer.getPos() + packet.data.byteLength >= 2 ** 32) {
80
- throw new Error(
81
- 'Adding more audio data would exceed the maximum RIFF size of 4 GiB. To write larger files, use'
82
- + ' RF64 by setting `large: true` in the WavOutputFormatOptions.',
83
- );
84
- }
85
-
86
- this.writer.write(packet.data);
87
- this.dataSize += packet.data.byteLength;
88
- this.sampleCount += Math.round(packet.duration * this.sampleRate!);
89
-
90
- await this.writer.flush();
91
- } finally {
92
- release();
93
- }
94
- }
95
-
96
- async addSubtitleCue() {
97
- throw new Error('WAVE does not support subtitles.');
98
- }
99
-
100
- private writeHeader(track: OutputAudioTrack, config: AudioDecoderConfig) {
101
- if (this.format._options.onHeader) {
102
- this.writer.startTrackingWrites();
103
- }
104
-
105
- let format: WaveFormat;
106
-
107
- const codec = track.source._codec;
108
- const pcmInfo = parsePcmCodec(codec as PcmAudioCodec);
109
-
110
- if (pcmInfo.dataType === 'ulaw') {
111
- format = WaveFormat.MULAW;
112
- } else if (pcmInfo.dataType === 'alaw') {
113
- format = WaveFormat.ALAW;
114
- } else if (pcmInfo.dataType === 'float') {
115
- format = WaveFormat.IEEE_FLOAT;
116
- } else {
117
- format = WaveFormat.PCM;
118
- }
119
-
120
- const channels = config.numberOfChannels;
121
- const sampleRate = config.sampleRate;
122
- const blockSize = pcmInfo.sampleSize * channels;
123
-
124
- // RIFF header
125
- this.riffWriter.writeAscii(this.isRf64 ? 'RF64' : 'RIFF');
126
-
127
- if (this.isRf64) {
128
- this.riffWriter.writeU32(0xffffffff); // Not used in RF64
129
- } else {
130
- this.riffSizePos = this.writer.getPos();
131
- this.riffWriter.writeU32(0); // File size placeholder
132
- }
133
-
134
- this.riffWriter.writeAscii('WAVE');
135
-
136
- if (this.isRf64) {
137
- this.riffWriter.writeAscii('ds64');
138
- this.riffWriter.writeU32(28); // Chunk size
139
-
140
- this.ds64RiffSizePos = this.writer.getPos();
141
- this.riffWriter.writeU64(0); // RIFF size placeholder
142
-
143
- this.ds64DataSizePos = this.writer.getPos();
144
- this.riffWriter.writeU64(0); // Data size placeholder
145
-
146
- this.ds64SampleCountPos = this.writer.getPos();
147
- this.riffWriter.writeU64(0); // Sample count placeholder
148
-
149
- this.riffWriter.writeU32(0); // Table length
150
- // Empty table
151
- }
152
-
153
- // fmt chunk
154
- this.riffWriter.writeAscii('fmt ');
155
- this.riffWriter.writeU32(16); // Chunk size
156
- this.riffWriter.writeU16(format);
157
- this.riffWriter.writeU16(channels);
158
- this.riffWriter.writeU32(sampleRate);
159
- this.riffWriter.writeU32(sampleRate * blockSize); // Bytes per second
160
- this.riffWriter.writeU16(blockSize);
161
- this.riffWriter.writeU16(8 * pcmInfo.sampleSize);
162
-
163
- // Metadata tags
164
- if (!metadataTagsAreEmpty(this.output._metadataTags)) {
165
- const metadataFormat = this.format._options.metadataFormat ?? 'info';
166
-
167
- if (metadataFormat === 'info') {
168
- this.writeInfoChunk(this.output._metadataTags);
169
- } else if (metadataFormat === 'id3') {
170
- this.writeId3Chunk(this.output._metadataTags);
171
- } else {
172
- assertNever(metadataFormat);
173
- }
174
- }
175
-
176
- // data chunk
177
- this.riffWriter.writeAscii('data');
178
-
179
- if (this.isRf64) {
180
- this.riffWriter.writeU32(0xffffffff); // Not used in RF64
181
- } else {
182
- this.dataSizePos = this.writer.getPos();
183
- this.riffWriter.writeU32(0); // Data size placeholder
184
- }
185
-
186
- if (this.format._options.onHeader) {
187
- const { data, start } = this.writer.stopTrackingWrites();
188
- this.format._options.onHeader(data, start);
189
- }
190
- }
191
-
192
- private writeInfoChunk(metadata: MetadataTags) {
193
- const startPos = this.writer.getPos();
194
-
195
- this.riffWriter.writeAscii('LIST');
196
- this.riffWriter.writeU32(0); // Size placeholder
197
- this.riffWriter.writeAscii('INFO');
198
-
199
- const writtenTags = new Set<string>();
200
-
201
- const writeInfoTag = (tag: string, value: string) => {
202
- if (!isIso88591Compatible(value)) {
203
- // No Unicode supported here
204
- console.warn(`Didn't write tag '${tag}' because '${value}' is not ISO 8859-1-compatible.`);
205
- return;
206
- }
207
-
208
- const size = value.length + 1; // +1 for null terminator
209
- const bytes = new Uint8Array(size);
210
-
211
- for (let i = 0; i < value.length; i++) {
212
- bytes[i] = value.charCodeAt(i);
213
- }
214
-
215
- this.riffWriter.writeAscii(tag);
216
- this.riffWriter.writeU32(size);
217
- this.writer.write(bytes);
218
-
219
- // Add padding byte if size is odd
220
- if (size & 1) {
221
- this.writer.write(new Uint8Array(1));
222
- }
223
-
224
- writtenTags.add(tag);
225
- };
226
-
227
- for (const { key, value } of keyValueIterator(metadata)) {
228
- switch (key) {
229
- case 'title': {
230
- writeInfoTag('INAM', value);
231
- writtenTags.add('INAM');
232
- }; break;
233
-
234
- case 'artist': {
235
- writeInfoTag('IART', value);
236
- writtenTags.add('IART');
237
- }; break;
238
-
239
- case 'album': {
240
- writeInfoTag('IPRD', value);
241
- writtenTags.add('IPRD');
242
- }; break;
243
-
244
- case 'trackNumber': {
245
- const string = metadata.tracksTotal !== undefined
246
- ? `${value}/${metadata.tracksTotal}`
247
- : value.toString();
248
-
249
- writeInfoTag('ITRK', string);
250
- writtenTags.add('ITRK');
251
- }; break;
252
-
253
- case 'genre': {
254
- writeInfoTag('IGNR', value);
255
- writtenTags.add('IGNR');
256
- }; break;
257
-
258
- case 'date': {
259
- writeInfoTag('ICRD', value.toISOString().slice(0, 10));
260
- writtenTags.add('ICRD');
261
- }; break;
262
-
263
- case 'comment': {
264
- writeInfoTag('ICMT', value);
265
- writtenTags.add('ICMT');
266
- }; break;
267
-
268
- case 'albumArtist':
269
- case 'discNumber':
270
- case 'tracksTotal':
271
- case 'discsTotal':
272
- case 'description':
273
- case 'lyrics':
274
- case 'images': {
275
- // Not supported in RIFF INFO
276
- }; break;
277
-
278
- case 'raw': {
279
- // Handled later
280
- }; break;
281
-
282
- default: assertNever(key);
283
- }
284
- }
285
-
286
- if (metadata.raw) {
287
- for (const key in metadata.raw) {
288
- const value = metadata.raw[key];
289
- if (value == null || key.length !== 4 || writtenTags.has(key)) {
290
- continue;
291
- }
292
-
293
- if (typeof value === 'string') {
294
- writeInfoTag(key, value);
295
- }
296
- }
297
- }
298
-
299
- const endPos = this.writer.getPos();
300
- const chunkSize = endPos - startPos - 8;
301
-
302
- this.writer.seek(startPos + 4);
303
- this.riffWriter.writeU32(chunkSize);
304
- this.writer.seek(endPos);
305
-
306
- // Add padding byte if chunk size is odd
307
- if (chunkSize & 1) {
308
- this.writer.write(new Uint8Array(1));
309
- }
310
- }
311
-
312
- private writeId3Chunk(metadata: MetadataTags) {
313
- const startPos = this.writer.getPos();
314
-
315
- // Write RIFF chunk header
316
- this.riffWriter.writeAscii('ID3 ');
317
- this.riffWriter.writeU32(0); // Size placeholder
318
-
319
- const id3Writer = new Id3V2Writer(this.writer);
320
- const id3TagSize = id3Writer.writeId3V2Tag(metadata);
321
-
322
- const endPos = this.writer.getPos();
323
-
324
- // Update RIFF chunk size
325
- this.writer.seek(startPos + 4);
326
- this.riffWriter.writeU32(id3TagSize);
327
- this.writer.seek(endPos);
328
-
329
- // Add padding byte if chunk size is odd
330
- if (id3TagSize & 1) {
331
- this.writer.write(new Uint8Array(1));
332
- }
333
- }
334
-
335
- async finalize() {
336
- const release = await this.mutex.acquire();
337
-
338
- const endPos = this.writer.getPos();
339
-
340
- if (this.isRf64) {
341
- // Write riff size
342
- assert(this.ds64RiffSizePos !== null);
343
- this.writer.seek(this.ds64RiffSizePos);
344
- this.riffWriter.writeU64(endPos - 8);
345
-
346
- // Write data size
347
- assert(this.ds64DataSizePos !== null);
348
- this.writer.seek(this.ds64DataSizePos);
349
- this.riffWriter.writeU64(this.dataSize);
350
-
351
- // Write sample count
352
- assert(this.ds64SampleCountPos !== null);
353
- this.writer.seek(this.ds64SampleCountPos);
354
- this.riffWriter.writeU64(this.sampleCount);
355
- } else {
356
- // Write file size
357
- assert(this.riffSizePos !== null);
358
- this.writer.seek(this.riffSizePos);
359
- this.riffWriter.writeU32(endPos - 8);
360
-
361
- // Write data chunk size
362
- assert(this.dataSizePos !== null);
363
- this.writer.seek(this.dataSizePos);
364
- this.riffWriter.writeU32(this.dataSize);
365
- }
366
-
367
- this.writer.seek(endPos);
368
-
369
- release();
370
- }
371
- }