annotate examples/10-Instruments/d-box/ADSR.h @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children
rev   line source
robert@464 1 //
robert@464 2 // ADRS.h
robert@464 3 //
robert@464 4 // Created by Nigel Redmon on 12/18/12.
robert@464 5 // EarLevel Engineering: earlevel.com
robert@464 6 // Copyright 2012 Nigel Redmon
robert@464 7 //
robert@464 8 // For a complete explanation of the ADSR envelope generator and code,
robert@464 9 // read the series of articles by the author, starting here:
robert@464 10 // http://www.earlevel.com/main/2013/06/01/envelope-generators/
robert@464 11 //
robert@464 12 // License:
robert@464 13 //
robert@464 14 // This source code is provided as is, without warranty.
robert@464 15 // You may copy and distribute verbatim copies of this document.
robert@464 16 // You may modify and use this source code to create binary code for your own purposes, free or commercial.
robert@464 17 //
robert@464 18
robert@464 19 #ifndef ADRS_h
robert@464 20 #define ADRS_h
robert@464 21
robert@464 22 #include <stdio.h>
robert@464 23 #include <string>
robert@464 24
robert@464 25 using namespace std;
robert@464 26
robert@464 27 enum envState {
robert@464 28 env_idle = 0,
robert@464 29 env_attack,
robert@464 30 env_decay,
robert@464 31 env_sustain,
robert@464 32 env_release
robert@464 33 };
robert@464 34
robert@464 35 class ADSR {
robert@464 36 public:
robert@464 37 ADSR(void);
robert@464 38 ~ADSR(void);
robert@464 39 float process(void);
robert@464 40 float process(int sampleCount);
robert@464 41 float getOutput(void);
robert@464 42 int getState(void);
robert@464 43 void gate(int on);
robert@464 44 void setAttackRate(float rate);
robert@464 45 void setDecayRate(float rate);
robert@464 46 void setReleaseRate(float rate);
robert@464 47 void setSustainLevel(float level);
robert@464 48 void setTargetRatioA(float targetRatio);
robert@464 49 void setTargetRatioDR(float targetRatio);
robert@464 50 void reset(void);
robert@464 51
robert@464 52 protected:
robert@464 53 int state;
robert@464 54 float output;
robert@464 55 float attackRate;
robert@464 56 float decayRate;
robert@464 57 float releaseRate;
robert@464 58 float attackCoef;
robert@464 59 float decayCoef;
robert@464 60 float releaseCoef;
robert@464 61 float sustainLevel;
robert@464 62 float targetRatioA;
robert@464 63 float targetRatioDR;
robert@464 64 float attackBase;
robert@464 65 float decayBase;
robert@464 66 float releaseBase;
robert@464 67 string name;
robert@464 68 float calcCoef(float rate, float targetRatio);
robert@464 69 };
robert@464 70
robert@464 71 inline float ADSR::process() {
robert@464 72 switch (state) {
robert@464 73 case env_idle:
robert@464 74 break;
robert@464 75 case env_attack:
robert@464 76 output = attackBase + output * attackCoef;
robert@464 77 if (output >= 1.0) {
robert@464 78 output = 1.0;
robert@464 79 state = env_decay;
robert@464 80 }
robert@464 81 break;
robert@464 82 case env_decay:
robert@464 83 output = decayBase + output * decayCoef;
robert@464 84 if (output <= sustainLevel) {
robert@464 85 output = sustainLevel;
robert@464 86 state = env_sustain;
robert@464 87 }
robert@464 88 break;
robert@464 89 case env_sustain:
robert@464 90 break;
robert@464 91 case env_release:
robert@464 92 output = releaseBase + output * releaseCoef;
robert@464 93 if (output <= 0.0) {
robert@464 94 output = 0.0;
robert@464 95 state = env_idle;
robert@464 96 }
robert@464 97 break;
robert@464 98 }
robert@464 99 return output;
robert@464 100 }
robert@464 101
robert@464 102 inline float ADSR::process(int sampleCount)
robert@464 103 {
robert@464 104 float retVal = 0;
robert@464 105
robert@464 106 if(state != env_idle)
robert@464 107 {
robert@464 108 for(int i=0; i<sampleCount; i++)
robert@464 109 retVal = process();
robert@464 110 }
robert@464 111
robert@464 112 return retVal;
robert@464 113 }
robert@464 114
robert@464 115 inline void ADSR::gate(int gate) {
robert@464 116
robert@464 117 if (gate)
robert@464 118 state = env_attack;
robert@464 119 else if (state != env_idle)
robert@464 120 state = env_release;
robert@464 121 }
robert@464 122
robert@464 123 inline int ADSR::getState() {
robert@464 124 return state;
robert@464 125 }
robert@464 126
robert@464 127 inline void ADSR::reset() {
robert@464 128 state = env_idle;
robert@464 129 output = 0.0;
robert@464 130 }
robert@464 131
robert@464 132 inline float ADSR::getOutput() {
robert@464 133 return output;
robert@464 134 }
robert@464 135
robert@464 136
robert@464 137 #endif