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
+12 -3
View File
@@ -146,7 +146,10 @@ const onMessage = (data: { id: number; command: WorkerCommand }) => {
command.data.sampleRate,
command.data.bitrate,
);
result = { success: true };
result = {
type: command.type,
success: true,
};
}; break;
case 'encode': {
@@ -154,13 +157,19 @@ const onMessage = (data: { id: number; command: WorkerCommand }) => {
command.data.audioData,
command.data.numberOfFrames,
);
result = { encodedData };
result = {
type: command.type,
encodedData,
};
transferables.push(encodedData);
}; break;
case 'flush': {
const flushedData = flush();
result = { flushedData };
result = {
type: command.type,
flushedData,
};
transferables.push(flushedData);
}; break;
}
+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);
+3
View File
@@ -24,10 +24,13 @@ export type WorkerCommand = {
};
export type WorkerResponseData = {
type: 'init';
success: boolean;
} | {
type: 'encode';
encodedData: ArrayBuffer;
} | {
type: 'flush';
flushedData: ArrayBuffer;
};
-23
View File
@@ -1,23 +0,0 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "../dist/modules",
"declaration": true,
"declarationMap": true,
"stripInternal": true,
"noEmit": false,
"moduleResolution": "nodenext",
"module": "nodenext",
"allowJs": true,
"paths": {
"mediabunny": ["../../../src/index.ts"],
},
},
"include": [
"**/*",
"../../../shared/**/*"
],
"references": [
{ "path": "../../../src" }
]
}