forked from zaf/g711
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g711_test.go
205 lines (191 loc) · 4.99 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
/*
Copyright (C) 2016 - 2017, Lefteris Zafiris <[email protected]>
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.0 compressed sound data.
G.711 is an ITU-T standard for audio companding.
*/
package g711
import (
"bytes"
"io/ioutil"
"log"
"testing"
)
var EncoderTest = []struct {
data []byte
expected int
}{
{[]byte{}, 0},
{[]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, 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, 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(ioutil.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(ioutil.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(ioutil.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, 2*tc.expected)
i, _ := adec.Read(p)
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, 2*tc.expected)
i, _ := udec.Read(p)
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 := ioutil.ReadFile("testing/speech.raw")
if err != nil {
log.Printf("Failed to read test data: %s\n", err)
b.FailNow()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
encoder, err := NewAlawEncoder(ioutil.Discard, Lpcm)
if err != nil {
log.Printf("Failed to create Writer: %s\n", err)
b.FailNow()
}
_, err = encoder.Write(rawData)
if err != nil {
log.Printf("Encoding failed: %s\n", err)
b.FailNow()
}
}
}
// Benchmark Encoding data to Ulaw
func BenchmarkUEncode(b *testing.B) {
rawData, err := ioutil.ReadFile("testing/speech.raw")
if err != nil {
log.Printf("Failed to read test data: %s\n", err)
b.FailNow()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
encoder, err := NewUlawEncoder(ioutil.Discard, Lpcm)
if err != nil {
log.Printf("Failed to create Writer: %s\n", err)
b.FailNow()
}
_, err = encoder.Write(rawData)
if err != nil {
log.Printf("Encoding failed: %s\n", err)
b.FailNow()
}
}
}
// Benchmark transcoding g711 data
func BenchmarkTranscode(b *testing.B) {
alawData, err := ioutil.ReadFile("testing/speech.alaw")
if err != nil {
log.Printf("Failed to read test data: %s\n", err)
b.FailNow()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
transcoder, err := NewAlawEncoder(ioutil.Discard, Ulaw)
if err != nil {
log.Printf("Failed to create Writer: %s\n", err)
b.FailNow()
}
_, err = transcoder.Write(alawData)
if err != nil {
log.Printf("Transcoding failed: %s\n", err)
b.FailNow()
}
}
}
// Benchmark Decoding Ulaw data
func BenchmarkUDecode(b *testing.B) {
ulawData, err := ioutil.ReadFile("testing/speech.ulaw")
if err != nil {
log.Printf("Failed to read test data: %s\n", err)
b.FailNow()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoder, err := NewUlawDecoder(bytes.NewReader(ulawData))
if err != nil {
log.Printf("Failed to create Reader: %s\n", err)
b.FailNow()
}
_, err = ioutil.ReadAll(decoder)
if err != nil {
log.Printf("Decoding failed: %s\n", err)
b.FailNow()
}
}
}
// Benchmark Decoding Alaw data
func BenchmarkADecode(b *testing.B) {
alawData, err := ioutil.ReadFile("testing/speech.alaw")
if err != nil {
log.Printf("Failed to read test data: %s\n", err)
b.FailNow()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoder, err := NewAlawDecoder(bytes.NewReader(alawData))
if err != nil {
log.Printf("Failed to create Reader: %s\n", err)
b.FailNow()
}
_, err = ioutil.ReadAll(decoder)
if err != nil {
log.Printf("Decoding failed: %s\n", err)
b.FailNow()
}
}
}