Skip to content

Commit 3fbc489

Browse files
authored
Unconditional AudioContext() API support (#25356)
When targeting a build that unconditionally has AudioContext() API, do not emit code to check for its presence.
1 parent bce7571 commit 3fbc489

File tree

3 files changed

+80
-72
lines changed

3 files changed

+80
-72
lines changed

src/lib/libwebaudio.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ var LibraryWebAudio = {
5454
// this avoids the user needing to manually add the dependency on the command line.
5555
emscripten_create_audio_context__deps: ['$emscriptenRegisterAudioObject', '$emscriptenGetAudioObject'],
5656
emscripten_create_audio_context: (options) => {
57+
// Safari added unprefixed AudioContext support in Safari 14.5 on iOS: https://caniuse.com/audio-api
58+
#if MIN_SAFARI_VERSION < 140500 || ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
5759
var ctx = window.AudioContext || window.webkitAudioContext;
5860
#if ASSERTIONS
5961
if (!ctx) console.error('emscripten_create_audio_context failed! Web Audio is not supported.');
62+
#endif
6063
#endif
6164

6265
var opts = options ? {
@@ -69,7 +72,12 @@ var LibraryWebAudio = {
6972
console.dir(opts);
7073
#endif
7174

75+
#if MIN_SAFARI_VERSION < 140500 || ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
7276
return ctx && emscriptenRegisterAudioObject(new ctx(opts));
77+
#else
78+
// We are targeting an environment where we assume that AudioContext() API unconditionally exists.
79+
return emscriptenRegisterAudioObject(new AudioContext(opts));
80+
#endif
7381
},
7482

7583
emscripten_resume_audio_context_async: (contextHandle, callback, userData) => {

test/codesize/audio_worklet_wasm.expected.js

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ q && !r && (onmessage = a => {
1818
});
1919

2020
if (r) {
21-
function a(c) {
22-
class e extends AudioWorkletProcessor {
23-
constructor(d) {
21+
function a(e) {
22+
class d extends AudioWorkletProcessor {
23+
constructor(c) {
2424
super();
25-
d = d.processorOptions;
26-
this.v = A.get(d.v);
27-
this.A = d.A;
28-
this.u = d.u;
25+
c = c.processorOptions;
26+
this.v = A.get(c.v);
27+
this.A = c.A;
28+
this.u = c.u;
2929
this.s = 4 * this.u;
3030
this.B = Array(Math.min((t.F - 16) / this.s | 0, 64));
3131
this.K();
3232
}
3333
K() {
34-
for (var d = C(), f = D(this.B.length * this.s) >> 2, g = this.B.length - 1; 0 <= g; g--) this.B[g] = E.subarray(f, f += this.u);
35-
F(d);
34+
for (var c = C(), f = D(this.B.length * this.s) >> 2, g = this.B.length - 1; 0 <= g; g--) this.B[g] = E.subarray(f, f += this.u);
35+
F(c);
3636
}
3737
static get parameterDescriptors() {
38-
return c;
38+
return e;
3939
}
40-
process(d, f, g) {
41-
var n = d.length, x = f.length, k, p, h = 12 * (n + x), l = 0;
42-
for (k of d) l += k.length;
40+
process(c, f, g) {
41+
var n = c.length, x = f.length, k, p, h = 12 * (n + x), l = 0;
42+
for (k of c) l += k.length;
4343
l *= this.s;
4444
var G = 0;
4545
for (k of f) G += k.length;
@@ -50,39 +50,39 @@ if (r) {
5050
h = D(B);
5151
l = h + (B - l);
5252
B = h;
53-
for (k of d) {
53+
for (k of c) {
5454
H[h >> 2] = k.length;
5555
H[h + 4 >> 2] = this.u;
5656
H[h + 8 >> 2] = l;
5757
h += 12;
5858
for (p of k) E.set(p, l >> 2), l += this.s;
5959
}
60-
d = h;
60+
c = h;
6161
for (k = 0; p = g[k++]; ) H[h >> 2] = p.length, H[h + 4 >> 2] = l, h += 8, E.set(p, l >> 2),
6262
l += 4 * p.length;
6363
g = h;
6464
for (k of f) H[h >> 2] = k.length, H[h + 4 >> 2] = this.u, H[h + 8 >> 2] = l, h += 12,
6565
l += this.s * k.length;
66-
if (n = this.v(n, B, x, g, O, d, this.A)) for (k of f) for (p of k) p.set(this.B[--G]);
66+
if (n = this.v(n, B, x, g, O, c, this.A)) for (k of f) for (p of k) p.set(this.B[--G]);
6767
F(V);
6868
return !!n;
6969
}
7070
}
71-
return e;
71+
return d;
7272
}
7373
var I;
7474
class b extends AudioWorkletProcessor {
75-
constructor(c) {
75+
constructor(e) {
7676
super();
77-
u(c.processorOptions);
77+
u(e.processorOptions);
7878
I = this.port;
79-
I.onmessage = async e => {
79+
I.onmessage = async d => {
8080
await z;
81-
e = e.data;
82-
e._wpn ? (registerProcessor(e._wpn, a(e.H)), I.postMessage({
83-
_wsc: e.v,
84-
C: [ e.I, 1, e.A ]
85-
})) : e._wsc && A.get(e._wsc)(...e.C);
81+
d = d.data;
82+
d._wpn ? (registerProcessor(d._wpn, a(d.H)), I.postMessage({
83+
_wsc: d.v,
84+
C: [ d.I, 1, d.A ]
85+
})) : d._wsc && A.get(d._wsc)(...d.C);
8686
};
8787
}
8888
process() {}
@@ -110,64 +110,64 @@ var L = [], M = a => {
110110
b && A.get(b)(...a.x);
111111
}, N = a => {
112112
L.push(a);
113-
}, Q = (a, b, c, e) => {
113+
}, Q = (a, b, e, d) => {
114114
b = P[b];
115-
P[a].connect(b.destination || b, c, e);
115+
P[a].connect(b.destination || b, e, d);
116116
}, P = {}, R = 0, S = globalThis.TextDecoder ? new TextDecoder : void 0, T = (a = 0) => {
117-
for (var b = J, c = a, e = c + void 0; b[c] && !(c >= e); ) ++c;
118-
if (16 < c - a && b.buffer && S) return S.decode(b.slice(a, c));
119-
for (e = ""; a < c; ) {
120-
var d = b[a++];
121-
if (d & 128) {
117+
for (var b = J, e = a, d = e + void 0; b[e] && !(e >= d); ) ++e;
118+
if (16 < e - a && b.buffer && S) return S.decode(b.slice(a, e));
119+
for (d = ""; a < e; ) {
120+
var c = b[a++];
121+
if (c & 128) {
122122
var f = b[a++] & 63;
123-
if (192 == (d & 224)) e += String.fromCharCode((d & 31) << 6 | f); else {
123+
if (192 == (c & 224)) d += String.fromCharCode((c & 31) << 6 | f); else {
124124
var g = b[a++] & 63;
125-
d = 224 == (d & 240) ? (d & 15) << 12 | f << 6 | g : (d & 7) << 18 | f << 12 | g << 6 | b[a++] & 63;
126-
65536 > d ? e += String.fromCharCode(d) : (d -= 65536, e += String.fromCharCode(55296 | d >> 10, 56320 | d & 1023));
125+
c = 224 == (c & 240) ? (c & 15) << 12 | f << 6 | g : (c & 7) << 18 | f << 12 | g << 6 | b[a++] & 63;
126+
65536 > c ? d += String.fromCharCode(c) : (c -= 65536, d += String.fromCharCode(55296 | c >> 10, 56320 | c & 1023));
127127
}
128-
} else e += String.fromCharCode(d);
128+
} else d += String.fromCharCode(c);
129129
}
130-
return e;
130+
return d;
131131
}, U = a => {
132-
var b = window.AudioContext || window.webkitAudioContext;
133132
if (a) {
134-
var c = H[a >> 2];
133+
var b = H[a >> 2];
135134
a = {
136-
latencyHint: (c ? T(c) : "") || void 0,
135+
latencyHint: (b ? T(b) : "") || void 0,
137136
sampleRate: H[a + 4 >> 2] || void 0
138137
};
139138
} else a = void 0;
140-
if (c = b) b = new b(a), P[++R] = b, c = R;
141-
return c;
142-
}, W = (a, b, c, e, d) => {
143-
var f = c ? K[c + 4 >> 2] : 0;
144-
if (c) {
145-
var g = K[c >> 2];
146-
c = H[c + 8 >> 2];
139+
a = new AudioContext(a);
140+
P[++R] = a;
141+
return R;
142+
}, W = (a, b, e, d, c) => {
143+
var f = e ? K[e + 4 >> 2] : 0;
144+
if (e) {
145+
var g = K[e >> 2];
146+
e = H[e + 8 >> 2];
147147
var n = f;
148-
if (c) {
149-
c >>= 2;
150-
for (var x = []; n--; ) x.push(H[c++]);
151-
c = x;
152-
} else c = void 0;
153-
e = {
148+
if (e) {
149+
e >>= 2;
150+
for (var x = []; n--; ) x.push(H[e++]);
151+
e = x;
152+
} else e = void 0;
153+
d = {
154154
numberOfInputs: g,
155155
numberOfOutputs: f,
156-
outputChannelCount: c,
156+
outputChannelCount: e,
157157
processorOptions: {
158-
v: e,
159-
A: d,
158+
v: d,
159+
A: c,
160160
u: 128
161161
}
162162
};
163-
} else e = void 0;
164-
a = new AudioWorkletNode(P[a], b ? T(b) : "", e);
163+
} else d = void 0;
164+
a = new AudioWorkletNode(P[a], b ? T(b) : "", d);
165165
P[++R] = a;
166166
return R;
167-
}, aa = (a, b, c, e) => {
168-
var d = [], f = (f = H[b >> 2]) ? T(f) : "", g = K[b + 4 >> 2];
167+
}, aa = (a, b, e, d) => {
168+
var c = [], f = (f = H[b >> 2]) ? T(f) : "", g = K[b + 4 >> 2];
169169
b = H[b + 8 >> 2];
170-
for (var n = 0; g--; ) d.push({
170+
for (var n = 0; g--; ) c.push({
171171
name: n++,
172172
defaultValue: E[b >> 2],
173173
minValue: E[b + 4 >> 2],
@@ -176,18 +176,18 @@ var L = [], M = a => {
176176
}), b += 16;
177177
P[a].audioWorklet.D.port.postMessage({
178178
_wpn: f,
179-
H: d,
179+
H: c,
180180
I: a,
181-
v: c,
182-
A: e
181+
v: e,
182+
A: d
183183
});
184184
}, ba = () => !1, ca = 1, da = a => {
185185
a = a.data;
186186
var b = a._wsc;
187187
b && A.get(b)(...a.C);
188-
}, ea = (a, b, c, e, d) => {
188+
}, ea = (a, b, e, d, c) => {
189189
var f = P[a], g = f.audioWorklet, n = () => {
190-
A.get(e)(a, 0, d);
190+
A.get(d)(a, 0, c);
191191
};
192192
if (!g) return n();
193193
g.addModule(m.js).then((() => {
@@ -197,11 +197,11 @@ var L = [], M = a => {
197197
G: m.wasm,
198198
L: v,
199199
J: b,
200-
F: c
200+
F: e
201201
}
202202
});
203203
g.D.port.onmessage = da;
204-
A.get(e)(a, 1, d);
204+
A.get(d)(a, 1, c);
205205
})).catch(n);
206206
};
207207

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 519,
33
"a.html.gz": 357,
4-
"a.js": 3919,
5-
"a.js.gz": 2074,
4+
"a.js": 3866,
5+
"a.js.gz": 2041,
66
"a.wasm": 1308,
77
"a.wasm.gz": 876,
8-
"total": 5746,
9-
"total_gz": 3307
8+
"total": 5693,
9+
"total_gz": 3274
1010
}

0 commit comments

Comments
 (0)