From a5fd304c9db6d6191a1ae2368bbe013f54c78aff Mon Sep 17 00:00:00 2001 From: skal Date: Wed, 28 Jan 2026 11:24:42 +0100 Subject: perf(synth): Optimize Hamming window generation Moves the Hamming window initialization from synth_render to synth_init() to ensure it's generated only once, improving performance. --- src/audio/synth.cc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/audio') diff --git a/src/audio/synth.cc b/src/audio/synth.cc index e4f6c75..b9af771 100644 --- a/src/audio/synth.cc +++ b/src/audio/synth.cc @@ -7,6 +7,7 @@ #include "audio/window.h" #include #include +#include // For printf #include // For memset struct Voice { @@ -33,11 +34,14 @@ static struct { static Voice g_voices[MAX_VOICES]; static volatile float g_current_output_peak = 0.0f; // Global peak for visualization +static float g_hamming_window[WINDOW_SIZE]; // Static window for optimization void synth_init() { memset(&g_synth_data, 0, sizeof(g_synth_data)); memset(g_voices, 0, sizeof(g_voices)); g_current_output_peak = 0.0f; + // Initialize the Hamming window once + hamming_window_512(g_hamming_window); } void synth_shutdown() { @@ -130,8 +134,9 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) { } void synth_render(float *output_buffer, int num_frames) { - float window[WINDOW_SIZE]; - hamming_window_512(window); + // Use the pre-calculated window + // float window[WINDOW_SIZE]; + // hamming_window_512(window); // Faster decay for more responsive visuals g_current_output_peak *= 0.90f; @@ -160,7 +165,7 @@ void synth_render(float *output_buffer, int num_frames) { float windowed_frame[DCT_SIZE]; for (int j = 0; j < DCT_SIZE; ++j) { - windowed_frame[j] = spectral_frame[j] * window[j]; + windowed_frame[j] = spectral_frame[j] * g_hamming_window[j]; // Use static window } idct_512(windowed_frame, v.time_domain_buffer); @@ -196,4 +201,6 @@ int synth_get_active_voice_count() { return count; } -float synth_get_output_peak() { return g_current_output_peak; } \ No newline at end of file +float synth_get_output_peak() { + return g_current_output_peak; +} \ No newline at end of file -- cgit v1.2.3