From 7c94ea53cba951b2365b5901d7214d7418adb306 Mon Sep 17 00:00:00 2001 From: Thomas Weber Date: Sat, 18 Oct 2025 14:45:51 -0500 Subject: [PATCH] Restrict max sample rate --- src/AudioEngine.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/AudioEngine.js b/src/AudioEngine.js index fb219ab4..fb202310 100644 --- a/src/AudioEngine.js +++ b/src/AudioEngine.js @@ -42,7 +42,25 @@ const makeAudioContext = () => { if (!AudioContext) { throw new Error('Browser does not support AudioContext'); } - return new AudioContext(); + + const audioContextWithDefaultRate = new AudioContext(); + + // By default, browsers will use the sample rate of the output device. For people who have this + // configured very high, this can result in extreme increases in memory usage because we + // pre-decode all sounds into buffers at this sample rate. + if (audioContextWithDefaultRate.sampleRate > 48000) { + try { + return new AudioContext({ + sampleRate: 48000 + }); + } catch (e) { + // If browser can't support our requested rate, we'll have to continue with whatever + // rate we got earlier, even if it's not going to be ideal. + return audioContextWithDefaultRate; + } + } + + return audioContextWithDefaultRate; }; /**