@kenzuya/mediabunny 1.26.0 → 1.28.6
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.
- package/README.md +1 -1
- package/dist/bundles/{mediabunny.mjs → mediabunny.js} +21963 -21390
- package/dist/bundles/mediabunny.min.js +490 -0
- package/dist/modules/shared/mp3-misc.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-demuxer.d.ts +6 -6
- package/dist/modules/src/adts/adts-demuxer.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-muxer.d.ts +4 -4
- package/dist/modules/src/adts/adts-muxer.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-reader.d.ts +1 -1
- package/dist/modules/src/adts/adts-reader.d.ts.map +1 -1
- package/dist/modules/src/avi/avi-demuxer.d.ts +44 -0
- package/dist/modules/src/avi/avi-demuxer.d.ts.map +1 -0
- package/dist/modules/src/avi/avi-misc.d.ts +88 -0
- package/dist/modules/src/avi/avi-misc.d.ts.map +1 -0
- package/dist/modules/src/avi/avi-muxer.d.ts +45 -0
- package/dist/modules/src/avi/avi-muxer.d.ts.map +1 -0
- package/dist/modules/src/avi/riff-writer.d.ts +26 -0
- package/dist/modules/src/avi/riff-writer.d.ts.map +1 -0
- package/dist/modules/src/codec-data.d.ts +8 -3
- package/dist/modules/src/codec-data.d.ts.map +1 -1
- package/dist/modules/src/codec.d.ts +10 -10
- package/dist/modules/src/codec.d.ts.map +1 -1
- package/dist/modules/src/conversion.d.ts +33 -16
- package/dist/modules/src/conversion.d.ts.map +1 -1
- package/dist/modules/src/custom-coder.d.ts +8 -8
- package/dist/modules/src/custom-coder.d.ts.map +1 -1
- package/dist/modules/src/demuxer.d.ts +3 -3
- package/dist/modules/src/demuxer.d.ts.map +1 -1
- package/dist/modules/src/encode.d.ts +8 -8
- package/dist/modules/src/encode.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-demuxer.d.ts +7 -7
- package/dist/modules/src/flac/flac-demuxer.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-misc.d.ts +3 -3
- package/dist/modules/src/flac/flac-misc.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-muxer.d.ts +5 -5
- package/dist/modules/src/flac/flac-muxer.d.ts.map +1 -1
- package/dist/modules/src/id3.d.ts +3 -3
- package/dist/modules/src/id3.d.ts.map +1 -1
- package/dist/modules/src/index.d.ts +20 -20
- package/dist/modules/src/index.d.ts.map +1 -1
- package/dist/modules/src/input-format.d.ts +22 -0
- package/dist/modules/src/input-format.d.ts.map +1 -1
- package/dist/modules/src/input-track.d.ts +8 -8
- package/dist/modules/src/input-track.d.ts.map +1 -1
- package/dist/modules/src/input.d.ts +12 -12
- package/dist/modules/src/isobmff/isobmff-boxes.d.ts +2 -2
- package/dist/modules/src/isobmff/isobmff-boxes.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-demuxer.d.ts +12 -12
- package/dist/modules/src/isobmff/isobmff-demuxer.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-misc.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-muxer.d.ts +11 -11
- package/dist/modules/src/isobmff/isobmff-muxer.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-reader.d.ts +2 -2
- package/dist/modules/src/isobmff/isobmff-reader.d.ts.map +1 -1
- package/dist/modules/src/matroska/ebml.d.ts +3 -3
- package/dist/modules/src/matroska/ebml.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-demuxer.d.ts +13 -13
- package/dist/modules/src/matroska/matroska-demuxer.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-input.d.ts +33 -0
- package/dist/modules/src/matroska/matroska-input.d.ts.map +1 -0
- package/dist/modules/src/matroska/matroska-misc.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-muxer.d.ts +5 -5
- package/dist/modules/src/matroska/matroska-muxer.d.ts.map +1 -1
- package/dist/modules/src/media-sink.d.ts +5 -5
- package/dist/modules/src/media-sink.d.ts.map +1 -1
- package/dist/modules/src/media-source.d.ts +22 -4
- package/dist/modules/src/media-source.d.ts.map +1 -1
- package/dist/modules/src/metadata.d.ts +2 -2
- package/dist/modules/src/metadata.d.ts.map +1 -1
- package/dist/modules/src/misc.d.ts +5 -4
- package/dist/modules/src/misc.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-demuxer.d.ts +7 -7
- package/dist/modules/src/mp3/mp3-demuxer.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-muxer.d.ts +4 -4
- package/dist/modules/src/mp3/mp3-muxer.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-reader.d.ts +2 -2
- package/dist/modules/src/mp3/mp3-reader.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-writer.d.ts +1 -1
- package/dist/modules/src/mp3/mp3-writer.d.ts.map +1 -1
- package/dist/modules/src/muxer.d.ts +4 -4
- package/dist/modules/src/muxer.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-demuxer.d.ts +7 -7
- package/dist/modules/src/ogg/ogg-demuxer.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-misc.d.ts +1 -1
- package/dist/modules/src/ogg/ogg-misc.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-muxer.d.ts +5 -5
- package/dist/modules/src/ogg/ogg-muxer.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-reader.d.ts +1 -1
- package/dist/modules/src/ogg/ogg-reader.d.ts.map +1 -1
- package/dist/modules/src/output-format.d.ts +51 -6
- package/dist/modules/src/output-format.d.ts.map +1 -1
- package/dist/modules/src/output.d.ts +13 -13
- package/dist/modules/src/output.d.ts.map +1 -1
- package/dist/modules/src/packet.d.ts +1 -1
- package/dist/modules/src/packet.d.ts.map +1 -1
- package/dist/modules/src/pcm.d.ts.map +1 -1
- package/dist/modules/src/reader.d.ts +2 -2
- package/dist/modules/src/reader.d.ts.map +1 -1
- package/dist/modules/src/sample.d.ts +57 -15
- package/dist/modules/src/sample.d.ts.map +1 -1
- package/dist/modules/src/source.d.ts +3 -3
- package/dist/modules/src/source.d.ts.map +1 -1
- package/dist/modules/src/subtitles.d.ts +1 -1
- package/dist/modules/src/subtitles.d.ts.map +1 -1
- package/dist/modules/src/target.d.ts +2 -2
- package/dist/modules/src/target.d.ts.map +1 -1
- package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
- package/dist/modules/src/wave/riff-writer.d.ts +1 -1
- package/dist/modules/src/wave/riff-writer.d.ts.map +1 -1
- package/dist/modules/src/wave/wave-demuxer.d.ts +6 -6
- package/dist/modules/src/wave/wave-demuxer.d.ts.map +1 -1
- package/dist/modules/src/wave/wave-muxer.d.ts +4 -4
- package/dist/modules/src/wave/wave-muxer.d.ts.map +1 -1
- package/dist/modules/src/writer.d.ts +1 -1
- package/dist/modules/src/writer.d.ts.map +1 -1
- package/dist/packages/eac3/eac3.wasm +0 -0
- package/dist/packages/eac3/mediabunny-eac3.js +1058 -0
- package/dist/packages/eac3/mediabunny-eac3.min.js +44 -0
- package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.js +694 -0
- package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.min.js +58 -0
- package/dist/packages/mpeg4/mediabunny-mpeg4.js +1198 -0
- package/dist/packages/mpeg4/mediabunny-mpeg4.min.js +44 -0
- package/dist/packages/mpeg4/xvid.wasm +0 -0
- package/package.json +18 -57
- package/dist/bundles/mediabunny.cjs +0 -26140
- package/dist/bundles/mediabunny.min.cjs +0 -147
- package/dist/bundles/mediabunny.min.mjs +0 -146
- package/dist/mediabunny.d.ts +0 -3319
- package/dist/modules/shared/mp3-misc.js +0 -147
- package/dist/modules/src/adts/adts-demuxer.js +0 -239
- package/dist/modules/src/adts/adts-muxer.js +0 -80
- package/dist/modules/src/adts/adts-reader.js +0 -63
- package/dist/modules/src/codec-data.js +0 -1730
- package/dist/modules/src/codec.js +0 -869
- package/dist/modules/src/conversion.js +0 -1459
- package/dist/modules/src/custom-coder.js +0 -117
- package/dist/modules/src/demuxer.js +0 -12
- package/dist/modules/src/encode.js +0 -442
- package/dist/modules/src/flac/flac-demuxer.js +0 -504
- package/dist/modules/src/flac/flac-misc.js +0 -135
- package/dist/modules/src/flac/flac-muxer.js +0 -222
- package/dist/modules/src/id3.js +0 -848
- package/dist/modules/src/index.js +0 -28
- package/dist/modules/src/input-format.js +0 -480
- package/dist/modules/src/input-track.js +0 -372
- package/dist/modules/src/input.js +0 -188
- package/dist/modules/src/isobmff/isobmff-boxes.js +0 -1480
- package/dist/modules/src/isobmff/isobmff-demuxer.js +0 -2618
- package/dist/modules/src/isobmff/isobmff-misc.js +0 -20
- package/dist/modules/src/isobmff/isobmff-muxer.js +0 -966
- package/dist/modules/src/isobmff/isobmff-reader.js +0 -72
- package/dist/modules/src/matroska/ebml.js +0 -653
- package/dist/modules/src/matroska/matroska-demuxer.js +0 -2133
- package/dist/modules/src/matroska/matroska-misc.js +0 -20
- package/dist/modules/src/matroska/matroska-muxer.js +0 -1017
- package/dist/modules/src/media-sink.js +0 -1736
- package/dist/modules/src/media-source.js +0 -1825
- package/dist/modules/src/metadata.js +0 -193
- package/dist/modules/src/misc.js +0 -623
- package/dist/modules/src/mp3/mp3-demuxer.js +0 -285
- package/dist/modules/src/mp3/mp3-muxer.js +0 -123
- package/dist/modules/src/mp3/mp3-reader.js +0 -26
- package/dist/modules/src/mp3/mp3-writer.js +0 -78
- package/dist/modules/src/muxer.js +0 -50
- package/dist/modules/src/node.d.ts +0 -9
- package/dist/modules/src/node.d.ts.map +0 -1
- package/dist/modules/src/node.js +0 -9
- package/dist/modules/src/ogg/ogg-demuxer.js +0 -763
- package/dist/modules/src/ogg/ogg-misc.js +0 -78
- package/dist/modules/src/ogg/ogg-muxer.js +0 -353
- package/dist/modules/src/ogg/ogg-reader.js +0 -65
- package/dist/modules/src/output-format.js +0 -527
- package/dist/modules/src/output.js +0 -300
- package/dist/modules/src/packet.js +0 -182
- package/dist/modules/src/pcm.js +0 -85
- package/dist/modules/src/reader.js +0 -236
- package/dist/modules/src/sample.js +0 -1056
- package/dist/modules/src/source.js +0 -1182
- package/dist/modules/src/subtitles.js +0 -575
- package/dist/modules/src/target.js +0 -140
- package/dist/modules/src/wave/riff-writer.js +0 -30
- package/dist/modules/src/wave/wave-demuxer.js +0 -447
- package/dist/modules/src/wave/wave-muxer.js +0 -318
- package/dist/modules/src/writer.js +0 -370
- package/src/adts/adts-demuxer.ts +0 -331
- package/src/adts/adts-muxer.ts +0 -111
- package/src/adts/adts-reader.ts +0 -85
- package/src/codec-data.ts +0 -2078
- package/src/codec.ts +0 -1092
- package/src/conversion.ts +0 -2112
- package/src/custom-coder.ts +0 -197
- package/src/demuxer.ts +0 -24
- package/src/encode.ts +0 -739
- package/src/flac/flac-demuxer.ts +0 -730
- package/src/flac/flac-misc.ts +0 -164
- package/src/flac/flac-muxer.ts +0 -320
- package/src/id3.ts +0 -925
- package/src/index.ts +0 -221
- package/src/input-format.ts +0 -541
- package/src/input-track.ts +0 -529
- package/src/input.ts +0 -235
- package/src/isobmff/isobmff-boxes.ts +0 -1719
- package/src/isobmff/isobmff-demuxer.ts +0 -3190
- package/src/isobmff/isobmff-misc.ts +0 -29
- package/src/isobmff/isobmff-muxer.ts +0 -1348
- package/src/isobmff/isobmff-reader.ts +0 -91
- package/src/matroska/ebml.ts +0 -730
- package/src/matroska/matroska-demuxer.ts +0 -2481
- package/src/matroska/matroska-misc.ts +0 -29
- package/src/matroska/matroska-muxer.ts +0 -1276
- package/src/media-sink.ts +0 -2179
- package/src/media-source.ts +0 -2243
- package/src/metadata.ts +0 -320
- package/src/misc.ts +0 -798
- package/src/mp3/mp3-demuxer.ts +0 -383
- package/src/mp3/mp3-muxer.ts +0 -166
- package/src/mp3/mp3-reader.ts +0 -34
- package/src/mp3/mp3-writer.ts +0 -120
- package/src/muxer.ts +0 -88
- package/src/node.ts +0 -11
- package/src/ogg/ogg-demuxer.ts +0 -1053
- package/src/ogg/ogg-misc.ts +0 -116
- package/src/ogg/ogg-muxer.ts +0 -497
- package/src/ogg/ogg-reader.ts +0 -93
- package/src/output-format.ts +0 -945
- package/src/output.ts +0 -488
- package/src/packet.ts +0 -263
- package/src/pcm.ts +0 -112
- package/src/reader.ts +0 -323
- package/src/sample.ts +0 -1461
- package/src/source.ts +0 -1688
- package/src/subtitles.ts +0 -711
- package/src/target.ts +0 -204
- package/src/tsconfig.json +0 -16
- package/src/wave/riff-writer.ts +0 -36
- package/src/wave/wave-demuxer.ts +0 -529
- package/src/wave/wave-muxer.ts +0 -371
- package/src/writer.ts +0 -490
package/src/wave/wave-muxer.ts
DELETED
|
@@ -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
|
-
}
|