-
Notifications
You must be signed in to change notification settings - Fork 1
/
note.js
1 lines (1 loc) · 1.27 KB
/
note.js
1
class Note{constructor(t,e={}){this.context=t.context||t,this.attack=e.attack||.02,this.decay=e.decay||.02,this.sustain=e.sustain||.04,this.release=e.release||.01,this.velocity=e.velocity||1,this.triggerTime=e.triggerTime||this.context.currentTime,this.envGain=this.context.createGain(),this.envGain.gain.setValueAtTime(0,this.triggerTime),this.envGain.gain.linearRampToValueAtTime(this.velocity,this.triggerTime+this.attack),this.envGain.gain.setTargetAtTime(this.sustain*this.velocity,this.triggerTime+this.attack,this.decay),this.envGain.connect(t),this.oscs=[]}releaseNote(){this.stopNote(this.context.currentTime+20*this.release),this.envGain.gain.setTargetAtTime(0,Math.max(this.context.currentTime,this.triggerTime+this.attack+this.decay),this.release)}stopNote(t=this.context.currentTime){for(const e of this.oscs)e.stop(t)}}class OscillatorNote extends Note{constructor(t,e={},i=[{}]){super(t,e);for(const t of i){let e=this.context.createGain();e.gain.value=t.gain||.5;let i=this.context.createOscillator();i.detune.value=t.detune||0,i.frequency.value=t.frequency||440,"custom"===t.type?i.setPeriodicWave(this.context.createPeriodicWave(new Float32Array(t.real||[0,1]),new Float32Array(t.imag||[0,0]))):i.type=t.type||"sine",i.connect(e).connect(this.envGain),i.start(),this.oscs.push(i)}}}