Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Created June 11, 2013 00:45
Show Gist options
  • Save srikumarks/5753704 to your computer and use it in GitHub Desktop.
Save srikumarks/5753704 to your computer and use it in GitHub Desktop.
Get an AudioContext class that has "future compatible" names for methods.
// Gets the AudioContext class when in a browser environment.
function getAudioContext() {
try {
var AC = (window.AudioContext || window.webkitAudioContext || window.mozAudioContext);
function myAC() {
var ac = new AC();
// Future compatible names.
ac.createGain = ac.createGainNode = (ac.createGain || ac.createGainNode);
ac.createDelay = ac.createDelayNode = ac.createDelay || ac.createDelayNode;
ac.createScriptProcessor = ac.createJavaScriptNode = (ac.createScriptProcessor || ac.createJavaScriptNode);
var AudioParam = ac.createGain().gain.__proto__.__proto__;
AudioParam.setTargetAtTime = AudioParam.setTargetValueAtTime = (AudioParam.setTargetAtTime || AudioParam.setTargetValueAtTime);
var BufferSource = ac.createBufferSource().__proto__;
BufferSource.start = BufferSource.noteOn = (BufferSource.start || BufferSource.noteOn);
BufferSource.stop = BufferSource.noteOff = (BufferSource.stop || BufferSource.noteOff);
var Oscillator = ac.createOscillator().__proto__;
Oscillator.start = Oscillator.noteOn = (Oscillator.start || Oscillator.noteOn);
Oscillator.stop = Oscillator.noteOff = (Oscillator.stop || Oscillator.noteOff);
return ac;
}
return myAC;
} catch (e) {
return undefined;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment