Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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