ADPCM: Simplified for loops for creating and encoding/decoding blocks

This commit is contained in:
Trek H 2019-02-13 17:30:11 +10:30
parent 408a952c9a
commit ebdd65ea09
4 changed files with 24 additions and 24 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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]

View File

@ -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