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

@kevincennis kevincennis commented Aug 15, 2014

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

@AndrewDavis

This comment has been minimized.

Copy link

@AndrewDavis AndrewDavis commented Jul 7, 2020

let audio = new window.AudioContext(),
scale = {
    g: 391.9954,
    f: 349.2282,
    e: 329.6276,
    d: 293.6648,
    c: 261.6256
},
song = 'edcdeee-ddd-egg-edcdeeeeddedc---';

It's in the wrong key/mode, essentially. All of the notes are shifted in the current key (presumably C because no sharps or flats). Song should be like the above.

Also now just use AudioContext.

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