Created
August 4, 2020 21:46
-
-
Save LolWalid/ebcaf341eabdba070bdd9561e85be164 to your computer and use it in GitHub Desktop.
Für Elise Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mostly Inspired by https://github.com/KilledByAPixel/1Keys | |
var octave = 1 | |
var key = 0 | |
var shape = 'sine' | |
var audioCtx = new AudioContext(); | |
var instruments = [ "1", "2", "4", "8" ] | |
var soundDuration = 250 | |
play = (i) => { | |
if (i < 0) return; | |
instruments.map(j => { | |
oscillator = audioCtx.createOscillator() // create oscillator | |
oscillator.connect( // oscillator to gain | |
oscillator.g = audioCtx.createGain( // create gain node | |
oscillator.frequency.value = // set frequency | |
j * 55 * // A 55 root note | |
2**((i+3)/12 // music scale formula | |
+ octave*1 // octave control | |
+ key/12))) // key control | |
.connect(audioCtx.destination) // gain to destination | |
oscillator.g.gain.value = .2/(1+Math.log2(j)) // set gain | |
oscillator.type = shape // get type setting | |
oscillator.start() // play sound | |
oscillator.g.gain.linearRampToValueAtTime(oscillator.g.gain.value, audioCtx.currentTime + 0) | |
oscillator.stop(audioCtx.currentTime + soundDuration / 1000) | |
return oscillator | |
} | |
) | |
} | |
playMusic = (s) => { | |
time = 0 | |
s.split(" ").map((e) => { | |
setTimeout(() => play(parseInt(e)), time * soundDuration) | |
time++; | |
}) | |
} | |
// Partition in progress | |
s = "23 22 23 22 23 17 21 19 16 -1 36 28 33 35 -1 28 32 35 36 -1 23 22 23 22 23 17 21 19 16 -1 36 28 33 35 -1 28 36 35 33" | |
playMusic(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment