-
Notifications
You must be signed in to change notification settings - Fork 15
/
buffer_test.go
239 lines (221 loc) · 6.84 KB
/
buffer_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
238
239
package beep_test
import (
"math"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/gopxl/beep/v2"
"github.com/gopxl/beep/v2/generators"
)
type bufferFormatTestCase struct {
Name string
Precision int
NumChannels int
Signed bool
Bytes []byte
Samples [2]float64
SkipDecodeTest bool
}
var bufferFormatTests = []bufferFormatTestCase{
// See https://gist.github.com/endolith/e8597a58bcd11a6462f33fa8eb75c43d
// for an explanation about the asymmetry in sample encodings in WAV when
// converting between ints and floats. Note that Beep does not follow the
// suggested solution. Instead, integer samples are divided by 1 more, so
// that the resulting float value falls within the range of -1.0 and 1.0.
// This is similar to how some other tools do the conversion.
{
Name: "1 channel 8bit WAV negative full scale",
Precision: 1,
NumChannels: 1,
Signed: false,
Bytes: []byte{0x00},
Samples: [2]float64{-1.0, -1.0},
},
{
Name: "1 channel 8bit WAV midpoint",
Precision: 1,
NumChannels: 1,
Signed: false,
Bytes: []byte{0x80},
Samples: [2]float64{0.0, 0.0},
},
{
// Because the WAV integer range is asymmetric, converting it to float
// by division will not result in an exactly 1.0 full scale float value.
// It will be 1 least significant bit integer value lower. "1", converted
// to float for an 8-bit WAV sample is 1 / (1 << 7).
Name: "1 channel 8bit WAV positive full scale minus 1 significant bit",
Precision: 1,
NumChannels: 1,
Signed: false,
Bytes: []byte{0xFF},
Samples: [2]float64{1.0 - (1.0 / (1 << 7)), 1.0 - (1.0 / (1 << 7))},
},
{
Name: "2 channel 8bit WAV full scale",
Precision: 1,
NumChannels: 2,
Signed: false,
Bytes: []byte{0x00, 0xFF},
Samples: [2]float64{-1.0, 1.0 - (1.0 / (1 << 7))},
},
{
Name: "1 channel 16bit WAV negative full scale",
Precision: 2,
NumChannels: 1,
Signed: true,
Bytes: []byte{0x00, 0x80},
Samples: [2]float64{-1.0, -1.0},
},
{
Name: "1 channel 16bit WAV midpoint",
Precision: 2,
NumChannels: 1,
Signed: true,
Bytes: []byte{0x00, 0x00},
Samples: [2]float64{0.0, 0.0},
},
{
// Because the WAV integer range is asymmetric, converting it to float
// by division will not result in an exactly 1.0 full scale float value.
// It will be 1 least significant bit integer value lower. "1", converted
// to float for an 16-bit WAV sample is 1 / (1 << 15).
Name: "1 channel 16bit WAV positive full scale minus 1 significant bit",
Precision: 2,
NumChannels: 1,
Signed: true,
Bytes: []byte{0xFF, 0x7F},
Samples: [2]float64{1.0 - (1.0 / (1 << 15)), 1.0 - (1.0 / (1 << 15))},
},
{
Name: "1 channel 8bit WAV float positive full scale clipping test",
Precision: 1,
NumChannels: 1,
Signed: false,
Bytes: []byte{0xFF},
Samples: [2]float64{1.0, 1.0},
SkipDecodeTest: true,
},
{
Name: "1 channel 16bit WAV float positive full scale clipping test",
Precision: 2,
NumChannels: 1,
Signed: true,
Bytes: []byte{0xFF, 0x7F},
Samples: [2]float64{1.0, 1.0},
SkipDecodeTest: true,
},
}
func TestFormatDecode(t *testing.T) {
for _, test := range bufferFormatTests {
if test.SkipDecodeTest {
continue
}
t.Run(test.Name, func(t *testing.T) {
format := beep.Format{
SampleRate: 44100,
Precision: test.Precision,
NumChannels: test.NumChannels,
}
var sample [2]float64
var n int
if test.Signed {
sample, n = format.DecodeSigned(test.Bytes)
} else {
sample, n = format.DecodeUnsigned(test.Bytes)
}
assert.Equal(t, len(test.Bytes), n)
assert.Equal(t, test.Samples, sample)
})
}
}
func TestFormatEncode(t *testing.T) {
for _, test := range bufferFormatTests {
t.Run(test.Name, func(t *testing.T) {
format := beep.Format{
SampleRate: 44100,
Precision: test.Precision,
NumChannels: test.NumChannels,
}
bytes := make([]byte, test.Precision*test.NumChannels)
var n int
if test.Signed {
n = format.EncodeSigned(bytes, test.Samples)
} else {
n = format.EncodeUnsigned(bytes, test.Samples)
}
assert.Equal(t, len(test.Bytes), n)
assert.Equal(t, test.Bytes, bytes)
})
}
}
func TestFormatEncodeDecode(t *testing.T) {
formats := make(chan beep.Format)
go func() {
defer close(formats)
for _, sampleRate := range []beep.SampleRate{100, 2347, 44100, 48000} {
for _, numChannels := range []int{1, 2, 3, 4} {
for _, precision := range []int{1, 2, 3, 4, 5, 6} {
formats <- beep.Format{
SampleRate: sampleRate,
NumChannels: numChannels,
Precision: precision,
}
}
}
}
}()
for format := range formats {
for i := 0; i < 20; i++ {
deviation := 2.0 / (math.Pow(2, float64(format.Precision)*8) - 2)
sample := [2]float64{rand.Float64()*2 - 1, rand.Float64()*2 - 1}
tmp := make([]byte, format.Width())
format.EncodeSigned(tmp, sample)
decoded, _ := format.DecodeSigned(tmp)
if format.NumChannels == 1 {
if math.Abs((sample[0]+sample[1])/2-decoded[0]) > deviation || decoded[0] != decoded[1] {
t.Fatalf("signed decoded sample is too different: %v -> %v (deviation: %v)", sample, decoded, deviation)
}
} else {
if math.Abs(sample[0]-decoded[0]) > deviation || math.Abs(sample[1]-decoded[1]) > deviation {
t.Fatalf("signed decoded sample is too different: %v -> %v (deviation: %v)", sample, decoded, deviation)
}
}
format.EncodeUnsigned(tmp, sample)
decoded, _ = format.DecodeUnsigned(tmp)
if format.NumChannels == 1 {
if math.Abs((sample[0]+sample[1])/2-decoded[0]) > deviation || decoded[0] != decoded[1] {
t.Fatalf("unsigned decoded sample is too different: %v -> %v (deviation: %v)", sample, decoded, deviation)
}
} else {
if math.Abs(sample[0]-decoded[0]) > deviation || math.Abs(sample[1]-decoded[1]) > deviation {
t.Fatalf("unsigned decoded sample is too different: %v -> %v (deviation: %v)", sample, decoded, deviation)
}
}
}
}
}
func TestBufferAppendPop(t *testing.T) {
formats := make(chan beep.Format)
go func() {
defer close(formats)
for _, numChannels := range []int{1, 2, 3, 4} {
formats <- beep.Format{
SampleRate: 44100,
NumChannels: numChannels,
Precision: 2,
}
}
}()
for format := range formats {
b := beep.NewBuffer(format)
b.Append(generators.Silence(768))
if b.Len() != 768 {
t.Fatalf("buffer length isn't equal to appended stream length: expected: %v, actual: %v (NumChannels: %v)", 768, b.Len(), format.NumChannels)
}
b.Pop(512)
if b.Len() != 768-512 {
t.Fatalf("buffer length isn't as expected after Pop: expected: %v, actual: %v (NumChannels: %v)", 768-512, b.Len(), format.NumChannels)
}
}
}