mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-28 11:23:45 +02:00
120 lines
3.1 KiB
TypeScript
120 lines
3.1 KiB
TypeScript
import { Muxer } from '../muxer';
|
|
import { Output, OutputAudioTrack } from '../output';
|
|
import { EncodedAudioSample } from '../sample';
|
|
import { parsePcmCodec, PcmAudioCodec } from '../codec';
|
|
import { WaveFormat } from './wave-demuxer';
|
|
import { RiffWriter } from './riff-writer';
|
|
import { Writer } from '../writer';
|
|
|
|
export class WaveMuxer extends Muxer {
|
|
private writer: Writer;
|
|
private riffWriter: RiffWriter;
|
|
private headerWritten = false;
|
|
private dataSize = 0;
|
|
|
|
constructor(output: Output) {
|
|
super(output);
|
|
|
|
this.writer = output._writer;
|
|
this.riffWriter = new RiffWriter(output._writer);
|
|
}
|
|
|
|
async start() {
|
|
// Nothing needed here - we'll write the header with the first sample
|
|
}
|
|
|
|
async addEncodedVideoSample() {
|
|
throw new Error('WAVE format does not support video.');
|
|
}
|
|
|
|
async addEncodedAudioSample(
|
|
track: OutputAudioTrack,
|
|
sample: EncodedAudioSample,
|
|
meta?: EncodedAudioChunkMetadata,
|
|
) {
|
|
const release = await this.mutex.acquire();
|
|
|
|
try {
|
|
if (!this.headerWritten) {
|
|
if (!meta?.decoderConfig) {
|
|
throw new Error('Decoder config is required for first audio sample.');
|
|
}
|
|
|
|
this.writeHeader(track, meta.decoderConfig);
|
|
this.headerWritten = true;
|
|
}
|
|
|
|
this.validateAndNormalizeTimestamp(track, sample.timestamp, sample.type === 'key');
|
|
|
|
if (sample.data) {
|
|
this.output._writer.write(sample.data);
|
|
this.dataSize += sample.data.byteLength;
|
|
}
|
|
|
|
await this.output._writer.flush();
|
|
} finally {
|
|
release();
|
|
}
|
|
}
|
|
|
|
async addSubtitleCue() {
|
|
throw new Error('WAVE format does not support subtitles.');
|
|
}
|
|
|
|
private writeHeader(track: OutputAudioTrack, config: AudioDecoderConfig) {
|
|
const codec = track.source._codec;
|
|
let format: WaveFormat;
|
|
let sampleSize: number;
|
|
|
|
const pcmInfo = parsePcmCodec(codec as PcmAudioCodec);
|
|
|
|
if (pcmInfo.dataType === 'ulaw') {
|
|
format = WaveFormat.MULAW;
|
|
sampleSize = 1;
|
|
} else if (pcmInfo.dataType === 'alaw') {
|
|
format = WaveFormat.ALAW;
|
|
sampleSize = 1;
|
|
} else {
|
|
format = pcmInfo.dataType === 'float' ? WaveFormat.IEEE_FLOAT : WaveFormat.PCM;
|
|
sampleSize = pcmInfo.sampleSize;
|
|
}
|
|
|
|
const channels = config.numberOfChannels;
|
|
const sampleRate = config.sampleRate;
|
|
const blockSize = sampleSize * channels;
|
|
|
|
// RIFF header
|
|
this.riffWriter.writeAscii('RIFF');
|
|
this.riffWriter.writeU32(0); // File size placeholder
|
|
this.riffWriter.writeAscii('WAVE');
|
|
|
|
// fmt chunk
|
|
this.riffWriter.writeAscii('fmt ');
|
|
this.riffWriter.writeU32(16); // Chunk size
|
|
this.riffWriter.writeU16(format);
|
|
this.riffWriter.writeU16(channels);
|
|
this.riffWriter.writeU32(sampleRate);
|
|
this.riffWriter.writeU32(sampleRate * blockSize); // Bytes per second
|
|
this.riffWriter.writeU16(blockSize);
|
|
this.riffWriter.writeU16(8 * sampleSize);
|
|
|
|
// data chunk
|
|
this.riffWriter.writeAscii('data');
|
|
this.riffWriter.writeU32(0); // Data size placeholder
|
|
}
|
|
|
|
async finalize() {
|
|
const currentPos = this.writer.getPos();
|
|
|
|
// Write file size
|
|
this.writer.seek(4);
|
|
this.riffWriter.writeU32(this.dataSize + 36); // File size - 8
|
|
|
|
// Write data chunk size
|
|
this.writer.seek(40);
|
|
this.riffWriter.writeU32(this.dataSize);
|
|
|
|
this.writer.seek(currentPos);
|
|
}
|
|
}
|