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:
Trek H 2019-07-25 13:32:53 +09:30
parent 2c436b7edf
commit cdc5b52f36
2 changed files with 40 additions and 18 deletions

View File

@ -26,12 +26,40 @@ const byteDepth = 2, // We are working with 16-bit samples. TODO(Trek): make con
bytesPerEnc = samplesPerEnc * byteDepth, bytesPerEnc = samplesPerEnc * byteDepth,
compFact = 4; // In general ADPCM compresses by a factor of 4. compFact = 4; // In general ADPCM compresses by a factor of 4.
let est, // Estimation of sample based on quantised ADPCM nibble. let est = 0, // Estimation of sample based on quantised ADPCM nibble.
idx, // Index to step used for estimation. idx = 0, // Index to step used for estimation.
step; step = 0;
function decodeSample(nibble) { 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) { function decode(b) {
@ -42,7 +70,7 @@ function decode(b) {
} }
// Initialize Decoder with first 4 bytes of 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]; idx = b[byteDepth];
step = stepTable[idx]; step = stepTable[idx];
@ -63,7 +91,6 @@ function decode(b) {
var samp = decodeSample(padNib) var samp = decodeSample(padNib)
result = concat(result, samp) result = concat(result, samp)
} }
return result; return result;
} }

View File

@ -7,21 +7,16 @@ window.onload = function () {
reader.onload = event => { reader.onload = event => {
bytes = new Uint8Array(event.target.result) bytes = new Uint8Array(event.target.result)
console.log(bytes.slice(0, 16))
var decoded = decode(bytes) var decoded = decode(bytes)
console.log("playing file") console.log("playing file")
var player = new PCMPlayer({
console.log(decoded.slice(0, 16)) encoding: '16bitInt',
channels: 1,
// var player = new PCMPlayer({ sampleRate: 48000,
// encoding: '16bitInt', flushingTime: 2000
// channels: 1, });
// sampleRate: 48000, player.feed(decoded)
// flushingTime: 2000
// });
// player.feed(bytes)
} }
reader.onerror = error => reject(error) reader.onerror = error => reject(error)
reader.readAsArrayBuffer(input) reader.readAsArrayBuffer(input)