Skip to content

Instantly share code, notes, and snippets.

@Sgeo
Created January 5, 2021 21:41
Show Gist options
  • Save Sgeo/867dcd7f62f51d5de03f83bbade0662c to your computer and use it in GitHub Desktop.
Save Sgeo/867dcd7f62f51d5de03f83bbade0662c to your computer and use it in GitHub Desktop.
Add a setVolume() function to affect all AudioContexts
// Based on https://github.com/notthetup/webaudio-volume-control/blob/master/mastervolume.js
(function() {
window.setVolume = function(){};
let origAudioContextConstructor = window.AudioContext;
window.AudioContext = function(options) {
let origAudioContext = new origAudioContextConstructor(options);
let masterGain = origAudioContext.createGain();
masterGain.connect(origAudioContext.destination);
let origSetVolume = window.setVolume;
window.setVolume = function(volume) {
masterGain.gain.value = volume;
origSetVolume(volume);
};
Object.defineProperty(origAudioContext, "destination", {
value: masterGain
});
return origAudioContext;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment