-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
149 lines (137 loc) · 4.81 KB
/
reader_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
package wave_test
import (
"bytes"
"fmt"
"log"
"testing"
"github.com/bake/wave"
)
func TestReader(t *testing.T) {
r := bytes.NewReader([]byte{
// R, I, F, F, 2084, W, A, V, E,
0x52, 0x49, 0x46, 0x46, 0x24, 0x08, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,
// f, m, t, ␣, 16, 1, 2,
0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00,
// 22050, 88200, 4, 16,
0x22, 0x56, 0x00, 0x00, 0x88, 0x58, 0x01, 0x00, 0x04, 0x00, 0x10, 0x00,
// s, l, n, t, 4,
0x73, 0x6c, 0x6e, 0x74, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
// d, a, t, a, 28, 0, 0,
0x64, 0x61, 0x74, 0x61, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 5924, -3298, 4924, 5180, -1770, -1768,
0x24, 0x17, 0x1e, 0xf3, 0x3c, 0x13, 0x3c, 0x14, 0x16, 0xf9, 0x18, 0xf9,
// -6348, -23005, -3524, -3548, -12783, 3354,
0x34, 0xe7, 0x23, 0xa6, 0x3c, 0xf2, 0x24, 0xf2, 0x11, 0xce, 0x1a, 0x0d,
// d, a, t, a, 16, 0, 0,
0x64, 0x61, 0x74, 0x61, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 5924, -3298, 4924, 5180, -1770, -1768,
0x24, 0x17, 0x1e, 0xf3, 0x3c, 0x13, 0x3c, 0x14, 0x16, 0xf9, 0x18, 0xf9,
})
out := []int{
0, 0, 5924, -3298, 4924, 5180, -1770, -1768,
-6348, -23005, -3524, -3548, -12783, 3354,
0, 0, 5924, -3298, 4924, 5180, -1770, -1768,
}
wavr, err := wave.NewReader(r)
if err != nil {
t.Fatalf("could not create new wave reader: %v", err)
}
samples, err := wavr.Samples()
if err != nil {
t.Fatalf("could not read samples: %v", err)
}
if fmt.Sprint(samples) != fmt.Sprint(out) {
t.Fatalf("expected samples to be\n%v, got\n%v", out, samples)
}
}
func TestNewReader(t *testing.T) {
tt := []struct {
name string
res bool
data []byte
}{
{
name: "empty reader",
res: false,
data: []byte{},
},
{
name: "wrong riff id",
res: false,
data: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
{
name: "wrong riff type",
res: false,
data: []byte{
// R, I, F, F, 4, 0, 0, 0, 0,
0x52, 0x49, 0x46, 0x46, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
{
name: "no format chunk",
res: false,
data: []byte{
// R, I, F, F, 4, W, A, V, E,
0x52, 0x49, 0x46, 0x46, 0x04, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,
},
},
{
name: "unexpected chunk id",
res: false,
data: []byte{
// R, I, F, F, 12, W, A, V, E,
0x52, 0x49, 0x46, 0x46, 0x0c, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,
// s, l, n, t, 4,
0x73, 0x6c, 0x6e, 0x74, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
r := bytes.NewReader(tc.data)
_, err := wave.NewReader(r)
if err != nil && tc.res {
t.Fatalf("unexpected error: %v\n", err)
}
if err == nil && !tc.res {
t.Fatal("expected an error")
}
})
}
}
func ExampleReader() {
r := bytes.NewReader([]byte{
// R, I, F, F, 76, W, A, V, E,
0x52, 0x49, 0x46, 0x46, 0x50, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,
// f, m, t, ␣, 16, 1, 2,
0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00,
// 44100, 176400, 4, 16,
0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, 0x00, 0x10, 0x00,
// d, a, t, a, 44, 0, 0,
0x64, 0x61, 0x74, 0x61, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 5924, -3298, 4924, 5180, -1770, -1768,
0x24, 0x17, 0x1e, 0xf3, 0x3c, 0x13, 0x3c, 0x14, 0x16, 0xf9, 0x18, 0xf9,
// -6348, -23005, -3524, -3548, -12783, 3354,
0x34, 0xe7, 0x23, 0xa6, 0x3c, 0xf2, 0x24, 0xf2, 0x11, 0xce, 0x1a, 0x0d,
// 0, 0, 5924, -3298, 4924, 5180,
0x00, 0x00, 0x00, 0x00, 0x24, 0x17, 0x1e, 0xf3, 0x3c, 0x13, 0x3c, 0x14,
// -1770, -1768,
0x16, 0xf9, 0x18, 0xf9,
})
wavr, err := wave.NewReader(r)
if err != nil {
log.Fatalf("could not create new wave reader: %v", err)
}
fmt.Printf("SampleRate: %d\n", wavr.Format.SampleRate)
samples, err := wavr.Samples()
if err != nil {
log.Fatalf("could not read samples: %v", err)
}
fmt.Printf("Samples: %v\n", samples[:10])
// Output:
// SampleRate: 44100
// Samples: [0 0 5924 -3298 4924 5180 -1770 -1768 -6348 -23005]
}