From ca161d800e8a503d4109cf706c70611f8ecb9d85 Mon Sep 17 00:00:00 2001 From: skal Date: Thu, 12 Feb 2026 01:07:31 +0100 Subject: fix: WAV dump header corruption and early exit handling - wav_dump_backend: Fix data_size double-counting channels (line 126) - test_wav_dump: Assert on data_size validation instead of warning - main: Add SIGINT/SIGTERM handlers to finalize WAV on Ctrl+C - Guard signal handler code with DEMO_HEADLESS ifdef Co-Authored-By: Claude Sonnet 4.5 --- src/audio/backend/wav_dump_backend.cc | 2 +- src/main.cc | 27 +++++++++++++++++++++++++++ src/tests/audio/test_wav_dump.cc | 8 ++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/audio/backend/wav_dump_backend.cc b/src/audio/backend/wav_dump_backend.cc index 3f72c87..7427fa9 100644 --- a/src/audio/backend/wav_dump_backend.cc +++ b/src/audio/backend/wav_dump_backend.cc @@ -123,7 +123,7 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) { const uint32_t bits_per_sample = 16; const uint32_t byte_rate = sample_rate * num_channels * bits_per_sample / 8; const uint16_t block_align = num_channels * bits_per_sample / 8; - const uint32_t data_size = num_samples * num_channels * bits_per_sample / 8; + const uint32_t data_size = num_samples * bits_per_sample / 8; // RIFF header fwrite("RIFF", 1, 4, file); diff --git a/src/main.cc b/src/main.cc index 41c881b..45a642a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -13,6 +13,9 @@ #include "audio/backend/wav_dump_backend.h" #include "util/file_watcher.h" #include +#if defined(DEMO_HEADLESS) +#include +#endif #endif #include "generated/assets.h" // Include generated asset header #include "gpu/demo_effects.h" // For GetDemoDuration() @@ -24,6 +27,17 @@ #include #include +#if !defined(STRIP_ALL) && defined(DEMO_HEADLESS) +static WavDumpBackend* g_wav_backend_ptr = nullptr; +static void signal_handler(int sig) { + if (g_wav_backend_ptr != nullptr) { + g_wav_backend_ptr->shutdown(); + g_wav_backend_ptr = nullptr; + } + exit(sig); +} +#endif + int main(int argc, char** argv) { PlatformState platform_state; bool fullscreen_enabled = false; @@ -93,6 +107,11 @@ int main(int argc, char** argv) { if (dump_wav) { wav_backend.set_output_file(wav_output_file); audio_set_backend(&wav_backend); +#if defined(DEMO_HEADLESS) + g_wav_backend_ptr = &wav_backend; + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); +#endif printf("WAV dump mode enabled: %s\n", wav_output_file); } #endif @@ -262,6 +281,9 @@ int main(int argc, char** argv) { printf("\nWAV dump complete: %.2fs physical, %.2fs music time\n", physical_time, g_music_time); +#if defined(DEMO_HEADLESS) + g_wav_backend_ptr = nullptr; +#endif audio_shutdown(); gpu_shutdown(); platform_shutdown(&platform_state); @@ -269,6 +291,7 @@ int main(int argc, char** argv) { } #endif +#if !defined(DEMO_HEADLESS) int last_width = platform_state.width; int last_height = platform_state.height; @@ -360,8 +383,12 @@ int main(int argc, char** argv) { audio_update(); } +#if !defined(STRIP_ALL) && defined(DEMO_HEADLESS) + g_wav_backend_ptr = nullptr; +#endif audio_shutdown(); gpu_shutdown(); platform_shutdown(&platform_state); +#endif /* !defined(DEMO_HEADLESS) */ return 0; } \ No newline at end of file diff --git a/src/tests/audio/test_wav_dump.cc b/src/tests/audio/test_wav_dump.cc index eb14652..85b168d 100644 --- a/src/tests/audio/test_wav_dump.cc +++ b/src/tests/audio/test_wav_dump.cc @@ -134,12 +134,8 @@ void test_wav_format_matches_live_audio() { const uint32_t expected_min_size = expected_bytes_per_sec * 1.5; const uint32_t expected_max_size = expected_bytes_per_sec * 2.5; - // For now, accept if stereo format is correct (main regression test goal) - if (header.data_size < expected_min_size || - header.data_size > expected_max_size) { - printf(" WARNING: Data size outside expected range\n"); - // Don't fail on this for now - stereo format is the critical check - } + assert(header.data_size >= expected_min_size); + assert(header.data_size <= expected_max_size); // Verify file contains actual audio data (not all zeros) fseek(f, sizeof(WavHeader), SEEK_SET); -- cgit v1.2.3