Last active
June 12, 2016 10:21
-
-
Save christopheralcock/5b40bc84d6ea62ac854f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //set context | |
| var audioContext = new AudioContext() | |
| var speakers = audioContext.destination | |
| //call play function | |
| play(0, 3, .5) | |
| play(1, 10, .5) | |
| play(2, 15, .5) | |
| //play function | |
| function play (delay, pitch, duration) { | |
| //set variables that decode your params | |
| var startTime = audioContext.currentTime + delay | |
| var endTime = startTime + duration | |
| var tailEnd = endTime + 2 | |
| //create your audio elements | |
| var oscillator = audioContext.createOscillator() | |
| var amp = audioContext.createGain() | |
| var lfo = audioContext.createOscillator() | |
| var delayInput = audioContext.createGain() | |
| var delayFeedback = audioContext.createGain() | |
| var delayTimer = audioContext.createDelay() | |
| var delayOutput = audioContext.createGain() | |
| //connect your audio elements to eachother, in reverse order (the thing that connects to the destination, followed by that and that | |
| delayOutput.connect(audioContext.destination) | |
| delayInput.connect(delayOutput) | |
| delayFeedback.connect(delayOutput) | |
| delayFeedback.connect(delayTimer) | |
| delayTimer.connect(delayFeedback) | |
| delayInput.connect(delayTimer) | |
| amp.connect(delayInput) | |
| lfo.connect(amp.gain) | |
| oscillator.connect(amp) | |
| //describe the elements one by one | |
| amp.gain.value = 0 | |
| amp.gain.setTargetAtTime(1, startTime, 0.1) | |
| amp.gain.setTargetAtTime(0, endTime, 0.2) | |
| lfo.frequency.value = 5 | |
| lfo.start(startTime) | |
| lfo.stop(tailEnd) | |
| oscillator.type = 'sawtooth' | |
| oscillator.detune.value = pitch * 100 | |
| oscillator.start(startTime) | |
| oscillator.stop(tailEnd) | |
| delayTimer.delayTime.value = 10000 | |
| delayFeedback.gain.value = 0.999 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment