From ebdd65ea09c71181c53ae46cb9d90cb0da6becea Mon Sep 17 00:00:00 2001 From: Trek H Date: Wed, 13 Feb 2019 17:30:11 +1030 Subject: [PATCH] ADPCM: Simplified for loops for creating and encoding/decoding blocks --- cmd/adpcm/decode-pcm/decode-pcm.go | 12 ++++++------ cmd/adpcm/encode-pcm/encode-pcm.go | 12 ++++++------ stream/adpcm/adpcm.go | 2 +- stream/adpcm/adpcm_test.go | 22 +++++++++++----------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/cmd/adpcm/decode-pcm/decode-pcm.go b/cmd/adpcm/decode-pcm/decode-pcm.go index fded5005..793893ef 100644 --- a/cmd/adpcm/decode-pcm/decode-pcm.go +++ b/cmd/adpcm/decode-pcm/decode-pcm.go @@ -31,6 +31,7 @@ import ( "flag" "fmt" "io/ioutil" + "log" "bitbucket.org/ausocean/av/stream/adpcm" ) @@ -47,7 +48,7 @@ func main() { // read adpcm comp, err := ioutil.ReadFile(inPath) if err != nil { - panic(err) + log.Fatal(err) } fmt.Println("Read", len(comp), "bytes from file", inPath) @@ -56,20 +57,19 @@ func main() { 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] + for i := 0; i < numBlocks; i++ { + block := comp[inBSize*i : inBSize*(i+1)] decBlock, err := adpcm.DecodeBlock(block) if err != nil { - panic(err) + log.Fatal(err) } decoded = append(decoded, decBlock...) - start = i + 1 } // save pcm to file err = ioutil.WriteFile(outPath, decoded, 0644) if err != nil { - panic(err) + log.Fatal(err) } fmt.Println("Decoded and wrote", len(decoded), "bytes to file", outPath) } diff --git a/cmd/adpcm/encode-pcm/encode-pcm.go b/cmd/adpcm/encode-pcm/encode-pcm.go index 002e2c22..b2af502a 100644 --- a/cmd/adpcm/encode-pcm/encode-pcm.go +++ b/cmd/adpcm/encode-pcm/encode-pcm.go @@ -31,6 +31,7 @@ import ( "flag" "fmt" "io/ioutil" + "log" "bitbucket.org/ausocean/av/stream/adpcm" ) @@ -47,7 +48,7 @@ func main() { //read pcm pcm, err := ioutil.ReadFile(inPath) if err != nil { - panic(err) + log.Fatal(err) } fmt.Println("Read", len(pcm), "bytes from file", inPath) @@ -56,20 +57,19 @@ func main() { 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] + for i := 0; i < numBlocks; i++ { + block := pcm[inBSize*i : inBSize*(i+1)] encBlock, err := adpcm.EncodeBlock(block) if err != nil { - panic(err) + log.Fatal(err) } comp = append(comp, encBlock...) - start = i + 1 } // save adpcm to file err = ioutil.WriteFile(adpcmPath, comp, 0644) if err != nil { - panic(err) + log.Fatal(err) } fmt.Println("Encoded and wrote", len(comp), "bytes to file", adpcmPath) } diff --git a/stream/adpcm/adpcm.go b/stream/adpcm/adpcm.go index 5359253e..a55936d9 100644 --- a/stream/adpcm/adpcm.go +++ b/stream/adpcm/adpcm.go @@ -184,7 +184,7 @@ func calcHead(sample []byte) ([]byte, error) { intSample := int16(binary.LittleEndian.Uint16(sample)) encodeSample(intSample) - head := make([]byte, 2) + head := make([]byte, 2, 4) head[0] = sample[0] head[1] = sample[1] diff --git a/stream/adpcm/adpcm_test.go b/stream/adpcm/adpcm_test.go index a2c9c034..c420eee6 100644 --- a/stream/adpcm/adpcm_test.go +++ b/stream/adpcm/adpcm_test.go @@ -30,7 +30,10 @@ package adpcm import ( "bytes" "io/ioutil" + "log" "testing" + + "bitbucket.org/ausocean/av/stream/adpcm" ) // TestEncodeBlock will read PCM data, encode it in blocks and generate ADPCM @@ -47,15 +50,13 @@ func TestEncodeBlock(t *testing.T) { 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) + for i := 0; i < numBlocks; i++ { + block := pcm[inBSize*i : inBSize*(i+1)] + encBlock, err := adpcm.EncodeBlock(block) if err != nil { - t.Errorf("Unable to encode block: %v", err) + log.Fatal(err) } comp = append(comp, encBlock...) - start = i + 1 } //read expected adpcm file @@ -83,14 +84,13 @@ func TestDecodeBlock(t *testing.T) { 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) + for i := 0; i < numBlocks; i++ { + block := comp[inBSize*i : inBSize*(i+1)] + decBlock, err := adpcm.DecodeBlock(block) if err != nil { - t.Errorf("Unable to decode block: %v", err) + log.Fatal(err) } decoded = append(decoded, decBlock...) - start = i + 1 } //read expected pcm file