-
Notifications
You must be signed in to change notification settings - Fork 59
/
decoder_test.go
68 lines (61 loc) · 1.68 KB
/
decoder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright © Go Opus Authors (see AUTHORS file)
//
// License for use of this code is detailed in the LICENSE file
package opus
import (
"testing"
)
func TestDecoderNew(t *testing.T) {
dec, err := NewDecoder(48000, 1)
if err != nil || dec == nil {
t.Errorf("Error creating new decoder: %v", err)
}
dec, err = NewDecoder(12345, 1)
if err == nil || dec != nil {
t.Errorf("Expected error for illegal samplerate 12345")
}
}
func TestDecoderUnitialized(t *testing.T) {
var dec Decoder
_, err := dec.Decode(nil, nil)
if err != errDecUninitialized {
t.Errorf("Expected \"unitialized decoder\" error: %v", err)
}
_, err = dec.DecodeFloat32(nil, nil)
if err != errDecUninitialized {
t.Errorf("Expected \"unitialized decoder\" error: %v", err)
}
}
func TestDecoder_GetLastPacketDuration(t *testing.T) {
const G4 = 391.995
const SAMPLE_RATE = 48000
const FRAME_SIZE_MS = 60
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
pcm := make([]int16, FRAME_SIZE)
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
if err != nil || enc == nil {
t.Fatalf("Error creating new encoder: %v", err)
}
addSine(pcm, SAMPLE_RATE, G4)
data := make([]byte, 1000)
n, err := enc.Encode(pcm, data)
if err != nil {
t.Fatalf("Couldn't encode data: %v", err)
}
data = data[:n]
dec, err := NewDecoder(SAMPLE_RATE, 1)
if err != nil || dec == nil {
t.Fatalf("Error creating new decoder: %v", err)
}
n, err = dec.Decode(data, pcm)
if err != nil {
t.Fatalf("Couldn't decode data: %v", err)
}
samples, err := dec.LastPacketDuration()
if err != nil {
t.Fatalf("Couldn't get last packet duration: %v", err)
}
if samples != n {
t.Fatalf("Wrong duration length. Expected %d. Got %d", n, samples)
}
}