From 29f4acd7fe1d65eb576eb03139806cecb9ed43d2 Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 28 Feb 2019 13:12:41 +1030 Subject: [PATCH] ADPCM: decoder now writes to bytes.Buffer instead of returning a byte array, tests updated. --- stream/adpcm/adpcm.go | 13 ++++++------- stream/adpcm/adpcm_test.go | 9 ++++----- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/stream/adpcm/adpcm.go b/stream/adpcm/adpcm.go index 326f6758..9e9bb324 100644 --- a/stream/adpcm/adpcm.go +++ b/stream/adpcm/adpcm.go @@ -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. // 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 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.index = int16(block[2]) d.step = stepTable[d.index] - result = append(result, block[0:2]...) + d.dest.Write(block[0:2]) for i := 4; i < len(block); i++ { originalSample := block[i] @@ -266,12 +265,12 @@ func (d *Decoder) DecodeBlock(block []byte) ([]byte, error) { firstBytes := make([]byte, 2) binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(firstSample))) - result = append(result, firstBytes...) + d.dest.Write(firstBytes) secondBytes := make([]byte, 2) binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(secondSample))) - result = append(result, secondBytes...) + d.dest.Write(secondBytes) } - return result, nil + return nil } diff --git a/stream/adpcm/adpcm_test.go b/stream/adpcm/adpcm_test.go index 9ab1ac55..f82b421b 100644 --- a/stream/adpcm/adpcm_test.go +++ b/stream/adpcm/adpcm_test.go @@ -81,15 +81,14 @@ func TestDecodeBlock(t *testing.T) { inBSize := 256 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 - decoded := make([]byte, 0, outBSize*numBlocks) - dec := NewDecoder(nil) + decoded := bytes.NewBuffer(make([]byte, 0, outBSize*numBlocks)) + dec := NewDecoder(decoded) for i := 0; i < numBlocks; i++ { block := comp[inBSize*i : inBSize*(i+1)] - decBlock, err := dec.DecodeBlock(block) + err := dec.DecodeBlock(block) if err != nil { log.Fatal(err) } - decoded = append(decoded, decBlock...) } //read expected pcm file @@ -98,7 +97,7 @@ func TestDecodeBlock(t *testing.T) { 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") } }