Created
August 24, 2018 00:59
-
-
Save madebyollin/f142f909f1ad7a1709433c5ec6282476 to your computer and use it in GitHub Desktop.
Snippet to add an audio compressor to all videos on the current page
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 addCompressor(el) { | |
var AudioContext = window.AudioContext || window.webkitAudioContext; | |
var context = new AudioContext(); | |
var source = context.createMediaElementSource(el); | |
var compressor = context.createDynamicsCompressor(); | |
var makeupGain = context.createGain(); | |
// Set compressor params here | |
compressor.threshold.value = -40; | |
compressor.knee.value = 40; | |
compressor.ratio.value = 5; | |
compressor.attack.value = 0; | |
compressor.release.value = 0.25; | |
// Makeup gain formula from http://music-dsp.music.columbia.narkive.com/3BVi9E9D/computing-compressor-automatic-makeup-gain | |
makeupGain.gain.value = 0.5 * Math.abs(compressor.threshold.value) / compressor.ratio.value; | |
source.connect(compressor); | |
compressor.connect(makeupGain); | |
makeupGain.connect(context.destination); | |
console.log(`Applying ${makeupGain.gain.value}db makeup gain`); | |
} | |
document.querySelectorAll("video").forEach(addCompressor); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment