Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Aug 15, 2014

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
You can’t perform that action at this time.