-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadsr.cpp
More file actions
94 lines (79 loc) · 1.56 KB
/
Copy pathadsr.cpp
File metadata and controls
94 lines (79 loc) · 1.56 KB
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
88
89
90
91
92
93
94
#include "adsr.h"
ADSR::ADSR(float p)
{
period = p;
level = 0;
next_level = 0;
ph = phase_attack;
cycle_complete = false;
}
void ADSR::setAttack(float a) {
attack = a;
}
void ADSR::setDecay(float d) {
decay = d;
}
void ADSR::setSustain(float s) {
sustain = s;
}
void ADSR::setRelease(float r) {
release = r;
}
float ADSR::sample() {
if(cycle_complete) {
return 0;
}
level = next_level;
switch(ph){
case phase_attack:
next_level += attack_delta;
if(next_level > 1) {
next_level = 1;
ph = phase_decay;
}
break;
case phase_decay:
next_level -= decay_delta;
if(next_level < sustain) {
next_level = sustain;
ph = phase_sustain;
}
break;
case phase_sustain:
next_level = level;
break;
case phase_release:
next_level -= release_delta;
if(next_level < 0) {
cycle_complete = true;
next_level = 0;
}
break;
}
return level;
}
void ADSR::gate() {
attack_delta = (1 / attack) * period;
decay_delta = (1 / decay) * period;
}
void ADSR::cutoff() {
release_delta = (1 / release) * period;
ph = phase_release;
}
bool ADSR::isSustain() {
return ph == phase_sustain;
}
bool ADSR::isAttack() {
return ph == phase_attack;
}
bool ADSR::isDecay() {
return ph == phase_decay;
}
bool ADSR::isRelease() {
return ph == phase_release;
}
bool ADSR::isCycleComplete() {
return cycle_complete;
}
ADSR::~ADSR() {
}