Skip to content

Instantly share code, notes, and snippets.

@kevincennis
Created November 4, 2013 03:37
Show Gist options
  • Save kevincennis/7297747 to your computer and use it in GitHub Desktop.
Save kevincennis/7297747 to your computer and use it in GitHub Desktop.
Better timing
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();
@kevincennis
Copy link
Author

Oops. Not entirely sure why it's E and B instead of Eb and Bb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment