mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 10:53:50 +02:00
Add @mediabunny/ac3 extension package
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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" }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
|
||||
"extends": ["../../tsdoc.json"]
|
||||
}
|
||||
Reference in New Issue
Block a user