adpcm: modified the adpcm encoding and decoding to not use blocks

This commit is contained in:
Trek H 2019-03-29 16:08:10 +10:30
parent d491e1fb6d
commit 2600fa884f
1 changed files with 39 additions and 11 deletions

View File

@ -300,15 +300,20 @@ func (d *decoder) decodeBlock(block []byte) (int, error) {
// It writes its output to the encoder's dest.
// The number of bytes written out is returned along with any error that occured.
func (e *encoder) Write(inPcm []byte) (int, error) {
numBlocks := len(inPcm) / PcmBS
n := 0
for i := 0; i < numBlocks; i++ {
block := inPcm[PcmBS*i : PcmBS*(i+1)]
_n, err := e.encodeBlock(block)
n += _n
n, err := e.calcHead(inPcm[0:2])
if err != nil {
return n, err
}
for i := 3; i < len(inPcm); i += 4 {
nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(inPcm[i-1 : i+1])))
nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(inPcm[i+1 : i+3])))
err = e.dest.WriteByte(byte((nib2 << 4) | nib1))
if err != nil {
return n, err
}
n++
}
return n, nil
@ -318,11 +323,34 @@ func (e *encoder) Write(inPcm []byte) (int, error) {
// It writes its output to the decoder's dest.
// The number of bytes written out is returned along with any error that occured.
func (d *decoder) Write(inAdpcm []byte) (int, error) {
numBlocks := len(inAdpcm) / AdpcmBS
n := 0
for i := 0; i < numBlocks; i++ {
block := inAdpcm[AdpcmBS*i : AdpcmBS*(i+1)]
_n, err := d.decodeBlock(block)
// Initialize decoder with first 4 bytes of the inAdpcm.
d.pred = int16(binary.LittleEndian.Uint16(inAdpcm[0:2]))
d.index = int16(inAdpcm[2])
d.step = stepTable[d.index]
n, err := d.dest.Write(inAdpcm[0:2])
if err != nil {
return n, err
}
// For each byte, seperate it into two nibbles (each nibble is a compressed sample),
// then decode each nibble and output the resulting 16-bit samples.
for i := 4; i < len(inAdpcm); i++ {
twoNibs := inAdpcm[i]
nib2 := byte(twoNibs >> 4)
nib1 := byte((nib2 << 4) ^ twoNibs)
firstBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(nib1)))
_n, err := d.dest.Write(firstBytes)
n += _n
if err != nil {
return n, err
}
secondBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(nib2)))
_n, err = d.dest.Write(secondBytes)
n += _n
if err != nil {
return n, err