This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
forked from youpy/go-wav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
82 lines (61 loc) · 1.69 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
package wav
import (
"io/ioutil"
"math"
"testing"
)
func TestRead(t *testing.T) {
blockAlign := 4
file, err := fixtureFile("a.wav")
if err != nil {
t.Fatalf("Failed to open fixture file")
}
reader := NewReader(file)
fmt, err := reader.Format()
if err != nil {
t.Fatal(err)
}
if fmt.AudioFormat != AudioFormatPCM {
t.Fatalf("Audio format is invalid: %d", fmt.AudioFormat)
}
if fmt.NumChannels != 2 {
t.Fatalf("Number of channels is invalid: %d", fmt.NumChannels)
}
if fmt.SampleRate != 44100 {
t.Fatalf("Sample rate is invalid: %d", fmt.SampleRate)
}
if fmt.ByteRate != 44100*4 {
t.Fatalf("Byte rate is invalid: %d", fmt.ByteRate)
}
if int(fmt.BlockAlign) != blockAlign {
t.Fatalf("Block align is invalid: %d", fmt.BlockAlign)
}
if fmt.BitsPerSample != 16 {
t.Fatalf("Bits per sample is invalid: %d", fmt.BitsPerSample)
}
samples, err := reader.ReadSamples(1)
if len(samples) != 1 {
t.Fatalf("Length of samples is invalid: %d", len(samples))
}
sample := samples[0]
if reader.IntValue(sample, 0) != 318 {
t.Fatalf("Value is invalid: %d", reader.IntValue(sample, 0))
}
if reader.IntValue(sample, 1) != 289 {
t.Fatalf("Value is invalid: %d", reader.IntValue(sample, 1))
}
if math.Abs(reader.FloatValue(sample, 0)-0.009705) > 0.0001 {
t.Fatalf("Value is invalid: %f", reader.FloatValue(sample, 0))
}
if math.Abs(reader.FloatValue(sample, 1)-0.008820) > 0.0001 {
t.Fatalf("Value is invalid: %f", reader.FloatValue(sample, 1))
}
bytes, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
if len(bytes) != int(reader.WavData.Size)-(1*blockAlign) {
t.Fatalf("Data size is invalid: %d", len(bytes))
}
t.Logf("Data size: %d", len(bytes))
}