-
Notifications
You must be signed in to change notification settings - Fork 0
/
g711_test.go
237 lines (211 loc) · 5.44 KB
/
g711_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
Copyright (C) 2016 - 2024, Lefteris Zafiris <zaf@fastmail.com>
This program is free software, distributed under the terms of
the BSD 3-Clause License. See the LICENSE file
at the top of the source tree.
Package g711 implements encoding and decoding of G711 PCM sound data.
G.711 is an ITU-T standard for audio companding.
*/
package g711
import (
"bytes"
"io"
"os"
"testing"
"testing/iotest"
)
var EncoderTest = []struct {
data []byte
expected int
}{
{[]byte{}, 0},
{[]byte{0x01, 0x00}, 2},
{[]byte{0x01, 0x00, 0x7c, 0x7f, 0xd1, 0xd0, 0xd3, 0xd2, 0xdd, 0xdc, 0xdf, 0xde}, 12},
{[]byte{0x01, 0x00, 0x7c, 0x7f, 0xd1, 0xd0, 0xd3, 0xd2, 0xdd, 0xdc, 0xdf, 0xde, 0xd9}, 12},
}
var DecoderTest = []struct {
data []byte
expected int
}{
{[]byte{}, 0},
{[]byte{0x01, 0x00}, 4},
{[]byte{0x01, 0x00, 0x7c, 0x7f, 0xd1, 0xd0, 0xd3, 0xd2, 0xdd, 0xdc, 0xdf, 0xde}, 24},
{[]byte{0x01, 0x00, 0xdc, 0x7f, 0xd1, 0xd0, 0xd3, 0xd2, 0xdd, 0xdc, 0xdf, 0xde, 0xd9}, 26},
}
var TranscoderTest = []struct {
data []byte
expected int
}{
{[]byte{}, 0},
{[]byte{0x01, 0x00}, 2},
{[]byte{0x01, 0x00, 0x7c, 0x7f, 0xd1, 0xd0, 0xd3, 0xd2, 0xdd, 0xdc, 0xdf, 0xde}, 12},
{[]byte{0x01, 0x00, 0x7c, 0x7f, 0xd1, 0xd0, 0xd3, 0xd2, 0xdd, 0xdc, 0xdf, 0xde, 0xd9}, 13},
}
// Test Encoding
func TestEncode(t *testing.T) {
aenc, _ := NewAlawEncoder(io.Discard, Lpcm)
for _, tc := range EncoderTest {
i, _ := aenc.Write(tc.data)
if i != tc.expected {
t.Errorf("Alaw Encode: expected: %d , actual: %d", tc.expected, i)
}
}
uenc, _ := NewUlawEncoder(io.Discard, Lpcm)
for _, tc := range EncoderTest {
i, _ := uenc.Write(tc.data)
if i != tc.expected {
t.Errorf("ulaw Encode: expected: %d , actual: %d", tc.expected, i)
}
}
utrans, _ := NewUlawEncoder(io.Discard, Alaw)
for _, tc := range TranscoderTest {
i, _ := utrans.Write(tc.data)
if i != tc.expected {
t.Errorf("ulaw Transcode: expected: %d , actual: %d", tc.expected, i)
}
}
}
// Test Decoding
func TestDecode(t *testing.T) {
b := new(bytes.Buffer)
adec, _ := NewAlawDecoder(b)
for _, tc := range DecoderTest {
b.Write(tc.data)
p := make([]byte, 16)
var err error
var i, n int
for err == nil {
n, err = adec.Read(p)
i += n
}
if i != tc.expected {
t.Errorf("Alaw Decode: expected: %d , actual: %d", tc.expected, i)
}
}
b.Reset()
udec, _ := NewUlawDecoder(b)
for _, tc := range DecoderTest {
b.Write(tc.data)
p := make([]byte, 16)
var err error
var i, n int
for err == nil {
n, err = udec.Read(p)
i += n
}
if i != tc.expected {
t.Errorf("ulaw Decode: expected: %d , actual: %d", tc.expected, i)
}
}
b.Reset()
// Edge Case
udec, _ = NewUlawDecoder(iotest.TimeoutReader(b))
for _, tc := range DecoderTest {
b.Write(tc.data)
p := make([]byte, 16)
var err error
var i, n int
for err == nil || err.Error() == "timeout" {
n, err = udec.Read(p)
i += n
}
if i != tc.expected {
t.Errorf("ulaw Decode: expected: %d , actual: %d", tc.expected, i)
}
}
}
// Benchmark Encoding data to Alaw
func BenchmarkAEncode(b *testing.B) {
rawData, err := os.ReadFile("testing/speech.raw")
if err != nil {
b.Fatalf("Failed to read test data: %s\n", err)
}
b.SetBytes(int64(len(rawData)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
encoder, err := NewAlawEncoder(io.Discard, Lpcm)
if err != nil {
b.Fatalf("Failed to create Writer: %s\n", err)
}
_, err = encoder.Write(rawData)
if err != nil {
b.Fatalf("Encoding failed: %s\n", err)
}
}
}
// Benchmark Encoding data to Ulaw
func BenchmarkUEncode(b *testing.B) {
rawData, err := os.ReadFile("testing/speech.raw")
if err != nil {
b.Fatalf("Failed to read test data: %s\n", err)
}
b.SetBytes(int64(len(rawData)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
encoder, err := NewUlawEncoder(io.Discard, Lpcm)
if err != nil {
b.Fatalf("Failed to create Writer: %s\n", err)
}
_, err = encoder.Write(rawData)
if err != nil {
b.Fatalf("Encoding failed: %s\n", err)
}
}
}
// Benchmark transcoding g711 data
func BenchmarkTranscode(b *testing.B) {
alawData, err := os.ReadFile("testing/speech.alaw")
if err != nil {
b.Fatalf("Failed to read test data: %s\n", err)
}
b.SetBytes(int64(len(alawData)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
transcoder, err := NewAlawEncoder(io.Discard, Ulaw)
if err != nil {
b.Fatalf("Failed to create Writer: %s\n", err)
}
_, err = transcoder.Write(alawData)
if err != nil {
b.Fatalf("Transcoding failed: %s\n", err)
}
}
}
// Benchmark Decoding Ulaw data
func BenchmarkUDecode(b *testing.B) {
ulawData, err := os.ReadFile("testing/speech.ulaw")
if err != nil {
b.Fatalf("Failed to read test data: %s\n", err)
}
b.SetBytes(int64(len(ulawData)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoder, err := NewUlawDecoder(bytes.NewReader(ulawData))
if err != nil {
b.Fatalf("Failed to create Reader: %s\n", err)
}
_, err = io.Copy(io.Discard, decoder)
if err != nil {
b.Fatalf("Decoding failed: %s\n", err)
}
}
}
// Benchmark Decoding Alaw data
func BenchmarkADecode(b *testing.B) {
alawData, err := os.ReadFile("testing/speech.alaw")
if err != nil {
b.Fatalf("Failed to read test data: %s\n", err)
}
b.SetBytes(int64(len(alawData)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoder, err := NewAlawDecoder(bytes.NewReader(alawData))
if err != nil {
b.Fatalf("Failed to create Reader: %s\n", err)
}
_, err = io.Copy(io.Discard, decoder)
if err != nil {
b.Fatalf("Decoding failed: %s\n", err)
}
}
}