ADPCM: updated encode decode commands to use restructured encoder and decoder

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

View File

@ -28,6 +28,7 @@ LICENSE
package main package main
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -56,20 +57,20 @@ func main() {
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 := adpcm.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 := adpcm.DecodeBlock(block) err := dec.DecodeBlock(block)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
decoded = append(decoded, decBlock...)
} }
// save pcm to file // save pcm to file
err = ioutil.WriteFile(outPath, decoded, 0644) err = ioutil.WriteFile(outPath, decoded.Bytes(), 0644)
if err != nil { if err != nil {
log.Fatal(err) 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)
} }

View File

@ -28,6 +28,7 @@ LICENSE
package main package main
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -56,20 +57,20 @@ func main() {
inBSize := 1010 inBSize := 1010
numBlocks := len(pcm) / inBSize 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 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++ { for i := 0; i < numBlocks; i++ {
block := pcm[inBSize*i : inBSize*(i+1)] block := pcm[inBSize*i : inBSize*(i+1)]
encBlock, err := adpcm.EncodeBlock(block) err := enc.EncodeBlock(block)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
comp = append(comp, encBlock...)
} }
// save adpcm to file // save adpcm to file
err = ioutil.WriteFile(adpcmPath, comp, 0644) err = ioutil.WriteFile(adpcmPath, comp.Bytes(), 0644)
if err != nil { if err != nil {
log.Fatal(err) 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)
} }