-
Notifications
You must be signed in to change notification settings - Fork 3
/
envelope.js
66 lines (63 loc) · 2.14 KB
/
envelope.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {EventEmitter} from 'events'
class Envelope extends EventEmitter {
constructor(opts={}) {
super()
this.context = opts.context
this.midi = opts.midi
this.target = opts.target
this.attack = opts.attack || 0.005
this.decay = opts.decay || 0.5
this.sustain = opts.sustain || 0.5
this.release = opts.release || 1.0
this.attackLevel = opts.attackLevel || 1.0
this.decayLevel = opts.decayLevel || 0.6
this.midi.on('noteOn', this.noteOn.bind(this))
this.midi.on('noteOff', this.noteOff.bind(this))
this.reset();
}
reset() {
if (!this.target) return false
this.target.cancelScheduledValues(this.context.currentTime)
this.target.setValueAtTime(0, this.context.currentTime)
}
get target() {
return this._target
}
set target(target) {
this.reset()
this._target = target
return this._target
}
//
// get attack() {
// console.log(`this._attack is ${this._attack}`)
// console.log(`attack is ${this.context.currentTime + this._attack}`)
// return (this.currentTime + this._attack)
// }
// get decay() {
// console.log(`this._decay is ${this._decay}`)
// console.log(`decay is ${this.context.currentTime + this._decay}`)
// return (this.currentTime + this._attack + this._decay)
// }
noteOn() {
let cur = this.context.currentTime
console.log('noteOn')
console.log(`this.context.currentTime: ${cur}`)
this.reset()
this.target.linearRampToValueAtTime(this.attackLevel, cur + this.attack)
console.log(this.target)
// this.target
// .on('statechange',(e) => console.log('state change', e))
// .on('complete',(e) => console.log('complete', e))
// .on('ended',(e) => console.log('ended', e))
this.target.linearRampToValueAtTime(this.decayLevel, cur + this.attack + this.decay)
// TODO - how do we implement sustain? how can i callback here to check if
// we're still in noteon?
}
noteOff() {
console.log('noteOff')
console.log(`this.context.currentTime: ${this.context.currentTime}`)
this.target.linearRampToValueAtTime(0, this.context.currentTime + this.release)
}
}
module.exports = Envelope