Skip to content

Instantly share code, notes, and snippets.

@Angeldude
Forked from kevincennis/flippinawesome.js
Created November 10, 2015 23:47
Show Gist options
  • Save Angeldude/9d1229e6ebf563049124 to your computer and use it in GitHub Desktop.
Save Angeldude/9d1229e6ebf563049124 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment