From f72c9cc410f7b4c05398fc35cde821c9d2220a88 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 28 Jul 2026 10:58:14 -0700 Subject: [PATCH] Cleanup legacy browser support in libopenal.js. NFC This brings libopenal.js inline with libwebaudio.js in a couple of ways: - Only support webkitAudioContext on older safari versions. - Drop support for `new AudioContext` that does not accept options. This change should make is easy for us to remove this support once we drop support for Safari < 14.5. --- src/lib/libopenal.js | 34 ++++++++++++------- .../test_codesize_hello_dylink_all.json | 4 +-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/lib/libopenal.js b/src/lib/libopenal.js index 1961aeebcf447..92f715406b078 100644 --- a/src/lib/libopenal.js +++ b/src/lib/libopenal.js @@ -20,6 +20,12 @@ var LibraryOpenAL = { QUEUE_INTERVAL: 25, QUEUE_LOOKAHEAD: 100.0 / 1000.0, +#if MIN_SAFARI_VERSION < 140500 + AudioContext: globalThis.AudioContext ?? globalThis.webkitAudioContext, +#else + AudioContext: globalThis.AudioContext, +#endif + DEVICE_NAME: 'Emscripten OpenAL', CAPTURE_DEVICE_NAME: 'Emscripten OpenAL capture', @@ -1650,7 +1656,11 @@ var LibraryOpenAL = { return 0; } - var AudioContext = window.AudioContext || window.webkitAudioContext; +#if MIN_SAFARI_VERSION < 140500 + // We depend on the global AudioContext for modern browser, but + // old older safari versions we may still need to use webkitAudioContext. + var AudioContext = AL.AudioContext; +#endif if (!AL.sharedCaptureAudioCtx) { try { @@ -2048,7 +2058,7 @@ var LibraryOpenAL = { } } - if (globalThis.AudioContext || globalThis.webkitAudioContext) { + if (AL.getAudioContext) { var deviceId = AL.newId(); AL.deviceRefCounts[deviceId] = 0; return deviceId; @@ -2078,7 +2088,7 @@ var LibraryOpenAL = { return 0; } - var options = null; + var options; var attrs = []; var hrtf = null; pAttrList >>= 2; @@ -2143,15 +2153,13 @@ var LibraryOpenAL = { } } - var AudioContext = window.AudioContext || window.webkitAudioContext; - var ac = null; +#if MIN_SAFARI_VERSION < 140500 + var AudioContext = window.AudioContext ?? window.webkitAudioContext; +#endif + + var ac; try { - // Only try to pass options if there are any, for compat with browsers that don't support this - if (options) { - ac = new AudioContext(options); - } else { - ac = new AudioContext(); - } + ac = new AudioContext(options); } catch (e) { if (e.name === 'NotSupportedError') { #if OPENAL_DEBUG @@ -2382,14 +2390,14 @@ var LibraryOpenAL = { ret = 'Out of Memory'; break; case 0x1004 /* ALC_DEFAULT_DEVICE_SPECIFIER */: - if (globalThis.AudioContext || globalThis.webkitAudioContext) { + if (AL.AudioContext) { ret = AL.DEVICE_NAME; } else { return 0; } break; case 0x1005 /* ALC_DEVICE_SPECIFIER */: - if (globalThis.AudioContext || globalThis.webkitAudioContext) { + if (AL.AudioContext) { ret = AL.DEVICE_NAME + '\0'; } else { ret = '\0'; diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index 0903d03a9d1fc..408b12cfa3a04 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { - "a.out.js": 268229, + "a.out.js": 267924, "a.out.nodebug.wasm": 588309, - "total": 856538, + "total": 856233, "sent": [ "IMG_Init", "IMG_Load",