Created
March 9, 2015 09:09
-
-
Save leaysgur/c0bac5211dd899de0b72 to your computer and use it in GitHub Desktop.
impulseResponse
This file contains hidden or 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
// 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