envelope.hpp (1354B)
1 #pragma once 2 3 #include <stdlib.h> 4 5 #include "base.hpp" 6 #include "signal.hpp" 7 8 namespace bogaudio { 9 namespace dsp { 10 11 struct EnvelopeGenerator : Generator { 12 float _sampleRate = -1.0f; 13 float _sampleTime; 14 15 EnvelopeGenerator(float sampleRate = 1000.0f) { 16 setSampleRate(std::max(1.0f, sampleRate)); 17 } 18 19 void setSampleRate(float sampleRate); 20 virtual void _sampleRateChanged() {} 21 }; 22 23 struct ADSR : EnvelopeGenerator { 24 enum Stage { 25 STOPPED_STAGE, 26 ATTACK_STAGE, 27 DECAY_STAGE, 28 SUSTAIN_STAGE, 29 RELEASE_STAGE 30 }; 31 32 Stage _stage = STOPPED_STAGE; 33 bool _gated = false; 34 float _attack = 0.0f; 35 float _decay = 0.0f; 36 float _sustain = 1.0f; 37 float _release = 0.0f; 38 float _attackShape; 39 float _decayShape; 40 float _releaseShape; 41 float _stageProgress = 0.0f; 42 float _releaseLevel = 0.0f; 43 float _envelope = 0.0f; 44 45 ADSR(bool linear = false, float sampleRate = 1000.0f) : EnvelopeGenerator(sampleRate) { 46 setLinearShape(linear); 47 } 48 49 void reset(); 50 void setGate(bool high); 51 void setAttack(float seconds); 52 void setDecay(float seconds); 53 void setSustain(float level); 54 void setRelease(float seconds); 55 void setLinearShape(bool linear); 56 void setShapes(float attackShape, float decayShape, float releaseShape); 57 bool isStage(Stage stage) { return _stage == stage; } 58 void retrigger(); 59 60 float _next() override; 61 }; 62 63 } // namespace dsp 64 } // namespace bogaudio