59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
|
// audio for events
|
||
|
// AUDIO SETUP
|
||
|
let audioEngine = new(window.AudioContext || window.webkitAudioContext)()
|
||
|
let masterGain = audioEngine.createGain()
|
||
|
|
||
|
let voice = {
|
||
|
gain: audioEngine.createGain(),
|
||
|
osc: audioEngine.createOscillator(),
|
||
|
adsr: {
|
||
|
a: typeof amount === 'number' ? amount * 1000 : 0.025,
|
||
|
d: typeof amount === 'number' ? amount * 1000 : 0.5,
|
||
|
sv: typeof amount === 'number' ? amount : 0.8,
|
||
|
r: typeof amount === 'number' ? amount * 1000 : 0.5,
|
||
|
t: audioEngine.currentTime
|
||
|
},
|
||
|
play: function(note) {
|
||
|
freq = typeof note === 'number' ? note : (Math.random() * 350) + 50
|
||
|
this.osc.frequency.setValueAtTime(freq, audioEngine.currentTime)
|
||
|
this.engage()
|
||
|
},
|
||
|
engage: function() {
|
||
|
|
||
|
// clear preScheduled events
|
||
|
this.gain.gain.cancelScheduledValues(this.adsr.t)
|
||
|
// initial value
|
||
|
this.gain.gain.setValueAtTime(0.0, this.adsr.t)
|
||
|
// attack
|
||
|
this.gain.gain.setTargetAtTime(0.9, this.adsr.t, this.adsr.a)
|
||
|
// decay
|
||
|
this.gain.gain.setTargetAtTime(this.adsr.sv, this.adsr.t + this.adsr.a, this.adsr.d)
|
||
|
},
|
||
|
release: function() {
|
||
|
this.adsr.t = audioEngine.currentTime
|
||
|
// release
|
||
|
this.gain.gain.setTargetAtTime(0.0, this.adsr.t, this.adsr.r)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
window.onload = function() {
|
||
|
// create gain
|
||
|
voice.gain.gain.setValueAtTime(0.0001, audioEngine.currentTime)
|
||
|
masterGain.gain.value = 0.9
|
||
|
voice.gain.connect(masterGain)
|
||
|
|
||
|
// create oscilator
|
||
|
voice.osc.type = 'sine'
|
||
|
voice.osc.connect(voice.gain)
|
||
|
voice.osc.start()
|
||
|
|
||
|
masterGain.connect(audioEngine.destination)
|
||
|
}
|
||
|
|
||
|
|
||
|
// AMBIENT
|
||
|
|
||
|
|
||
|
|
||
|
// BUTTON HOVER
|