-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadsr.js
46 lines (45 loc) · 783 Bytes
/
adsr.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
module.exports = class ADSR {
constructor() {
this.state = "attack"
this.vol = 0
this.peak = 127
this.attack_rate = 1
this.decay_rate = 2
this.sustain = 63
this.release_rate = 10
}
next() {
switch (this.state) {
case "attack": {
this.vol += this.attack_rate
if (this.vol > this.peak) {
this.vol = this.peak
this.state = "decay"
}
break
}
case "decay": {
this.vol -= this.decay_rate
if (this.vol < this.sustain) {
this.vol = this.sustain
this.state = "sustain"
}
break
}
case "done": {
}
case "sustain": {
break
}
case "release": {
this.vol -= this.release_rate
if (this.vol < 0) {
this.vol = 0
this.state = "done"
}
break
}
}
return this.vol
}
}