I am making a DIY guitar pedal, i made a circuit with the pcm3060 and i am using the stm32h7 as the mcu
i can get clean sound and it works just great by assigning the input to the output. but i noticed that when i try to add effects to it the signal just gets distorted. a lot.
any ideas on how to prevent this
#include "tremolo.h"
#include <math.h>
#define TWO_PI (2.0f * 3.1415926535f)
#define MIN_RATE 0.1f
#define MAX_RATE 10.0f
#define MIN_DEPTH 0.0f
#define MAX_DEPTH 0.95f // Leave some headroom
void Tremolo_Init(Tremolo* trem, uint32_t sample_rate)
{
trem->rate_hz = 2.0f; // Default 2Hz
trem->depth = 0.5f; // Default 50% depth
trem->phase = 0.0f;
trem->sample_rate = sample_rate;
trem->phase_inc = TWO_PI * trem->rate_hz / (float)sample_rate;
trem->smoothed_lfo = 1.0f; // Start at unity gain
}
void Tremolo_SetRate(Tremolo* trem, float rate_hz)
{
// Constrain rate and convert to phase increment
trem->rate_hz = (rate_hz < MIN_RATE) ? MIN_RATE :
((rate_hz > MAX_RATE) ? MAX_RATE : rate_hz);
trem->phase_inc = TWO_PI * trem->rate_hz / (float)trem->sample_rate;
}
void Tremolo_SetDepth(Tremolo* trem, float depth)
{
// Constrain depth to safe range
trem->depth = (depth < MIN_DEPTH) ? MIN_DEPTH :
((depth > MAX_DEPTH) ? MAX_DEPTH : depth);
}
float Tremolo_Process(Tremolo* trem, float input)
{
// Generate triangle wave LFO (smoother than sine for tremolo)
float lfo_pos = fmodf(trem->phase / TWO_PI, 1.0f);
float raw_lfo = 1.0f - trem->depth * (1.0f - fabsf(2.0f * lfo_pos - 1.0f));
// Smooth LFO transitions to prevent clicks
trem->smoothed_lfo = 0.25f * raw_lfo + 0.75f * trem->smoothed_lfo;
// Update phase
trem->phase += trem->phase_inc;
if (trem->phase > TWO_PI * 4.0f) { // Keep phase bounded
trem->phase -= TWO_PI * 4.0f;
}
// Apply modulation with 10% minimum gain to avoid silence
return input * (0.1f + 0.8f * trem->smoothed_lfo);
}
this is what the tremolo code would look like and i would apply it like this
void GuitarPedal_ProcessAudio() {
if(tremolo_enabled) {
float in_sample = (float)input[i].left / 8388608.0f; // 24-bit to float
float out_sample = Tremolo_Process(&tremolo, in_sample);
output[i].left = (int32_t)(out_sample * 8388608.0f);
}
}
but i always get distorted sound like very distorted, not pleasant to hear