Skip to content

Instantly share code, notes, and snippets.

@madebyollin
Created August 24, 2018 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madebyollin/f142f909f1ad7a1709433c5ec6282476 to your computer and use it in GitHub Desktop.
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
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