Socket
Socket
Sign inDemoInstall

@wasm-audio-decoders/common

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wasm-audio-decoders/common - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

2

package.json
{
"name": "@wasm-audio-decoders/common",
"version": "0.0.1",
"version": "1.0.0",
"description": "Web Assembly Audio Decoders Common",

@@ -5,0 +5,0 @@ "module": "index.js",

@@ -7,3 +7,3 @@ const compiledWasm = new WeakMap();

this._pointers = [];
this._pointers = new Set();
}

@@ -45,10 +45,6 @@

// output buffer
[this._leftPtr, this._leftArr] = common.allocateTypedArray(
this._outputPtrSize,
[this._outputPtr, this._output] = common.allocateTypedArray(
this._outputChannels * this._outputPtrSize,
Float32Array
);
[this._rightPtr, this._rightArr] = common.allocateTypedArray(
this._outputPtrSize,
Float32Array
);

@@ -88,2 +84,41 @@ return common;

static getDecodedAudioMultiChannel(
input,
channelsDecoded,
samplesDecoded,
sampleRate
) {
const channelData = [];
for (let i = 0; i < channelsDecoded; i++) {
const channel = [];
for (let j = 0; j < input.length; j++) {
channel.push(input[j][i]);
}
channelData.push(
WASMAudioDecoderCommon.concatFloat32(channel, samplesDecoded)
);
}
return WASMAudioDecoderCommon.getDecodedAudio(
channelData,
samplesDecoded,
sampleRate
);
}
getOutputChannels(outputData, channelsDecoded, samplesDecoded) {
const output = [];
for (let i = 0; i < channelsDecoded; i++)
output.push(
outputData.slice(
i * samplesDecoded,
i * samplesDecoded + samplesDecoded
)
);
return output;
}
allocateTypedArray(length, TypedArray) {

@@ -93,3 +128,3 @@ const pointer = this._wasm._malloc(TypedArray.BYTES_PER_ELEMENT * length);

this._pointers.push(pointer);
this._pointers.add(pointer);
return [pointer, array];

@@ -99,4 +134,4 @@ }

free() {
this._pointers.forEach((ptr) => this._wasm._free(ptr));
this._pointers = [];
for (const pointer of this._pointers) this._wasm._free(pointer);
this._pointers.clear();
}

@@ -103,0 +138,0 @@

import Worker from "web-worker";
import WASMAudioDecoderCommon from "./WASMAudioDecoderCommon.js";
// statically store web worker source code
const sources = new WeakMap();
export default class WASMAudioDecoderWorker extends Worker {
constructor(Decoder, EmscriptenWASM) {
let source = sources.get(Decoder);
constructor(options, Decoder, EmscriptenWASM) {
const webworkerSourceCode =
"'use strict';" +
// dependencies need to be manually resolved when stringifying this function
`(${((_options, _Decoder, _WASMAudioDecoderCommon, _EmscriptenWASM) => {
// We're in a Web Worker
_Decoder.WASMAudioDecoderCommon = _WASMAudioDecoderCommon;
_Decoder.EmscriptenWASM = _EmscriptenWASM;
_Decoder.isWebWorker = true;
if (!source) {
const webworkerSourceCode =
"'use strict';" +
// dependencies need to be manually resolved when stringifying this function
`(${((_WASMAudioDecoderCommon, _Decoder, _EmscriptenWASM) => {
// We're in a Web Worker
const decoder = new _Decoder(
_WASMAudioDecoderCommon,
_EmscriptenWASM
);
const decoder = new _Decoder(_options);
const detachBuffers = (buffer) =>
Array.isArray(buffer)
? buffer.map((buffer) => new Uint8Array(buffer))
: new Uint8Array(buffer);
const detachBuffers = (buffer) =>
Array.isArray(buffer)
? buffer.map((buffer) => new Uint8Array(buffer))
: new Uint8Array(buffer);
self.onmessage = ({ data: { id, command, data } }) => {
switch (command) {
case "ready":
decoder.ready.then(() => {
self.postMessage({
id,
});
self.onmessage = ({ data: { id, command, data } }) => {
switch (command) {
case "ready":
decoder.ready.then(() => {
self.postMessage({
id,
});
break;
case "free":
decoder.free();
});
break;
case "free":
decoder.free();
self.postMessage({
id,
});
break;
case "reset":
decoder.reset().then(() => {
self.postMessage({
id,
});
break;
case "reset":
decoder.reset().then(() => {
self.postMessage({
id,
});
});
break;
case "decode":
case "decodeFrame":
case "decodeFrames":
const { channelData, samplesDecoded, sampleRate } = decoder[
command
](detachBuffers(data));
});
break;
case "decode":
case "decodeFrame":
case "decodeFrames":
const { channelData, samplesDecoded, sampleRate } = decoder[
command
](detachBuffers(data));
self.postMessage(
{
id,
channelData,
samplesDecoded,
sampleRate,
},
// The "transferList" parameter transfers ownership of channel data to main thread,
// which avoids copying memory.
channelData.map((channel) => channel.buffer)
);
break;
default:
this.console.error(
"Unknown command sent to worker: " + command
);
}
};
}).toString()})(${WASMAudioDecoderCommon}, ${Decoder}, ${EmscriptenWASM})`;
self.postMessage(
{
id,
channelData,
samplesDecoded,
sampleRate,
},
// The "transferList" parameter transfers ownership of channel data to main thread,
// which avoids copying memory.
channelData.map((channel) => channel.buffer)
);
break;
default:
this.console.error("Unknown command sent to worker: " + command);
}
};
}).toString()})(${JSON.stringify(
options
)}, ${Decoder}, ${WASMAudioDecoderCommon}, ${EmscriptenWASM})`;
const type = "text/javascript";
const type = "text/javascript";
let source;
try {
// browser
source = URL.createObjectURL(new Blob([webworkerSourceCode], { type }));
} catch {
// nodejs
source = `data:${type};base64,${Buffer.from(
webworkerSourceCode
).toString("base64")}`;
}
sources.set(Decoder, source);
try {
// browser
source = URL.createObjectURL(new Blob([webworkerSourceCode], { type }));
} catch {
// nodejs
source = `data:${type};base64,${Buffer.from(webworkerSourceCode).toString(
"base64"
)}`;
}

@@ -90,0 +83,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc