av/stream/adpcm/adpcm_test.go

76 lines
1.9 KiB
Go

package adpcm
import (
"bytes"
"io/ioutil"
"testing"
)
// TestEncodeBlock will read PCM data, encode it in blocks and generate ADPCM
// then compare the result with expected ADPCM.
func TestEncodeBlock(t *testing.T) {
//read input pcm
pcm, err := ioutil.ReadFile("../../../test/test-data/av/input/raw-voice.pcm")
if err != nil {
t.Errorf("Unable to read input PCM file: %v", err)
}
//encode adpcm
var comp []byte
start := 0
for i := 0; i < len(pcm); i++ {
if i%1010 == 1009 {
block := pcm[start : i+1]
encBlock, err := EncodeBlock(block)
if err != nil {
//todo: use correct logging of error
t.Errorf("Unable to encode block: %v", err)
}
comp = append(comp, encBlock...)
start = i + 1
}
}
//read expected adpcm file
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded-voice.adpcm")
if err != nil {
t.Errorf("Unable to read expected ADPCM file: %v", err)
}
if !bytes.Equal(comp, exp) {
t.Error("ADPCM generated does not match expected ADPCM")
}
}
// TestDecodeBlock will read encoded ADPCM, decode it in blocks and then compare the
// resulting PCM with the expected decoded PCM.
func TestDecodeBlock(t *testing.T) {
//read adpcm
comp, err := ioutil.ReadFile("../../../test/test-data/av/input/encoded-voice.adpcm")
if err != nil {
t.Errorf("Unable to read input ADPCM file: %v", err)
}
//decode adpcm
var decoded []byte
start := 0
for i := 0; i < len(comp); i++ {
if i%256 == 255 {
block := comp[start : i+1]
decBlock, err := DecodeBlock(block)
if err != nil {
t.Errorf("Unable to decode block: %v", err)
}
decoded = append(decoded, decBlock...)
start = i + 1
}
}
//read expected pcm file
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded-voice.pcm")
if err != nil {
t.Errorf("Unable to read expected PCM file: %v", err)
}
if !bytes.Equal(decoded, exp) {
t.Error("PCM generated does not match expected PCM")
}
}