@wasm-audio-decoders/common
Advanced tools
Comparing version 4.0.0 to 4.0.1
{ | ||
"name": "@wasm-audio-decoders/common", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "Web Assembly Audio Decoders Common", | ||
@@ -5,0 +5,0 @@ "module": "index.js", |
export default function WASMAudioDecoderCommon(caller) { | ||
// setup static methods | ||
const uint8Array = Uint8Array; | ||
const uint16Array = Uint16Array; | ||
const float32Array = Float32Array; | ||
if (!WASMAudioDecoderCommon.concatFloat32) { | ||
Object.defineProperties(WASMAudioDecoderCommon, { | ||
concatFloat32: { | ||
value: (buffers, length) => { | ||
const ret = new Float32Array(length); | ||
value(buffers, length) { | ||
let ret = new float32Array(length), | ||
i = 0, | ||
offset = 0; | ||
for (let i = 0, offset = 0; i < buffers.length; i++) { | ||
while (i < buffers.length) { | ||
ret.set(buffers[i], offset); | ||
offset += buffers[i].length; | ||
offset += buffers[i++].length; | ||
} | ||
@@ -19,20 +25,18 @@ | ||
getDecodedAudio: { | ||
value: (channelData, samplesDecoded, sampleRate) => { | ||
return { | ||
channelData, | ||
samplesDecoded, | ||
sampleRate, | ||
}; | ||
}, | ||
value: (channelData, samplesDecoded, sampleRate) => ({ | ||
channelData, | ||
samplesDecoded, | ||
sampleRate, | ||
}), | ||
}, | ||
getDecodedAudioMultiChannel: { | ||
value: (input, channelsDecoded, samplesDecoded, sampleRate) => { | ||
const channelData = []; | ||
value(input, channelsDecoded, samplesDecoded, sampleRate) { | ||
let channelData = [], | ||
i, | ||
j; | ||
for (let i = 0; i < channelsDecoded; i++) { | ||
for (i = 0; i < channelsDecoded; i++) { | ||
const channel = []; | ||
for (let j = 0; j < input.length; j++) { | ||
channel.push(input[j][i]); | ||
} | ||
for (j = 0; j < input.length; ) channel.push(input[j++][i]); | ||
channelData.push( | ||
@@ -58,4 +62,4 @@ WASMAudioDecoderCommon.concatFloat32(channel, samplesDecoded) | ||
inflateDynEncodeString: { | ||
value: (source, dest) => { | ||
const output = new Uint8Array(source.length); | ||
value(source, dest) { | ||
const output = new uint8Array(source.length); | ||
const offset = parseInt(source.substring(11, 13), 16); | ||
@@ -66,6 +70,7 @@ const offsetReverse = 256 - offset; | ||
byteIndex = 0, | ||
byte; | ||
byte, | ||
i = 13; | ||
for (let i = 13; i < source.length; i++) { | ||
byte = source.charCodeAt(i); | ||
while (i < source.length) { | ||
byte = source.charCodeAt(i++); | ||
@@ -94,11 +99,13 @@ if (byte === 61 && !escaped) { | ||
inflate: { | ||
value: (source, dest) => { | ||
value(source, dest) { | ||
const TINF_OK = 0; | ||
const TINF_DATA_ERROR = -3; | ||
const _16 = 16, | ||
_24 = 24, | ||
_30 = 30, | ||
_144 = 144, | ||
_256 = 256; | ||
const uint8Array = Uint8Array; | ||
const uint16Array = Uint16Array; | ||
function Tree() { | ||
this.t = new uint16Array(16); /* table of code length counts */ | ||
this.t = new uint16Array(_16); /* table of code length counts */ | ||
this.trans = new uint16Array( | ||
@@ -130,12 +137,30 @@ 288 | ||
/* extra bits and base tables for length codes */ | ||
const length_bits = new uint8Array(30); | ||
const length_base = new uint16Array(30); | ||
const length_bits = new uint8Array(_30); | ||
const length_base = new uint16Array(_30); | ||
/* extra bits and base tables for distance codes */ | ||
const dist_bits = new uint8Array(30); | ||
const dist_base = new uint16Array(30); | ||
const dist_bits = new uint8Array(_30); | ||
const dist_base = new uint16Array(_30); | ||
/* special ordering of code length codes */ | ||
const clcidx = new uint8Array([ | ||
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, | ||
_16, | ||
17, | ||
18, | ||
0, | ||
8, | ||
7, | ||
9, | ||
6, | ||
10, | ||
5, | ||
11, | ||
4, | ||
12, | ||
3, | ||
13, | ||
2, | ||
14, | ||
1, | ||
15, | ||
]); | ||
@@ -156,9 +181,9 @@ | ||
/* build bits table */ | ||
for (i = 0; i < delta; ++i) bits[i] = 0; | ||
for (i = 0; i < 30 - delta; ++i) bits[i + delta] = (i / delta) | 0; | ||
for (i = 0; i < delta; ) bits[i++] = 0; | ||
for (i = 0; i < _30 - delta; ) bits[i + delta] = (i++ / delta) | 0; | ||
/* build base table */ | ||
for (sum = first, i = 0; i < 30; ++i) { | ||
for (sum = first, i = 0; i < _30; ) { | ||
base[i] = sum; | ||
sum += 1 << bits[i]; | ||
sum += 1 << bits[i++]; | ||
} | ||
@@ -172,23 +197,23 @@ }; | ||
/* build fixed length tree */ | ||
for (i = 0; i < 7; ++i) lt.t[i] = 0; | ||
for (i = 0; i < 7; ) lt.t[i++] = 0; | ||
lt.t[7] = 24; | ||
lt.t[7] = _24; | ||
lt.t[8] = 152; | ||
lt.t[9] = 112; | ||
for (i = 0; i < 24; ++i) lt.trans[i] = 256 + i; | ||
for (i = 0; i < 144; ++i) lt.trans[24 + i] = i; | ||
for (i = 0; i < 8; ++i) lt.trans[24 + 144 + i] = 280 + i; | ||
for (i = 0; i < 112; ++i) lt.trans[24 + 144 + 8 + i] = 144 + i; | ||
for (i = 0; i < _24; ) lt.trans[i] = _256 + i++; | ||
for (i = 0; i < _144; ) lt.trans[_24 + i] = i++; | ||
for (i = 0; i < 8; ) lt.trans[_24 + _144 + i] = 280 + i++; | ||
for (i = 0; i < 112; ) lt.trans[_24 + _144 + 8 + i] = _144 + i++; | ||
/* build fixed distance tree */ | ||
for (i = 0; i < 5; ++i) dt.t[i] = 0; | ||
for (i = 0; i < 5; ) dt.t[i++] = 0; | ||
dt.t[5] = 32; | ||
for (i = 0; i < 32; ++i) dt.trans[i] = i; | ||
for (i = 0; i < 32; ) dt.trans[i] = i++; | ||
}; | ||
/* given an array of code lengths, build a tree */ | ||
const offs = new uint16Array(16); | ||
const offs = new uint16Array(_16); | ||
@@ -199,6 +224,6 @@ const tinf_build_tree = (t, lengths, off, num) => { | ||
/* clear code length count table */ | ||
for (i = 0; i < 16; ++i) t.t[i] = 0; | ||
for (i = 0; i < _16; ) t.t[i++] = 0; | ||
/* scan symbol lengths, and sum code length counts */ | ||
for (i = 0; i < num; ++i) t.t[lengths[off + i]]++; | ||
for (i = 0; i < num; ) t.t[lengths[off + i++]]++; | ||
@@ -208,11 +233,10 @@ t.t[0] = 0; | ||
/* compute offset table for distribution sort */ | ||
for (sum = 0, i = 0; i < 16; ++i) { | ||
for (sum = 0, i = 0; i < _16; ) { | ||
offs[i] = sum; | ||
sum += t.t[i]; | ||
sum += t.t[i++]; | ||
} | ||
/* create code->symbol translation table (symbols sorted by code) */ | ||
for (i = 0; i < num; ++i) { | ||
for (i = 0; i < num; ++i) | ||
if (lengths[off + i]) t.trans[offs[lengths[off + i]]++] = i; | ||
} | ||
}; | ||
@@ -244,3 +268,3 @@ | ||
while (d.bitcount < 24) { | ||
while (d.bitcount < _24) { | ||
d.t |= d.s[d.i++] << d.bitcount; | ||
@@ -250,3 +274,3 @@ d.bitcount += 8; | ||
const val = d.t & (0xffff >>> (16 - num)); | ||
const val = d.t & (65535 >>> (_16 - num)); | ||
d.t >>>= num; | ||
@@ -259,3 +283,3 @@ d.bitcount -= num; | ||
const tinf_decode_symbol = (d, t) => { | ||
while (d.bitcount < 24) { | ||
while (d.bitcount < _24) { | ||
d.t |= d.s[d.i++] << d.bitcount; | ||
@@ -288,3 +312,5 @@ d.bitcount += 8; | ||
const tinf_decode_trees = (d, lt, dt) => { | ||
let i, length; | ||
let i, | ||
length, | ||
num = 0; | ||
@@ -300,9 +326,9 @@ /* get 5 bits HLIT (257-286) */ | ||
for (i = 0; i < 19; ++i) lengths[i] = 0; | ||
for (i = 0; i < 19; ) lengths[i++] = 0; | ||
/* read code lengths for code length alphabet */ | ||
for (i = 0; i < hclen; ++i) { | ||
for (i = 0; i < hclen; ) { | ||
/* get 3 bits code length (0-7) */ | ||
const clen = tinf_read_bits(d, 3, 0); | ||
lengths[clcidx[i]] = clen; | ||
lengths[clcidx[i++]] = clen; | ||
} | ||
@@ -314,24 +340,21 @@ | ||
/* decode code lengths for the dynamic trees */ | ||
for (let num = 0; num < hlit + hdist; ) { | ||
while (num < hlit + hdist) { | ||
const sym = tinf_decode_symbol(d, code_tree); | ||
switch (sym) { | ||
case 16: | ||
case _16: | ||
/* copy previous code length 3-6 times (read 2 bits) */ | ||
const prev = lengths[num - 1]; | ||
for (length = tinf_read_bits(d, 2, 3); length; --length) { | ||
lengths[num++] = prev; | ||
} | ||
length = tinf_read_bits(d, 2, 3); | ||
while (length--) lengths[num++] = prev; | ||
break; | ||
case 17: | ||
/* repeat code length 0 for 3-10 times (read 3 bits) */ | ||
for (length = tinf_read_bits(d, 3, 3); length; --length) { | ||
lengths[num++] = 0; | ||
} | ||
length = tinf_read_bits(d, 3, 3); | ||
while (length--) lengths[num++] = 0; | ||
break; | ||
case 18: | ||
/* repeat code length 0 for 11-138 times (read 7 bits) */ | ||
for (length = tinf_read_bits(d, 7, 11); length; --length) { | ||
lengths[num++] = 0; | ||
} | ||
length = tinf_read_bits(d, 7, 11); | ||
while (length--) lengths[num++] = 0; | ||
break; | ||
@@ -360,7 +383,5 @@ default: | ||
/* check for end of block */ | ||
if (sym === 256) { | ||
return TINF_OK; | ||
} | ||
if (sym === _256) return TINF_OK; | ||
if (sym < 256) { | ||
if (sym < _256) { | ||
d.dest[d.destLen++] = sym; | ||
@@ -383,4 +404,4 @@ } else { | ||
/* copy match */ | ||
for (let i = offs; i < offs + length; ++i) { | ||
d.dest[d.destLen++] = d.dest[i]; | ||
for (let i = offs; i < offs + length; ) { | ||
d.dest[d.destLen++] = d.dest[i++]; | ||
} | ||
@@ -403,10 +424,10 @@ } | ||
length = d.s[d.i + 1]; | ||
length = 256 * length + d.s[d.i]; | ||
length = _256 * length + d.s[d.i]; | ||
/* get one's complement of length */ | ||
invlength = d.s[d.i + 3]; | ||
invlength = 256 * invlength + d.s[d.i + 2]; | ||
invlength = _256 * invlength + d.s[d.i + 2]; | ||
/* check length */ | ||
if (length !== (~invlength & 0x0000ffff)) return TINF_DATA_ERROR; | ||
if (length !== (~invlength & 65535)) return TINF_DATA_ERROR; | ||
@@ -416,3 +437,3 @@ d.i += 4; | ||
/* copy block */ | ||
for (let i = length; i; --i) d.dest[d.destLen++] = d.s[d.i++]; | ||
while (length--) d.dest[d.destLen++] = d.s[d.i++]; | ||
@@ -472,9 +493,5 @@ /* make sure we start next block on a byte boundary */ | ||
if (d.destLen < d.dest.length) { | ||
if (typeof d.dest.slice === "function") | ||
return d.dest.slice(0, d.destLen); | ||
else return d.dest.subarray(0, d.destLen); | ||
} | ||
return d.dest; | ||
return d.destLen < d.dest.length | ||
? d.dest.subarray(0, d.destLen) | ||
: d.dest; | ||
}, | ||
@@ -491,9 +508,10 @@ }, | ||
this.getOutputChannels = (outputData, channelsDecoded, samplesDecoded) => { | ||
const output = []; | ||
let output = [], | ||
i = 0; | ||
for (let i = 0; i < channelsDecoded; i++) | ||
while (i < channelsDecoded) | ||
output.push( | ||
outputData.slice( | ||
i * samplesDecoded, | ||
i * samplesDecoded + samplesDecoded | ||
i++ * samplesDecoded + samplesDecoded | ||
) | ||
@@ -517,4 +535,5 @@ ); | ||
this.free = () => { | ||
for (let i = 0; i < this._pointers.length; i++) | ||
this._wasm._free(this._pointers[i]); | ||
this._pointers.forEach((ptr) => { | ||
this._wasm._free(ptr); | ||
}); | ||
this._pointers.clear(); | ||
@@ -527,3 +546,3 @@ }; | ||
return this._wasm.ready.then(() => { | ||
caller._input = this.allocateTypedArray(caller._inputSize, Uint8Array); | ||
caller._input = this.allocateTypedArray(caller._inputSize, uint8Array); | ||
@@ -533,3 +552,3 @@ // output buffer | ||
caller._outputChannels * caller._outputChannelSize, | ||
Float32Array | ||
float32Array | ||
); | ||
@@ -536,0 +555,0 @@ |
@@ -19,7 +19,2 @@ import Worker from "web-worker"; | ||
const detachBuffers = (buffer) => | ||
Array.isArray(buffer) | ||
? buffer.map((buffer) => new Uint8Array(buffer)) | ||
: new Uint8Array(buffer); | ||
self.onmessage = ({ data: { id, command, data } }) => { | ||
@@ -52,3 +47,8 @@ switch (command) { | ||
command | ||
](detachBuffers(data)); | ||
]( | ||
// detach buffers | ||
Array.isArray(data) | ||
? data.map((data) => new Uint8Array(data)) | ||
: new Uint8Array(data) | ||
); | ||
@@ -73,3 +73,3 @@ self.postMessage( | ||
options | ||
)}, ${Decoder.toString()}, ${WASMAudioDecoderCommon.toString()}, ${EmscriptenWASM.toString()})`; | ||
)}, ${Decoder}, ${WASMAudioDecoderCommon}, ${EmscriptenWASM})`; | ||
@@ -76,0 +76,0 @@ const type = "text/javascript"; |
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
19742
540