-
Notifications
You must be signed in to change notification settings - Fork 0
/
Envelope.h
68 lines (61 loc) · 1.44 KB
/
Envelope.h
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
#pragma once
class Envelope
{
public:
Envelope()
{
SetAttack(0.0);
SetDecay(0.0);
SetSustain(1.0);
SetRelease(0.0);
}
void SetAttack(double value)
{
m_dAttack = value + 0.0005; // Add a very small number to attack so it doesn't pop when the value is 0
}
void SetDecay(double value)
{
m_dDecay = value + 0.01; // Add 0.01 to decay so we're never dividing by zero during GetAmplitude()
}
void SetSustain(double value)
{
m_dSustain = value;
m_dSustainInverse = 1.0 - value;
}
void SetRelease(double value)
{
m_dRelease = value + 0.01; // Add 0.01 to release so it doesn't pop when the value is 0
}
/*
activeTime is the duration that the note has been active
releaseTime is the duration since the note has been released
*/
double GetAmplitude(bool noteActive, double activeTime, double releaseTime, double prevAmplitude)
{
if (noteActive)
{
if (activeTime < m_dAttack) // Attack
{
return activeTime / (m_dAttack);
}
else if (activeTime < m_dAttack + m_dDecay) // Decay
{
return 1.0 - m_dSustainInverse * ((activeTime - m_dAttack) / m_dDecay);
}
else // Sustain
{
return m_dSustain;
}
}
else // Release
{
return prevAmplitude * (1 - releaseTime / m_dRelease);
}
}
private:
double m_dAttack;
double m_dDecay;
double m_dSustain;
double m_dSustainInverse;
double m_dRelease;
};