2019-02-13 04:23:06 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
adpcm_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
adpcm_test.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
2019-02-11 07:55:10 +03:00
|
|
|
package adpcm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2019-02-13 10:00:11 +03:00
|
|
|
"log"
|
2019-02-11 07:55:10 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-02-12 07:19:16 +03:00
|
|
|
// TestEncodeBlock will read PCM data, encode it in blocks and generate ADPCM
|
|
|
|
// then compare the result with expected ADPCM.
|
2019-02-11 07:55:10 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-11 07:55:10 +03:00
|
|
|
//encode adpcm
|
2019-02-13 09:28:21 +03:00
|
|
|
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)
|
2019-02-13 10:00:11 +03:00
|
|
|
for i := 0; i < numBlocks; i++ {
|
|
|
|
block := pcm[inBSize*i : inBSize*(i+1)]
|
2019-02-15 06:01:16 +03:00
|
|
|
encBlock, err := EncodeBlock(block)
|
2019-02-13 09:23:25 +03:00
|
|
|
if err != nil {
|
2019-02-13 10:00:11 +03:00
|
|
|
log.Fatal(err)
|
2019-02-11 07:55:10 +03:00
|
|
|
}
|
2019-02-13 09:23:25 +03:00
|
|
|
comp = append(comp, encBlock...)
|
2019-02-11 07:55:10 +03:00
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-11 07:55:10 +03:00
|
|
|
//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")
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 07:19:16 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-12 07:19:16 +03:00
|
|
|
//decode adpcm
|
2019-02-13 09:28:21 +03:00
|
|
|
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)
|
2019-02-13 10:00:11 +03:00
|
|
|
for i := 0; i < numBlocks; i++ {
|
|
|
|
block := comp[inBSize*i : inBSize*(i+1)]
|
2019-02-15 06:01:16 +03:00
|
|
|
decBlock, err := DecodeBlock(block)
|
2019-02-13 09:23:25 +03:00
|
|
|
if err != nil {
|
2019-02-13 10:00:11 +03:00
|
|
|
log.Fatal(err)
|
2019-02-12 07:19:16 +03:00
|
|
|
}
|
2019-02-13 09:23:25 +03:00
|
|
|
decoded = append(decoded, decBlock...)
|
2019-02-12 07:19:16 +03:00
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-12 07:19:16 +03:00
|
|
|
//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")
|
|
|
|
}
|
|
|
|
}
|