opus-decoder
Advanced tools
Comparing version 0.3.0 to 0.3.1
{ | ||
"name": "opus-decoder", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"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.2 KiB minified bundle size | ||
* 87.4 KiB minified bundle size | ||
* Browser and NodeJS support | ||
* Built in Web Worker support | ||
@@ -6,0 +7,0 @@ * Based on [`libopus`](https://github.com/xiph/opus) |
@@ -7,4 +7,6 @@ import OpusDecodedAudio from "./OpusDecodedAudio.js"; | ||
export default class OpusDecoder { | ||
constructor() { | ||
this._ready = new Promise((resolve) => this._init().then(resolve)); | ||
constructor(_OpusDecodedAudio, _EmscriptenWASM) { | ||
this._ready = new Promise((resolve) => | ||
this._init(_OpusDecodedAudio, _EmscriptenWASM).then(resolve) | ||
); | ||
} | ||
@@ -30,19 +32,22 @@ | ||
async _init() { | ||
// injects dependencies when running as a web worker | ||
async _init(_OpusDecodedAudio, _EmscriptenWASM) { | ||
if (!this._api) { | ||
let isMainThread; | ||
const isWebWorker = _OpusDecodedAudio && _EmscriptenWASM; | ||
try { | ||
if (wasm || !wasm) isMainThread = true; | ||
} catch { | ||
isMainThread = false; | ||
} | ||
if (isWebWorker) { | ||
// use classes injected into constructor parameters | ||
this._OpusDecodedAudio = _OpusDecodedAudio; | ||
this._EmscriptenWASM = _EmscriptenWASM; | ||
if (isMainThread) { | ||
// running as a webworker, use class level singleton for wasm compilation | ||
this._api = new this._EmscriptenWASM(); | ||
} else { | ||
// use classes from es6 imports | ||
this._OpusDecodedAudio = OpusDecodedAudio; | ||
this._EmscriptenWASM = EmscriptenWASM; | ||
// use a global scope singleton so wasm compilation happens once only if class is instantiated | ||
if (!wasm) wasm = new EmscriptenWASM(); | ||
if (!wasm) wasm = new this._EmscriptenWASM(); | ||
this._api = wasm; | ||
} else { | ||
// running as a webworker, use class level singleton for wasm compilation | ||
this._api = new EmscriptenWASM(); | ||
} | ||
@@ -95,3 +100,3 @@ } | ||
return new OpusDecodedAudio( | ||
return new this._OpusDecodedAudio( | ||
[ | ||
@@ -118,3 +123,3 @@ this._leftArr.slice(0, samplesDecoded), | ||
return new OpusDecodedAudio( | ||
return new this._OpusDecodedAudio( | ||
[ | ||
@@ -121,0 +126,0 @@ OpusDecoder.concatFloat32(left, samples), |
@@ -10,8 +10,6 @@ import Worker from "web-worker"; | ||
"'use strict';" + | ||
EmscriptenWASM.toString() + | ||
OpusDecodedAudio.toString() + | ||
OpusDecoder.toString() + | ||
`(${(() => { | ||
// dependencies need to be manually resolved when stringifying this function | ||
`(${((_OpusDecoder, _OpusDecodedAudio, _EmscriptenWASM) => { | ||
// We're in a Web Worker | ||
const decoder = new OpusDecoder(); | ||
const decoder = new _OpusDecoder(_OpusDecodedAudio, _EmscriptenWASM); | ||
@@ -67,3 +65,3 @@ const detachBuffers = (buffer) => | ||
}; | ||
}).toString()})()`; | ||
}).toString()})(${OpusDecoder}, ${OpusDecodedAudio}, ${EmscriptenWASM})`; | ||
@@ -70,0 +68,0 @@ const type = "text/javascript"; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
212675
1312
164
0