diff --git a/src/effect_delay.cpp b/src/effect_delay.cpp index a5a7b7c..a79536d 100644 --- a/src/effect_delay.cpp +++ b/src/effect_delay.cpp @@ -5,10 +5,26 @@ LOGMODULE ("fx chorus"); AudioEffectDelay::AudioEffectDelay(float32_t samplerate) : AudioEffect(samplerate) { + bufferSize = (int) samplerate * MAX_DELAY_TIME; + bufferL = new float32_t[this->bufferSize]; + bufferR = new float32_t[this->bufferSize]; + index = 0; + + for (size_t i = 0; i < bufferSize; i++) + { + bufferL[i] = 0.0f; + bufferR[i] = 0.0f; + } + + timeL = 0.36f; + timeR = 0.36f; + feedback = 0.3f; } AudioEffectDelay::~AudioEffectDelay() { + delete bufferL; + delete bufferR; } unsigned AudioEffectDelay::getId() @@ -20,7 +36,31 @@ void AudioEffectDelay::doProcess(const float32_t* inblockL, const float32_t* inb { for (uint16_t i=0; i < len; i++) { - outblockL[i] = inblockL[i]; - outblockR[i] = inblockR[i]; + // Update buffers + bufferL[index] = inblockL[i]; + bufferR[index] = inblockR[i]; + + // Calculate offsets + int offsetL = index - (timeL * samplerate); + if (offsetL < 0) { + offsetL = bufferSize + offsetL; + } + int offsetR = index - (timeR * samplerate); + if (offsetR < 0) { + offsetR = bufferSize + offsetR; + } + + bufferL[index] += bufferL[offsetL] * feedback; + bufferR[index] += bufferR[offsetR] * feedback; + + outblockL[i] = bufferL[index]; + outblockR[i] = bufferR[index]; + + // Update index + index++; + if (index >= bufferSize) + { + index = 0; + } } } diff --git a/src/effect_delay.h b/src/effect_delay.h index 8d58aac..43a707b 100644 --- a/src/effect_delay.h +++ b/src/effect_delay.h @@ -3,6 +3,8 @@ #include "effect_base.h" +#define MAX_DELAY_TIME 1 + class AudioEffectDelay : public AudioEffect { public: @@ -12,15 +14,15 @@ public: virtual unsigned getId(); protected: virtual void doProcess(const float32_t* inblockL, const float32_t* inblockR, float32_t* outblockL, float32_t* outblockR, uint16_t len); -// private: - // const size_t MaxSampleDelayTime; - // unsigned write_pos_L_; - // unsigned write_pos_R_; - // float32_t* buffer_L_; - // float32_t* buffer_R_; - // float32_t delay_time_L_; // Left delay time in seconds (0.0 - 2.0) - // float32_t delay_time_R_; // Right delay time in seconds (0.0 - 2.0) - // float32_t feedback_; // Feedback (0.0 - 1.0) +private: + size_t bufferSize; + float32_t* bufferL; + float32_t* bufferR; + unsigned index; + + float32_t timeL; // Left delay time in seconds (0.0 - 2.0) + float32_t timeR; // Right delay time in seconds (0.0 - 2.0) + float32_t feedback; // Feedback (0.0 - 1.0) }; #endif // _EFFECT_DELAY_H \ No newline at end of file