-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdsrEnvelope.cpp
156 lines (122 loc) · 3.08 KB
/
AdsrEnvelope.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Dependencies */
#include "Response.h"
#include "AdsrEnvelope.h"
namespace CheapTune {
AdsrEnvelope::AdsrEnvelope(Response* response, AdsrTiming_t adsr) :
_adsr(adsr), _state(ENV_ENDED), _response(response), _timeTick(0), _currentTick(
0) {
}
void AdsrEnvelope::setResponse(Response* response) {
_response = response;
}
void AdsrEnvelope::setAdsrTimings(AdsrTiming_t adsr) {
_adsr = adsr;
}
void AdsrEnvelope::setState(EnvelopeState_t state) {
/* Store the new state and reset the tick counter */
_state = state;
_currentTick = 0;
/* Switch according the new state */
switch (state) {
case ENV_SUSTAIN:
case ENV_ENDED:
_timeTick = 0xFFFF; // Avoid the getSample() routine to do useless stuff
break;
case ENV_ATTACK:
_timeTick = _adsr.attack_time;
break;
case ENV_DECAY:
_timeTick = _adsr.decay_time;
break;
case ENV_RELEASE:
_timeTick = _adsr.release_time;
break;
}
}
void AdsrEnvelope::noteOn() {
setState(ENV_ATTACK);
}
void AdsrEnvelope::noteOff() {
if (_state != ENV_ENDED) /* Avoid glitch */
setState(ENV_RELEASE);
}
uint8_t AdsrEnvelope::getSample() {
/* Check if the envelope has ended */
if (_state == ENV_ENDED)
return 0;
/* Check if the current state has finish */
if (_currentTick == _timeTick) {
/* Reset the tick counter */
_currentTick = 0;
/* Switch according the current state */
switch (_state) {
case ENV_ATTACK:
/* Test if the decay time is null */
if (_adsr.decay_time) {
/* Upgrade the envelope state to the DECAY phase */
_state = ENV_DECAY;
_timeTick = _adsr.decay_time;
} else { /* No decay phase */
/* Upgrade the envelope state to the SUSTAIN phase */
_state = ENV_SUSTAIN;
_timeTick = 0xFFFF;
}
break;
case ENV_DECAY:
/* Upgrade the envelope state to the SUSTAIN phase */
_state = ENV_SUSTAIN;
_timeTick = 0xFFFF;
break;
case ENV_SUSTAIN:
/* Nothing to do, this phase is stable */
/* Only setState(ENV_RELEASE) can make the state change */
break;
case ENV_RELEASE:
/* Upgrade the envelope state to the ENDED phase */
_state = ENV_ENDED;
return 0;
default:
break;
}
}
/* At this step the envelope is already upgraded if necessary and not in ENV_DISABLE or ENV_ENDED state */
{
/* The generated sample */
uint8_t sample;
/* Switch according the current state */
switch (_state) {
case ENV_ATTACK:
sample = _response->getResponse(_currentTick, _timeTick, 0, 255);
break;
case ENV_DECAY:
sample = 255
- _response->getResponse(_currentTick, _timeTick, 0,
_adsr.sustain_level);
break;
case ENV_SUSTAIN:
sample = _adsr.sustain_level;
break;
case ENV_RELEASE:
sample = _adsr.sustain_level
- _response->getResponse(_currentTick, _timeTick, 0,
_adsr.sustain_level);
break;
default:
break;
}
/* Increment the tick counter */
++_currentTick;
/* Return the generated sample */
return sample;
}
}
void AdsrEnvelope::reset() {
_adsr.attack_time = 0;
_adsr.decay_time = 0;
_adsr.sustain_level = 0;
_adsr.release_time = 0;
_state = ENV_ENDED;
_timeTick = 0;
_currentTick = 0;
}
}