Mercurial > hg > beaglert
view projects/d-box/ADSR.h @ 39:638bc1ae2500 staging
Improved readibility of the DIGITAL code in the PRU, using register names instead of aliases and expanding some of the macros, removing unused macros. Binaries were not modified
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Wed, 13 May 2015 12:18:10 +0100 |
parents | 8a575ba3ab52 |
children |
line wrap: on
line source
// // ADRS.h // // Created by Nigel Redmon on 12/18/12. // EarLevel Engineering: earlevel.com // Copyright 2012 Nigel Redmon // // For a complete explanation of the ADSR envelope generator and code, // read the series of articles by the author, starting here: // http://www.earlevel.com/main/2013/06/01/envelope-generators/ // // License: // // This source code is provided as is, without warranty. // You may copy and distribute verbatim copies of this document. // You may modify and use this source code to create binary code for your own purposes, free or commercial. // #ifndef ADRS_h #define ADRS_h #include <stdio.h> #include <string> using namespace std; enum envState { env_idle = 0, env_attack, env_decay, env_sustain, env_release }; class ADSR { public: ADSR(void); ~ADSR(void); float process(void); float process(int sampleCount); float getOutput(void); int getState(void); void gate(int on); void setAttackRate(float rate); void setDecayRate(float rate); void setReleaseRate(float rate); void setSustainLevel(float level); void setTargetRatioA(float targetRatio); void setTargetRatioDR(float targetRatio); void reset(void); protected: int state; float output; float attackRate; float decayRate; float releaseRate; float attackCoef; float decayCoef; float releaseCoef; float sustainLevel; float targetRatioA; float targetRatioDR; float attackBase; float decayBase; float releaseBase; string name; float calcCoef(float rate, float targetRatio); }; inline float ADSR::process() { switch (state) { case env_idle: break; case env_attack: output = attackBase + output * attackCoef; if (output >= 1.0) { output = 1.0; state = env_decay; } break; case env_decay: output = decayBase + output * decayCoef; if (output <= sustainLevel) { output = sustainLevel; state = env_sustain; } break; case env_sustain: break; case env_release: output = releaseBase + output * releaseCoef; if (output <= 0.0) { output = 0.0; state = env_idle; } break; } return output; } inline float ADSR::process(int sampleCount) { float retVal = 0; if(state != env_idle) { for(int i=0; i<sampleCount; i++) retVal = process(); } return retVal; } inline void ADSR::gate(int gate) { if (gate) state = env_attack; else if (state != env_idle) state = env_release; } inline int ADSR::getState() { return state; } inline void ADSR::reset() { state = env_idle; output = 0.0; } inline float ADSR::getOutput() { return output; } #endif