From 1697ef470799bb05e8dd9298b1636cb272bc90e5 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Sun, 8 Mar 2015 21:25:38 +0000 Subject: [PATCH] Test encoding / decoding API Doesn't test the round trip signal, just that no errors were returned. --- opus_test.go | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/opus_test.go b/opus_test.go index 74affc6..c6ad59f 100644 --- a/opus_test.go +++ b/opus_test.go @@ -1,17 +1,18 @@ package opus import ( + "math" "strings" "testing" ) -func Test_Version(t *testing.T) { +func TestVersion(t *testing.T) { if ver := Version(); !strings.HasPrefix(ver, "libopus") { t.Errorf("Unexpected linked libopus version: " + ver) } } -func Test_NewEncoder(t *testing.T) { +func TestEncoderNew(t *testing.T) { enc, err := NewEncoder(48000, 1, APPLICATION_VOIP) if err != nil || enc == nil { t.Errorf("Error creating new encoder: %v", err) @@ -22,7 +23,7 @@ func Test_NewEncoder(t *testing.T) { } } -func Test_NewDecoder(t *testing.T) { +func TestDecoderNew(t *testing.T) { dec, err := NewDecoder(48000, 1) if err != nil || dec == nil { t.Errorf("Error creating new decoder: %v", err) @@ -33,12 +34,38 @@ func Test_NewDecoder(t *testing.T) { } } -func Test_loop(t *testing.T) { +func addSine(buf []float32, sampleRate int, freq float64) { + factor := 2 * math.Pi * freq / float64(sampleRate) + for i := range buf { + buf[i] += float32(math.Sin(float64(i) * factor)) + } +} + +func TestCodecFloat32(t *testing.T) { // Create bogus input sound + const G4 = 391.995 const SAMPLE_RATE = 48000 const FRAME_SIZE_MS = 10 const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000 pcm := make([]float32, FRAME_SIZE) - //you know what I'll finish this later - _ = pcm + enc, err := NewEncoder(SAMPLE_RATE, 1, APPLICATION_VOIP) + if err != nil || enc == nil { + t.Fatalf("Error creating new encoder: %v", err) + } + addSine(pcm, SAMPLE_RATE, G4) + data, err := enc.EncodeFloat32(pcm) + if err != nil { + t.Fatalf("Couldn't encode data: %v", err) + } + dec, err := NewDecoder(SAMPLE_RATE, 1) + if err != nil || dec == nil { + t.Fatalf("Error creating new decoder: %v", err) + } + pcm2, err := dec.DecodeFloat32(data) + if err != nil { + t.Fatalf("Couldn't decode data: %v", err) + } + // TODO: Well, since this is lossy, checking the output signal is not + // straightforward.. I guess a FFT could work? + _ = pcm2 } -- 2.48.1