diff options
| author | skal <pascal.massimino@gmail.com> | 2026-01-28 01:58:43 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-01-28 01:58:43 +0100 |
| commit | 164c471fb9488f7013be0bcaef7f790020aee916 (patch) | |
| tree | 16505f92f1e0c243c1934dd68041907a96411208 | |
| parent | d21a074e20ae82b3ddd21e54391ea3556f5cd66f (diff) | |
fix(synth): Implement peak meter with decay for smooth visuals
The graphics appeared static because the audio peak value was transient, only lasting for a single audio buffer. The main graphics loop, running much faster, would mostly read a zero value.
This fix implements a simple peak meter with an exponential decay. The peak value now rises instantly to the maximum sample value but fades out smoothly over several frames, providing a persistent and smooth value for the visualizer.
| -rw-r--r-- | src/audio/synth.cc | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/audio/synth.cc b/src/audio/synth.cc index 98c12d9..77a0d05 100644 --- a/src/audio/synth.cc +++ b/src/audio/synth.cc @@ -26,7 +26,7 @@ static struct { } g_synth_data; static Voice g_voices[MAX_VOICES]; -static float g_current_output_peak = 0.0f; // Global peak for visualization +static volatile float g_current_output_peak = 0.0f; // Global peak for visualization void synth_init() { memset(&g_synth_data, 0, sizeof(g_synth_data)); @@ -125,7 +125,8 @@ void synth_render(float *output_buffer, int num_frames) { float window[WINDOW_SIZE]; hamming_window_512(window); - float current_peak_in_frame = 0.0f; + // Apply a decay to the peak value for smooth visuals + g_current_output_peak *= 0.95f; for (int i = 0; i < num_frames; ++i) { float left_sample = 0.0f; @@ -170,10 +171,10 @@ void synth_render(float *output_buffer, int num_frames) { output_buffer[i * 2] = left_sample; output_buffer[i * 2 + 1] = right_sample; - current_peak_in_frame = - fmaxf(current_peak_in_frame, fmaxf(fabsf(left_sample), fabsf(right_sample))); + // Update the peak with the new max (attack) + g_current_output_peak = + fmaxf(g_current_output_peak, fmaxf(fabsf(left_sample), fabsf(right_sample))); } - g_current_output_peak = current_peak_in_frame; } int synth_get_active_voice_count() { |
