@wasm-audio-decoders/common
Advanced tools
Comparing version 0.0.1 to 1.0.0
{ | ||
"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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18200
535
0