ADPCM: decoder now writes to bytes.Buffer instead of returning a byte array, tests updated.

This commit is contained in:
Trek H 2019-02-28 13:12:41 +10:30
parent e45c67e157
commit 29f4acd7fe
2 changed files with 10 additions and 12 deletions

View File

@ -247,17 +247,16 @@ func (e *Encoder) EncodeBlock(block []byte) error {
// DecodeBlock takes a slice of 256 bytes, each byte should contain two ADPCM encoded nibbles. // DecodeBlock takes a slice of 256 bytes, each byte should contain two ADPCM encoded nibbles.
// It returns a byte slice containing the resulting decoded (uncompressed) 16-bit PCM samples. // It returns a byte slice containing the resulting decoded (uncompressed) 16-bit PCM samples.
func (d *Decoder) DecodeBlock(block []byte) ([]byte, error) { func (d *Decoder) DecodeBlock(block []byte) error {
bSize := 256 bSize := 256
if len(block) != bSize { if len(block) != bSize {
return nil, fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), bSize) return fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), bSize)
} }
var result []byte
d.pred = int16(binary.LittleEndian.Uint16(block[0:2])) d.pred = int16(binary.LittleEndian.Uint16(block[0:2]))
d.index = int16(block[2]) d.index = int16(block[2])
d.step = stepTable[d.index] d.step = stepTable[d.index]
result = append(result, block[0:2]...) d.dest.Write(block[0:2])
for i := 4; i < len(block); i++ { for i := 4; i < len(block); i++ {
originalSample := block[i] originalSample := block[i]
@ -266,12 +265,12 @@ func (d *Decoder) DecodeBlock(block []byte) ([]byte, error) {
firstBytes := make([]byte, 2) firstBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(firstSample))) binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(firstSample)))
result = append(result, firstBytes...) d.dest.Write(firstBytes)
secondBytes := make([]byte, 2) secondBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(secondSample))) binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(secondSample)))
result = append(result, secondBytes...) d.dest.Write(secondBytes)
} }
return result, nil return nil
} }

View File

@ -81,15 +81,14 @@ func TestDecodeBlock(t *testing.T) {
inBSize := 256 inBSize := 256
numBlocks := len(comp) / inBSize numBlocks := len(comp) / inBSize
outBSize := 2 + (inBSize-4)*4 // 2 bytes are copied, 2 are used as block header info, the remaining bytes are decompressed 1:4 outBSize := 2 + (inBSize-4)*4 // 2 bytes are copied, 2 are used as block header info, the remaining bytes are decompressed 1:4
decoded := make([]byte, 0, outBSize*numBlocks) decoded := bytes.NewBuffer(make([]byte, 0, outBSize*numBlocks))
dec := NewDecoder(nil) dec := NewDecoder(decoded)
for i := 0; i < numBlocks; i++ { for i := 0; i < numBlocks; i++ {
block := comp[inBSize*i : inBSize*(i+1)] block := comp[inBSize*i : inBSize*(i+1)]
decBlock, err := dec.DecodeBlock(block) err := dec.DecodeBlock(block)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
decoded = append(decoded, decBlock...)
} }
//read expected pcm file //read expected pcm file
@ -98,7 +97,7 @@ func TestDecodeBlock(t *testing.T) {
t.Errorf("Unable to read expected PCM file: %v", err) t.Errorf("Unable to read expected PCM file: %v", err)
} }
if !bytes.Equal(decoded, exp) { if !bytes.Equal(decoded.Bytes(), exp) {
t.Error("PCM generated does not match expected PCM") t.Error("PCM generated does not match expected PCM")
} }
} }