-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathebur128_test.go
More file actions
214 lines (184 loc) · 5.26 KB
/
ebur128_test.go
File metadata and controls
214 lines (184 loc) · 5.26 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package ebur128
import (
"math"
"os"
"path/filepath"
"testing"
)
const testdataDir = "testdata"
func skipIfMissing(t *testing.T, path string) {
t.Helper()
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Skipf("run testdata/generate.sh to generate test files (missing %s)", filepath.Base(path))
}
}
func loadAndMeasure(t *testing.T, file string) Result {
t.Helper()
path := filepath.Join(testdataDir, file)
skipIfMissing(t, path)
wav, err := readWAV(path)
if err != nil {
t.Fatalf("reading %s: %v", file, err)
}
if wav.sampleRate != sampleRate48k {
t.Fatalf("%s: expected %d Hz, got %d Hz", file, sampleRate48k, wav.sampleRate)
}
meter, err := New(LayoutStereo, sampleRate48k)
if err != nil {
t.Fatal(err)
}
meter.Write(wav.samples)
return meter.Loudness()
}
// ffmpeg reference: loud=-20.0, quiet=-40.0, lra=-20.1, gated=-20.1 LUFS
func TestIntegrated(t *testing.T) {
tests := []struct {
name string
file string
expected float64
tol float64
}{
{"loud-sine", "sine_stereo_1k_loud.wav", -20.0, 0.2},
{"quiet-sine", "sine_stereo_1k_quiet.wav", -40.0, 0.2},
{"lra-mix", "lra_stereo.wav", -20.1, 0.2},
{"gated", "gated_stereo.wav", -20.1, 0.2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := loadAndMeasure(t, tt.file)
if math.Abs(result.IntegratedLoudness-tt.expected) > tt.tol {
t.Errorf("integrated loudness = %.2f LUFS, expected %.1f ±%.1f",
result.IntegratedLoudness, tt.expected, tt.tol)
}
})
}
}
func TestLRA(t *testing.T) {
tests := []struct {
name string
file string
expected float64
tol float64
}{
{"constant-loud", "sine_stereo_1k_loud.wav", 0.0, 1.0},
{"constant-quiet", "sine_stereo_1k_quiet.wav", 0.0, 1.0},
{"loud-quiet-mix", "lra_stereo.wav", 20.0, 1.0},
{"silence-then-loud", "gated_stereo.wav", 7.8, 2.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := loadAndMeasure(t, tt.file)
if math.Abs(result.LoudnessRange-tt.expected) > tt.tol {
t.Errorf("LRA = %.2f LU, expected %.1f ±%.1f",
result.LoudnessRange, tt.expected, tt.tol)
}
})
}
}
func TestTruePeak(t *testing.T) {
tests := []struct {
name string
file string
expected float64
tol float64
}{
{"loud-sine", "sine_stereo_1k_loud.wav", -20.0, 0.5},
{"quiet-sine", "sine_stereo_1k_quiet.wav", -40.0, 0.5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := loadAndMeasure(t, tt.file)
if math.Abs(result.TruePeak-tt.expected) > tt.tol {
t.Errorf("true peak = %.2f dBTP, expected %.1f ±%.1f",
result.TruePeak, tt.expected, tt.tol)
}
})
}
}
func TestGating(t *testing.T) {
result := loadAndMeasure(t, "gated_stereo.wav")
// 5s silence + 5s sine: silence should be gated out
if result.IntegratedLoudness < -21.0 || result.IntegratedLoudness > -19.0 {
t.Errorf("integrated loudness = %.2f LUFS, expected ~-20",
result.IntegratedLoudness)
}
if result.GatedBlockCount == 0 {
t.Error("expected non-zero gated block count")
}
}
func TestNewUnsupportedSampleRate(t *testing.T) {
_, err := New(LayoutStereo, 44100)
if err == nil {
t.Fatal("expected error for unsupported sample rate")
}
}
func TestNewUnsupportedLayout(t *testing.T) {
_, err := New(ChannelLayout(99), sampleRate48k)
if err == nil {
t.Fatal("expected error for unsupported channel layout")
}
}
func TestWriteFloat32(t *testing.T) {
const n = 48000
f64 := make([]float64, n*2)
f32 := make([]float32, n*2)
for i := range n {
s := math.Sin(2 * math.Pi * 1000 * float64(i) / sampleRate48k)
f64[i*2] = s
f64[i*2+1] = s
f32[i*2] = float32(s)
f32[i*2+1] = float32(s)
}
ref, _ := New(LayoutStereo, sampleRate48k)
ref.Write(f64)
got1 := ref.Loudness()
meter, _ := New(LayoutStereo, sampleRate48k)
meter.WriteFloat32(f32)
got2 := meter.Loudness()
if math.Abs(got1.IntegratedLoudness-got2.IntegratedLoudness) > 0.5 {
t.Errorf("WriteFloat32 loudness %.2f differs from Write %.2f by more than 0.5 LUFS",
got2.IntegratedLoudness, got1.IntegratedLoudness)
}
}
func TestReset(t *testing.T) {
meter, _ := New(LayoutStereo, sampleRate48k)
buf := make([]float64, 48000*2)
for i := range 48000 {
s := math.Sin(2 * math.Pi * 1000 * float64(i) / sampleRate48k)
buf[i*2] = s
buf[i*2+1] = s
}
meter.Write(buf)
got := meter.Loudness()
if got.IntegratedLoudness < -50 {
t.Fatalf("expected measurable loudness, got %.2f", got.IntegratedLoudness)
}
meter.Reset()
got = meter.Loudness()
if got.IntegratedLoudness != loudnessUnmeasurable {
t.Errorf("after Reset, expected %.0f LUFS, got %.2f", loudnessUnmeasurable, got.IntegratedLoudness)
}
}
func TestFinalize(t *testing.T) {
meter, _ := New(LayoutStereo, sampleRate48k)
// 4.5 sub-blocks: without Finalize = 1 gating block, with = 2
n := 4800*4 + 2400
buf := make([]float64, n*2)
for i := range n {
s := math.Sin(2 * math.Pi * 1000 * float64(i) / sampleRate48k)
buf[i*2] = s
buf[i*2+1] = s
}
meter.Write(buf)
got := meter.Loudness()
meter.Reset()
meter.Write(buf)
meter.Finalize()
gotFin := meter.Loudness()
if got.GatedBlockCount != 1 {
t.Errorf("without Finalize: expected 1 gated block, got %d", got.GatedBlockCount)
}
if gotFin.GatedBlockCount != 2 {
t.Errorf("with Finalize: expected 2 gated blocks, got %d", gotFin.GatedBlockCount)
}
}