Created
January 5, 2021 21:41
-
-
Save Sgeo/867dcd7f62f51d5de03f83bbade0662c to your computer and use it in GitHub Desktop.
Add a setVolume() function to affect all AudioContexts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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