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;
|
step = 0;
|
||||||
|
|
||||||
function decodeSample(nibble) {
|
function decodeSample(nibble) {
|
||||||
|
|
||||||
let diff = 0;
|
let diff = 0;
|
||||||
if ((nibble & 4) != 0) {
|
if ((nibble & 4) != 0) {
|
||||||
diff += step;
|
diff += step;
|
||||||
|
@ -58,46 +57,41 @@ function decodeSample(nibble) {
|
||||||
|
|
||||||
step = stepTable[idx];
|
step = stepTable[idx];
|
||||||
|
|
||||||
result = new Uint8Array(new Uint16Array([est]).buffer);
|
result = est;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function decode(b) {
|
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.
|
// 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];
|
idx = b[byteDepth];
|
||||||
step = stepTable[idx];
|
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++) {
|
for (var i = headBytes; i < b.length - b[3]; i++) {
|
||||||
var twoNibs = b[i];
|
var twoNibs = b[i];
|
||||||
var nib2 = twoNibs >> 4;
|
var nib2 = twoNibs >> 4;
|
||||||
var nib1 = (nib2 << 4) ^ twoNibs
|
var nib1 = (nib2 << 4) ^ twoNibs
|
||||||
var firstBytes = decodeSample(nib1)
|
|
||||||
result = concat(result, firstBytes)
|
|
||||||
|
|
||||||
var secondBytes = decodeSample(nib2)
|
var sample1 = int16ToBytes(decodeSample(nib1))
|
||||||
result = concat(result, secondBytes)
|
result.push(...sample1)
|
||||||
|
|
||||||
|
var sample2 = int16ToBytes(decodeSample(nib2))
|
||||||
|
result.push(...sample2)
|
||||||
}
|
}
|
||||||
if (b[3] == 1) {
|
if (b[3] == 1) {
|
||||||
var padNib = b[b.length - 1]
|
var padNib = b[b.length - 1]
|
||||||
var samp = decodeSample(padNib)
|
var sample = int16ToBytes(decodeSample(padNib))
|
||||||
result = concat(result, samp)
|
result.push(...sample)
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// concat concatenates TypedArrays a and b of same type.
|
function int16ToBytes(num) {
|
||||||
function concat(a, b) {
|
return [(num & 0x00ff), (num & 0xff00) >> 8];
|
||||||
var c = new(a.constructor)(a.length + b.length);
|
}
|
||||||
c.set(a, 0);
|
|
||||||
c.set(b, a.length);
|
function bytesToInt16(b) {
|
||||||
return c;
|
return (b[0] | (b[1] << 8))
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ window.onload = function () {
|
||||||
reader.onload = event => {
|
reader.onload = event => {
|
||||||
bytes = new Uint8Array(event.target.result)
|
bytes = new Uint8Array(event.target.result)
|
||||||
|
|
||||||
var decoded = decode(bytes)
|
var decoded = decode(Array.from(bytes))
|
||||||
|
|
||||||
console.log("playing file")
|
console.log("playing file")
|
||||||
var player = new PCMPlayer({
|
var player = new PCMPlayer({
|
||||||
|
@ -16,7 +16,7 @@ window.onload = function () {
|
||||||
sampleRate: 48000,
|
sampleRate: 48000,
|
||||||
flushingTime: 2000
|
flushingTime: 2000
|
||||||
});
|
});
|
||||||
player.feed(decoded)
|
player.feed(Uint8Array.from(decoded))
|
||||||
}
|
}
|
||||||
reader.onerror = error => reject(error)
|
reader.onerror = error => reject(error)
|
||||||
reader.readAsArrayBuffer(input)
|
reader.readAsArrayBuffer(input)
|
||||||
|
|
Loading…
Reference in New Issue