This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvelope.pde
87 lines (83 loc) · 2.13 KB
/
envelope.pde
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class envelope{
float value;
float attackLength;
float decayLength;
float sustainLevel;
float releaseLength;
int phase;
int stage;
boolean triggered;
boolean released;
float releasedTime;
float currentLevel;
envelope(){
this.stage = 5;
this.phase = 1;
this.attackLength = 0;
this.decayLength = 0;
this.sustainLevel = 1;
this.releaseLength = 0;
}
void set(float _attackLength,float _decayLength,float _sustainLevel,float _releaseLength){
this.attackLength = _attackLength*(44100/1000);
this.decayLength = _decayLength*(44100/1000);
this.sustainLevel = _sustainLevel;
this.releaseLength = _releaseLength*(44100/1000);
}
void setAttack(float _attackLength){
this.attackLength = _attackLength;
}
void setDecay(float _decayLength){
this.decayLength = _decayLength;
}
void setSustain(float _sustainLevel){
this.sustainLevel = _sustainLevel;
}
void setRelease(float _releaseLength){
this.releaseLength = _releaseLength;
}
void trigger(){
this.triggered = true;
phase=1;
stage=1;
}
void release(){
this.releasedTime = this.phase;
this.stage = 4;
this.released = true;
}
float getValue(){
phase++;
if(this.stage == 1){
if(this.phase >= this.attackLength){
stage++;
}
this.currentLevel = this.phase/this.attackLength;
}
else if(this.stage == 2){
if(this.phase >= this.attackLength + this.decayLength){
stage++;
}
this.currentLevel = 1 - ((this.phase-this.attackLength)/this.decayLength)*(1-this.sustainLevel);
}
else if(this.stage == 3){
this.currentLevel = this.sustainLevel;
}
else if(this.stage == 4){
if(this.phase >= (this.releasedTime + this.releaseLength)){
this.stage++;
}
this.currentLevel = (float)this.currentLevel - (((float)this.phase- (float)this.releasedTime)/ (float)this.releaseLength)* (float)this.currentLevel;
}
else{
phase = 0;
this.currentLevel = 0;
}
if(this.currentLevel >= 0 && this.currentLevel <= 1){
return this.currentLevel;
}
else{
return 0;
}
}
}