-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio-processor.js
More file actions
198 lines (160 loc) · 6.69 KB
/
Copy pathaudio-processor.js
File metadata and controls
198 lines (160 loc) · 6.69 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
'use strict';
// audio-processor.js — runs on Chrome's dedicated audio rendering thread
// Implements ITU-R BS.1770-4 K-weighting in Direct Form II Transposed IIR,
// accumulates mean-square blocks, computes LUFS, and applies smooth gain.
// ── K-weighting coefficients ───────────────────────────────────────────────
function computeKWeightCoeffs(fs) {
const sr = Math.round(fs);
if (sr === 48000) {
return {
shelf: [1.53512485958697, -2.69169618940638, 1.19839281085285, -1.69065929318241, 0.73248077421585],
hp: [1.0, -2.0, 1.0, -1.99004745483398, 0.99007225036603]
};
}
if (sr === 44100) {
return {
shelf: [1.5462167024919462, -2.680150567300573, 1.1938892730575532, -1.6459508112909427, 0.7082671125584606],
hp: [1.0, -2.0, 1.0, -1.9891696855088082, 0.9892297251653353]
};
}
const A = Math.pow(10, 4.0 / 40);
const w0 = 2.0 * Math.PI * 1681.97 / fs;
const c0 = Math.cos(w0), s0 = Math.sin(w0), sqA = Math.sqrt(A);
const al = s0 * 0.5 * Math.SQRT2;
const ib0s = A * ((A+1) + (A-1)*c0 + 2*sqA*al);
const ib1s = -2*A * ((A-1) + (A+1)*c0);
const ib2s = A * ((A+1) + (A-1)*c0 - 2*sqA*al);
const ia0s = (A+1) - (A-1)*c0 + 2*sqA*al;
const ia1s = 2 * ((A-1) - (A+1)*c0);
const ia2s = (A+1) - (A-1)*c0 - 2*sqA*al;
const w1 = 2.0 * Math.PI * 38.13 / fs;
const c1 = Math.cos(w1), s1 = Math.sin(w1);
const al1 = s1;
const ib0h = (1+c1)*0.5, ib1h = -(1+c1), ib2h = (1+c1)*0.5;
const ia0h = 1+al1, ia1h = -2*c1, ia2h = 1-al1;
return {
shelf: [ib0s/ia0s, ib1s/ia0s, ib2s/ia0s, ia1s/ia0s, ia2s/ia0s],
hp: [ib0h/ia0h, ib1h/ia0h, ib2h/ia0h, ia1h/ia0h, ia2h/ia0h]
};
}
const TARGET_LUFS = -12.0;
const ABS_GATE_LUFS = -70.0;
const REL_GATE_LU = 10.0;
const MIN_GAIN_LINEAR = Math.pow(10, -20.0 / 20);
const MAX_GAIN_LINEAR = Math.pow(10, 20.0 / 20);
registerProcessor('loudness-processor', class LoudnessProcessor extends AudioWorkletProcessor {
constructor() {
super();
const cf = computeKWeightCoeffs(sampleRate);
[this.bs0, this.bs1, this.bs2, this.bas1, this.bas2] = cf.shelf;
[this.bh0, this.bh1, this.bh2, this.bah1, this.bah2] = cf.hp;
this.stateL = new Float64Array(4);
this.stateR = new Float64Array(4);
this.blocksPerSec = sampleRate / 128;
this.BLOCKS_PER_400MS = Math.ceil(this.blocksPerSec * 0.4);
this.BLOCKS_PER_3S = Math.ceil(this.blocksPerSec * 3.0);
this.momentaryBuf = new Float64Array(this.BLOCKS_PER_400MS);
this.shortTermBuf = new Float64Array(this.BLOCKS_PER_3S);
this.momentaryPos = 0;
this.shortTermPos = 0;
this.integratedBlocks = [];
const tickMs = 1000 / this.blocksPerSec;
this.attackAlpha = 1.0 - Math.exp(-tickMs / 200.0);
this.releaseAlpha = 1.0 - Math.exp(-tickMs / 500.0);
this.smoothedGain = 1.0;
this.framesSinceReport = 0;
this.reportIntervalBlocks = Math.ceil(this.blocksPerSec / 10); // 10 FPS
}
process(inputs, outputs) {
const inp = inputs[0];
const out = outputs[0];
if (!inp || !inp[0] || inp[0].length === 0) return true;
const N = inp[0].length;
const nCh = Math.min(inp.length, 2);
const bs0 = this.bs0, bs1 = this.bs1, bs2 = this.bs2;
const bas1 = this.bas1, bas2 = this.bas2;
const bh0 = this.bh0, bh1 = this.bh1, bh2 = this.bh2;
const bah1 = this.bah1, bah2 = this.bah2;
let sumMS = 0.0;
for (let ch = 0; ch < nCh; ch++) {
const inCh = inp[ch];
const outCh = out[ch];
const st = ch === 0 ? this.stateL : this.stateR;
let s1w1 = st[0], s1w2 = st[1];
let s2w1 = st[2], s2w2 = st[3];
let chMS = 0.0;
for (let i = 0; i < N; i++) {
const x = inCh[i];
const y1 = bs0 * x + s1w1;
s1w1 = bs1 * x - bas1 * y1 + s1w2;
s1w2 = bs2 * x - bas2 * y1;
const y2 = bh0 * y1 + s2w1;
s2w1 = bh1 * y1 - bah1 * y2 + s2w2;
s2w2 = bh2 * y1 - bah2 * y2;
chMS += y2 * y2;
outCh[i] = x * this.smoothedGain;
}
st[0] = s1w1; st[1] = s1w2;
st[2] = s2w1; st[3] = s2w2;
sumMS += chMS / N;
}
if (nCh === 1 && out.length >= 2 && out[1]) {
out[1].set(out[0]);
}
this.processMetrics(sumMS);
return true;
}
processMetrics(ms) {
this.momentaryBuf[this.momentaryPos % this.BLOCKS_PER_400MS] = ms;
this.momentaryPos++;
let mCount = Math.min(this.momentaryPos, this.BLOCKS_PER_400MS);
let mSum = 0.0;
for (let i = 0; i < mCount; i++) mSum += this.momentaryBuf[i];
let mAvg = mSum / mCount;
let momentaryLUFS = mAvg < 1e-10 ? -70.0 : -0.691 + 10.0 * Math.log10(mAvg);
this.shortTermBuf[this.shortTermPos % this.BLOCKS_PER_3S] = ms;
this.shortTermPos++;
let stCount = Math.min(this.shortTermPos, this.BLOCKS_PER_3S);
let stSum = 0.0;
for (let i = 0; i < stCount; i++) stSum += this.shortTermBuf[i];
let stAvg = stSum / stCount;
let shortTermLUFS = stAvg < 1e-10 ? -70.0 : -0.691 + 10.0 * Math.log10(stAvg);
if (momentaryLUFS > ABS_GATE_LUFS && this.momentaryPos >= this.BLOCKS_PER_400MS) {
this.integratedBlocks.push({ ms, lufs: momentaryLUFS });
if (this.integratedBlocks.length > 3000) this.integratedBlocks.shift();
}
let integratedLUFS = -70.0;
if (this.integratedBlocks.length > 0) {
let p1Sum = 0.0;
for (const b of this.integratedBlocks) p1Sum += b.ms;
let p1Avg = p1Sum / this.integratedBlocks.length;
let p1LUFS = p1Avg < 1e-10 ? -70.0 : -0.691 + 10.0 * Math.log10(p1Avg);
let relGate = p1LUFS - REL_GATE_LU;
let p2Sum = 0.0, p2Count = 0;
for (const b of this.integratedBlocks) {
if (b.lufs >= relGate) { p2Sum += b.ms; p2Count++; }
}
if (p2Count > 0) {
let p2Avg = p2Sum / p2Count;
integratedLUFS = p2Avg < 1e-10 ? -70.0 : -0.691 + 10.0 * Math.log10(p2Avg);
}
}
if (momentaryLUFS > ABS_GATE_LUFS) {
let rawTarget = Math.pow(10, (TARGET_LUFS - momentaryLUFS) / 20.0);
let clamped = Math.max(MIN_GAIN_LINEAR, Math.min(MAX_GAIN_LINEAR, rawTarget));
let alpha = clamped > this.smoothedGain ? this.attackAlpha : this.releaseAlpha;
this.smoothedGain += (clamped - this.smoothedGain) * alpha;
}
this.framesSinceReport++;
if (this.framesSinceReport >= this.reportIntervalBlocks) {
this.framesSinceReport = 0;
let gainDB = 20.0 * Math.log10(Math.max(1e-9, this.smoothedGain));
this.port.postMessage({
momentaryLUFS: Math.max(-70.0, momentaryLUFS),
shortTermLUFS: Math.max(-70.0, shortTermLUFS),
integratedLUFS: Math.max(-70.0, integratedLUFS),
gainDB
});
}
}
});