Skip to content

Commit 7c94ea5

Browse files
committed
Restrict max sample rate
1 parent aba00cd commit 7c94ea5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/AudioEngine.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,25 @@ const makeAudioContext = () => {
4242
if (!AudioContext) {
4343
throw new Error('Browser does not support AudioContext');
4444
}
45-
return new AudioContext();
45+
46+
const audioContextWithDefaultRate = new AudioContext();
47+
48+
// By default, browsers will use the sample rate of the output device. For people who have this
49+
// configured very high, this can result in extreme increases in memory usage because we
50+
// pre-decode all sounds into buffers at this sample rate.
51+
if (audioContextWithDefaultRate.sampleRate > 48000) {
52+
try {
53+
return new AudioContext({
54+
sampleRate: 48000
55+
});
56+
} catch (e) {
57+
// If browser can't support our requested rate, we'll have to continue with whatever
58+
// rate we got earlier, even if it's not going to be ideal.
59+
return audioContextWithDefaultRate;
60+
}
61+
}
62+
63+
return audioContextWithDefaultRate;
4664
};
4765

4866
/**

0 commit comments

Comments
 (0)