-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADSREnvelope.soul
90 lines (70 loc) · 2.55 KB
/
ADSREnvelope.soul
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
// referenced from SOUL/example/PadSynth.soul
// TODO: implement proper gain tracking, ramp output value.
processor ADSREnvelope
{
input stream float triggerLevel;
output stream float envelopeLevel;
input event float threshhold;
event threshhold (float f)
{
threshholdLevel = f * (1.0f / 10000.0f);
}
const float sampleRate = float (processor.frequency);
float64 attackRamp = 1.0;
float64 decayMultiplier = 0.995;
float64 sustainLevel = 0.0;
float64 releaseMultiplier = 0.99991;
float32 velocitySensitivity = 0.1f;
float64 targetValue = 1.0;
float threshholdLevel = 0.5;
void calculateTargetValue (float noteVelocity)
{
// Use the velocitySensitivity to decide how the noteVelocity affects the target volume
// We determine a dB attenuation range, then the note velocity decides where we are on it
// Full velocity sensitivity is -12dB
// We use 100 as a 'loud' note, so that's 0.75 out of 100 as 'normal', any higher velocity will be louder
let attenuation = -12.0f * velocitySensitivity * (0.75f - noteVelocity);
targetValue = pow (10.0f, attenuation / 10.0f);
}
void run()
{
float64 value = 0;
loop
{
if (triggerLevel > threshholdLevel)
{
// Use the value of the trigger to modify our target value
calculateTargetValue (triggerLevel);
while (value < targetValue)
{
value += attackRamp;
envelopeLevel << float(value);
advance();
if (triggerLevel <= threshholdLevel)
break;
}
// Cap it to the target value
value = min (value, targetValue);
// Decay stage
while (value > targetValue * sustainLevel && triggerLevel > threshholdLevel)
{
value = value * decayMultiplier;
envelopeLevel << float (value);
advance();
}
// Sustain stage
while (triggerLevel > threshholdLevel)
{
envelopeLevel << float (value);
advance();
}
}
// console << value;
// console << "\n";
value *= releaseMultiplier;
// console << value;
envelopeLevel << float (value);
advance();
}
}
}