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 78a3127632
commit bd70144deb
4 changed files with 24 additions and 24 deletions

View File

@ -31,6 +31,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"bitbucket.org/ausocean/av/stream/adpcm" "bitbucket.org/ausocean/av/stream/adpcm"
) )
@ -47,7 +48,7 @@ func main() {
// read adpcm // read adpcm
comp, err := ioutil.ReadFile(inPath) comp, err := ioutil.ReadFile(inPath)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
fmt.Println("Read", len(comp), "bytes from file", inPath) fmt.Println("Read", len(comp), "bytes from file", inPath)
@ -56,20 +57,19 @@ func main() {
numBlocks := int(len(comp) / inBSize) 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 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 := make([]byte, 0, outBSize*numBlocks)
for i, start := inBSize-1, 0; i < len(comp); i += inBSize { for i := 0; i < numBlocks; i++ {
block := comp[start : i+1] block := comp[inBSize*i : inBSize*(i+1)]
decBlock, err := adpcm.DecodeBlock(block) decBlock, err := adpcm.DecodeBlock(block)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
decoded = append(decoded, decBlock...) decoded = append(decoded, decBlock...)
start = i + 1
} }
// save pcm to file // save pcm to file
err = ioutil.WriteFile(outPath, decoded, 0644) err = ioutil.WriteFile(outPath, decoded, 0644)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
fmt.Println("Decoded and wrote", len(decoded), "bytes to file", outPath) fmt.Println("Decoded and wrote", len(decoded), "bytes to file", outPath)
} }

View File

@ -31,6 +31,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"bitbucket.org/ausocean/av/stream/adpcm" "bitbucket.org/ausocean/av/stream/adpcm"
) )
@ -47,7 +48,7 @@ func main() {
//read pcm //read pcm
pcm, err := ioutil.ReadFile(inPath) pcm, err := ioutil.ReadFile(inPath)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
fmt.Println("Read", len(pcm), "bytes from file", inPath) fmt.Println("Read", len(pcm), "bytes from file", inPath)
@ -56,20 +57,19 @@ func main() {
numBlocks := int(len(pcm) / inBSize) 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 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) comp := make([]byte, 0, outBSize*numBlocks)
for i, start := inBSize-1, 0; i < len(pcm); i += inBSize { for i := 0; i < numBlocks; i++ {
block := pcm[start : i+1] block := pcm[inBSize*i : inBSize*(i+1)]
encBlock, err := adpcm.EncodeBlock(block) encBlock, err := adpcm.EncodeBlock(block)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
comp = append(comp, encBlock...) comp = append(comp, encBlock...)
start = i + 1
} }
// save adpcm to file // save adpcm to file
err = ioutil.WriteFile(adpcmPath, comp, 0644) err = ioutil.WriteFile(adpcmPath, comp, 0644)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
fmt.Println("Encoded and wrote", len(comp), "bytes to file", adpcmPath) 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)) intSample := int16(binary.LittleEndian.Uint16(sample))
encodeSample(intSample) encodeSample(intSample)
head := make([]byte, 2) head := make([]byte, 2, 4)
head[0] = sample[0] head[0] = sample[0]
head[1] = sample[1] head[1] = sample[1]

View File

@ -30,7 +30,10 @@ package adpcm
import ( import (
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"log"
"testing" "testing"
"bitbucket.org/ausocean/av/stream/adpcm"
) )
// TestEncodeBlock will read PCM data, encode it in blocks and generate 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) 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 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) comp := make([]byte, 0, outBSize*numBlocks)
for i, start := inBSize-1, 0; i < len(pcm); i += inBSize { for i := 0; i < numBlocks; i++ {
block := pcm[start : i+1] block := pcm[inBSize*i : inBSize*(i+1)]
encBlock, err := adpcm.EncodeBlock(block)
encBlock, err := EncodeBlock(block)
if err != nil { if err != nil {
t.Errorf("Unable to encode block: %v", err) log.Fatal(err)
} }
comp = append(comp, encBlock...) comp = append(comp, encBlock...)
start = i + 1
} }
//read expected adpcm file //read expected adpcm file
@ -83,14 +84,13 @@ func TestDecodeBlock(t *testing.T) {
numBlocks := int(len(comp) / inBSize) 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 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 := make([]byte, 0, outBSize*numBlocks)
for i, start := inBSize-1, 0; i < len(comp); i += inBSize { for i := 0; i < numBlocks; i++ {
block := comp[start : i+1] block := comp[inBSize*i : inBSize*(i+1)]
decBlock, err := DecodeBlock(block) decBlock, err := adpcm.DecodeBlock(block)
if err != nil { if err != nil {
t.Errorf("Unable to decode block: %v", err) log.Fatal(err)
} }
decoded = append(decoded, decBlock...) decoded = append(decoded, decBlock...)
start = i + 1
} }
//read expected pcm file //read expected pcm file