-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadsr.c
290 lines (247 loc) · 7.36 KB
/
adsr.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*!
* Polyphonic synthesizer for microcontrollers. ADSR Envelope generator.
* (C) 2017 Stuart Longland
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "debug.h"
#include "adsr.h"
#include <stdlib.h>
#ifdef __AVR_ARCH__
#include <avr/pgmspace.h>
#endif
/* ADSR attack/decay adjustments */
#define ADSR_LIN_AMP_FACTOR (5)
/*!
* Helper macro, returns the time in samples if the number of
* time units ≠ UINT8_MAX (infinite), otherwise it returns
* UINT32_MAX.
*/
static inline uint32_t adsr_num_samples(uint32_t scale, uint8_t units) {
if (units != ADSR_INFINITE)
return scale * units;
else
return UINT32_MAX;
}
/*!
* ADSR Attack amplitude exponential shift.
*/
static uint8_t adsr_attack_amp(uint8_t amp, uint8_t count) {
if (count >= 8)
return 0;
amp >>= count+1;
return amp;
}
/*!
* ADSR Release amplitude exponential shift.
*/
static uint8_t adsr_release_amp(uint8_t amp, uint8_t count) {
return adsr_attack_amp(amp, 16 - count);
}
/*!
* Compute the ADSR amplitude
*/
uint8_t adsr_next(struct adsr_env_gen_t* const adsr) {
if (adsr->next_event) {
/* Still waiting for next event */
if (adsr->next_event != UINT32_MAX)
adsr->next_event--;
_DPRINTF("adsr=%p amp=%d next_in=%d\n",
adsr, adsr->amplitude, adsr->next_event);
return adsr->amplitude;
}
/*
* We use if statements here since we might want to jump
* between states. This lets us do that more easily.
*/
if (adsr->state == ADSR_STATE_IDLE) {
_DPRINTF("adsr=%p IDLE time_scale=%d "
"delay_time=%d "
"attack_time=%d "
"decay_time=%d "
"sustain_time=%d "
"release_time=%d "
"peak_amp=%d "
"sustain_amp=%d\n",
adsr, adsr->time_scale,
adsr->delay_time,
adsr->attack_time,
adsr->decay_time,
adsr->sustain_time,
adsr->release_time,
adsr->peak_amp,
adsr->sustain_amp);
/* Are registers set up? */
if (!adsr->def.time_scale)
return 0;
_DPRINTF("adsr=%p time scale set\n", adsr);
if (!(adsr->def.delay_time || adsr->def.attack_time
|| adsr->def.decay_time
|| adsr->def.sustain_time
|| adsr->def.release_time))
return 0;
_DPRINTF("adsr=%p envelope timings set\n", adsr);
if (!(adsr->def.peak_amp || adsr->def.sustain_amp))
return 0;
_DPRINTF("adsr=%p envelope amplitudes set\n", adsr);
/* All good */
if (adsr->def.delay_time)
adsr->state = ADSR_STATE_DELAY_INIT;
else
adsr->state = ADSR_STATE_DELAY_EXPIRE;
}
if (adsr->state == ADSR_STATE_DELAY_INIT) {
_DPRINTF("adsr=%p DELAY INIT\n", adsr);
/* Setting up a delay */
adsr->amplitude = 0;
adsr->next_event = adsr_num_samples(
adsr->def.time_scale, adsr->def.delay_time);
adsr->state = ADSR_STATE_DELAY_EXPIRE;
/* Wait for delay */
return adsr->amplitude;
}
if (adsr->state == ADSR_STATE_DELAY_EXPIRE) {
_DPRINTF("adsr=%p DELAY EXPIRE\n", adsr);
/* Delay has expired */
if (adsr->def.attack_time)
adsr->state = ADSR_STATE_ATTACK_INIT;
else
adsr->state = ADSR_STATE_ATTACK_EXPIRE;
}
if (adsr->state == ADSR_STATE_ATTACK_INIT) {
/* Attack is divided into 16 segments */
adsr->time_step = (uint16_t)((adsr->def.attack_time
* adsr->def.time_scale) >> 4);
adsr->counter = 16;
adsr->next_event = adsr->time_step;
adsr->state = ADSR_STATE_ATTACK;
_DPRINTF("adsr=%p ATTACK INIT tstep=%d\n",
adsr, adsr->time_step);
}
if (adsr->state == ADSR_STATE_ATTACK) {
_DPRINTF("adsr=%p ATTACK count=%d\n", adsr,
adsr->counter);
if (adsr->counter) {
/* Change of amplitude */
uint16_t lin_amp = (16-adsr->counter)
* adsr->def.peak_amp;
uint16_t exp_amp = adsr_attack_amp(
adsr->def.peak_amp, adsr->counter);
lin_amp >>= ADSR_LIN_AMP_FACTOR;
_DPRINTF("adsr=%p ATTACK lin=%d exp=%d\n",
adsr, lin_amp, exp_amp);
adsr->amplitude = lin_amp + exp_amp;
/* Go around again */
adsr->counter--;
adsr->next_event = adsr->time_step;
return adsr->amplitude;
} else {
adsr->state = ADSR_STATE_ATTACK_EXPIRE;
}
}
if (adsr->state == ADSR_STATE_ATTACK_EXPIRE) {
_DPRINTF("adsr=%p ATTACK EXPIRE\n", adsr);
if (adsr->def.decay_time)
adsr->state = ADSR_STATE_DECAY_INIT;
else
adsr->state = ADSR_STATE_DECAY_EXPIRE;
}
if (adsr->state == ADSR_STATE_DECAY_INIT) {
_DPRINTF("adsr=%p DECAY INIT\n", adsr);
/* We should be at full amplitude */
adsr->amplitude = adsr->def.peak_amp;
adsr->time_step = (uint16_t)((adsr->def.decay_time
* adsr->def.time_scale) >> 4);
adsr->counter = 16;
adsr->next_event = adsr->time_step;
adsr->state = ADSR_STATE_DECAY;
}
if (adsr->state == ADSR_STATE_DECAY) {
_DPRINTF("adsr=%p DECAY\n", adsr);
if (adsr->counter) {
/* Linear decrease in amplitude */
uint16_t delta = adsr->def.peak_amp
- adsr->def.sustain_amp;
delta *= adsr->counter;
delta >>= 4;
adsr->amplitude = adsr->def.sustain_amp + delta;
adsr->next_event = adsr->time_step;
adsr->counter--;
} else {
adsr->state = ADSR_STATE_DECAY_EXPIRE;
}
}
if (adsr->state == ADSR_STATE_DECAY_EXPIRE) {
_DPRINTF("adsr=%p DECAY EXPIRE\n", adsr);
if (adsr->def.sustain_time)
adsr->state = ADSR_STATE_SUSTAIN_INIT;
else
adsr->state = ADSR_STATE_SUSTAIN_EXPIRE;
}
if (adsr->state == ADSR_STATE_SUSTAIN_INIT) {
_DPRINTF("adsr=%p SUSTAIN INIT\n", adsr);
adsr->amplitude = adsr->def.sustain_amp;
adsr->next_event = adsr_num_samples(
adsr->def.time_scale, adsr->def.sustain_time);
adsr->state = ADSR_STATE_SUSTAIN_EXPIRE;
/* Wait for delay */
return adsr->amplitude;
}
if (adsr->state == ADSR_STATE_SUSTAIN_EXPIRE) {
_DPRINTF("adsr=%p SUSTAIN EXPIRE\n", adsr);
if (adsr->def.release_time)
adsr->state = ADSR_STATE_RELEASE_INIT;
else
adsr->state = ADSR_STATE_RELEASE_EXPIRE;
}
if (adsr->state == ADSR_STATE_RELEASE_INIT) {
_DPRINTF("adsr=%p RELEASE INIT\n", adsr);
adsr->time_step = (uint16_t)((adsr->def.release_time
* adsr->def.time_scale) >> 4);
adsr->counter = 16;
adsr->next_event = adsr->time_step;
adsr->state = ADSR_STATE_RELEASE;
}
if (adsr->state == ADSR_STATE_RELEASE) {
_DPRINTF("adsr=%p RELEASE\n", adsr);
if (adsr->counter) {
/* Change of amplitude */
uint16_t lin_amp = adsr->counter
* adsr->def.sustain_amp;
uint16_t exp_amp = adsr_release_amp(
adsr->def.sustain_amp, adsr->counter);
lin_amp >>= ADSR_LIN_AMP_FACTOR;
_DPRINTF("adsr=%p RELEASE lin=%d exp=%d\n",
adsr, lin_amp, exp_amp);
adsr->amplitude = lin_amp + exp_amp;
/* Go around again */
adsr->counter--;
adsr->next_event = adsr->time_step;
} else {
adsr->state = ADSR_STATE_RELEASE_EXPIRE;
}
}
if (adsr->state == ADSR_STATE_RELEASE_EXPIRE) {
_DPRINTF("adsr=%p RELEASE EXPIRE\n", adsr);
/* Reset the state */
adsr->state = ADSR_STATE_DONE;
adsr->amplitude = 0;
}
return adsr->amplitude;
}
/*
* vim: set sw=8 ts=8 noet si tw=72
*/