2019-02-13 04:23:06 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
adpcm_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-02-25 04:25:13 +03:00
|
|
|
adpcm_test.go contains tests for the adpcm package.
|
2019-02-13 04:23:06 +03:00
|
|
|
|
|
|
|
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
|
2019-02-27 09:16:15 +03:00
|
|
|
for more details.
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-27 09:16:15 +03:00
|
|
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
|
|
|
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
2019-02-13 04:23:06 +03:00
|
|
|
*/
|
|
|
|
|
2019-02-11 07:55:10 +03:00
|
|
|
package adpcm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"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) {
|
2019-03-07 08:38:00 +03:00
|
|
|
// Read input pcm.
|
2019-05-05 11:38:50 +03:00
|
|
|
pcm, err := ioutil.ReadFile("../../../test/test-data/av/input/original_8kHz_adpcm_test.pcm")
|
2019-02-11 07:55:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to read input PCM file: %v", err)
|
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Encode adpcm.
|
2019-03-15 08:17:08 +03:00
|
|
|
numBlocks := len(pcm) / PcmBS
|
|
|
|
comp := bytes.NewBuffer(make([]byte, 0, AdpcmBS*numBlocks))
|
2019-02-28 05:23:51 +03:00
|
|
|
enc := NewEncoder(comp)
|
2019-03-15 08:17:08 +03:00
|
|
|
_, err = enc.Write(pcm)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to write to encoder: %v", err)
|
2019-02-11 07:55:10 +03:00
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Read expected adpcm file.
|
2019-05-05 11:38:50 +03:00
|
|
|
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded_8kHz_adpcm_test.adpcm")
|
2019-02-11 07:55:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to read expected ADPCM file: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-28 05:23:51 +03:00
|
|
|
if !bytes.Equal(comp.Bytes(), exp) {
|
2019-02-11 07:55:10 +03:00
|
|
|
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) {
|
2019-03-07 08:38:00 +03:00
|
|
|
// Read adpcm.
|
2019-05-05 11:38:50 +03:00
|
|
|
comp, err := ioutil.ReadFile("../../../test/test-data/av/input/encoded_8kHz_adpcm_test.adpcm")
|
2019-02-12 07:19:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to read input ADPCM file: %v", err)
|
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Decode adpcm.
|
2019-03-15 08:17:08 +03:00
|
|
|
numBlocks := len(comp) / AdpcmBS
|
|
|
|
decoded := bytes.NewBuffer(make([]byte, 0, PcmBS*numBlocks))
|
2019-02-28 05:42:41 +03:00
|
|
|
dec := NewDecoder(decoded)
|
2019-03-15 09:37:22 +03:00
|
|
|
_, err = dec.Write(comp)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to write to decoder: %v", err)
|
2019-02-12 07:19:16 +03:00
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Read expected pcm file.
|
2019-05-05 11:38:50 +03:00
|
|
|
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded_8kHz_adpcm_test.pcm")
|
2019-02-12 07:19:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to read expected PCM file: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-28 05:42:41 +03:00
|
|
|
if !bytes.Equal(decoded.Bytes(), exp) {
|
2019-02-12 07:19:16 +03:00
|
|
|
t.Error("PCM generated does not match expected PCM")
|
|
|
|
}
|
|
|
|
}
|