-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathadenv.cpp
More file actions
167 lines (151 loc) · 3.51 KB
/
Copy pathadenv.cpp
File metadata and controls
167 lines (151 loc) · 3.51 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
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
#include <algorithm>
#include <limits>
#include <math.h>
#include "adenv.h"
using namespace daisysp;
//#define EXPF expf
// This causes with infinity with certain curves,
// which then causes NaN erros...
#define EXPF expf_fast
// To resolve annoying bugs when using this you can:
// if (val != val)
// val = 0.0f; // This will un-NaN the value.
// Fast Exp approximation
// 8x multiply version
//inline float expf_fast(float x)
//{
// x = 1.0f + x / 256.0f;
// x *= x;
// x *= x;
// x *= x;
// x *= x;
// x *= x;
// x *= x;
// x *= x;
// x *= x;
// return x;
//}
// 10x multiply version
inline float expf_fast(float x)
{
x = 1.0f + x / 1024.0f;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
return x;
}
// Private Functions
void AdEnv::Init(float sample_rate)
{
sample_rate_ = sample_rate;
current_segment_ = ADENV_SEG_IDLE;
curve_scalar_ = 0.0f; // full linear
phase_ = 0;
min_ = 0.0f;
max_ = 1.0f;
output_ = 0.0001f;
for(uint8_t i = 0; i < ADENV_SEG_LAST; i++)
{
segment_time_[i] = 0.05f;
}
}
float AdEnv::Process()
{
uint32_t time_samps;
float val, out, end, beg, inc;
// Handle Retriggering
if(trigger_)
{
trigger_ = 0;
current_segment_ = ADENV_SEG_ATTACK;
phase_ = 0;
curve_x_ = 0.0f;
retrig_val_ = hard_trigger_ ? 0.0 : output_;
}
time_samps = (uint32_t)(segment_time_[current_segment_] * sample_rate_);
// Fixed for now, but we could always make this a more flexible multi-segment envelope
switch(current_segment_)
{
case ADENV_SEG_ATTACK:
beg = retrig_val_;
end = 1.0f;
break;
case ADENV_SEG_DECAY:
beg = 1.0f;
end = 0.0f;
break;
case ADENV_SEG_IDLE:
default:
beg = 0;
end = 0;
break;
}
if(prev_segment_ != current_segment_)
{
//Reset at segment beginning
curve_x_ = 0;
phase_ = 0;
}
//recalculate increment value
if(curve_scalar_ == 0.0f)
{
c_inc_ = (end - beg) / time_samps;
}
else
{
c_inc_ = (end - beg) / (1.0f - EXPF(curve_scalar_));
}
if(c_inc_ >= 0.0f)
{
c_inc_ = std::max(c_inc_, std::numeric_limits<float>::epsilon());
}
else
{
c_inc_ = std::min(c_inc_, -std::numeric_limits<float>::epsilon());
}
// update output
val = output_;
inc = c_inc_;
out = val;
if(curve_scalar_ == 0.0f)
{
val += inc;
}
else
{
curve_x_ += (curve_scalar_ / time_samps);
val = beg + inc * (1.0f - EXPF(curve_x_));
if(val != val)
val = 0.0f; // NaN check
}
// Update Segment
phase_ += 1;
prev_segment_ = current_segment_;
if(current_segment_ != ADENV_SEG_IDLE)
{
if((out >= 1.f && current_segment_ == ADENV_SEG_ATTACK)
|| (out <= 0.f && current_segment_ == ADENV_SEG_DECAY))
{
// Advance segment
current_segment_++;
// TODO: Add Cycling feature here.
if(current_segment_ > ADENV_SEG_DECAY)
{
current_segment_ = ADENV_SEG_IDLE;
}
}
}
if(current_segment_ == ADENV_SEG_IDLE)
{
val = out = 0.0f;
}
output_ = val;
return out * (max_ - min_) + min_;
}