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
+1 -3
View File
@@ -102,8 +102,6 @@ The WASM build itself is a performance-optimized, SIMD-enabled build of LAME 3.1
## Building and development
Building this library is done using the build commands in the [Mediabunny root](https://github.com/Vanilagy/mediabunny).
For simplicity, all built WASM artifacts are included in the repo, since these rarely change. However, here are the instructions for building them from scratch:
### Prerequisites
@@ -148,7 +146,7 @@ emcc src/lame-bridge.c build/libmp3lame.a \
-o build/lame.js
```
This generates `build/lame.js`, which contains both the JavaScript "glue code" as well as the compiled WASM encoded with Base64.
This generates `build/lame.js`, which contains both the JavaScript "glue code" as well as the compiled WASM inlined.
### Building the package
+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;
};
@@ -1,7 +1,8 @@
{
"extends": "../../../tsconfig.json",
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../dist/modules",
"composite": true,
"outDir": "./dist/modules",
"declaration": true,
"declarationMap": true,
"stripInternal": true,
@@ -10,14 +11,15 @@
"module": "nodenext",
"allowJs": true,
"paths": {
"mediabunny": ["../../../src/index.ts"],
"mediabunny": ["../../src/index.ts"],
},
},
"include": [
"**/*",
"../../../shared/**/*"
"./src/**/*",
"./build/**/*",
"../../shared/**/*",
],
"references": [
{ "path": "../../../src" }
{ "path": "../../src" }
]
}
}
+4
View File
@@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
"extends": ["../../tsdoc.json"]
}