av/codec/pcm/pcm_test.go

118 lines
2.8 KiB
Go
Raw Normal View History

2019-04-09 09:12:44 +03:00
/*
NAME
pcm_test.go
DESCRIPTION
pcm_test.go contains functions for testing the pcm package.
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
pcm_test.go is Copyright (C) 2019 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 in gpl.txt.
If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
2019-04-09 09:12:44 +03:00
package pcm
import (
"bytes"
"io/ioutil"
"log"
"testing"
)
// TestResample tests the Resample function using a pcm file that contains audio of a freq. sweep.
// The output of the Resample function is compared with a file containing the expected result.
func TestResample(t *testing.T) {
inPath := "../../../test/test-data/av/input/sweep_400Hz_20000Hz_-3dBFS_5s_48khz.pcm"
expPath := "../../../test/test-data/av/output/sweep_400Hz_20000Hz_resampled_48to8kHz.pcm"
// Read input pcm.
inPcm, err := ioutil.ReadFile(inPath)
if err != nil {
log.Fatal(err)
}
2019-11-13 09:24:41 +03:00
format := BufferFormat{
Channels: 1,
Rate: 48000,
SFormat: S16_LE,
2019-04-09 09:12:44 +03:00
}
2019-11-13 09:24:41 +03:00
buf := Buffer{
2019-04-09 09:12:44 +03:00
Format: format,
Data: inPcm,
}
// Resample pcm.
2019-11-13 09:24:41 +03:00
resampled, err := Resample(buf, 8000)
2019-04-09 09:12:44 +03:00
if err != nil {
log.Fatal(err)
}
// Read expected resampled pcm.
exp, err := ioutil.ReadFile(expPath)
if err != nil {
log.Fatal(err)
}
// Compare result with expected.
if !bytes.Equal(resampled.Data, exp) {
2019-04-09 09:12:44 +03:00
t.Error("Resampled data does not match expected result.")
}
}
// TestStereoToMono tests the StereoToMono function using a pcm file that contains stereo audio.
// The output of the StereoToMono function is compared with a file containing the expected mono audio.
func TestStereoToMono(t *testing.T) {
inPath := "../../../test/test-data/av/input/stereo_DTMF_tones.pcm"
expPath := "../../../test/test-data/av/output/mono_DTMF_tones.pcm"
// Read input pcm.
inPcm, err := ioutil.ReadFile(inPath)
if err != nil {
log.Fatal(err)
}
2019-11-13 09:24:41 +03:00
format := BufferFormat{
Channels: 2,
Rate: 44100,
SFormat: S16_LE,
2019-04-09 09:12:44 +03:00
}
2019-11-13 09:24:41 +03:00
buf := Buffer{
2019-04-09 09:12:44 +03:00
Format: format,
Data: inPcm,
}
// Convert audio.
2019-11-13 09:24:41 +03:00
mono, err := StereoToMono(buf)
2019-04-09 09:12:44 +03:00
if err != nil {
log.Fatal(err)
}
// Read expected mono pcm.
exp, err := ioutil.ReadFile(expPath)
if err != nil {
log.Fatal(err)
}
// Compare result with expected.
if !bytes.Equal(mono.Data, exp) {
2019-04-09 09:12:44 +03:00
t.Error("Converted data does not match expected result.")
}
}