opus-decoder
Advanced tools
Comparing version 0.3.2 to 0.3.3
{ | ||
"name": "opus-decoder", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Web Assembly streaming Opus decoder", | ||
@@ -5,0 +5,0 @@ "main": "dist/opus-decoder.min.js", |
# `opus-decoder` | ||
`opus-decoder` is a Web Assembly Opus audio decoder. | ||
* 87.4 KiB minified bundle size | ||
* 87.2 KiB minified bundle size | ||
* Browser and NodeJS support | ||
@@ -6,0 +6,0 @@ * Built in Web Worker support |
@@ -25,5 +25,5 @@ import OpusDecodedAudio from "./OpusDecodedAudio.js"; | ||
_createOutputArray(length) { | ||
const pointer = this._api._malloc(Float32Array.BYTES_PER_ELEMENT * length); | ||
const array = new Float32Array(this._api.HEAPF32.buffer, pointer, length); | ||
_allocateTypedArray(length, TypedArray) { | ||
const pointer = this._api._malloc(TypedArray.BYTES_PER_ELEMENT * length); | ||
const array = new TypedArray(this._api.HEAP, pointer, length); | ||
return [pointer, array]; | ||
@@ -60,6 +60,16 @@ } | ||
// max size of stereo Opus packet 120ms @ 510kbs | ||
this._dataPtr = this._api._malloc((0.12 * 510000) / 8); | ||
[this._inputPtr, this._input] = this._allocateTypedArray( | ||
(0.12 * 510000) / 8, | ||
Uint8Array | ||
); | ||
// max audio output of Opus packet 120ms @ 48000Hz | ||
[this._leftPtr, this._leftArr] = this._createOutputArray(120 * 48); | ||
[this._rightPtr, this._rightArr] = this._createOutputArray(120 * 48); | ||
[this._leftPtr, this._leftArr] = this._allocateTypedArray( | ||
120 * 48, | ||
Float32Array | ||
); | ||
[this._rightPtr, this._rightArr] = this._allocateTypedArray( | ||
120 * 48, | ||
Float32Array | ||
); | ||
} | ||
@@ -79,3 +89,3 @@ | ||
this._api._free(this._dataPtr); | ||
this._api._free(this._inputPtr); | ||
this._api._free(this._leftPtr); | ||
@@ -91,7 +101,7 @@ this._api._free(this._rightPtr); | ||
this._api.HEAPU8.set(opusFrame, this._dataPtr); | ||
this._input.set(opusFrame); | ||
const samplesDecoded = this._api._opus_frame_decode_float_deinterleaved( | ||
this._decoder, | ||
this._dataPtr, | ||
this._inputPtr, | ||
opusFrame.length, | ||
@@ -98,0 +108,0 @@ this._leftPtr, |
@@ -10,61 +10,63 @@ import Worker from "web-worker"; | ||
constructor() { | ||
const webworkerSourceCode = | ||
"'use strict';" + | ||
// dependencies need to be manually resolved when stringifying this function | ||
`(${((_OpusDecoder, _OpusDecodedAudio, _EmscriptenWASM) => { | ||
// We're in a Web Worker | ||
const decoder = new _OpusDecoder(_OpusDecodedAudio, _EmscriptenWASM); | ||
if (!sourceURL) { | ||
const webworkerSourceCode = | ||
"'use strict';" + | ||
// dependencies need to be manually resolved when stringifying this function | ||
`(${((_OpusDecoder, _OpusDecodedAudio, _EmscriptenWASM) => { | ||
// We're in a Web Worker | ||
const decoder = new _OpusDecoder(_OpusDecodedAudio, _EmscriptenWASM); | ||
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, opusData } }) => { | ||
switch (command) { | ||
case "ready": | ||
decoder.ready.then(() => { | ||
self.postMessage({ | ||
id, | ||
self.onmessage = ({ data: { id, command, opusData } }) => { | ||
switch (command) { | ||
case "ready": | ||
decoder.ready.then(() => { | ||
self.postMessage({ | ||
id, | ||
}); | ||
}); | ||
}); | ||
break; | ||
case "free": | ||
decoder.free(); | ||
self.postMessage({ | ||
id, | ||
}); | ||
break; | ||
case "reset": | ||
decoder.reset().then(() => { | ||
break; | ||
case "free": | ||
decoder.free(); | ||
self.postMessage({ | ||
id, | ||
}); | ||
}); | ||
break; | ||
case "decodeFrame": | ||
case "decodeFrames": | ||
const { channelData, samplesDecoded, sampleRate } = decoder[ | ||
command | ||
](detachBuffers(opusData)); | ||
break; | ||
case "reset": | ||
decoder.reset().then(() => { | ||
self.postMessage({ | ||
id, | ||
}); | ||
}); | ||
break; | ||
case "decodeFrame": | ||
case "decodeFrames": | ||
const { channelData, samplesDecoded, sampleRate } = decoder[ | ||
command | ||
](detachBuffers(opusData)); | ||
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()})(${OpusDecoder}, ${OpusDecodedAudio}, ${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()})(${OpusDecoder}, ${OpusDecodedAudio}, ${EmscriptenWASM})`; | ||
if (!sourceURL) { | ||
const type = "text/javascript"; | ||
@@ -71,0 +73,0 @@ try { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
1317
212298