forked from faiface/beep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_test.go
61 lines (53 loc) · 1.85 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
package beep_test
import (
"math"
"math/rand"
"testing"
"github.com/faiface/beep"
)
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)
}
}
}
}
}