-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioduration_test.go
150 lines (140 loc) · 3.86 KB
/
audioduration_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
150
package audioduration
import (
"fmt"
"os"
"testing"
)
const delta = 1e-2
type audioTest struct {
path string
duration float64
}
func TestFLAC(t *testing.T) {
var sampleDuration float64 = 3.3993650793650794
testFile := "samples/sample.flac"
file, err := os.Open(testFile)
if err != nil {
t.Errorf("Sample FLAC file(%s): %s.\n", testFile, err)
}
defer file.Close()
d, err := FLAC(file)
fmt.Println(sampleDuration, d)
if err != nil {
t.Errorf("%s\n", err)
}
if (d - sampleDuration) > delta {
t.Errorf("too much error, expected '%v', found '%v'\n", sampleDuration, d)
}
}
func TestMp4(t *testing.T) {
var sampleDuration float64 = 3.4133333333333336
testFile := "samples/sample.mp4"
file, err := os.Open(testFile)
if err != nil {
t.Errorf("Sample MP4 file(%s): %s.\n", testFile, err)
}
defer file.Close()
d, err := Mp4(file)
fmt.Println(sampleDuration, d)
if err != nil {
t.Errorf("%s\n", err)
}
if (d - sampleDuration) > delta {
t.Errorf("too much error, expected '%v', found '%v'\n", sampleDuration, d)
}
}
func TestM4a(t *testing.T) {
var sampleDuration float64 = 3.4133333333333336
testFile := "samples/sample.m4a"
file, err := os.Open(testFile)
if err != nil {
t.Errorf("Sample M4A file(%s): %s.\n", testFile, err)
}
defer file.Close()
d, err := Mp4(file)
fmt.Println(sampleDuration, d)
if err != nil {
t.Errorf("%s\n", err)
}
if (d - sampleDuration) > delta {
t.Errorf("too much error, expected '%v', found '%v'\n", sampleDuration, d)
}
}
func TestMp3FileSet(t *testing.T) {
testFileSet := map[string]audioTest{
// https://commons.wikimedia.org/w/index.php?title=File%3ABWV_543-prelude.ogg
"MPEG Layer 3 (CBR)": {"samples/sample_cbr.mp3", 3.030204},
// https://commons.wikimedia.org/w/index.php?title=File%3ABWV_543-prelude.ogg
"MPEG Layer 3 (VBR)": {"samples/sample_vbr.mp3", 3.030204},
// https://github.com/dhowden/tag/tree/master/testdata
"MP3 with ID3 tags": {"samples/sample.id3v24.mp3", 3.4560563793862875},
"MP3 without ID3 tags": {"samples/sample.mp3", 3.4560563793862875},
}
for k, v := range testFileSet {
fmt.Printf("Testing: %s\n", k)
file, err := os.Open(v.path)
if err != nil {
t.Errorf("Sample MP3 file(%s): %s.\n", v.path, err)
}
defer file.Close()
d, err := Mp3(file)
fmt.Println(v.duration, d)
if err != nil {
t.Errorf("Sample MP3 file(%s): %s.\n", v.path, err)
}
if (d - v.duration) > delta {
t.Errorf("too much error, expected '%v', found '%v' on item '%v'\n", v.duration, d, k)
}
}
}
func TestMp2(t *testing.T) {
t.SkipNow()
testFile := "samples/sample.mp2"
file, err := os.Open(testFile)
if err != nil {
t.Errorf("Sample MP3 file(%s): %s.\n", testFile, err)
}
defer file.Close()
d, err := Mp3(file)
if err != nil {
t.Errorf("Sample MP3 file(%s): %s.\n", testFile, err)
}
fmt.Println(d)
}
func TestOgg(t *testing.T) {
var sampleDuration float64 = 6.104036281179138
// https://commons.wikimedia.org/wiki/File:Example.ogg
// https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg
testFile := "samples/example.ogg"
file, err := os.Open(testFile)
if err != nil {
t.Errorf("Sample OGG file(%s): %s.\n", testFile, err)
}
defer file.Close()
d, err := Ogg(file)
fmt.Println(sampleDuration, d)
if err != nil {
t.Errorf("Sample OGG file(%s): %s.\n", testFile, err)
}
if (d - sampleDuration) > delta {
t.Errorf("too much error, expected '%v', found '%v'\n", sampleDuration, d)
}
}
func TestDSD(t *testing.T) {
// t.SkipNow()
var sampleDuration float64 = 1.468
testFile := "samples/sample.dsf"
file, err := os.Open(testFile)
if err != nil {
t.Errorf("Sample DSD file(%s): %s.\n", testFile, err)
}
d, err := DSD(file)
defer file.Close()
fmt.Println(sampleDuration, d)
if err != nil {
t.Errorf("Sample DSD file(%s): %s.\n", testFile, err)
}
if (d - sampleDuration) > delta {
t.Errorf("too much error, expected '%v', found '%v'\n", sampleDuration, d)
}
}