audio-player: added decBytes function

I added a function called decBytes to calculate the number of PCM bytes that will be generated from a given array of ADPCM bytes.
This commit is contained in:
Trek H 2019-09-27 18:59:08 +09:30
parent 7f2c77368d
commit d170afea8e
1 changed files with 27 additions and 3 deletions

View File

@ -61,9 +61,10 @@ class Decoder {
];
}
static get byteDepth() { return 2; } // We are working with 16-bit samples. TODO(Trek): make configurable.
static get headSize() { return 8; } // Number of bytes in the header of ADPCM.
static get chunkLenSize() { return 4; }
static get byteDepth() { return 2; } // We are working with 16-bit samples. TODO(Trek): make configurable.
static get headSize() { return 8; } // Number of bytes in the header of ADPCM.
static get chunkLenSize() { return 4; } // Length in bytes of the chunk length field in header.
static get compFact() { return 4; } // In general ADPCM compresses by a factor of 4.
// decodeSample takes 4 bits which represents a single ADPCM nibble, and returns a 16 bit decoded PCM sample.
decodeSample(nibble) {
@ -152,4 +153,27 @@ class Decoder {
(b[2] << 16) |
(b[3] << 24));
}
// decBytes takes a parameter that is assumed to be a byte array containing one or more adpcm chunks.
// It reads the chunk lengths from the chunk headers to calculate and return the number of pcm bytes that are expected to be decoded from the adpcm.
static decBytes(b){
let chunkLen;
let n = 0;
for (let off = 0; off + Decoder.headSize <= b.length; off += chunkLen) {
chunkLen = Decoder.bytesToInt32(b.slice(off, off + Decoder.chunkLenSize))
if (off + chunkLen > b.length) {
break;
}
// Account for uncompressed sample in header.
n += Decoder.byteDepth;
// Account for PCM bytes that will result from decoding ADPCM.
n += (chunkLen - Decoder.headSize)*Decoder.compFact;
// Account for padding.
if(b[off+Decoder.chunkLenSize+3] == 0x01){
n -= Decoder.byteDepth;
}
}
return n;
}
}