forked from hajimehoshi/ebiten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_test.go
259 lines (229 loc) · 6.08 KB
/
loop_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package audio_test
import (
"bytes"
"io"
"math"
"testing"
"github.com/hajimehoshi/ebiten/v2/audio"
)
func TestInfiniteLoop(t *testing.T) {
indexToByte := func(index int) byte {
return byte(math.Sin(float64(index)) * 256)
}
src := make([]byte, 256)
for i := range src {
src[i] = indexToByte(i)
}
l := audio.NewInfiniteLoop(bytes.NewReader(src), int64(len(src)))
buf := make([]byte, len(src)*4)
if _, err := io.ReadFull(l, buf); err != nil {
t.Error(err)
}
for i, b := range buf {
got := b
want := indexToByte(i % len(src))
if got != want {
t.Errorf("index: %d, got: %v, want: %v", i, got, want)
}
}
n, err := l.Seek(int64(len(src))*5+128, io.SeekStart)
if err != nil {
t.Error(err)
}
if want := int64(128); n != want {
t.Errorf("got: %v, want: %v", n, want)
}
n2, err := l.Seek(int64(len(src))*6+64, io.SeekCurrent)
if err != nil {
t.Error(err)
}
if want := int64(192); n2 != want {
t.Errorf("got: %v, want: %v", n, want)
}
buf2 := make([]byte, len(src)*7)
if _, err := io.ReadFull(l, buf2); err != nil {
t.Error(err)
}
for i, b := range buf2 {
got := b
want := indexToByte((i + 192) % len(src))
if got != want {
t.Errorf("index: %d, got: %v, want: %v", i, got, want)
}
}
// Seek to negative position is an error.
if _, err := l.Seek(-1, io.SeekStart); err == nil {
t.Errorf("got: %v, want: %v", err, nil)
}
}
func TestInfiniteLoopWithIntro(t *testing.T) {
const (
srcLength = 17 * 4
introLength = 19 * 4
loopLength = 23 * 4
)
indexToByte := func(index int) byte {
return byte(math.Sin(float64(index)) * 256)
}
src := make([]byte, srcLength)
for i := range src {
src[i] = indexToByte(i)
}
srcInf := audio.NewInfiniteLoop(bytes.NewReader(src), srcLength)
srcInf.SetNoBlendForTesting(true)
l := audio.NewInfiniteLoopWithIntro(srcInf, introLength, loopLength)
l.SetNoBlendForTesting(true)
buf := make([]byte, srcLength*4)
if _, err := io.ReadFull(l, buf); err != nil {
t.Error(err)
}
for i, b := range buf {
got := b
want := byte(0)
if i < introLength {
want = indexToByte(i % srcLength)
} else {
want = indexToByte(((i-introLength)%loopLength + introLength) % srcLength)
}
if got != want {
t.Errorf("index: %d, got: %v, want: %v", i, got, want)
}
}
n, err := l.Seek(srcLength*5+128, io.SeekStart)
if err != nil {
t.Error(err)
}
if want := int64((srcLength*5+128-introLength)%loopLength + introLength); n != want {
t.Errorf("got: %v, want: %v", n, want)
}
n2, err := l.Seek(srcLength*6+64, io.SeekCurrent)
if err != nil {
t.Error(err)
}
if want := int64(((srcLength*11+192)-introLength)%loopLength + introLength); n2 != want {
t.Errorf("got: %v, want: %v", n, want)
}
buf2 := make([]byte, srcLength*7)
if _, err := io.ReadFull(l, buf2); err != nil {
t.Error(err)
}
for i, b := range buf2 {
got := b
idx := ((int(n2+int64(i))-introLength)%loopLength + introLength) % srcLength
want := indexToByte(idx)
if got != want {
t.Errorf("index: %d, got: %v, want: %v", i, got, want)
}
}
// Seek to negative position is an error.
if _, err := l.Seek(-1, io.SeekStart); err == nil {
t.Errorf("got: %v, want: %v", err, nil)
}
}
func TestInfiniteLoopWithIncompleteSize(t *testing.T) {
// s1 should work as if 4092 is given.
s1 := audio.NewInfiniteLoop(bytes.NewReader(make([]byte, 4096)), 4095)
n1, err := s1.Seek(4093, io.SeekStart)
if err != nil {
t.Error(err)
}
if got, want := n1, int64(4093-4092); got != want {
t.Errorf("got: %d, want: %d", got, want)
}
// s2 should work as if 2044 and 2044 are given.
s2 := audio.NewInfiniteLoopWithIntro(bytes.NewReader(make([]byte, 4096)), 2047, 2046)
n2, err := s2.Seek(4093, io.SeekStart)
if err != nil {
t.Error(err)
}
if got, want := n2, int64(2044+(4093-(2044+2044))); got != want {
t.Errorf("got: %d, want: %d", got, want)
}
}
type slowReader struct {
src io.ReadSeeker
eof bool
}
func (s *slowReader) Read(buf []byte) (int, error) {
if len(buf) == 0 {
if s.eof {
return 0, io.EOF
}
return 0, nil
}
n, err := s.src.Read(buf[:1])
if err == io.EOF {
s.eof = true
}
return n, err
}
func (s *slowReader) Seek(offset int64, whence int) (int64, error) {
s.eof = false
return s.src.Seek(offset, whence)
}
func TestInfiniteLoopWithSlowSource(t *testing.T) {
src := make([]byte, 4096)
for i := range src {
src[i] = byte(i)
}
r := &slowReader{
src: bytes.NewReader(src),
}
loop := audio.NewInfiniteLoop(r, 4096)
buf := make([]byte, 4096)
// With a slow source, whose Read always reads at most one byte,
// an infinite loop should adjust the reading size along with bitDepthInBytes (= 2).
n0, err := loop.Read(buf)
if err != nil {
t.Error(err)
}
if got, want := n0, 0; got != want {
t.Errorf("got: %d, want: %d", got, want)
}
n1, err := loop.Read(buf)
if err != nil {
t.Error(err)
}
if got, want := n1, 2; got != want {
t.Errorf("got: %d, want: %d", got, want)
}
if got, want := buf[0], byte(0); got != want {
t.Errorf("got: %d, want: %d", got, want)
}
if got, want := buf[1], byte(1); got != want {
t.Errorf("got: %d, want: %d", got, want)
}
n2, err := loop.Read(buf)
if err != nil {
t.Error(err)
}
if got, want := n2, 0; got != want {
t.Errorf("got: %d, want: %d", got, want)
}
n3, err := loop.Read(buf)
if err != nil {
t.Error(err)
}
if got, want := n3, 2; got != want {
t.Errorf("got: %d, want: %d", got, want)
}
if got, want := buf[0], byte(2); got != want {
t.Errorf("got: %d, want: %d", got, want)
}
if got, want := buf[1], byte(3); got != want {
t.Errorf("got: %d, want: %d", got, want)
}
}