forked from sjlongland/atinysynth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adsr.c
76 lines (72 loc) · 2.26 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
/*!
* Polyphonic synthesizer for microcontrollers. ADSR Envelope generator.
* (C) 2017 Stuart Longland - Luciano Martorella
*
* 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 "adsr.h"
#include "voice.h"
#include <stdlib.h>
/*!
* Configure the ADSR.
*/
void adsr_config(struct seq_frame_t* const frame) {
cur_voice->adsr.def.release_start = frame->adsr_release_start;
cur_voice->adsr.next_event = cur_voice->adsr.def.time_scale = frame->adsr_time_scale_1;
cur_voice->adsr.state_counter = ADSR_STATE_INIT; // 1
// Start from mute
cur_voice->adsr.gain = 6;
}
/*!
* Compute the ADSR gain
*/
void adsr_next() {
if (cur_voice->adsr.next_event) {
/* Still waiting for next event */
cur_voice->adsr.next_event--;
} else {
if (!cur_voice->adsr.state_counter) {
// Abort
cur_voice->adsr.gain = 6;
return;
}
if (cur_voice->adsr.state_counter < ADSR_STATE_SUSTAIN_START) {
// Counter from 1 to 6: 5 steps.
// From 6 to 0
cur_voice->adsr.gain--;
}
else if (cur_voice->adsr.state_counter < ADSR_STATE_DECAY_START) {
// Remain to zero
}
else if (cur_voice->adsr.state_counter < cur_voice->adsr.def.release_start) {
// Then decay to 1 and stay
cur_voice->adsr.gain = 1;
}
else {
// Decrease from 2 to 8 every 8 counters
if (!(cur_voice->adsr.state_counter & 0x7)) {
cur_voice->adsr.gain++;
}
}
if (cur_voice->adsr.state_counter > ADSR_TIME_UNITS) {
// 0 is the final state (fast to check)
cur_voice->adsr.state_counter = ADSR_STATE_END;
} else {
cur_voice->adsr.next_event = cur_voice->adsr.def.time_scale;
cur_voice->adsr.state_counter++;
}
}
}