From 408a952c9a7dd31b9898f05b0391e8181764f88d Mon Sep 17 00:00:00 2001 From: Trek H Date: Wed, 13 Feb 2019 16:58:21 +1030 Subject: [PATCH] ADPCM: changed adpcm_test.go to use pre-allocation of byte slices --- stream/adpcm/adpcm_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/stream/adpcm/adpcm_test.go b/stream/adpcm/adpcm_test.go index bbda1d31..a2c9c034 100644 --- a/stream/adpcm/adpcm_test.go +++ b/stream/adpcm/adpcm_test.go @@ -43,9 +43,11 @@ func TestEncodeBlock(t *testing.T) { } //encode adpcm - var comp []byte - bSize := 1010 - for i, start := bSize-1, 0; i < len(pcm); i += bSize { + inBSize := 1010 + numBlocks := int(len(pcm) / inBSize) + outBSize := int(float32(inBSize/4) + float32(3.5)) // compression is 4:1 and 3.5 bytes of info are added to each block + comp := make([]byte, 0, outBSize*numBlocks) + for i, start := inBSize-1, 0; i < len(pcm); i += inBSize { block := pcm[start : i+1] encBlock, err := EncodeBlock(block) @@ -77,9 +79,11 @@ func TestDecodeBlock(t *testing.T) { } //decode adpcm - var decoded []byte - bSize := 256 - for i, start := bSize-1, 0; i < len(comp); i += bSize { + inBSize := 256 + numBlocks := int(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) + for i, start := inBSize-1, 0; i < len(comp); i += inBSize { block := comp[start : i+1] decBlock, err := DecodeBlock(block) if err != nil {