Skip to content

Commit c13936a

Browse files
committed
Restrict max sample rate
1 parent aba00cd commit c13936a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/AudioEngine.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,24 @@ 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 48000 is not supported then we take whatever we can get.
58+
return audioContextWithDefaultRate;
59+
}
60+
}
61+
62+
return audioContextWithDefaultRate;
4663
};
4764

4865
/**

0 commit comments

Comments
 (0)