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