-
Notifications
You must be signed in to change notification settings - Fork 42
/
writer_test.go
122 lines (103 loc) · 3.1 KB
/
writer_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
package wav
import (
"bytes"
"io/ioutil"
"os"
"testing"
"github.com/cheekybits/is"
)
var wf = File{
SampleRate: 44100,
Channels: 1,
SignificantBits: 16,
}
func TestNewWriter_Header(t *testing.T) {
t.Parallel()
is := is.New(t)
f, err := ioutil.TempFile("", "wavPkgtest")
is.NoErr(err)
wr, err := wf.NewWriter(f)
is.NoErr(err)
is.Nil(wr.Close())
f, err = os.Open(f.Name())
is.NoErr(err)
b, err := ioutil.ReadAll(f)
is.NoErr(err)
is.Equal(len(b), 44)
is.True(bytes.Contains(b, riff))
is.True(bytes.Contains(b, wave))
is.True(bytes.Contains(b, fmt20))
is.Nil(os.Remove(f.Name()))
}
func TestNewWriter_1Sample(t *testing.T) {
t.Parallel()
is := is.New(t)
f, err := ioutil.TempFile("", "wavPkgtest")
is.NoErr(err)
wr, err := wf.NewWriter(f)
is.NoErr(err)
err = wr.WriteSample([]byte{1, 1})
is.NoErr(err)
is.Nil(wr.Close())
f, err = os.Open(f.Name())
is.NoErr(err)
b, err := ioutil.ReadAll(f)
is.NoErr(err)
is.Equal(len(b), 46)
is.True(bytes.Contains(b, riff))
is.True(bytes.Contains(b, wave))
is.True(bytes.Contains(b, fmt20))
is.Nil(os.Remove(f.Name()))
}
func bWriteByteSlice(sample []byte, samples int, b *testing.B) {
b.StopTimer()
is := is.New(b)
f, err := ioutil.TempFile("", "wavPkgtest")
is.NoErr(err)
defer os.Remove(f.Name())
wr, err := wf.NewWriter(f)
defer wr.Close()
is.NoErr(err)
b.SetBytes(int64(2 * samples))
b.StartTimer()
for i := 0; i < b.N; i++ {
for i := 0; i < samples; i++ {
if err := wr.WriteSample(sample); err != nil {
b.Fatal(err)
}
}
}
}
func BenchmarkWriteBuf_16sample(b *testing.B) { bWriteByteSlice([]byte{0, 0}, 16, b) }
func BenchmarkWriteBuf_32sample(b *testing.B) { bWriteByteSlice([]byte{0, 0}, 32, b) }
func BenchmarkWriteBuf_64sample(b *testing.B) { bWriteByteSlice([]byte{0, 0}, 64, b) }
func BenchmarkWriteBuf_10thSec(b *testing.B) { bWriteByteSlice([]byte{0, 0}, 44100/10, b) }
func BenchmarkWriteBuf_HalfSec(b *testing.B) { bWriteByteSlice([]byte{0, 0}, 44100/2, b) }
func BenchmarkWriteBuf_1Sec(b *testing.B) { bWriteByteSlice([]byte{0, 0}, 44100, b) }
func BenchmarkWriteBuf_2Sec(b *testing.B) { bWriteByteSlice([]byte{0, 0}, 2*44100, b) }
func benchWriteInt(sample int32, samples int, b *testing.B) {
b.StopTimer()
is := is.New(b)
f, err := ioutil.TempFile("", "wavPkgtest")
is.NoErr(err)
defer os.Remove(f.Name())
wr, err := wf.NewWriter(f)
defer wr.Close()
is.NoErr(err)
b.SetBytes(int64(2 * samples))
b.StartTimer()
for i := 0; i < b.N; i++ {
for i := 0; i < samples/2; i++ {
if err := wr.WriteInt32(sample); err != nil {
b.Fatal(err)
}
}
}
}
func BenchmarkWriteInt32_16sample(b *testing.B) { benchWriteInt(0, 16, b) }
func BenchmarkWriteInt32_32sample(b *testing.B) { benchWriteInt(0, 32, b) }
func BenchmarkWriteInt32_64sample(b *testing.B) { benchWriteInt(0, 64, b) }
func BenchmarkWriteInt32_10thSec(b *testing.B) { benchWriteInt(0, 44100/10, b) }
func BenchmarkWriteInt32_HalfSec(b *testing.B) { benchWriteInt(0, 44100/2, b) }
func BenchmarkWriteInt32_1Sec(b *testing.B) { benchWriteInt(0, 44100, b) }
func BenchmarkWriteInt32_2Sec(b *testing.B) { benchWriteInt(0, 2*44100, b) }