-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnvelopeGenerator.cpp
More file actions
131 lines (109 loc) · 3.08 KB
/
Copy pathEnvelopeGenerator.cpp
File metadata and controls
131 lines (109 loc) · 3.08 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
#include "EnvelopeGenerator.h"
CEnvelopeGenerator::CEnvelopeGenerator(void)
{
// defaults
m_dSampleRate = 44100;
m_dAttackTime_mSec = EG_DEFAULT_STATE_TIME;
m_dDecayTime_mSec = EG_DEFAULT_STATE_TIME;
m_dReleaseTime_mSec = EG_DEFAULT_STATE_TIME;
m_dSustainLevel = 1.0;
m_dEnvelopeOutput = 0.0;
m_dIncShutdown = 0.0;
// --- user normally not allowed to set the time
m_dShutdownTime_mSec = 10.0; // mSec
// --- states and flags
m_uState = off;
m_bOutputEG = false;
m_uEGMode = analog;
setEGMode(m_uEGMode);
m_bResetToZero = false;
m_bLegatoMode = false;
}
CEnvelopeGenerator::~CEnvelopeGenerator(void)
{
}
void CEnvelopeGenerator::setEGMode(UINT u)
{
// --- save it
m_uEGMode = u;
// --- analog - use e^-1.5x, e^-4.95x
if(m_uEGMode == analog)
{
m_dAttackTCO = exp(-1.5); // fast attack
m_dDecayTCO = exp(-4.95);
m_dReleaseTCO = m_dDecayTCO;
}
else
{
// digital is linear-in-dB so use
m_dAttackTCO = 0.99999;
m_dDecayTCO = m_dDecayTCO = exp(-11.05);
m_dReleaseTCO = m_dDecayTCO;
}
// --- recalc these
calculateAttackTime();
calculateDecayTime();
calculateReleaseTime();
}
// reset
void CEnvelopeGenerator::reset()
{
// --- state
m_uState = off;
// --- reset
setEGMode(m_uEGMode);
// --- may be modified in noteOff()
calculateReleaseTime();
// --- if reset to zero, clear
// else let it stay frozen
if(m_bResetToZero)
{
m_dEnvelopeOutput = 0.0;
}
}
void CEnvelopeGenerator::calculateAttackTime()
{
// --- samples for the exponential rate
double dSamples = m_dSampleRate*((m_dAttackTime_mSec)/1000.0);
// --- coeff and base for iterative exponential calculation
m_dAttackCoeff = exp(-log((1.0 + m_dAttackTCO)/m_dAttackTCO)/dSamples);
m_dAttackOffset = (1.0 + m_dAttackTCO)*(1.0 - m_dAttackCoeff);
}
// --- changes to the decay rate OR the sustain level require
// an update in analog mode
void CEnvelopeGenerator::calculateDecayTime()
{
// --- samples for the exponential rate
double dSamples = m_dSampleRate*((m_dDecayTime_mSec)/1000.0);
// --- coeff and base for iterative exponential calculation
m_dDecayCoeff = exp(-log((1.0 + m_dDecayTCO)/m_dDecayTCO)/dSamples);
m_dDecayOffset = (m_dSustainLevel - m_dDecayTCO)*(1.0 - m_dDecayCoeff);
}
void CEnvelopeGenerator::calculateReleaseTime()
{
// --- samples for the exponential rate
double dSamples = m_dSampleRate*(m_dReleaseTime_mSec/1000.0);
// --- coeff and base for iterative exponential calculation
m_dReleaseCoeff = exp(-log((1.0 + m_dReleaseTCO)/m_dReleaseTCO)/dSamples);
m_dReleaseOffset = -m_dReleaseTCO*(1.0 - m_dReleaseCoeff);
}
// --- go to release state; reset
void CEnvelopeGenerator::noteOff()
{
// --- go directly to release state
if(m_dEnvelopeOutput > 0)
m_uState = release;
else // sustain was already at zero
m_uState = off;
}
// --- goto shutdown state
void CEnvelopeGenerator::shutDown()
{
// --- legato mode - ignore
if(m_bLegatoMode)
return;
// --- calculate the linear inc values based on current outputs
m_dIncShutdown = -(1000.0*m_dEnvelopeOutput)/m_dShutdownTime_mSec/m_dSampleRate;
// --- set state and reset counter
m_uState = shutdown;
}