-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoefficients.go
More file actions
128 lines (115 loc) · 3.02 KB
/
coefficients.go
File metadata and controls
128 lines (115 loc) · 3.02 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
package ebur128
type biquadCoeffs struct { // DF2T biquad
b0, b1, b2 float64
a1, a2 float64
}
// Coefficients from BS.1770-4 Table 1.
var kweightStage1 = biquadCoeffs{ // head model high-shelf
b0: 1.53512485958697,
b1: -2.69169618940638,
b2: 1.19839281085285,
a1: -1.69065929318241,
a2: 0.73248077421585,
}
var kweightStage2 = biquadCoeffs{ // RLB high-pass
b0: 1.0,
b1: -2.0,
b2: 1.0,
a1: -1.99004745483398,
a2: 0.99007225036621,
}
// True peak FIR: 49-tap windowed sinc (Hanning), 4x polyphase oversampling.
// Phase 0 has 13 taps, phases 1-3 have 12 taps ([12] = 0).
//
// h[j] = sinc((j-24)*pi/4) * 0.5*(1-cos(2*pi*j/48)) j=0..48
// firCoeffs[j%4][j/4] = h[j]
const (
firFactor = 4 // oversampling factor (= number of polyphase phases)
firDelay = 13 // delay line size = ceil(49/4)
)
// firCoeffs[phase][tap], phase 0..3, tap 0..12.
var firCoeffs = [firFactor][firDelay]float64{
// Phase 0 (13 taps) - identity (Dirac at tap 6)
{
0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
},
// Phase 1 (12 taps, [12] unused)
{
-1.6744197591618296e-04,
+4.8959831425043820e-03,
-1.8526005935860405e-02,
+4.6265053486303574e-02,
-1.0345672595291239e-01,
+2.8868335557343622e-01,
+8.9646515071103394e-01,
-1.6145852728990459e-01,
+6.9158469679911128e-02,
-3.0107748292905211e-02,
+1.0359954969807055e-02,
-1.6317261636570237e-03,
0.0,
},
// Phase 2 (12 taps, [12] unused) - symmetric
{
-9.8601330506667092e-04,
+1.0358978571612684e-02,
-3.3703603627858556e-02,
+8.0138909394514291e-02,
-1.8112965507435574e-01,
+6.2577362601184794e-01,
+6.2577362601184794e-01,
-1.8112965507435577e-01,
+8.0138909394514291e-02,
-3.3703603627858576e-02,
+1.0358978571612687e-02,
-9.8601330506667721e-04,
0.0,
},
// Phase 3 (12 taps, [12] unused) - mirror of phase 1
{
-1.6317261636570261e-03,
+1.0359954969807033e-02,
-3.0107748292905193e-02,
+6.9158469679911128e-02,
-1.6145852728990456e-01,
+8.9646515071103394e-01,
+2.8868335557343622e-01,
-1.0345672595291240e-01,
+4.6265053486303574e-02,
-1.8526005935860422e-02,
+4.8959831425043872e-03,
-1.6744197591618296e-04,
0.0,
},
}
const (
weightFront = 1.0 // L, R, C
weightSurround = 1.41 // Ls, Rs (+1.5 dB)
weightLFE = 0.0 // LFE excluded
)
const (
sampleRate48k = 48000
subBlockMs = 100 // sub-block duration in ms
gatingBlocks = 4 // 400ms = 4 sub-blocks
shortTermBlocks = 30 // 3s = 30 sub-blocks
// returned when input is too short, silent, or fully gated out
loudnessUnmeasurable = -99.0
)
type kweightFilter struct { // DF2T
s1z1, s1z2 float64 // stage 1 state
s2z1, s2z2 float64 // stage 2 state
}
func (f *kweightFilter) process(x float64) float64 {
// high shelf
y := kweightStage1.b0*x + f.s1z1
f.s1z1 = kweightStage1.b1*x - kweightStage1.a1*y + f.s1z2
f.s1z2 = kweightStage1.b2*x - kweightStage1.a2*y
// high pass
x = y
y = kweightStage2.b0*x + f.s2z1
f.s2z1 = kweightStage2.b1*x - kweightStage2.a1*y + f.s2z2
f.s2z2 = kweightStage2.b2*x - kweightStage2.a2*y
return y
}