-
Notifications
You must be signed in to change notification settings - Fork 15
/
mixer_test.go
128 lines (99 loc) · 3.19 KB
/
mixer_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
package beep_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/gopxl/beep/v2"
"github.com/gopxl/beep/v2/internal/testtools"
)
func TestMixer_MixesSamples(t *testing.T) {
epsilon := 0.000001
s1, data1 := testtools.RandomDataStreamer(200)
s2, data2 := testtools.RandomDataStreamer(200)
m := beep.Mixer{}
m.Add(s1)
m.Add(s2)
samples := testtools.CollectNum(100, &m)
for i, s := range samples {
wantL := data1[i][0] + data2[i][0]
wantR := data1[i][1] + data2[i][1]
if s[0] < wantL-epsilon || s[0] > wantL+epsilon {
t.Fatalf("unexpected value for mixed samples; expected: %f, got: %f", wantL, s[0])
}
if s[1] < wantR-epsilon || s[1] > wantR+epsilon {
t.Fatalf("unexpected value for mixed samples; expected: %f, got: %f", wantR, s[1])
}
}
s3, data3 := testtools.RandomDataStreamer(100)
m.Add(s3)
samples = testtools.CollectNum(100, &m)
for i, s := range samples {
wantL := data1[100+i][0] + data2[100+i][0] + data3[i][0]
wantR := data1[100+i][1] + data2[100+i][1] + data3[i][1]
if s[0] < wantL-epsilon || s[0] > wantL+epsilon {
t.Fatalf("unexpected value for mixed samples; expected: %f, got: %f", wantL, s[0])
}
if s[1] < wantR-epsilon || s[1] > wantR+epsilon {
t.Fatalf("unexpected value for mixed samples; expected: %f, got: %f", wantR, s[1])
}
}
}
func TestMixer_DrainedStreamersAreRemoved(t *testing.T) {
s1, _ := testtools.RandomDataStreamer(50)
s2, _ := testtools.RandomDataStreamer(65)
m := beep.Mixer{}
m.Add(s1)
m.Add(s2)
// Drain s1 but not so far it returns false.
samples := testtools.CollectNum(50, &m)
assert.Len(t, samples, 50)
assert.Equal(t, 2, m.Len())
// Drain s1 (s1 returns !ok, n == 0)
samples = testtools.CollectNum(10, &m)
assert.Len(t, samples, 10)
assert.Equal(t, 1, m.Len())
// Drain s2 (s2 returns ok, n < len(samples))
samples = testtools.CollectNum(10, &m)
assert.Len(t, samples, 10)
assert.Equal(t, 0, m.Len())
}
func TestMixer_PlaysSilenceWhenNoStreamersProduceSamples(t *testing.T) {
m := beep.Mixer{}
// Test silence before streamers are added.
samples := testtools.CollectNum(10, &m)
assert.Len(t, samples, 10)
assert.Equal(t, make([][2]float64, 10), samples)
// Test silence after streamer has only streamed part of the requested samples.
s, data := testtools.RandomDataStreamer(50)
m.Add(s)
samples = testtools.CollectNum(100, &m)
assert.Len(t, samples, 100)
assert.Equal(t, 0, m.Len())
assert.Equal(t, data, samples[:50])
assert.Equal(t, make([][2]float64, 50), samples[50:])
// Test silence after streamers have been drained & removed.
samples = testtools.CollectNum(10, &m)
assert.Len(t, samples, 10)
assert.Equal(t, make([][2]float64, 10), samples)
}
func BenchmarkMixer_MultipleStreams(b *testing.B) {
s1, _ := testtools.RandomDataStreamer(b.N)
s2, _ := testtools.RandomDataStreamer(b.N)
m := beep.Mixer{}
m.Add(s1)
m.Add(s2)
b.StartTimer()
testtools.CollectNum(b.N, &m)
}
func BenchmarkMixer_OneStream(b *testing.B) {
s, _ := testtools.RandomDataStreamer(b.N)
m := beep.Mixer{}
m.Add(s)
b.StartTimer()
testtools.CollectNum(b.N, &m)
}
func BenchmarkMixer_Silence(b *testing.B) {
m := beep.Mixer{}
// Don't add any streamers
b.StartTimer()
testtools.CollectNum(b.N, &m)
}