Files
CelloEffects/Appli/Core/Src/fx_echo.c
T
2026-10-18 14:06:40 +02:00

39 lines
1.1 KiB
C

#include "fx_echo.h"
#include <string.h>
void FxEcho_Init(FxEchoState *state, FxEchoParams *params)
{
memset(state->buffer, 0, sizeof(state->buffer));
state->write_idx = 0;
params->enabled = false;
params->delay_ms = 300.0f;
params->feedback = 0.35f;
params->mix = 0.3f;
}
float FxEcho_Process(FxEchoState *state, const FxEchoParams *params, float input)
{
if (!params->enabled)
{
return input;
}
uint32_t delay_samples = (uint32_t)(params->delay_ms * (FX_ECHO_SAMPLE_RATE / 1000.0f));
if (delay_samples >= FX_ECHO_BUFFER_SIZE)
{
delay_samples = FX_ECHO_BUFFER_SIZE - 1U;
}
uint32_t read_idx = (state->write_idx + FX_ECHO_BUFFER_SIZE - delay_samples) % FX_ECHO_BUFFER_SIZE;
float delayed = (float)state->buffer[read_idx] / 32768.0f;
float to_store = input + delayed * params->feedback;
if (to_store > 1.0f) to_store = 1.0f;
if (to_store < -1.0f) to_store = -1.0f;
state->buffer[state->write_idx] = (int16_t)(to_store * 32767.0f);
state->write_idx = (state->write_idx + 1U) % FX_ECHO_BUFFER_SIZE;
return input * (1.0f - params->mix) + delayed * params->mix;
}