ofxPDSP
ADSR.h
1 
2 // ADSR.h
3 // ofxPDSP
4 // Nicola Pisanti, MIT License, 2016
5 
6 #ifndef PDSP_ENV_ADSRLEG_H_INCLUDED
7 #define PDSP_ENV_ADSRLEG_H_INCLUDED
8 
9 #include "../pdspCore.h"
10 #include "stages/EnvelopeStage.h"
11 #include "stages/AttackStage.h"
12 #include "stages/DecayStage.h"
13 #include "stages/RiseStage.h"
14 #include "stages/SustainStage.h"
15 #include "stages/ReleaseStage.h"
16 
17 namespace pdsp{
18 
26 class ADSR : public Unit,
27  public virtual EnvelopeStage,
28  public virtual AttackStage,
29  public virtual DecayStage,
30  public virtual RiseStage,
31  public virtual SustainStage,
32  public virtual ReleaseStage
33 {
34 
35 public:
36  ADSR();
37 
46  Patchable& set(float attackTimeMs, float decayTimeMs, float sustainLevel, float releaseTimeMs, float velocity = 1.0f );
47 
48 
53  void setAttackCurve(float hardness);
54 
59  void setReleaseCurve(float hardness);
60 
65  void setCurve(float hardness);
66 
67 
73  void enableDBTriggering(float dBmin, float dBmax=0.0f);
74 
78  void disableDBTriggering();
79 
83  Patchable& in_trig();
84 
89 
94 
99 
104 
109 
114 
118  Patchable& out_bias();
119 
123  float meter_output() const;
124 
128  int meter_triggers() const;
129 
130 private:
131 
132  void process(int bufferSize) noexcept override;
133  void prepareUnit( int expectedBufferSize, double sampleRate) override;
134  void releaseResources() override;
135 
136  inline_f void doEnvelope() {
137  switch (stageSwitch){
138  case off : envelopeOutput = 0.0; break;
139  case attackStage : Attack(stageSwitch, decayStage); break;
140  case decayStage : Decay(stageSwitch, sustainStage); break;
141  case sustainStage: Sustain(); break;
142  case releaseStage: Release(stageSwitch, off); break;
143  case riseStage : Rise(stageSwitch, decayStage); break;
144  }
145  };
146 
147  void onRetrigger(float triggerValue, int n);
148 
149  void process_run(int bufferSize);
150  void process_T( const float* &trigBuffer, const int &bufferSize);
151 
152  InputNode input_trig;
153  OutputNode output;
154  InputNode input_attack;
155  InputNode input_decay;
156  InputNode input_sustain;
157  InputNode input_release;
158  InputNode input_velocity;
159 
160  std::atomic<int> trigcount;
161  std::atomic<bool> dBtrig;
162  float dBmin;
163  float dBmax;
164 
165  int stageSwitch;
166 
167  static const int off = 0;
168  static const int attackStage = 1;
169  static const int decayStage = 2;
170  static const int sustainStage = 3;
171  static const int releaseStage = 4;
172  static const int riseStage = 5;
173 
174  std::atomic<float> meter;
175 };
176 
177 } // pdsp namespace end
178 
179 #endif // PDSP_ENV_ADSRLEG_H_INCLUDED
void setAttackCurve(float hardness)
sets the curve of the attack stage the envelope, from a smoother linear in dB curve to an harder anal...
Definition: ADSR.cpp:52
Patchable & in_sustain()
Sets "sustain" as selected input and returns this Unit ready to be patched. This input sets the susta...
Definition: ADSR.cpp:106
void disableDBTriggering()
disable the alternate mode for the trigger dynamics.
Definition: ADSR.cpp:82
void enableDBTriggering(float dBmin, float dBmax=0.0f)
enable an alternate mode for the trigger dynamics, trigger will output linear value that are the equi...
Definition: ADSR.cpp:76
void setReleaseCurve(float hardness)
sets the curve of the decay and release stages of the envelope, from a smoother linear in dB curve to...
Definition: ADSR.cpp:60
Abstract class for implementing Units.
Definition: BasicNodes.h:223
Abstract class for implementing Units and Modules.
Definition: BasicNodes.h:82
Patchable & in_release()
Sets "release" as selected input and returns this Unit ready to be patched. This input sets the relea...
Definition: ADSR.cpp:110
Input of a Unit, process all the connected Outputs and sum them. Has an internal variable state...
Definition: BasicNodes.h:536
float meter_output() const
returns the first value of the last processed output buffer. This method is thread-safe.
Definition: ADSR.cpp:86
Attack-Decay-Sustain-Release envelope.
Definition: ADSR.h:26
Patchable & out_bias()
Sets "bias" as selected output and returns this Unit ready to be patched. This is the negation of the...
Definition: ADSR.cpp:122
Patchable & in_trig()
Sets "trig" as selected input and returns this Unit ready to be patched. This is the default input...
Definition: ADSR.cpp:94
Thread-safe value control with smoothing.
Definition: SequencerBridge.h:11
void setCurve(float hardness)
sets the curve of the envelope, from a smoother linear in dB curve to an harder analog-like curve...
Definition: ADSR.cpp:71
Patchable & in_attack()
Sets "attack" as selected input and returns this Unit ready to be patched. This input sets the attack...
Definition: ADSR.cpp:98
Output of a Unit, contains a buffer of rendered floats and has a variable state flag.
Definition: BasicNodes.h:354
Patchable & out_signal()
Sets "signal" as selected output and returns this Unit ready to be patched. This is the default outpu...
Definition: ADSR.cpp:118
int meter_triggers() const
returns number of received triggers since the start of the program, as int. Can overflow. This method is thread-safe.
Definition: ADSR.cpp:90
Patchable & in_velocity()
Sets "velocity" as selected input and returns this Unit ready to be patched. This input rapresent the...
Definition: ADSR.cpp:114
Patchable & in_decay()
Sets "decay" as selected input and returns this Unit ready to be patched. This input sets the decay t...
Definition: ADSR.cpp:102