Add .gitignore to exclude all node packages and lock files
This commit is contained in:
Generated
Vendored
+3829
File diff suppressed because one or more lines are too long
Generated
Vendored
+3510
File diff suppressed because one or more lines are too long
Generated
Vendored
+3509
File diff suppressed because one or more lines are too long
Generated
Vendored
+3792
File diff suppressed because one or more lines are too long
Generated
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Registers AC-3 and E-AC-3 decoders, which Mediabunny will then use automatically when applicable. Make sure to call
|
||||
* this function before starting any decoding task.
|
||||
*
|
||||
* @group \@mediabunny/ac3
|
||||
* @public
|
||||
*/
|
||||
export declare const registerAc3Decoder: () => void;
|
||||
|
||||
/**
|
||||
* Registers AC-3 and E-AC-3 encoders, which Mediabunny will then use automatically when applicable. Make sure to call
|
||||
* this function before starting any encoding task.
|
||||
*
|
||||
* @group \@mediabunny/ac3
|
||||
* @public
|
||||
*/
|
||||
export declare const registerAc3Encoder: () => void;
|
||||
|
||||
export { }
|
||||
export as namespace MediabunnyAc3;
|
||||
Generated
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
export default Module;
|
||||
declare function Module(moduleArg?: {}): Promise<{}>;
|
||||
//# sourceMappingURL=ac3.d.ts.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ac3.d.ts","sourceRoot":"","sources":["../../../build/ac3.js"],"names":[],"mappings":";AAAA,qDACkB"}
|
||||
Generated
Vendored
BIN
Binary file not shown.
Generated
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
export {};
|
||||
//# sourceMappingURL=codec.worker.d.ts.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"codec.worker.d.ts","sourceRoot":"","sources":["../../../src/codec.worker.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
|
||||
Generated
Vendored
+269
@@ -0,0 +1,269 @@
|
||||
"use strict";
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const ac3_1 = __importDefault(require("../build/ac3"));
|
||||
let module;
|
||||
let modulePromise = null;
|
||||
let initDecoderFn;
|
||||
let configureDecodePacket;
|
||||
let decodePacket;
|
||||
let getDecodedFormat;
|
||||
let getDecodedPlanePtr;
|
||||
let getDecodedChannels;
|
||||
let getDecodedSampleRate;
|
||||
let getDecodedSampleCount;
|
||||
let getDecodedPts;
|
||||
let flushDecoderFn;
|
||||
let closeDecoderFn;
|
||||
let initEncoderFn;
|
||||
let getEncoderFrameSize;
|
||||
let getEncodeInputPtr;
|
||||
let encodeFrameFn;
|
||||
let flushEncoderFn;
|
||||
let getEncodedData;
|
||||
let getEncodedPts;
|
||||
let getEncodedDuration;
|
||||
let closeEncoderFn;
|
||||
const codecToId = (codec) => codec === 'ac3' ? 0 : 1;
|
||||
const ensureModule = async () => {
|
||||
if (!module) {
|
||||
if (modulePromise) {
|
||||
// If we don't do this we can have a race condition
|
||||
return modulePromise;
|
||||
}
|
||||
modulePromise = (0, ac3_1.default)();
|
||||
module = await modulePromise;
|
||||
modulePromise = null;
|
||||
initDecoderFn = module.cwrap('init_decoder', 'number', ['number']);
|
||||
configureDecodePacket = module.cwrap('configure_decode_packet', 'number', ['number', 'number']);
|
||||
decodePacket = module.cwrap('decode_packet', 'number', ['number', 'number']);
|
||||
getDecodedFormat = module.cwrap('get_decoded_format', 'number', ['number']);
|
||||
getDecodedPlanePtr = module.cwrap('get_decoded_plane_ptr', 'number', ['number', 'number']);
|
||||
getDecodedChannels = module.cwrap('get_decoded_channels', 'number', ['number']);
|
||||
getDecodedSampleRate = module.cwrap('get_decoded_sample_rate', 'number', ['number']);
|
||||
getDecodedSampleCount = module.cwrap('get_decoded_sample_count', 'number', ['number']);
|
||||
getDecodedPts = module.cwrap('get_decoded_pts', 'number', ['number']);
|
||||
flushDecoderFn = module.cwrap('flush_decoder', null, ['number']);
|
||||
closeDecoderFn = module.cwrap('close_decoder', null, ['number']);
|
||||
initEncoderFn = module.cwrap('init_encoder', 'number', ['number', 'number', 'number', 'number']);
|
||||
getEncoderFrameSize = module.cwrap('get_encoder_frame_size', 'number', ['number']);
|
||||
getEncodeInputPtr = module.cwrap('get_encode_input_ptr', 'number', ['number', 'number']);
|
||||
encodeFrameFn = module.cwrap('encode_frame', 'number', ['number', 'number']);
|
||||
flushEncoderFn = module.cwrap('flush_encoder', null, ['number']);
|
||||
getEncodedData = module.cwrap('get_encoded_data', 'number', ['number']);
|
||||
getEncodedPts = module.cwrap('get_encoded_pts', 'number', ['number']);
|
||||
getEncodedDuration = module.cwrap('get_encoded_duration', 'number', ['number']);
|
||||
closeEncoderFn = module.cwrap('close_encoder', null, ['number']);
|
||||
}
|
||||
};
|
||||
const initDecoder = async (codec) => {
|
||||
await ensureModule();
|
||||
const ctx = initDecoderFn(codecToId(codec));
|
||||
if (ctx === 0) {
|
||||
throw new Error('Failed to initialize AC3 decoder.');
|
||||
}
|
||||
return { ctx, frameSize: 0 };
|
||||
};
|
||||
// Keys are AVSampleFormat enum values
|
||||
const AV_FORMAT_MAP = {
|
||||
0: { format: 'u8', bytesPerSample: 1, planar: false },
|
||||
1: { format: 's16', bytesPerSample: 2, planar: false },
|
||||
2: { format: 's32', bytesPerSample: 4, planar: false },
|
||||
3: { format: 'f32', bytesPerSample: 4, planar: false },
|
||||
5: { format: 'u8-planar', bytesPerSample: 1, planar: true },
|
||||
6: { format: 's16-planar', bytesPerSample: 2, planar: true },
|
||||
7: { format: 's32-planar', bytesPerSample: 4, planar: true },
|
||||
8: { format: 'f32-planar', bytesPerSample: 4, planar: true },
|
||||
};
|
||||
const decode = (ctx, encodedData, timestamp) => {
|
||||
const bytes = new Uint8Array(encodedData);
|
||||
const dataPtr = configureDecodePacket(ctx, bytes.length);
|
||||
if (dataPtr === 0) {
|
||||
throw new Error('Failed to configure decode packet.');
|
||||
}
|
||||
module.HEAPU8.set(bytes, dataPtr);
|
||||
const ret = decodePacket(ctx, timestamp);
|
||||
if (ret < 0) {
|
||||
throw new Error(`Decode failed with error code ${ret}.`);
|
||||
}
|
||||
const avFormat = getDecodedFormat(ctx);
|
||||
const info = AV_FORMAT_MAP[avFormat];
|
||||
if (!info) {
|
||||
throw new Error(`Unsupported AVSampleFormat: ${avFormat}`);
|
||||
}
|
||||
const channels = getDecodedChannels(ctx);
|
||||
const sampleRate = getDecodedSampleRate(ctx);
|
||||
const sampleCount = getDecodedSampleCount(ctx);
|
||||
const pts = getDecodedPts(ctx);
|
||||
let pcmData;
|
||||
if (info.planar) {
|
||||
const planeSize = sampleCount * info.bytesPerSample;
|
||||
const buffer = new Uint8Array(planeSize * channels);
|
||||
for (let ch = 0; ch < channels; ch++) {
|
||||
const ptr = getDecodedPlanePtr(ctx, ch);
|
||||
buffer.set(module.HEAPU8.subarray(ptr, ptr + planeSize), ch * planeSize);
|
||||
}
|
||||
pcmData = buffer.buffer;
|
||||
}
|
||||
else {
|
||||
const totalSize = sampleCount * channels * info.bytesPerSample;
|
||||
const ptr = getDecodedPlanePtr(ctx, 0);
|
||||
pcmData = module.HEAPU8.slice(ptr, ptr + totalSize).buffer;
|
||||
}
|
||||
return { pcmData, format: info.format, channels, sampleRate, sampleCount, pts };
|
||||
};
|
||||
const initEncoder = async (codec, numberOfChannels, sampleRate, bitrate) => {
|
||||
await ensureModule();
|
||||
const ctx = initEncoderFn(codecToId(codec), numberOfChannels, sampleRate, bitrate);
|
||||
if (ctx === 0) {
|
||||
throw new Error('Failed to initialize AC3 encoder.');
|
||||
}
|
||||
return { ctx, frameSize: getEncoderFrameSize(ctx) };
|
||||
};
|
||||
const encode = (ctx, audioData, timestamp) => {
|
||||
const audioBytes = new Uint8Array(audioData);
|
||||
const inputPtr = getEncodeInputPtr(ctx, audioBytes.length);
|
||||
if (inputPtr === 0) {
|
||||
throw new Error('Failed to allocate encoder input buffer.');
|
||||
}
|
||||
module.HEAPU8.set(audioBytes, inputPtr);
|
||||
const bytesWritten = encodeFrameFn(ctx, timestamp);
|
||||
if (bytesWritten < 0) {
|
||||
throw new Error(`Encode failed with error code ${bytesWritten}.`);
|
||||
}
|
||||
const ptr = getEncodedData(ctx);
|
||||
const encodedData = module.HEAPU8.slice(ptr, ptr + bytesWritten).buffer;
|
||||
const pts = getEncodedPts(ctx);
|
||||
const duration = getEncodedDuration(ctx);
|
||||
return { encodedData, pts, duration };
|
||||
};
|
||||
const flushEncoder = (ctx) => {
|
||||
flushEncoderFn(ctx);
|
||||
};
|
||||
const onMessage = (data) => {
|
||||
const { id, command } = data;
|
||||
const handleCommand = async () => {
|
||||
try {
|
||||
let result;
|
||||
const transferables = [];
|
||||
switch (command.type) {
|
||||
case 'init-decoder':
|
||||
{
|
||||
const { ctx, frameSize } = await initDecoder(command.data.codec);
|
||||
result = { type: command.type, ctx, frameSize };
|
||||
}
|
||||
;
|
||||
break;
|
||||
case 'decode':
|
||||
{
|
||||
const decoded = decode(command.data.ctx, command.data.encodedData, command.data.timestamp);
|
||||
result = {
|
||||
type: command.type,
|
||||
pcmData: decoded.pcmData,
|
||||
format: decoded.format,
|
||||
channels: decoded.channels,
|
||||
sampleRate: decoded.sampleRate,
|
||||
sampleCount: decoded.sampleCount,
|
||||
pts: decoded.pts,
|
||||
};
|
||||
transferables.push(decoded.pcmData);
|
||||
}
|
||||
;
|
||||
break;
|
||||
case 'flush-decoder':
|
||||
{
|
||||
flushDecoderFn(command.data.ctx);
|
||||
result = { type: command.type };
|
||||
}
|
||||
;
|
||||
break;
|
||||
case 'close-decoder':
|
||||
{
|
||||
closeDecoderFn(command.data.ctx);
|
||||
result = { type: command.type };
|
||||
}
|
||||
;
|
||||
break;
|
||||
case 'init-encoder':
|
||||
{
|
||||
const { ctx, frameSize } = await initEncoder(command.data.codec, command.data.numberOfChannels, command.data.sampleRate, command.data.bitrate);
|
||||
result = { type: command.type, ctx, frameSize };
|
||||
}
|
||||
;
|
||||
break;
|
||||
case 'encode':
|
||||
{
|
||||
const encoded = encode(command.data.ctx, command.data.audioData, command.data.timestamp);
|
||||
result = {
|
||||
type: command.type,
|
||||
encodedData: encoded.encodedData,
|
||||
pts: encoded.pts,
|
||||
duration: encoded.duration,
|
||||
};
|
||||
transferables.push(encoded.encodedData);
|
||||
}
|
||||
;
|
||||
break;
|
||||
case 'flush-encoder':
|
||||
{
|
||||
flushEncoder(command.data.ctx);
|
||||
result = { type: command.type };
|
||||
}
|
||||
;
|
||||
break;
|
||||
case 'close-encoder':
|
||||
{
|
||||
closeEncoderFn(command.data.ctx);
|
||||
result = { type: command.type };
|
||||
}
|
||||
;
|
||||
break;
|
||||
}
|
||||
const response = {
|
||||
id,
|
||||
success: true,
|
||||
data: result,
|
||||
};
|
||||
sendMessage(response, transferables);
|
||||
}
|
||||
catch (error) {
|
||||
const response = {
|
||||
id,
|
||||
success: false,
|
||||
error,
|
||||
};
|
||||
sendMessage(response);
|
||||
}
|
||||
};
|
||||
void handleCommand();
|
||||
};
|
||||
const sendMessage = (data, transferables) => {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage(data, transferables ?? []);
|
||||
}
|
||||
else {
|
||||
self.postMessage(data, { transfer: transferables ?? [] });
|
||||
}
|
||||
};
|
||||
let parentPort = null;
|
||||
if (typeof self === 'undefined') {
|
||||
const workerModule = 'worker_threads';
|
||||
// eslint-disable-next-line @stylistic/max-len
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-member-access
|
||||
parentPort = require(workerModule).parentPort;
|
||||
}
|
||||
if (parentPort) {
|
||||
parentPort.on('message', onMessage);
|
||||
}
|
||||
else {
|
||||
self.addEventListener('message', event => onMessage(event.data));
|
||||
}
|
||||
Generated
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
/**
|
||||
* Registers AC-3 and E-AC-3 decoders, which Mediabunny will then use automatically when applicable. Make sure to call
|
||||
* this function before starting any decoding task.
|
||||
*
|
||||
* @group \@mediabunny/ac3
|
||||
* @public
|
||||
*/
|
||||
export declare const registerAc3Decoder: () => void;
|
||||
//# sourceMappingURL=decoder.d.ts.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"decoder.d.ts","sourceRoot":"","sources":["../../../src/decoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAsDH;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,YAE9B,CAAC"}
|
||||
Generated
Vendored
+61
@@ -0,0 +1,61 @@
|
||||
"use strict";
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.registerAc3Decoder = void 0;
|
||||
const mediabunny_1 = require("mediabunny");
|
||||
const worker_client_1 = require("./worker-client");
|
||||
class Ac3Decoder extends mediabunny_1.CustomAudioDecoder {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.ctx = 0;
|
||||
}
|
||||
static supports(codec) {
|
||||
return codec === 'ac3' || codec === 'eac3';
|
||||
}
|
||||
async init() {
|
||||
const result = await (0, worker_client_1.sendCommand)({
|
||||
type: 'init-decoder',
|
||||
data: { codec: this.codec },
|
||||
});
|
||||
this.ctx = result.ctx;
|
||||
}
|
||||
async decode(packet) {
|
||||
const encodedData = packet.data.slice().buffer;
|
||||
const timestamp = Math.round(packet.timestamp * this.config.sampleRate);
|
||||
const result = await (0, worker_client_1.sendCommand)({
|
||||
type: 'decode',
|
||||
data: { ctx: this.ctx, encodedData, timestamp },
|
||||
}, [encodedData]);
|
||||
const sample = new mediabunny_1.AudioSample({
|
||||
data: result.pcmData,
|
||||
format: result.format,
|
||||
numberOfChannels: result.channels,
|
||||
sampleRate: result.sampleRate,
|
||||
timestamp: result.pts / result.sampleRate,
|
||||
});
|
||||
this.onSample(sample);
|
||||
}
|
||||
async flush() {
|
||||
await (0, worker_client_1.sendCommand)({ type: 'flush-decoder', data: { ctx: this.ctx } });
|
||||
}
|
||||
close() {
|
||||
void (0, worker_client_1.sendCommand)({ type: 'close-decoder', data: { ctx: this.ctx } });
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Registers AC-3 and E-AC-3 decoders, which Mediabunny will then use automatically when applicable. Make sure to call
|
||||
* this function before starting any decoding task.
|
||||
*
|
||||
* @group \@mediabunny/ac3
|
||||
* @public
|
||||
*/
|
||||
const registerAc3Decoder = () => {
|
||||
(0, mediabunny_1.registerDecoder)(Ac3Decoder);
|
||||
};
|
||||
exports.registerAc3Decoder = registerAc3Decoder;
|
||||
Generated
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
/**
|
||||
* Registers AC-3 and E-AC-3 encoders, which Mediabunny will then use automatically when applicable. Make sure to call
|
||||
* this function before starting any encoding task.
|
||||
*
|
||||
* @group \@mediabunny/ac3
|
||||
* @public
|
||||
*/
|
||||
export declare const registerAc3Encoder: () => void;
|
||||
//# sourceMappingURL=encoder.d.ts.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../../src/encoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA+KH;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,YAE9B,CAAC"}
|
||||
Generated
Vendored
+151
@@ -0,0 +1,151 @@
|
||||
"use strict";
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.registerAc3Encoder = void 0;
|
||||
const mediabunny_1 = require("mediabunny");
|
||||
const worker_client_1 = require("./worker-client");
|
||||
const shared_1 = require("./shared");
|
||||
const ac3_misc_1 = require("../../../shared/ac3-misc");
|
||||
class Ac3Encoder extends mediabunny_1.CustomAudioEncoder {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.ctx = 0;
|
||||
this.encoderFrameSize = 0;
|
||||
this.sampleRate = 0;
|
||||
this.numberOfChannels = 0;
|
||||
this.chunkMetadata = {};
|
||||
// Accumulate interleaved f32 samples until we have a full frame
|
||||
this.pendingBuffer = new Float32Array(2 ** 16);
|
||||
this.pendingFrames = 0;
|
||||
this.nextSampleTimestampInSamples = null;
|
||||
this.nextPacketTimestampInSamples = null;
|
||||
}
|
||||
static supports(codec, config) {
|
||||
const sampleRates = codec === 'eac3'
|
||||
? [...ac3_misc_1.AC3_SAMPLE_RATES, ...ac3_misc_1.EAC3_REDUCED_SAMPLE_RATES]
|
||||
: ac3_misc_1.AC3_SAMPLE_RATES;
|
||||
return (codec === 'ac3' || codec === 'eac3')
|
||||
&& config.numberOfChannels >= 1
|
||||
&& config.numberOfChannels <= 8
|
||||
&& sampleRates.includes(config.sampleRate);
|
||||
}
|
||||
async init() {
|
||||
(0, shared_1.assert)(this.config.bitrate);
|
||||
this.sampleRate = this.config.sampleRate;
|
||||
this.numberOfChannels = this.config.numberOfChannels;
|
||||
const result = await (0, worker_client_1.sendCommand)({
|
||||
type: 'init-encoder',
|
||||
data: {
|
||||
codec: this.codec,
|
||||
numberOfChannels: this.config.numberOfChannels,
|
||||
sampleRate: this.config.sampleRate,
|
||||
bitrate: this.config.bitrate,
|
||||
},
|
||||
});
|
||||
this.ctx = result.ctx;
|
||||
this.encoderFrameSize = result.frameSize;
|
||||
this.resetInternalState();
|
||||
}
|
||||
resetInternalState() {
|
||||
this.pendingFrames = 0;
|
||||
this.nextSampleTimestampInSamples = null;
|
||||
this.nextPacketTimestampInSamples = null;
|
||||
this.chunkMetadata = {
|
||||
decoderConfig: {
|
||||
codec: this.codec === 'ac3' ? 'ac-3' : 'ec-3',
|
||||
numberOfChannels: this.config.numberOfChannels,
|
||||
sampleRate: this.config.sampleRate,
|
||||
},
|
||||
};
|
||||
}
|
||||
async encode(audioSample) {
|
||||
if (this.nextSampleTimestampInSamples === null) {
|
||||
this.nextSampleTimestampInSamples = Math.round(audioSample.timestamp * this.sampleRate);
|
||||
this.nextPacketTimestampInSamples = this.nextSampleTimestampInSamples;
|
||||
}
|
||||
const channels = this.numberOfChannels;
|
||||
const incomingFrames = audioSample.numberOfFrames;
|
||||
// Extract interleaved f32 data
|
||||
const totalBytes = audioSample.allocationSize({ format: 'f32', planeIndex: 0 });
|
||||
const audioBytes = new Uint8Array(totalBytes);
|
||||
audioSample.copyTo(audioBytes, { format: 'f32', planeIndex: 0 });
|
||||
const incomingData = new Float32Array(audioBytes.buffer);
|
||||
const requiredSamples = (this.pendingFrames + incomingFrames) * channels;
|
||||
if (requiredSamples > this.pendingBuffer.length) {
|
||||
let newSize = this.pendingBuffer.length;
|
||||
while (newSize < requiredSamples) {
|
||||
newSize *= 2;
|
||||
}
|
||||
const newBuffer = new Float32Array(newSize);
|
||||
newBuffer.set(this.pendingBuffer.subarray(0, this.pendingFrames * channels));
|
||||
this.pendingBuffer = newBuffer;
|
||||
}
|
||||
this.pendingBuffer.set(incomingData, this.pendingFrames * channels);
|
||||
this.pendingFrames += incomingFrames;
|
||||
while (this.pendingFrames >= this.encoderFrameSize) {
|
||||
await this.encodeOneFrame();
|
||||
}
|
||||
}
|
||||
async flush() {
|
||||
// Pad remaining samples with silence to fill a full frame
|
||||
if (this.pendingFrames > 0) {
|
||||
const channels = this.numberOfChannels;
|
||||
const frameSize = this.encoderFrameSize;
|
||||
const usedSamples = this.pendingFrames * channels;
|
||||
const frameSamples = frameSize * channels;
|
||||
this.pendingBuffer.fill(0, usedSamples, frameSamples);
|
||||
this.pendingFrames = frameSize;
|
||||
await this.encodeOneFrame();
|
||||
}
|
||||
await (0, worker_client_1.sendCommand)({ type: 'flush-encoder', data: { ctx: this.ctx } });
|
||||
this.resetInternalState();
|
||||
}
|
||||
close() {
|
||||
void (0, worker_client_1.sendCommand)({ type: 'close-encoder', data: { ctx: this.ctx } });
|
||||
}
|
||||
async encodeOneFrame() {
|
||||
(0, shared_1.assert)(this.nextSampleTimestampInSamples !== null);
|
||||
(0, shared_1.assert)(this.nextPacketTimestampInSamples !== null);
|
||||
const channels = this.numberOfChannels;
|
||||
const frameSize = this.encoderFrameSize;
|
||||
const frameSamples = frameSize * channels;
|
||||
const frameData = this.pendingBuffer.slice(0, frameSamples);
|
||||
// Shift remaining using copyWithin
|
||||
this.pendingFrames -= frameSize;
|
||||
if (this.pendingFrames > 0) {
|
||||
this.pendingBuffer.copyWithin(0, frameSamples, frameSamples + this.pendingFrames * channels);
|
||||
}
|
||||
const audioData = frameData.buffer;
|
||||
const result = await (0, worker_client_1.sendCommand)({
|
||||
type: 'encode',
|
||||
data: {
|
||||
ctx: this.ctx,
|
||||
audioData,
|
||||
timestamp: this.nextSampleTimestampInSamples,
|
||||
},
|
||||
}, [audioData]);
|
||||
this.nextSampleTimestampInSamples += frameSize;
|
||||
// We always get exactly one packet because we encode the correct frame size
|
||||
const packet = new mediabunny_1.EncodedPacket(new Uint8Array(result.encodedData), 'key', this.nextPacketTimestampInSamples / this.sampleRate, result.duration / this.sampleRate);
|
||||
this.nextPacketTimestampInSamples += result.duration;
|
||||
this.onPacket(packet, this.chunkMetadata);
|
||||
this.chunkMetadata = {};
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Registers AC-3 and E-AC-3 encoders, which Mediabunny will then use automatically when applicable. Make sure to call
|
||||
* this function before starting any encoding task.
|
||||
*
|
||||
* @group \@mediabunny/ac3
|
||||
* @public
|
||||
*/
|
||||
const registerAc3Encoder = () => {
|
||||
(0, mediabunny_1.registerEncoder)(Ac3Encoder);
|
||||
};
|
||||
exports.registerAc3Encoder = registerAc3Encoder;
|
||||
Generated
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
export { registerAc3Decoder } from './decoder';
|
||||
export { registerAc3Encoder } from './encoder';
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAaH,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC"}
|
||||
Generated
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.registerAc3Encoder = exports.registerAc3Decoder = void 0;
|
||||
const AC3_LOADED_SYMBOL = Symbol.for('@mediabunny/ac3 loaded');
|
||||
if (globalThis[AC3_LOADED_SYMBOL]) {
|
||||
console.error('[WARNING]\n@mediabunny/ac3 was loaded twice.'
|
||||
+ ' This will likely cause the encoder/decoder not to work correctly.'
|
||||
+ ' Check if multiple dependencies are importing different versions of @mediabunny/ac3,'
|
||||
+ ' or if something is being bundled incorrectly.');
|
||||
}
|
||||
globalThis[AC3_LOADED_SYMBOL] = true;
|
||||
var decoder_1 = require("./decoder");
|
||||
Object.defineProperty(exports, "registerAc3Decoder", { enumerable: true, get: function () { return decoder_1.registerAc3Decoder; } });
|
||||
var encoder_1 = require("./encoder");
|
||||
Object.defineProperty(exports, "registerAc3Encoder", { enumerable: true, get: function () { return encoder_1.registerAc3Encoder; } });
|
||||
Generated
Vendored
+96
@@ -0,0 +1,96 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
export type WorkerCommand = {
|
||||
type: 'init-decoder';
|
||||
data: {
|
||||
codec: string;
|
||||
};
|
||||
} | {
|
||||
type: 'decode';
|
||||
data: {
|
||||
ctx: number;
|
||||
encodedData: ArrayBuffer;
|
||||
timestamp: number;
|
||||
};
|
||||
} | {
|
||||
type: 'flush-decoder';
|
||||
data: {
|
||||
ctx: number;
|
||||
};
|
||||
} | {
|
||||
type: 'close-decoder';
|
||||
data: {
|
||||
ctx: number;
|
||||
};
|
||||
} | {
|
||||
type: 'init-encoder';
|
||||
data: {
|
||||
codec: string;
|
||||
numberOfChannels: number;
|
||||
sampleRate: number;
|
||||
bitrate: number;
|
||||
};
|
||||
} | {
|
||||
type: 'encode';
|
||||
data: {
|
||||
ctx: number;
|
||||
audioData: ArrayBuffer;
|
||||
timestamp: number;
|
||||
};
|
||||
} | {
|
||||
type: 'flush-encoder';
|
||||
data: {
|
||||
ctx: number;
|
||||
};
|
||||
} | {
|
||||
type: 'close-encoder';
|
||||
data: {
|
||||
ctx: number;
|
||||
};
|
||||
};
|
||||
export type WorkerResponseData = {
|
||||
type: 'init-decoder';
|
||||
ctx: number;
|
||||
frameSize: number;
|
||||
} | {
|
||||
type: 'decode';
|
||||
pcmData: ArrayBuffer;
|
||||
format: AudioSampleFormat;
|
||||
channels: number;
|
||||
sampleRate: number;
|
||||
sampleCount: number;
|
||||
pts: number;
|
||||
} | {
|
||||
type: 'flush-decoder';
|
||||
} | {
|
||||
type: 'close-decoder';
|
||||
} | {
|
||||
type: 'init-encoder';
|
||||
ctx: number;
|
||||
frameSize: number;
|
||||
} | {
|
||||
type: 'encode';
|
||||
encodedData: ArrayBuffer;
|
||||
pts: number;
|
||||
duration: number;
|
||||
} | {
|
||||
type: 'flush-encoder';
|
||||
} | {
|
||||
type: 'close-encoder';
|
||||
};
|
||||
export type WorkerResponse = {
|
||||
id: number;
|
||||
} & ({
|
||||
success: true;
|
||||
data: WorkerResponseData;
|
||||
} | {
|
||||
success: false;
|
||||
error: unknown;
|
||||
});
|
||||
export declare function assert(x: unknown): asserts x;
|
||||
//# sourceMappingURL=shared.d.ts.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../../src/shared.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,MAAM,aAAa,GAAG;IAC3B,IAAI,EAAE,cAAc,CAAC;IACrB,IAAI,EAAE;QACL,KAAK,EAAE,MAAM,CAAC;KACd,CAAC;CACF,GAAG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE;QACL,GAAG,EAAE,MAAM,CAAC;QACZ,WAAW,EAAE,WAAW,CAAC;QACzB,SAAS,EAAE,MAAM,CAAC;KAClB,CAAC;CACF,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;IACtB,IAAI,EAAE;QACL,GAAG,EAAE,MAAM,CAAC;KACZ,CAAC;CACF,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;IACtB,IAAI,EAAE;QACL,GAAG,EAAE,MAAM,CAAC;KACZ,CAAC;CACF,GAAG;IACH,IAAI,EAAE,cAAc,CAAC;IACrB,IAAI,EAAE;QACL,KAAK,EAAE,MAAM,CAAC;QACd,gBAAgB,EAAE,MAAM,CAAC;QACzB,UAAU,EAAE,MAAM,CAAC;QACnB,OAAO,EAAE,MAAM,CAAC;KAChB,CAAC;CACF,GAAG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE;QACL,GAAG,EAAE,MAAM,CAAC;QACZ,SAAS,EAAE,WAAW,CAAC;QACvB,SAAS,EAAE,MAAM,CAAC;KAClB,CAAC;CACF,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;IACtB,IAAI,EAAE;QACL,GAAG,EAAE,MAAM,CAAC;KACZ,CAAC;CACF,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;IACtB,IAAI,EAAE;QACL,GAAG,EAAE,MAAM,CAAC;KACZ,CAAC;CACF,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAChC,IAAI,EAAE,cAAc,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;CAClB,GAAG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,EAAE,WAAW,CAAC;IACrB,MAAM,EAAE,iBAAiB,CAAC;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACZ,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;CACtB,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;CACtB,GAAG;IACH,IAAI,EAAE,cAAc,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;CAClB,GAAG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf,WAAW,EAAE,WAAW,CAAC;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;CACjB,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;CACtB,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B,EAAE,EAAE,MAAM,CAAC;CACX,GAAG,CAAC;IACJ,OAAO,EAAE,IAAI,CAAC;IACd,IAAI,EAAE,kBAAkB,CAAC;CACzB,GAAG;IACH,OAAO,EAAE,KAAK,CAAC;IACf,KAAK,EAAE,OAAO,CAAC;CACf,CAAC,CAAC;AAEH,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAI5C"}
|
||||
Generated
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.assert = assert;
|
||||
function assert(x) {
|
||||
if (!x) {
|
||||
throw new Error('Assertion failed.');
|
||||
}
|
||||
}
|
||||
Generated
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
import { type WorkerCommand, type WorkerResponseData } from './shared';
|
||||
export declare const sendCommand: <T extends string>(command: WorkerCommand & {
|
||||
type: T;
|
||||
}, transferables?: Transferable[]) => Promise<WorkerResponseData & {
|
||||
type: T;
|
||||
}>;
|
||||
//# sourceMappingURL=worker-client.d.ts.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"worker-client.d.ts","sourceRoot":"","sources":["../../../src/worker-client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAU,KAAK,aAAa,EAAuB,KAAK,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAWpG,eAAO,MAAM,WAAW,GAAU,CAAC,SAAS,MAAM,EACjD,SAAS,aAAa,GAAG;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,EACpC,gBAAgB,YAAY,EAAE;UAIkB,CAAC;EAajD,CAAC"}
|
||||
Generated
Vendored
+61
@@ -0,0 +1,61 @@
|
||||
"use strict";
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.sendCommand = void 0;
|
||||
const shared_1 = require("./shared");
|
||||
// @ts-expect-error An esbuild plugin handles this, TypeScript doesn't need to understand
|
||||
const codec_worker_1 = __importDefault(require("./codec.worker"));
|
||||
let workerPromise;
|
||||
let nextMessageId = 0;
|
||||
const pendingMessages = new Map();
|
||||
const sendCommand = async (command, transferables) => {
|
||||
const worker = await ensureWorker();
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = nextMessageId++;
|
||||
pendingMessages.set(id, {
|
||||
resolve: resolve,
|
||||
reject,
|
||||
});
|
||||
if (transferables) {
|
||||
worker.postMessage({ id, command }, transferables);
|
||||
}
|
||||
else {
|
||||
worker.postMessage({ id, command });
|
||||
}
|
||||
});
|
||||
};
|
||||
exports.sendCommand = sendCommand;
|
||||
const ensureWorker = () => {
|
||||
return workerPromise ??= (async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
||||
const worker = (await (0, codec_worker_1.default)());
|
||||
const onMessage = (data) => {
|
||||
const pending = pendingMessages.get(data.id);
|
||||
(0, shared_1.assert)(pending !== undefined);
|
||||
pendingMessages.delete(data.id);
|
||||
if (data.success) {
|
||||
pending.resolve(data.data);
|
||||
}
|
||||
else {
|
||||
pending.reject(data.error);
|
||||
}
|
||||
};
|
||||
if (worker.addEventListener) {
|
||||
worker.addEventListener('message', event => onMessage(event.data));
|
||||
}
|
||||
else {
|
||||
const nodeWorker = worker;
|
||||
nodeWorker.on('message', onMessage);
|
||||
}
|
||||
return worker;
|
||||
})();
|
||||
};
|
||||
Generated
Vendored
+1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user