Skip to content

Commit 4e558cf

Browse files
committed
Delay Effect
1 parent 5612a63 commit 4e558cf

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/AudioEffects/AudioEffect.h

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class Fuzz : public AudioEffect {
128128
class Tremolo : public AudioEffect {
129129
public:
130130
/// Tremolo constructor - use e.g. duration_ms=2000; depth=0.5; sampleRate=44100
131-
Tremolo(int16_t &duration_ms, float &depth, uint16_t sampleRate) {
131+
Tremolo(int16_t &duration_ms, float &depth, uint16_t sampleRate=44100) {
132132
int32_t rate_count = static_cast<int32_t>(duration_ms) * sampleRate / 1000;
133133
rate_count_half = rate_count / 2;
134134

@@ -165,4 +165,48 @@ class Tremolo : public AudioEffect {
165165

166166
};
167167

168+
/**
169+
* @brief Delay AudioEffect. We mix the actual signal with the delayed signal
170+
* @author Phil Schatzmann
171+
* @copyright GPLv3
172+
*/
173+
class Delay : public AudioEffect {
174+
public:
175+
/// e.g. percent=50, ms=1000, sampleRate=44100
176+
Delay(uint8_t& percent, uint16_t &ms, uint32_t sampleRate=44100) {
177+
this->sampleRate = sampleRate;
178+
p_percent = &percent;
179+
p_ms = &ms;
180+
sampleCount = sampleRate * ms / 1000;
181+
p_history = new RingBuffer<effect_t>(sampleCount);
182+
}
183+
184+
effect_t process(effect_t input) {
185+
updateBufferSize();
186+
// get value from buffer
187+
int32_t value = (p_history->available()<sampleCount) ? input : p_history->read();
188+
// add actual input value
189+
p_history->write(input);
190+
// mix input with result
191+
return (value * (*p_percent) / 100) + (input * (100-(*p_percent))/100);
192+
}
193+
194+
protected:
195+
RingBuffer<effect_t>* p_history=nullptr;
196+
uint8_t* p_percent;
197+
uint16_t *p_ms;
198+
uint16_t sampleCount;
199+
uint32_t sampleRate;
200+
201+
void updateBufferSize(){
202+
uint16_t newSampleCount = sampleRate * (*p_ms) / 1000;
203+
if (newSampleCount>sampleCount){
204+
delete p_history;
205+
sampleCount = newSampleCount;
206+
p_history = new RingBuffer<effect_t>(sampleCount);
207+
}
208+
}
209+
210+
};
211+
168212
}

0 commit comments

Comments
 (0)