Add @mediabunny/ac3 extension package

This commit is contained in:
Vanilagy
2026-02-12 18:31:59 +01:00
parent e3aa7108f5
commit a97200f3f0
46 changed files with 2083 additions and 71 deletions
+16 -9
View File
@@ -80,6 +80,12 @@ class Mp3Encoder extends CustomAudioEncoder {
},
});
this.resetInternalState();
}
private resetInternalState() {
this.currentBufferOffset = 0;
this.currentTimestamp = null;
this.chunkMetadata = {
decoderConfig: {
codec: 'mp3',
@@ -119,15 +125,15 @@ class Mp3Encoder extends CustomAudioEncoder {
},
}, [audioData]);
assert('encodedData' in result);
this.digestOutput(new Uint8Array(result.encodedData));
}
async flush() {
const result = await this.sendCommand({ type: 'flush' });
assert('flushedData' in result);
this.digestOutput(new Uint8Array(result.flushedData));
this.resetInternalState();
}
close() {
@@ -171,9 +177,7 @@ class Mp3Encoder extends CustomAudioEncoder {
const duration = header.audioSamplesInFrame / header.sampleRate;
this.onPacket(new EncodedPacket(data, 'key', this.currentTimestamp, duration), this.chunkMetadata);
if (this.currentTimestamp === 0) {
this.chunkMetadata = {}; // Mimic WebCodecs-like behavior
}
this.chunkMetadata = {}; // Mimic WebCodecs-like behavior
this.currentTimestamp += duration;
pos += header.totalSize;
@@ -186,13 +190,16 @@ class Mp3Encoder extends CustomAudioEncoder {
}
}
private sendCommand(
command: WorkerCommand,
private sendCommand<T extends string>(
command: WorkerCommand & { type: T },
transferables?: Transferable[],
) {
return new Promise<WorkerResponseData>((resolve, reject) => {
return new Promise<WorkerResponseData & { type: T }>((resolve, reject) => {
const id = this.nextMessageId++;
this.pendingMessages.set(id, { resolve, reject });
this.pendingMessages.set(id, {
resolve: resolve as (value: WorkerResponseData) => void,
reject,
});
assert(this.worker);