Skip to content

Instantly share code, notes, and snippets.

@ichabodcole
Created July 3, 2013 18:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ichabodcole/5921501 to your computer and use it in GitHub Desktop.
Save ichabodcole/5921501 to your computer and use it in GitHub Desktop.
Reference for fading in and out sounds with the Web Audio API
fadeTo: (value, fadeLength)->
fadeLength = fadeLength || @defaultfadeLength
currentTime = @ctx.currentTime
#time the fade should complete
fadeTime = currentTime + fadeLength
#set the start time
@masterGain.gain.setValueAtTime(@userVolume, currentTime)
@masterGain.gain.linearRampToValueAtTime(value, fadeTime)
fadeOut: (fadeLength)->
fadeLength = fadeLength || @defaultfadeLength
@fadeTo(0, fadeLength)
fadeIn: (fadeLength)->
fadeLength = fadeLength || @defaultfadeLength
@fadeTo(@userVolume, fadeLength)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment