Permalink
Browse files

stop popping, add a lowpass filter

  • Loading branch information...
brandly committed Mar 8, 2014
1 parent 450f52e commit 3c749111a00d9f7ef04b945efff0910a2c74bb9f
Showing with 28 additions and 4 deletions.
  1. +28 −4 src/scripts/coffee/app.coffee
@@ -23,25 +23,49 @@ keyboard = qwertyHancock keyboardSettings
context = new window.audioContext() context = new window.audioContext()
nodes = {} nodes = {}
volume = 0.3 # out of 1 # http://docs.webplatform.org/wiki/apis/webaudio/OscillatorNode/type
oscillatorTypes =
sine: 0
square: 1
sawtooth: 2
triangle: 3
# http://docs.webplatform.org/wiki/apis/webaudio/BiquadFilterNode/type
filterTypes =
lowpass: 0
highpass: 1
bandpass: 2
lowshelf: 3
highshelf: 4
peaking: 5
notch: 6
allpass: 7
volume = 0.4 # out of 1
attack = 0.1 # in seconds attack = 0.1 # in seconds
sustain = 0.8 # also seconds sustain = 0.8 # also seconds
playNote = (context, frequency) -> playNote = (context, frequency) ->
oscillator = context.createOscillator() oscillator = context.createOscillator()
gainNode = context.createGainNode() gainNode = context.createGainNode()
oscillator.type = 3 oscillator.type = oscillatorTypes.triangle
oscillator.frequency.value = frequency oscillator.frequency.value = frequency
oscillator.noteOn? 0
now = context.currentTime now = context.currentTime
gainNode.gain.cancelScheduledValues now gainNode.gain.cancelScheduledValues now
gainNode.gain.value = 0 gainNode.gain.value = 0
gainNode.gain.setTargetAtTime 0, now, .001 # prevents popping
gainNode.gain.linearRampToValueAtTime volume, now + attack gainNode.gain.linearRampToValueAtTime volume, now + attack
gainNode.gain.linearRampToValueAtTime 0, now + sustain gainNode.gain.linearRampToValueAtTime 0, now + sustain
oscillator.connect gainNode filter = context.createBiquadFilter()
oscillator.noteOn? 0 filter.type = filterTypes.lowpass
filter.frequency.value = 900
oscillator.connect filter
filter.connect gainNode
gainNode.connect context.destination gainNode.connect context.destination
return oscillator return oscillator

0 comments on commit 3c74911

Please sign in to comment.