mirror of https://bitbucket.org/ausocean/av.git
audio-player: decoding optimised, not using typed arrays
This commit is contained in:
parent
cdc5b52f36
commit
000b71068f
|
@ -31,7 +31,6 @@ let est = 0, // Estimation of sample based on quantised ADPCM nibble.
|
|||
step = 0;
|
||||
|
||||
function decodeSample(nibble) {
|
||||
|
||||
let diff = 0;
|
||||
if ((nibble & 4) != 0) {
|
||||
diff += step;
|
||||
|
@ -58,46 +57,41 @@ function decodeSample(nibble) {
|
|||
|
||||
step = stepTable[idx];
|
||||
|
||||
result = new Uint8Array(new Uint16Array([est]).buffer);
|
||||
result = est;
|
||||
return result;
|
||||
}
|
||||
|
||||
function decode(b) {
|
||||
// b should be a Uint8Array
|
||||
if (!(b instanceof Uint8Array)) {
|
||||
console.log("Error: data is not a Uint8Array");
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize Decoder with first 4 bytes of b.
|
||||
est = (new Uint16Array(b.slice(0, 2).buffer))[0];
|
||||
est = bytesToInt16(b[0], b[1]);
|
||||
idx = b[byteDepth];
|
||||
step = stepTable[idx];
|
||||
|
||||
var result = new Uint8Array(b.slice(0, 2));
|
||||
var result = b.slice(0, 2);
|
||||
|
||||
for (var i = headBytes; i < b.length - b[3]; i++) {
|
||||
var twoNibs = b[i];
|
||||
var nib2 = twoNibs >> 4;
|
||||
var nib1 = (nib2 << 4) ^ twoNibs
|
||||
var firstBytes = decodeSample(nib1)
|
||||
result = concat(result, firstBytes)
|
||||
|
||||
var secondBytes = decodeSample(nib2)
|
||||
result = concat(result, secondBytes)
|
||||
var sample1 = int16ToBytes(decodeSample(nib1))
|
||||
result.push(...sample1)
|
||||
|
||||
var sample2 = int16ToBytes(decodeSample(nib2))
|
||||
result.push(...sample2)
|
||||
}
|
||||
if (b[3] == 1) {
|
||||
var padNib = b[b.length - 1]
|
||||
var samp = decodeSample(padNib)
|
||||
result = concat(result, samp)
|
||||
var sample = int16ToBytes(decodeSample(padNib))
|
||||
result.push(...sample)
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// concat concatenates TypedArrays a and b of same type.
|
||||
function concat(a, b) {
|
||||
var c = new(a.constructor)(a.length + b.length);
|
||||
c.set(a, 0);
|
||||
c.set(b, a.length);
|
||||
return c;
|
||||
function int16ToBytes(num) {
|
||||
return [(num & 0x00ff), (num & 0xff00) >> 8];
|
||||
}
|
||||
|
||||
function bytesToInt16(b) {
|
||||
return (b[0] | (b[1] << 8))
|
||||
}
|
|
@ -7,7 +7,7 @@ window.onload = function () {
|
|||
reader.onload = event => {
|
||||
bytes = new Uint8Array(event.target.result)
|
||||
|
||||
var decoded = decode(bytes)
|
||||
var decoded = decode(Array.from(bytes))
|
||||
|
||||
console.log("playing file")
|
||||
var player = new PCMPlayer({
|
||||
|
@ -16,7 +16,7 @@ window.onload = function () {
|
|||
sampleRate: 48000,
|
||||
flushingTime: 2000
|
||||
});
|
||||
player.feed(decoded)
|
||||
player.feed(Uint8Array.from(decoded))
|
||||
}
|
||||
reader.onerror = error => reject(error)
|
||||
reader.readAsArrayBuffer(input)
|
||||
|
|
Loading…
Reference in New Issue