Skip to content

Instantly share code, notes, and snippets.

@internetbird
Created January 29, 2015 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save internetbird/b2aa28ac935882b28ec7 to your computer and use it in GitHub Desktop.
Save internetbird/b2aa28ac935882b28ec7 to your computer and use it in GitHub Desktop.
Audio API example which plays an A minor chord
<script>
var AFreq = 440;
var CFreq = 261.626;
var EFreq = 329.628;
var audioContext = new AudioContext();
var gainMix = audioContext.createGain();
gainMix.gain.value = 0.1;
var oscilators = [];
addNote(AFreq);
addNote(CFreq);
addNote(EFreq);
startMusic(2000);
function addNote(frequency, gain)
{
var osc = audioContext.createOscillator();
osc.type="sawtooth";
osc.frequency.value = frequency;
oscilators.push(osc);
var gainOsc = audioContext.createGain();
gainOsc.gain.value = gain || 0.1;
osc.connect(gainOsc);
gainOsc.connect(gainMix);
}
function startMusic(duration)
{
console.log(oscilators);
gainMix.connect(audioContext.destination);
oscilators.forEach(function(osc)
{
osc.start(audioContext.currentTime);
})
setTimeout(function(){
oscilators.forEach(function(osc)
{
osc.stop();
});
}, duration);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment