arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioParameters.h
1#pragma once
2#include "AudioTools/CoreAudio/AudioLogger.h"
3
4namespace audio_tools {
5
10 public:
11 virtual ~AbstractParameter() = default;
12
13 virtual float value() {
14 return act_value;
15 };
16
17 // triggers an update of the value
18 virtual float tick() {
19 act_value = update();
20 return act_value;
21 }
22
23 // to manage keyboard related parameters
24 virtual void keyOn(float tgt=0){}
25
26 // to manage keyboard related parameters
27 virtual void keyOff(){}
28
29 protected:
30 float act_value = 0;
31 friend class ScaledParameter;
32
33 virtual float update() = 0;
34};
35
41 public:
42 Parameter(float value){
43 act_value = value;
44 }
45 virtual float update(){ return act_value;}
46};
47
51class ADSR : public AbstractParameter {
52 public:
53
54 ADSR(float attack=0.001, float decay=0.001, float sustainLevel=0.5, float release= 0.005){
55 this->attack = attack;
56 this->decay = decay;
57 this->sustain = sustainLevel;
58 this->release = release;
59 }
60
61 ADSR(ADSR &copy) = default;
62
63 void setAttackRate(float a){
64 attack = a;
65 }
66
67 float attackRate(){
68 return attack;
69 }
70
71 void setDecayRate(float d){
72 decay = d;
73 }
74
75 float decayRate() {
76 return decay;
77 }
78
79 void setSustainLevel(float s){
80 sustain = s;
81 }
82
83 float sustainLevel(){
84 return sustain;
85 }
86
87 void setReleaseRate(float r){
88 release = r;
89 }
90
91 float releaseRate() {
92 return release;
93 }
94
95 void keyOn(float tgt=0){
96 LOGI("%s: %f", LOG_METHOD, tgt);
97 state = Attack;
98 this->target = tgt>0.0f && tgt<=1.0f ? tgt : sustain;
99 this->act_value = 0;
100 }
101
102 void keyOff(){
103 TRACEI();
104 if (state!=Idle){
105 state = Release;
106 target = 0;
107 }
108 }
109
110 bool isActive(){
111 return state!=Idle;
112 }
113
114 protected:
115 float attack, decay, sustain, release;
116 enum AdsrPhase {Idle, Attack, Decay, Sustain, Release};
117 const char* adsrNames[5] = {"Idle", "Attack", "Decay", "Sustain", "Release"};
118 AdsrPhase state = Idle;
119 float target = 0;
120 int zeroCount = 0;
121
122 inline float update( ) {
123
124 switch ( state ) {
125 case Attack:
126 act_value += attack;
127 if ( act_value >= target ) {
128 act_value = target;
129 target = sustain;
130 state = Decay;
131 }
132 break;
133
134 case Decay:
135 if ( act_value > sustain ) {
136 act_value -= decay;
137 if ( act_value <= sustain ) {
138 act_value = sustain;
139 state = Sustain;
140 }
141 }
142 else {
143 act_value += decay; // attack target < sustainLevel level
144 if ( act_value >= sustain ) {
145 act_value = sustain;
146 state = Sustain;
147 }
148 }
149 break;
150
151 case Release:
152 act_value -= release;
153 if ( act_value <= 0.0f ) {
154 act_value = 0.0;
155 state = Idle;
156 }
157 break;
158
159 default:
160 // nothing to be done
161 break;
162 }
163 return act_value;
164 }
165
166};
167
168
174 public:
175 ScaledParameter(AbstractParameter *parameter, float min, float max){
176 this->min = min;
177 this->max = max;
178 this->p_parameter = parameter;
179 }
180
181 float update() {
182 return p_parameter->update() + min * (max-min);
183 }
184
185 protected:
186 float min=0, max=0;
187 AbstractParameter *p_parameter;
188
189};
190
191
192}
Generates ADSR values between 0.0 and 1.0.
Definition AudioParameters.h:51
Base class for all parameters.
Definition AudioParameters.h:9
A constant value.
Definition AudioParameters.h:40
Scales a dynamic parameter to the indicated range.
Definition AudioParameters.h:173
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10