Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jingle bells
const audioCtx = new AudioContext
const gain = audioCtx.createGain()
gain.connect(audioCtx.destination)
gain.gain.value = 0.2
const noteDuration = 0.2
const notes = [
[659.25, 1], [659.25, 1], [659.25, 2],
[659.25, 1], [659.25, 1], [659.25, 2],
[659.25, 1], [783.99, 1], [523.25, 1], [587.33, 1],
[659.25, 4],
[698.46, 1], [698.46, 1], [698.46, 1.5], [698.46, .5],
[698.46, 1], [659.25, 1], [659.25, 1], [659.25, .5], [659.25, .5],
[659.25, 1], [587.33, 1], [587.33, 1], [659.25, 1],
[587.33, 2], [783.99, 2],
]
const {currentTime} = audioCtx
let i = 0
for (const [f, t] of notes) {
osc = audioCtx.createOscillator()
osc.connect(gain)
osc.type="square"
osc.frequency.value = f
osc.start(currentTime + noteDuration * i)
osc.stop(currentTime + noteDuration * i + (t - .1) * noteDuration)
i += t
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment