Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Created March 9, 2015 09:09
Show Gist options
  • Save leaysgur/c0bac5211dd899de0b72 to your computer and use it in GitHub Desktop.
Save leaysgur/c0bac5211dd899de0b72 to your computer and use it in GitHub Desktop.
impulseResponse
// var audioContext = new AudioContext();
function impulseResponse( duration, decay, reverse ) {
var sampleRate = audioContext.sampleRate;
var length = sampleRate * duration;
var impulse = audioContext.createBuffer(2, length, sampleRate);
var impulseL = impulse.getChannelData(0);
var impulseR = impulse.getChannelData(1);
if (!decay)
decay = 2.0;
for (var i = 0; i < length; i++){
var n = reverse ? length - i : i;
impulseL[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
impulseR[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
}
return impulse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment