Skip to content

Instantly share code, notes, and snippets.

@benji6
Created December 19, 2019 12:24
Show Gist options
  • Save benji6/2c14f48aaea41c5778b2f2bbc294187f to your computer and use it in GitHub Desktop.
Save benji6/2c14f48aaea41c5778b2f2bbc294187f to your computer and use it in GitHub Desktop.
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