SynthLab SDK
analogegcore.h
1 #pragma once
2 
3 #include "synthbase.h"
4 #include "synthfunctions.h"
5 
6 // -----------------------------
7 // --- SynthLab SDK File --- //
8 // ----------------------------
16 // -----------------------------------------------------------------------------
17 namespace SynthLab
18 {
84  class AnalogEGCore : public ModuleCore
85  {
86  public:
88  AnalogEGCore(); /* C-TOR */
89 
91  virtual ~AnalogEGCore() {} /* D-TOR */
92 
94  virtual bool reset(CoreProcData& processInfo) override;
95  virtual bool update(CoreProcData& processInfo) override;
96  virtual bool render(CoreProcData& processInfo) override;
97  virtual bool doNoteOn(CoreProcData& processInfo) override;
98  virtual bool doNoteOff(CoreProcData& processInfo) override;
99 
101  virtual int32_t getState() override { return enumToInt(state); }
102  virtual bool shutdown() override;
103  virtual void setSustainOverride(bool b) override;
104 
105  protected:
106  bool noteOff = false;
107  bool retriggered = false;
108  double lastTriggerMod = 0.0;
109 
110  // --- index of contour
111  double sustainLevel = 1.0;
112  double attackTime_mSec = 1.0;
113  double decayTime_mSec = 1.0;
114  double releaseTime_mSec = 1.0;
115 
117  inline void calcAttackCoeff(double attackTime, double attackTimeScalar = 1.0)
118  {
119  // --- store for comparing so don't need to waste cycles on updates
120  attackTime_mSec = attackTime;
121 
122  // --- samples for the exponential rate
123  double samples = sampleRate*((attackTime_mSec*attackTimeScalar) / 1000.0);
124 
125  // --- coeff and base for iterative exponential calculation
126  attackCoeff = exp(-log((1.0 + attackTCO) / attackTCO) / samples);
127  attackOffset = (1.0 + attackTCO)*(1.0 - attackCoeff);
128  }
129 
131  inline void calcDecayCoeff(double decayTime, double decayTimeScalar = 1.0)
132  {
133  // --- store for comparing so don't need to waste cycles on updates
134  decayTime_mSec = decayTime;
135 
136  // --- samples for the exponential rate
137  double samples = sampleRate*((decayTime_mSec*decayTimeScalar) / 1000.0);
138 
139  // --- coeff and base for iterative exponential calculation
140  decayCoeff = exp(-log((1.0 + decayTCO) / decayTCO) / samples);
142  }
143 
145  void calcReleaseCoeff(double releaseTime, double releaseTimeScalar = 1.0)
146  {
147  // --- store for comparing so don't need to waste cycles on updates
148  releaseTime_mSec = releaseTime;
149 
150  // --- samples for the exponential rate
151  double samples = sampleRate*((releaseTime_mSec*releaseTimeScalar) / 1000.0);
152 
153  // --- coeff and base for iterative exponential calculation
154  releaseCoeff = exp(-log((1.0 + releaseTCO) / releaseTCO) / samples);
156  }
157 
158  // --- sample rate for time calculations
159  double sampleRate = 0.0;
160 
161  // --- the current output of the EG
162  double envelopeOutput = 0.0;
163 
164 
165  //--- Coefficient, offset and TCO values
166  // for each state
167  double attackCoeff = 0.0;
168  double attackOffset = 0.0;
169  double attackTCO = 0.0;
170 
171  double decayCoeff = 0.0;
172  double decayOffset = 0.0;
173  double decayTCO = 0.0;
174 
175  double releaseCoeff = 0.0;
176  double releaseOffset = 0.0;
177  double releaseTCO = 0.0;
178 
179  // --- for sustain pedal input; these two bools keep track of the override,
180  // and eventual release when the sus pedal is released after user released key
181  bool sustainOverride = false;
182  bool releasePending = false;
183 
184  // --- inc value for shutdown
185  double incShutdown = 0.0;
186 
187  // --- stage variable
188  EGState state = EGState::kOff;
189  };
190 
191 
192 
193 } // namespace
194 
double decayOffset
TCO offset to allow proper attack/decay on [1, 0].
Definition: analogegcore.h:172
#define enumToInt(ENUM)
macro helper to cast a typed enum to an int
Definition: synthfunctions.h:251
virtual void setSustainOverride(bool b) override
Sustain pedal handler for EG.
Definition: analogegcore.cpp:420
void calcReleaseCoeff(double releaseTime, double releaseTimeScalar=1.0)
Definition: analogegcore.h:145
double lastTriggerMod
for retriggering EG trigger detection
Definition: analogegcore.h:108
double attackCoeff
exponential feedback coefficient
Definition: analogegcore.h:167
double attackOffset
TCO offset to allow proper attack/decay on [1, 0].
Definition: analogegcore.h:168
virtual bool reset(CoreProcData &processInfo) override
Resets object to initialized state.
Definition: analogegcore.cpp:75
double sustainLevel
level, not time
Definition: analogegcore.h:111
virtual int32_t getState() override
Definition: analogegcore.h:101
double sampleRate
sample rate
Definition: analogegcore.h:159
bool releasePending
a flag set when a note off event occurs while the sustain pedal is held, telling the EG to go to the ...
Definition: analogegcore.h:182
bool retriggered
for retriggering EG
Definition: analogegcore.h:107
bool sustainOverride
if true, places the EG into sustain mode
Definition: analogegcore.h:181
double releaseTCO
TCO value for calculating offset.
Definition: analogegcore.h:177
double incShutdown
shutdown linear incrementer
Definition: analogegcore.h:185
virtual ~AnalogEGCore()
Definition: analogegcore.h:91
virtual bool doNoteOn(CoreProcData &processInfo) override
Note-on handler for the ModuleCore.
Definition: analogegcore.cpp:317
void calcDecayCoeff(double decayTime, double decayTimeScalar=1.0)
Definition: analogegcore.h:131
Analog EG emulator.
Definition: analogegcore.h:84
Definition: addosccore.cpp:4
void calcAttackCoeff(double attackTime, double attackTimeScalar=1.0)
Definition: analogegcore.h:117
double envelopeOutput
the current envelope output sample
Definition: analogegcore.h:162
double releaseTime_mSec
rel: is a time to decay from max output to 0.0
Definition: analogegcore.h:114
EGState state
EG state variable.
Definition: analogegcore.h:188
bool noteOff
for retriggering EG
Definition: analogegcore.h:106
double decayTCO
TCO value for calculating offset.
Definition: analogegcore.h:173
virtual bool doNoteOff(CoreProcData &processInfo) override
Note-off handler for the ModuleCore.
Definition: analogegcore.cpp:372
EGState
Definition: synthlabparams.h:723
double releaseCoeff
exponential feedback coefficient
Definition: analogegcore.h:175
virtual bool render(CoreProcData &processInfo) override
Renders the output of the module.
Definition: analogegcore.cpp:179
double releaseOffset
TCO offset to allow proper attack/decay on [1, 0].
Definition: analogegcore.h:176
virtual bool shutdown() override
Shutdown handler for EG.
Definition: analogegcore.cpp:398
virtual bool update(CoreProcData &processInfo) override
Updates the object for the next block of audio processing.
Definition: analogegcore.cpp:117
double attackTCO
TCO value for calculating offset.
Definition: analogegcore.h:169
double attackTime_mSec
att: is a time duration
Definition: analogegcore.h:112
double decayTime_mSec
dcy: is a time to decay from max output to 0.0
Definition: analogegcore.h:113
double decayCoeff
exponential feedback coefficient
Definition: analogegcore.h:171
hard-coded arrays of FIR filter coefficients for the sample rate conversion objects (Interpolator and...
AnalogEGCore()
Construction: Cores follow the same construction pattern.
Definition: analogegcore.cpp:32
This structure holds all of the information needed to call functions on a ModuleCore object...
Definition: synthbase.h:1071
Abstract base class that encapsulates functionality of a module core; used with the Module-Core parad...
Definition: synthbase.h:1516
See also Designing Software Synthesizers in C++ 2nd Ed. by Will Pirkle.