-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser_test.go
149 lines (135 loc) · 3.41 KB
/
parser_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 riff
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
"time"
)
func TestParseHeader(t *testing.T) {
expectations := []struct {
input string
id [4]byte
size uint32
format [4]byte
}{
{"fixtures/sample.rmi", RiffID, 29632, rmiFormatID},
{"fixtures/sample.wav", RiffID, 53994, WavFormatID},
{"fixtures/sample.avi", RiffID, 230256, aviFormatID},
}
for _, exp := range expectations {
path, _ := filepath.Abs(exp.input)
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c := New(f)
err = c.ParseHeaders()
if err != nil {
t.Fatal(err)
}
if c.ID != exp.id {
t.Fatalf("%s of %s didn't match %s, got %s", "ID", exp.input, exp.id, c.ID)
}
if c.Size != exp.size {
t.Fatalf("%s of %s didn't match %d, got %d", "BlockSize", exp.input, exp.size, c.Size)
}
if c.Format != exp.format {
t.Fatalf("%s of %s didn't match %q, got %q", "Format", exp.input, exp.format, c.Format)
}
}
}
func TestParseWavHeaders(t *testing.T) {
expectations := []struct {
input string
headerSize uint32
format uint16
numChans uint16
sampleRate uint32
byteRate uint32
blockAlign uint16
bitsPerSample uint16
}{
// mono audio files
{"fixtures/sample.wav", 16, 1, 1, 44100, 88200, 2, 16},
// sterep audio files with junk, bext and more headers
{"fixtures/junkKick.wav", 40, 1, 2, 44100, 176400, 4, 16},
}
for _, exp := range expectations {
path, _ := filepath.Abs(exp.input)
t.Log(path)
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c := New(f)
if err := c.ParseHeaders(); err != nil {
t.Fatalf("%s for %s when parsing headers", err, path)
}
ch, err := c.NextChunk()
if err != nil {
t.Fatal(err)
}
for ; ch != nil; ch, err = c.NextChunk() {
if err != nil {
if err != io.EOF {
t.Fatal(err)
}
break
}
if bytes.Compare(ch.ID[:], FmtID[:]) == 0 {
ch.DecodeWavHeader(c)
} else {
ch.Done()
}
}
if c.wavHeaderSize != exp.headerSize {
t.Fatalf("%s didn't match %d, got %d", "header size", exp.headerSize, c.wavHeaderSize)
}
if c.WavAudioFormat != exp.format {
t.Fatalf("%s didn't match %d, got %d", "audio format", exp.format, c.WavAudioFormat)
}
if c.NumChannels != exp.numChans {
t.Fatalf("%s didn't match %d, got %d", "# of channels", exp.numChans, c.NumChannels)
}
if c.SampleRate != exp.sampleRate {
t.Fatalf("%s didn't match %d, got %d", "SampleRate", exp.sampleRate, c.SampleRate)
}
if c.AvgBytesPerSec != exp.byteRate {
t.Fatalf("%s didn't match %d, got %d", "ByteRate", exp.byteRate, c.AvgBytesPerSec)
}
if c.BlockAlign != exp.blockAlign {
t.Fatalf("%s didn't match %d, got %d", "BlockAlign", exp.blockAlign, c.BlockAlign)
}
if c.BitsPerSample != exp.bitsPerSample {
t.Fatalf("%s didn't match %d, got %d", "BitsPerSample", exp.bitsPerSample, c.BitsPerSample)
}
}
}
func TestContainerDuration(t *testing.T) {
expectations := []struct {
input string
dur time.Duration
}{
{"fixtures/sample.wav", time.Duration(612176870)},
}
for _, exp := range expectations {
path, _ := filepath.Abs(exp.input)
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c := New(f)
d, err := c.Duration()
if err != nil {
t.Fatal(err)
}
if d != exp.dur {
t.Fatalf("Container duration of %s didn't match %f, got %f", exp.input, exp.dur.Seconds(), d.Seconds())
}
}
}