Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Created June 14, 2013 06:44
Show Gist options
  • Save srikumarks/5779926 to your computer and use it in GitHub Desktop.
Save srikumarks/5779926 to your computer and use it in GitHub Desktop.
Simple Ping using Web Audio API
var audioContext = new AudioContext();
var kFreq = 660, kDecayTime = 0.5, kStartTime = 1.5, kGain = 0.25;
var oscNode = audioContext.createOscillator();
oscNode.frequency.value = kFreq;
var gainNode = audioContext.createGain();
gainNode.gain.value = kGain;
gainNode.gain.setTargetAtTime(0.0, audioContext.currentTime, kDecayTime);
oscNode.connect(gainNode);
gainNode.connect(audioContext.destination);
oscNode.start(audioContext.currentTime + kStartTime); // Start a little into the future.
oscNode.stop(audioContext.currentTime + kStartTime + 12 * kDecayTime); // Stop when the sound decays by enough.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment