Last active
April 10, 2024 19:09
-
-
Save brandonasuncion/873cc8c65c757578c783449de910e558 to your computer and use it in GitHub Desktop.
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
(function() { | |
console.log("Creating compressor"); | |
let context = new AudioContext(); | |
let compressor = context.createDynamicsCompressor(); | |
compressor.threshold.value = -50; | |
compressor.knee.value = 40; | |
compressor.ratio.value = 12; | |
compressor.attack.value = 0; | |
compressor.release.value = 0.25; | |
let source = null; | |
let active = false; | |
window.toggleCompressor = () => { | |
if (!source) { | |
let videoElements = document.getElementsByTagName('video'); | |
source = videoElements.length ? context.createMediaElementSource(videoElements[0]) : null; | |
source.connect(context.destination); | |
} | |
if (!source) return; | |
active = !active; | |
if (active) { | |
console.log("Enabling compressor"); | |
source.disconnect(context.destination); | |
source.connect(compressor); | |
compressor.connect(context.destination); | |
} else { | |
console.log("Disabling compressor"); | |
source.disconnect(compressor); | |
compressor.disconnect(context.destination); | |
source.connect(context.destination); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment