Skip to content

Commit 9e88848

Browse files
committed
Use values instead of references in AudioEffect subclasses
1 parent 6e02ce5 commit 9e88848

File tree

1 file changed

+101
-26
lines changed

1 file changed

+101
-26
lines changed

src/AudioEffects/AudioEffect.h

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,27 @@ class AudioEffect {
4848
class Boost : public AudioEffect {
4949
public:
5050
/// Boost Constructor: volume 0.1 - 1.0: decrease result; volume >0: increase result
51-
Boost(float &volume){
52-
p_effect_value = &volume;
51+
Boost(float volume=1.0){
52+
effect_value = volume;
53+
}
54+
55+
float volume() {
56+
return effect_value;
57+
}
58+
59+
void setVolume(float volume){
60+
effect_value = volume;
5361
}
5462

5563
effect_t process(effect_t input){
5664
if (!active()) return input;
57-
int32_t result = (*p_effect_value) * input;
65+
int32_t result = effect_value * input;
5866
// clip to int16_t
5967
return clip(result);
6068
}
6169

6270
protected:
63-
float *p_effect_value;
71+
float effect_value;
6472
};
6573

6674
/**
@@ -73,20 +81,36 @@ class Boost : public AudioEffect {
7381
class Distortion : public AudioEffect {
7482
public:
7583
/// Distortion Constructor: e.g. use clipThreashold 4990 and maxInput=6500
76-
Distortion(int16_t &clipThreashold, int16_t maxInput=6500){
77-
p_clip_threashold = &clipThreashold;
84+
Distortion(int16_t clipThreashold=4990, int16_t maxInput=6500){
85+
p_clip_threashold = clipThreashold;
7886
max_input = maxInput;
7987
}
8088

89+
void setClipThreashold(int16_t th){
90+
p_clip_threashold = th;
91+
}
92+
93+
int16_t clipThreashold(){
94+
return p_clip_threashold;
95+
}
96+
97+
void setMaxInput(int16_t maxInput){
98+
max_input = maxInput;
99+
}
100+
101+
int16_t maxInput(){
102+
return max_input;
103+
}
104+
81105
effect_t process(effect_t input){
82106
if (!active()) return input;
83107
//the input signal is 16bits (values from -32768 to +32768
84108
//the value of input is clipped to the distortion_threshold value
85-
return clip(input,*p_clip_threashold, max_input);
109+
return clip(input,p_clip_threashold, max_input);
86110
}
87111

88112
protected:
89-
int16_t *p_clip_threashold;
113+
int16_t p_clip_threashold;
90114
int16_t max_input;
91115

92116
};
@@ -101,20 +125,36 @@ class Distortion : public AudioEffect {
101125
class Fuzz : public AudioEffect {
102126
public:
103127
/// Fuzz Constructor: use e.g. effectValue=6.5; maxOut = 300
104-
Fuzz(float &fuzzEffectValue, uint16_t maxOut = 300){
105-
p_effect_value = &fuzzEffectValue;
128+
Fuzz(float fuzzEffectValue, uint16_t maxOut = 300){
129+
p_effect_value = fuzzEffectValue;
106130
max_out = maxOut;
107131
}
108132

133+
void setFuzzEffectValue(float v){
134+
p_effect_value = v;
135+
}
136+
137+
float fuzzEffectValue(){
138+
return p_effect_value;
139+
}
140+
141+
void setMaxOut(uint16_t v){
142+
max_out = v;
143+
}
144+
145+
uint16_t maxOut(){
146+
return max_out;
147+
}
148+
109149
effect_t process(effect_t input){
110150
if (!active()) return input;
111-
float v = *p_effect_value;
151+
float v = p_effect_value;
112152
int32_t result = clip(v * input) ;
113153
return map(result * v, -32768, +32767,-max_out, max_out);
114154
}
115155

116156
protected:
117-
float *p_effect_value;
157+
float p_effect_value;
118158
uint16_t max_out;
119159

120160
};
@@ -128,18 +168,37 @@ class Fuzz : public AudioEffect {
128168
class Tremolo : public AudioEffect {
129169
public:
130170
/// Tremolo constructor - use e.g. duration_ms=2000; depthPercent=50; sampleRate=44100
131-
Tremolo(int16_t &duration_ms, uint8_t& depthPercent, uint32_t sampleRate=44100) {
171+
Tremolo(int16_t duration_ms=2000, uint8_t depthPercent=50, uint32_t sampleRate=44100) {
172+
this->duration_ms = duration_ms;
173+
this->sampleRate = sampleRate;
174+
this->p_percent = depthPercent;
132175
int32_t rate_count = sampleRate * duration_ms / 1000;
133176
rate_count_half = rate_count / 2;
134-
p_percent = &depthPercent;
177+
}
178+
179+
void setDuration(int16_t ms){
180+
int32_t rate_count = sampleRate * ms / 1000;
181+
rate_count_half = rate_count / 2;
182+
}
183+
184+
int16_t duration(){
185+
return duration_ms;
186+
}
187+
188+
void setDepth(uint8_t percent){
189+
p_percent = percent;
190+
}
191+
192+
uint8_t depth() {
193+
return p_percent;
135194
}
136195

137196
effect_t process(effect_t input) {
138197
if (!active()) return input;
139198

140199
// limit value to max 100% and calculate factors
141-
float tremolo_depth = (*p_percent) > 100 ? 1.0 : 0.01 * (*p_percent);
142-
float signal_depth = (100.0 - (*p_percent)) / 100.0;
200+
float tremolo_depth = p_percent > 100 ? 1.0 : 0.01 * p_percent;
201+
float signal_depth = (100.0 - p_percent) / 100.0;
143202

144203
float tremolo_factor = tremolo_depth / rate_count_half;
145204
int32_t out = (signal_depth * input) + (tremolo_factor * count * input);
@@ -155,12 +214,13 @@ class Tremolo : public AudioEffect {
155214
return clip(out);
156215
}
157216

158-
159217
protected:
218+
int16_t duration_ms;
219+
uint32_t sampleRate;
160220
int32_t count = 0;
161221
int16_t inc = 1;
162222
int32_t rate_count_half; // number of samples for on raise and fall
163-
uint8_t* p_percent;
223+
uint8_t p_percent;
164224

165225
};
166226

@@ -172,10 +232,26 @@ class Tremolo : public AudioEffect {
172232
class Delay : public AudioEffect {
173233
public:
174234
/// e.g. depthPercent=50, ms=1000, sampleRate=44100
175-
Delay(uint16_t &duration_ms, uint8_t& depthPercent, uint32_t sampleRate=44100) {
235+
Delay(uint16_t duration_ms=1000, uint8_t depthPercent=50, uint32_t sampleRate=44100) {
176236
this->sampleRate = sampleRate;
177-
p_percent = &depthPercent;
178-
p_ms = &duration_ms;
237+
p_percent = depthPercent;
238+
p_ms = duration_ms;
239+
}
240+
241+
void setDuration(int16_t ms){
242+
p_ms = ms;
243+
}
244+
245+
int16_t duration(){
246+
return p_ms;
247+
}
248+
249+
void setDepth(uint8_t percent){
250+
p_percent = percent;
251+
}
252+
253+
uint8_t depth() {
254+
return p_percent;
179255
}
180256

181257
effect_t process(effect_t input) {
@@ -187,25 +263,24 @@ class Delay : public AudioEffect {
187263
// add actual input value
188264
p_history->write(input);
189265
// mix input with result
190-
return (value * (*p_percent) / 100) + (input * (100-(*p_percent))/100);
266+
return (value * p_percent / 100) + (input * (100-p_percent)/100);
191267
}
192268

193269
protected:
194270
RingBuffer<effect_t>* p_history=nullptr;
195-
uint8_t* p_percent;
196-
uint16_t *p_ms;
271+
uint8_t p_percent;
272+
uint16_t p_ms;
197273
uint16_t sampleCount=0;
198274
uint32_t sampleRate;
199275

200276
void updateBufferSize(){
201-
uint16_t newSampleCount = sampleRate * (*p_ms) / 1000;
277+
uint16_t newSampleCount = sampleRate * p_ms / 1000;
202278
if (newSampleCount>sampleCount){
203279
if (p_history!=nullptr) delete p_history;
204280
sampleCount = newSampleCount;
205281
p_history = new RingBuffer<effect_t>(sampleCount);
206282
}
207283
}
208-
209284
};
210285

211286
}

0 commit comments

Comments
 (0)