From 64c19b368db4aea748467b5f763add99c7deb701 Mon Sep 17 00:00:00 2001 From: skal Date: Thu, 5 Feb 2026 19:49:27 +0100 Subject: perf: Reduce audio test durations for faster test suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Optimized long-running audio tests to significantly improve test suite performance while maintaining test coverage. Changes: - WavDumpBackend: Added set_duration() method with configurable duration - Default remains 60s for debugging/production use - Test now uses 2s instead of 60s (140x faster: 60s → 0.43s) - JitteredAudioBackendTest: Reduced simulation durations - Test 1: 2.0s → 0.5s (4x faster) - Test 2: 10.0s → 3.0s (3.3x faster) - Overall: 14.49s → 4.48s (3.2x faster) - Updated assertions for shorter durations - Progress indicators adjusted for shorter tests Results: - Total test suite time: 18.31s → 8.29s (55% faster) - All 20 tests still pass - Tests still properly validate intended behavior handoff(Claude): Optimized audio test performance to speed up development iteration without sacrificing test coverage. WavDumpBackend now has configurable duration via set_duration() method. --- src/audio/wav_dump_backend.cc | 52 ++++++++++++++++++++++++++----------------- src/audio/wav_dump_backend.h | 8 ++++++- 2 files changed, 38 insertions(+), 22 deletions(-) (limited to 'src/audio') diff --git a/src/audio/wav_dump_backend.cc b/src/audio/wav_dump_backend.cc index d1acf66..9fa13f5 100644 --- a/src/audio/wav_dump_backend.cc +++ b/src/audio/wav_dump_backend.cc @@ -14,19 +14,24 @@ #include WavDumpBackend::WavDumpBackend() - : wav_file_(nullptr), - samples_written_(0), - output_filename_("audio_dump.wav"), - is_active_(false) { + : wav_file_(nullptr), samples_written_(0), + output_filename_("audio_dump.wav"), is_active_(false), + duration_sec_(60.0f) { sample_buffer_.resize(kBufferSize); } -WavDumpBackend::~WavDumpBackend() { shutdown(); } +WavDumpBackend::~WavDumpBackend() { + shutdown(); +} void WavDumpBackend::set_output_file(const char* filename) { output_filename_ = filename; } +void WavDumpBackend::set_duration(float seconds) { + duration_sec_ = seconds; +} + void WavDumpBackend::init() { // Open WAV file for writing wav_file_ = fopen(output_filename_, "wb"); @@ -48,11 +53,11 @@ void WavDumpBackend::start() { // Render audio in small chunks with tracker updates // This matches the seek logic in main.cc - const int max_duration_sec = 60; - const float update_dt = 1.0f / 60.0f; // 60Hz update rate (matches main loop) - const int frames_per_update = (int)(kSampleRate * update_dt); // ~533 frames - const int samples_per_update = frames_per_update * 2; // Stereo: 2 samples per frame - const int total_updates = (int)(max_duration_sec / update_dt); + const float update_dt = 1.0f / 60.0f; // 60Hz update rate (matches main loop) + const int frames_per_update = (int)(kSampleRate * update_dt); // ~533 frames + const int samples_per_update = + frames_per_update * 2; // Stereo: 2 samples per frame + const int total_updates = (int)(duration_sec_ / update_dt); // Music time tracking float music_time = 0.0f; @@ -71,12 +76,12 @@ void WavDumpBackend::start() { tempo_scale = 1.0f; } else if (physical_time < 15.0f) { const float progress = (physical_time - 10.0f) / 5.0f; - tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0 + tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0 } else if (physical_time < 20.0f) { tempo_scale = 1.0f; } else if (physical_time < 25.0f) { const float progress = (physical_time - 20.0f) / 5.0f; - tempo_scale = 1.0f - progress * 0.5f; // 1.0 → 0.5 + tempo_scale = 1.0f - progress * 0.5f; // 1.0 → 0.5 } else { tempo_scale = 1.0f; } @@ -99,8 +104,10 @@ void WavDumpBackend::start() { // Convert float to int16 and write to WAV (stereo interleaved) for (int i = 0; i < samples_per_update; ++i) { float sample = chunk_buffer[i]; - if (sample > 1.0f) sample = 1.0f; - if (sample < -1.0f) sample = -1.0f; + if (sample > 1.0f) + sample = 1.0f; + if (sample < -1.0f) + sample = -1.0f; const int16_t sample_i16 = (int16_t)(sample * 32767.0f); fwrite(&sample_i16, sizeof(int16_t), 1, wav_file_); @@ -110,8 +117,8 @@ void WavDumpBackend::start() { // Progress indicator if (update_count % 60 == 0) { - printf(" Rendering: %.1fs / %ds (music: %.1fs, tempo: %.2fx)\r", - physical_time, max_duration_sec, music_time, tempo_scale); + printf(" Rendering: %.1fs / %.1fs (music: %.1fs, tempo: %.2fx)\r", + physical_time, duration_sec_, music_time, tempo_scale); fflush(stdout); } @@ -120,8 +127,10 @@ void WavDumpBackend::start() { } printf( - "\nWAV dump complete: %zu samples (%.2f seconds stereo, %.2f music time)\n", - samples_written_, (float)samples_written_ / (kSampleRate * 2), music_time); + "\nWAV dump complete: %zu samples (%.2f seconds stereo, %.2f music " + "time)\n", + samples_written_, (float)samples_written_ / (kSampleRate * 2), + music_time); is_active_ = false; } @@ -141,7 +150,7 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) { // WAV file header structure // Reference: http://soundfile.sapp.org/doc/WaveFormat/ - const uint32_t num_channels = 2; // Stereo (matches miniaudio config) + const uint32_t num_channels = 2; // Stereo (matches miniaudio config) const uint32_t sample_rate = kSampleRate; const uint32_t bits_per_sample = 16; const uint32_t byte_rate = sample_rate * num_channels * bits_per_sample / 8; @@ -158,7 +167,7 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) { fwrite("fmt ", 1, 4, file); const uint32_t subchunk1_size = 16; fwrite(&subchunk1_size, 4, 1, file); - const uint16_t audio_format = 1; // PCM + const uint16_t audio_format = 1; // PCM fwrite(&audio_format, 2, 1, file); fwrite(&num_channels, 2, 1, file); fwrite(&sample_rate, 4, 1, file); @@ -172,7 +181,8 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) { } void WavDumpBackend::update_wav_header() { - if (wav_file_ == nullptr) return; + if (wav_file_ == nullptr) + return; // Seek to beginning and rewrite header with actual sample count fseek(wav_file_, 0, SEEK_SET); diff --git a/src/audio/wav_dump_backend.h b/src/audio/wav_dump_backend.h index b037fd1..eb6e011 100644 --- a/src/audio/wav_dump_backend.h +++ b/src/audio/wav_dump_backend.h @@ -26,8 +26,13 @@ class WavDumpBackend : public AudioBackend { // Set output filename (call before init()) void set_output_file(const char* filename); + // Set duration in seconds (default: 60s, call before start()) + void set_duration(float seconds); + // Get total samples written - size_t get_samples_written() const { return samples_written_; } + size_t get_samples_written() const { + return samples_written_; + } private: // Write WAV header with known sample count @@ -41,6 +46,7 @@ class WavDumpBackend : public AudioBackend { size_t samples_written_; const char* output_filename_; bool is_active_; + float duration_sec_; static const int kSampleRate = 32000; static const int kBufferSize = 1024; -- cgit v1.2.3