Created
January 29, 2015 01:38
-
-
Save internetbird/b2aa28ac935882b28ec7 to your computer and use it in GitHub Desktop.
Audio API example which plays an A minor chord
This file contains 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
<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