ADPCM: pre-allocating memory for entire known length of output byte slice

This commit is contained in:
Trek H 2019-02-13 16:53:25 +10:30
parent 43c6027888
commit 7d3829d19f
3 changed files with 48 additions and 47 deletions

View File

@ -29,6 +29,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"bitbucket.org/ausocean/av/stream/adpcm"
@ -39,22 +40,23 @@ import (
func main() {
var inPath string
var outPath string
flag.StringVar(&inPath, "in", "encoded.adpcm", "file path of input")
flag.StringVar(&outPath, "out", "decoded.pcm", "file path of output data")
flag.Parse()
//read adpcm
// read adpcm
comp, err := ioutil.ReadFile(inPath)
if err != nil {
panic(err)
}
fmt.Println("Read", len(comp), "bytes from file", inPath)
//decode adpcm
var decoded []byte
start := 0
for i := 0; i < len(comp); i++ {
if i%256 == 255 {
// decode adpcm
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 := adpcm.DecodeBlock(block)
if err != nil {
@ -63,11 +65,11 @@ func main() {
decoded = append(decoded, decBlock...)
start = i + 1
}
}
// save pcm to file
err = ioutil.WriteFile(outPath, decoded, 0644)
if err != nil {
panic(err)
}
fmt.Println("Decoded and wrote", len(decoded), "bytes to file", outPath)
}

View File

@ -29,11 +29,14 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"bitbucket.org/ausocean/av/stream/adpcm"
)
// This program accepts an input pcm file and outputs an encoded adpcm file.
// Input and output file names can be specified as arguments.
func main() {
var inPath string
var adpcmPath string
@ -46,15 +49,15 @@ func main() {
if err != nil {
panic(err)
}
fmt.Println("Read", len(pcm), "bytes from file", inPath)
//encode adpcm
var comp []byte
start := 0
bSize := 1010
for i := 0; i < len(pcm); i++ {
if i%bSize == bSize-1 {
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 := adpcm.EncodeBlock(block)
if err != nil {
panic(err)
@ -62,11 +65,11 @@ func main() {
comp = append(comp, encBlock...)
start = i + 1
}
}
// save adpcm to file
err = ioutil.WriteFile(adpcmPath, comp, 0644)
if err != nil {
panic(err)
}
fmt.Println("Encoded and wrote", len(comp), "bytes to file", adpcmPath)
}

View File

@ -44,9 +44,8 @@ func TestEncodeBlock(t *testing.T) {
//encode adpcm
var comp []byte
start := 0
for i := 0; i < len(pcm); i++ {
if i%1010 == 1009 {
bSize := 1010
for i, start := bSize-1, 0; i < len(pcm); i += bSize {
block := pcm[start : i+1]
encBlock, err := EncodeBlock(block)
@ -56,7 +55,6 @@ func TestEncodeBlock(t *testing.T) {
comp = append(comp, encBlock...)
start = i + 1
}
}
//read expected adpcm file
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded-voice.adpcm")
@ -80,9 +78,8 @@ func TestDecodeBlock(t *testing.T) {
//decode adpcm
var decoded []byte
start := 0
for i := 0; i < len(comp); i++ {
if i%256 == 255 {
bSize := 256
for i, start := bSize-1, 0; i < len(comp); i += bSize {
block := comp[start : i+1]
decBlock, err := DecodeBlock(block)
if err != nil {
@ -91,7 +88,6 @@ func TestDecodeBlock(t *testing.T) {
decoded = append(decoded, decBlock...)
start = i + 1
}
}
//read expected pcm file
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded-voice.pcm")