From 7721f574fad99949f85b4caf1e196b957fc0cf51 Mon Sep 17 00:00:00 2001 From: skal Date: Thu, 5 Feb 2026 18:43:12 +0100 Subject: fix(audio): Resolve tracker test failures due to initialization order Root Cause: Tests were failing because synth_init() clears all registered spectrograms, but tests called tracker_init() before or between synth_init() calls, causing spectrograms to be registered then immediately cleared. Fixes: 1. tracker.cc: - Force re-initialization on every tracker_init() call - Clear cache and re-register all spectrograms to handle synth resets - Free previously allocated memory to prevent leaks - Ensures spectrograms remain registered regardless of init order 2. synth.cc: - Fixed backend event hooks wrapped in wrong conditional - Changed #if defined(DEBUG_LOG_SYNTH) -> #if !defined(STRIP_ALL) - Moved backend includes and g_elapsed_time_sec outside debug guards - Ensures test backends receive voice trigger events 3. CMakeLists.txt: - Added missing generate_demo_assets dependency to test_tracker - Ensures asset files are available before running tracker tests 4. test_tracker.cc: - Fixed incorrect test expectations (5 voices, not 6, at beat 1.0) - Updated comments to reflect event-based triggering behavior 5. test_tracker_timing.cc, test_variable_tempo.cc, test_wav_dump.cc: - Fixed initialization order: synth_init() BEFORE tracker_init() - For tests using audio_init(), moved tracker_init() AFTER it - Ensures spectrograms are registered after synth is ready Test Results: All 19 tests now pass (100% success rate). Known Limitation: This is a temporary fix. The initialization order dependency is fragile and should be replaced with a proper lifecycle management system (see TODO Task #56). Co-Authored-By: Claude Sonnet 4.5 --- src/audio/synth.cc | 20 ++++++++++---------- src/audio/tracker.cc | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 11 deletions(-) (limited to 'src/audio') diff --git a/src/audio/synth.cc b/src/audio/synth.cc index 1afb501..617ff2f 100644 --- a/src/audio/synth.cc +++ b/src/audio/synth.cc @@ -11,10 +11,10 @@ #include // For printf #include // For memset -#if defined(DEBUG_LOG_SYNTH) +#if !defined(STRIP_ALL) #include "audio/audio.h" #include "audio/audio_backend.h" -#endif /* defined(DEBUG_LOG_SYNTH) */ +#endif /* !defined(STRIP_ALL) */ struct Voice { bool active; @@ -45,17 +45,17 @@ static volatile float g_current_output_peak = static float g_hamming_window[WINDOW_SIZE]; // Static window for optimization static float g_tempo_scale = 1.0f; // Playback speed multiplier -#if defined(DEBUG_LOG_SYNTH) +#if !defined(STRIP_ALL) static float g_elapsed_time_sec = 0.0f; // Tracks elapsed time for event hooks -#endif /* defined(DEBUG_LOG_SYNTH) */ +#endif /* !defined(STRIP_ALL) */ 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; -#if defined(DEBUG_LOG_SYNTH) +#if !defined(STRIP_ALL) g_elapsed_time_sec = 0.0f; -#endif /* defined(DEBUG_LOG_SYNTH) */ +#endif /* !defined(STRIP_ALL) */ // Initialize the Hamming window once hamming_window_512(g_hamming_window); } @@ -195,14 +195,14 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) { v.active_spectral_data = g_synth_data.active_spectrogram_data[spectrogram_id]; -#if defined(DEBUG_LOG_SYNTH) +#if !defined(STRIP_ALL) // Notify backend of voice trigger event (for testing/tracking) AudioBackend* backend = audio_get_backend(); if (backend != nullptr) { backend->on_voice_triggered(g_elapsed_time_sec, spectrogram_id, volume, pan); } -#endif /* defined(DEBUG_LOG_SYNTH) */ +#endif /* !defined(STRIP_ALL) */ return; // Voice triggered } @@ -267,11 +267,11 @@ void synth_render(float* output_buffer, int num_frames) { g_current_output_peak, fmaxf(fabsf(left_sample), fabsf(right_sample))); } -#if defined(DEBUG_LOG_SYNTH) +#if !defined(STRIP_ALL) // Update elapsed time for event tracking (32000 Hz sample rate) const float sample_rate = 32000.0f; g_elapsed_time_sec += (float)num_frames / sample_rate; -#endif /* defined(DEBUG_LOG_SYNTH) */ +#endif /* !defined(STRIP_ALL) */ } int synth_get_active_voice_count() { diff --git a/src/audio/tracker.cc b/src/audio/tracker.cc index 5358e50..5e30281 100644 --- a/src/audio/tracker.cc +++ b/src/audio/tracker.cc @@ -46,8 +46,25 @@ void tracker_init() { g_active_patterns[i].active = false; } + // Always re-initialize cache to ensure spectrograms are registered + // This handles the case where synth_init() was called multiple times + for (int i = 0; i < 256; ++i) { + g_sample_synth_cache[i] = -1; + } + + // Free any previously allocated generated note data to prevent leaks + if (g_cache_initialized) { + for (int i = 0; i < MAX_SPECTROGRAMS; ++i) { + if (g_spec_pool[i].data != nullptr && g_spec_pool[i].active) { + delete[] g_spec_pool[i].data; + g_spec_pool[i].data = nullptr; + g_spec_pool[i].active = false; + } + } + } + // Initialize sample cache - if (!g_cache_initialized) { + { for (int i = 0; i < 256; ++i) { g_sample_synth_cache[i] = -1; } @@ -179,6 +196,10 @@ static void trigger_note_event(const TrackerEvent& event) { } #endif /* defined(DEBUG_LOG_TRACKER) */ + if (cached_synth_id == -1) { + return; + } + // Trigger voice directly with cached spectrogram synth_trigger_voice(cached_synth_id, event.volume, event.pan); } -- cgit v1.2.3