-
Notifications
You must be signed in to change notification settings - Fork 42
/
rw_test.go
116 lines (87 loc) · 1.74 KB
/
rw_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
package wav
import (
"io/ioutil"
"math"
"os"
"testing"
"github.com/cheekybits/is"
)
func TestWriteRead_Int32(t *testing.T) {
is := is.New(t)
const (
bits = 32
rate = 44100
)
f, err := ioutil.TempFile("", "wavPkgtest")
is.NoErr(err)
testFname := f.Name()
meta := File{
Channels: 1,
SampleRate: rate,
SignificantBits: bits,
}
writer, err := meta.NewWriter(f)
is.NoErr(err)
var freq float64
freq = 0.0001
// one second
for n := 0; n < rate; n++ {
y := int32(0.8 * math.Pow(2, bits-1) * math.Sin(freq*float64(n)))
freq += 0.000002
err = writer.WriteInt32(y)
if err != nil {
t.Fatal(err)
}
}
err = writer.Close()
is.NoErr(err)
f, err = os.Open(testFname)
is.NoErr(err)
stat, err := f.Stat()
is.NoErr(err)
_, err = NewReader(f, stat.Size())
is.NoErr(err)
is.NoErr(os.Remove(testFname))
}
func TestWriteRead_Sample(t *testing.T) {
is := is.New(t)
const (
bits = 16
rate = 44100
)
f, err := ioutil.TempFile("", "wavPkgtest")
is.NoErr(err)
testFname := f.Name()
meta := File{
Channels: 1,
SampleRate: rate,
SignificantBits: bits,
}
writer, err := meta.NewWriter(f)
is.NoErr(err)
var freq float64
freq = 0.0001
var s = make([]byte, 2)
// one second
for n := 0; n < rate; n++ {
y := int32(0.8 * math.Pow(2, bits-1) * math.Sin(freq*float64(n)))
freq += 0.000002
// s[3] = byte((y >> 24) & 0xFF)
// s[2] = byte((y >> 16) & 0xFF)
s[1] = byte((y >> 8) & 0xFF)
s[0] = byte(y & 0xFF)
err = writer.WriteSample(s)
if err != nil {
t.Fatal(err)
}
}
err = writer.Close()
is.NoErr(err)
f, err = os.Open(testFname)
is.NoErr(err)
stat, err := f.Stat()
is.NoErr(err)
_, err = NewReader(f, stat.Size())
is.NoErr(err)
is.NoErr(os.Remove(testFname))
}