From d5cf171485317951f5ec2055c93bf6aedee9cf35 Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 28 Feb 2019 13:32:57 +1030 Subject: [PATCH] ADPCM: updated encode decode commands to use restructured encoder and decoder --- cmd/adpcm/decode-pcm/decode-pcm.go | 11 ++++++----- cmd/adpcm/encode-pcm/encode-pcm.go | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cmd/adpcm/decode-pcm/decode-pcm.go b/cmd/adpcm/decode-pcm/decode-pcm.go index e19993f5..d661a71d 100644 --- a/cmd/adpcm/decode-pcm/decode-pcm.go +++ b/cmd/adpcm/decode-pcm/decode-pcm.go @@ -28,6 +28,7 @@ LICENSE package main import ( + "bytes" "flag" "fmt" "io/ioutil" @@ -56,20 +57,20 @@ func main() { 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) + decoded := bytes.NewBuffer(make([]byte, 0, outBSize*numBlocks)) + dec := adpcm.NewDecoder(decoded) for i := 0; i < numBlocks; i++ { block := comp[inBSize*i : inBSize*(i+1)] - decBlock, err := adpcm.DecodeBlock(block) + err := dec.DecodeBlock(block) if err != nil { log.Fatal(err) } - decoded = append(decoded, decBlock...) } // save pcm to file - err = ioutil.WriteFile(outPath, decoded, 0644) + err = ioutil.WriteFile(outPath, decoded.Bytes(), 0644) if err != nil { log.Fatal(err) } - fmt.Println("Decoded and wrote", len(decoded), "bytes to file", outPath) + fmt.Println("Decoded and wrote", len(decoded.Bytes()), "bytes to file", outPath) } diff --git a/cmd/adpcm/encode-pcm/encode-pcm.go b/cmd/adpcm/encode-pcm/encode-pcm.go index 7e6bcfa6..f82b5a7f 100644 --- a/cmd/adpcm/encode-pcm/encode-pcm.go +++ b/cmd/adpcm/encode-pcm/encode-pcm.go @@ -28,6 +28,7 @@ LICENSE package main import ( + "bytes" "flag" "fmt" "io/ioutil" @@ -56,20 +57,20 @@ func main() { inBSize := 1010 numBlocks := len(pcm) / inBSize outBSize := int(float64(inBSize)/4 + 3.5) // compression is 4:1 and 3.5 bytes of info are added to each block - comp := make([]byte, 0, outBSize*numBlocks) + comp := bytes.NewBuffer(make([]byte, 0, outBSize*numBlocks)) + enc := adpcm.NewEncoder(comp) for i := 0; i < numBlocks; i++ { block := pcm[inBSize*i : inBSize*(i+1)] - encBlock, err := adpcm.EncodeBlock(block) + err := enc.EncodeBlock(block) if err != nil { log.Fatal(err) } - comp = append(comp, encBlock...) } // save adpcm to file - err = ioutil.WriteFile(adpcmPath, comp, 0644) + err = ioutil.WriteFile(adpcmPath, comp.Bytes(), 0644) if err != nil { log.Fatal(err) } - fmt.Println("Encoded and wrote", len(comp), "bytes to file", adpcmPath) + fmt.Println("Encoded and wrote", len(comp.Bytes()), "bytes to file", adpcmPath) }