Test encoding / decoding API
authorHraban Luyat <hraban@0brg.net>
Sun, 8 Mar 2015 21:25:38 +0000 (21:25 +0000)
committerHraban Luyat <hraban@0brg.net>
Sun, 8 Mar 2015 21:25:38 +0000 (21:25 +0000)
Doesn't test the round trip signal, just that no errors were returned.

opus_test.go

index 74affc69c17c8c587b7f00cf0dad67acb3e448e4..c6ad59f31639885c0a2ad0e01bdac45d8e7cccbc 100644 (file)
@@ -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
 }