Created
November 4, 2013 03:37
-
-
Save kevincennis/7297747 to your computer and use it in GitHub Desktop.
Better timing
This file contains hidden or 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
var audio = new window.webkitAudioContext(), | |
position = 0, | |
scale = { | |
g: 392, | |
f: 349.23, | |
e: 329.63, | |
b: 493.88 | |
}, | |
song = "gfefgg-fff-gbb-gfefggggffgfe---"; | |
function createOscillator(freq) { | |
var osc = audio.createOscillator(); | |
osc.frequency.value = freq || 0; | |
osc.type = "square"; | |
osc.connect(audio.destination); | |
osc.start(0); | |
osc.stop(audio.currentTime + (1/4)); | |
osc.onended = function() { | |
osc.disconnect(audio.destination); | |
play(); | |
}; | |
} | |
function play() { | |
var note = song.charAt(position), | |
freq = scale[note]; | |
position += 1; | |
if ( position >= song.length ) { | |
position = 0; | |
} | |
createOscillator(freq); | |
} | |
play(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops. Not entirely sure why it's E and B instead of Eb and Bb.