mirror of https://bitbucket.org/ausocean/av.git
audio-player: adpcm file playing in browser
Adpcm files can be uploaded and played however decoding takes a significant time. The process needs to be optimised.
This commit is contained in:
parent
2c436b7edf
commit
cdc5b52f36
|
@ -26,12 +26,40 @@ const byteDepth = 2, // We are working with 16-bit samples. TODO(Trek): make con
|
|||
bytesPerEnc = samplesPerEnc * byteDepth,
|
||||
compFact = 4; // In general ADPCM compresses by a factor of 4.
|
||||
|
||||
let est, // Estimation of sample based on quantised ADPCM nibble.
|
||||
idx, // Index to step used for estimation.
|
||||
step;
|
||||
let est = 0, // Estimation of sample based on quantised ADPCM nibble.
|
||||
idx = 0, // Index to step used for estimation.
|
||||
step = 0;
|
||||
|
||||
function decodeSample(nibble) {
|
||||
return new Uint8Array([1, 2])
|
||||
|
||||
let diff = 0;
|
||||
if ((nibble & 4) != 0) {
|
||||
diff += step;
|
||||
}
|
||||
if ((nibble & 2) != 0) {
|
||||
diff += step >> 1;
|
||||
}
|
||||
if ((nibble & 1) != 0) {
|
||||
diff += step >> 2;
|
||||
}
|
||||
diff += step >> 3;
|
||||
|
||||
if ((nibble & 8) != 0) {
|
||||
diff = -diff;
|
||||
}
|
||||
est += diff;
|
||||
idx += indexTable[nibble];
|
||||
|
||||
if (idx < 0) {
|
||||
idx = 0;
|
||||
} else if (idx > stepTable.length - 1) {
|
||||
idx = stepTable.length - 1;
|
||||
}
|
||||
|
||||
step = stepTable[idx];
|
||||
|
||||
result = new Uint8Array(new Uint16Array([est]).buffer);
|
||||
return result;
|
||||
}
|
||||
|
||||
function decode(b) {
|
||||
|
@ -42,7 +70,7 @@ function decode(b) {
|
|||
}
|
||||
|
||||
// Initialize Decoder with first 4 bytes of b.
|
||||
est = (new Uint16Array(b.slice(0, 2).buffer))[1];
|
||||
est = (new Uint16Array(b.slice(0, 2).buffer))[0];
|
||||
idx = b[byteDepth];
|
||||
step = stepTable[idx];
|
||||
|
||||
|
@ -63,7 +91,6 @@ function decode(b) {
|
|||
var samp = decodeSample(padNib)
|
||||
result = concat(result, samp)
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,21 +7,16 @@ window.onload = function () {
|
|||
reader.onload = event => {
|
||||
bytes = new Uint8Array(event.target.result)
|
||||
|
||||
console.log(bytes.slice(0, 16))
|
||||
|
||||
var decoded = decode(bytes)
|
||||
|
||||
console.log("playing file")
|
||||
|
||||
console.log(decoded.slice(0, 16))
|
||||
|
||||
// var player = new PCMPlayer({
|
||||
// encoding: '16bitInt',
|
||||
// channels: 1,
|
||||
// sampleRate: 48000,
|
||||
// flushingTime: 2000
|
||||
// });
|
||||
// player.feed(bytes)
|
||||
var player = new PCMPlayer({
|
||||
encoding: '16bitInt',
|
||||
channels: 1,
|
||||
sampleRate: 48000,
|
||||
flushingTime: 2000
|
||||
});
|
||||
player.feed(decoded)
|
||||
}
|
||||
reader.onerror = error => reject(error)
|
||||
reader.readAsArrayBuffer(input)
|
||||
|
|
Loading…
Reference in New Issue