Skip to content

Commit 5676c82

Browse files
authored
Merge pull request #344 from andysheen/feature-delay
Updated Delay AudioEffect
2 parents 89bdfe9 + aab1ea3 commit 5676c82

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/AudioEffects/AudioEffect.h

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include <stdint.h>
2+
#include <stdint.h>
33
#include "AudioTools/AudioLogger.h"
44
#include "AudioEffects/AudioParameters.h"
55

@@ -18,7 +18,7 @@ class AudioEffect {
1818
public:
1919
AudioEffect() = default;
2020
virtual ~AudioEffect() = default;
21-
21+
2222
/// calculates the effect output from the input
2323
virtual effect_t process(effect_t in) = 0;
2424

@@ -68,12 +68,12 @@ class AudioEffect {
6868
* @brief Boost AudioEffect
6969
* @author Phil Schatzmann
7070
* @copyright GPLv3
71-
*
71+
*
7272
*/
7373

7474
class Boost : public AudioEffect {
7575
public:
76-
/// Boost Constructor: volume 0.1 - 1.0: decrease result; volume >0: increase result
76+
/// Boost Constructor: volume 0.1 - 1.0: decrease result; volume >0: increase result
7777
Boost(float volume=1.0){
7878
effect_value = volume;
7979
}
@@ -91,7 +91,7 @@ class Boost : public AudioEffect {
9191
effect_t process(effect_t input){
9292
if (!active()) return input;
9393
int32_t result = effect_value * input;
94-
// clip to int16_t
94+
// clip to int16_t
9595
return clip(result);
9696
}
9797

@@ -105,7 +105,7 @@ class Boost : public AudioEffect {
105105

106106
/**
107107
* @brief Distortion AudioEffect
108-
*
108+
*
109109
* @author Phil Schatzmann
110110
* @copyright GPLv3
111111
*/
@@ -115,7 +115,7 @@ class Distortion : public AudioEffect {
115115
/// Distortion Constructor: e.g. use clipThreashold 4990 and maxInput=6500
116116
Distortion(int16_t clipThreashold=4990, int16_t maxInput=6500){
117117
p_clip_threashold = clipThreashold;
118-
max_input = maxInput;
118+
max_input = maxInput;
119119
}
120120

121121
Distortion(const Distortion &copy) = default;
@@ -134,12 +134,12 @@ class Distortion : public AudioEffect {
134134

135135
int16_t maxInput(){
136136
return max_input;
137-
}
137+
}
138138

139139
effect_t process(effect_t input){
140140
if (!active()) return input;
141141
//the input signal is 16bits (values from -32768 to +32768
142-
//the value of input is clipped to the distortion_threshold value
142+
//the value of input is clipped to the distortion_threshold value
143143
return clip(input,p_clip_threashold, max_input);
144144
}
145145

@@ -155,7 +155,7 @@ class Distortion : public AudioEffect {
155155

156156
/**
157157
* @brief Fuzz AudioEffect
158-
*
158+
*
159159
* @author Phil Schatzmann
160160
* @copyright GPLv3
161161
*/
@@ -247,7 +247,7 @@ class Tremolo : public AudioEffect {
247247
float signal_depth = (100.0 - p_percent) / 100.0;
248248

249249
float tremolo_factor = tremolo_depth / rate_count_half;
250-
int32_t out = (signal_depth * input) + (tremolo_factor * count * input);
250+
int32_t out = (signal_depth * input) + (tremolo_factor * count * input);
251251

252252
//saw tooth shaped counter
253253
count+=inc;
@@ -282,8 +282,9 @@ class Tremolo : public AudioEffect {
282282
class Delay : public AudioEffect {
283283
public:
284284
/// e.g. depthPercent=50, ms=1000, sampleRate=44100
285-
Delay(uint16_t duration_ms=1000, uint8_t depthPercent=50, uint32_t sampleRate=44100) {
285+
Delay(uint16_t duration_ms=1000, float depthPercent=50, float feedbackAmount=0.3, uint32_t sampleRate=44100) {
286286
this->sampleRate = sampleRate;
287+
p_feedback = feedbackAmount;
287288
p_percent = depthPercent;
288289
p_ms = duration_ms;
289290
}
@@ -298,24 +299,34 @@ class Delay : public AudioEffect {
298299
return p_ms;
299300
}
300301

301-
void setDepth(uint8_t percent){
302+
void setDepth(float percent){
302303
p_percent = percent;
303304
}
304305

305306
uint8_t depth() {
306307
return p_percent;
307308
}
308309

310+
void setFeedback(float feedback){
311+
p_feedback = feedback;
312+
}
313+
314+
uint8_t feedback() {
315+
return p_feedback;
316+
}
317+
309318
effect_t process(effect_t input) {
310319
if (!active()) return input;
311320

312321
updateBufferSize();
313322
// get value from buffer
314323
int32_t value = (p_history->available()<sampleCount) ? input : p_history->read();
315-
// add actual input value
316-
p_history->write(input);
324+
// add feedback decay
325+
int32_t delayValue = (value*p_feedback);
326+
// add input and delay to the buffer
327+
p_history->write(input+delayValue);
317328
// mix input with result
318-
return (value * p_percent / 100) + (input * (100-p_percent)/100);
329+
return (delayValue * p_percent) + (input * (1.0 - p_percent));
319330
}
320331

321332
Delay *clone() {
@@ -324,7 +335,8 @@ class Delay : public AudioEffect {
324335

325336
protected:
326337
RingBuffer<effect_t>* p_history=nullptr;
327-
uint8_t p_percent;
338+
float p_percent;
339+
float p_feedback;
328340
uint16_t p_ms;
329341
uint16_t sampleCount=0;
330342
uint32_t sampleRate;
@@ -391,7 +403,7 @@ class ADSRGain : public AudioEffect {
391403
float sustainLevel(){
392404
return adsr->sustainLevel();
393405
}
394-
406+
395407
void setReleaseRate(float r){
396408
adsr->setReleaseRate(r);
397409
}

0 commit comments

Comments
 (0)