From 0f79b532c886f338ab80d506d4b06048e1784056 Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 7 Feb 2026 10:46:43 +0100 Subject: refactor(audio): Remove tempo logic from WavDumpBackend Fixed design flaw where WavDumpBackend had hardcoded tempo curves duplicating logic from main.cc. Backend should be passive and just write audio data, not implement simulation logic. Changes: - WavDumpBackend.start() is now non-blocking (was blocking simulation loop) - Added write_audio() method for passive audio writing - Removed all tempo scaling logic from backend (lines 62-97) - Removed tracker_update() and audio_render_ahead() calls from backend - Removed set_duration() (no longer needed, frontend controls duration) Frontend (main.cc): - Added WAV dump mode loop that drives simulation with its own tempo logic - Reads from ring buffer and calls wav_backend.write_audio() - Tempo logic stays in one place (no duplication) - Added ring_buffer.h include for AudioRingBuffer access Test (test_wav_dump.cc): - Updated to use frontend-driven approach - Test manually drives simulation loop - Calls write_audio() after each frame - Verifies passive backend behavior Design: - Backend: Passive file writer (init/start/write_audio/shutdown) - Frontend: Active simulation driver (tempo, tracker, rendering) - Zero duplication of tempo/simulation logic - Clean separation of concerns All 27 tests pass. Co-Authored-By: Claude Sonnet 4.5 --- src/tests/test_wav_dump.cc | 50 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) (limited to 'src/tests') diff --git a/src/tests/test_wav_dump.cc b/src/tests/test_wav_dump.cc index c68578b..cc2de19 100644 --- a/src/tests/test_wav_dump.cc +++ b/src/tests/test_wav_dump.cc @@ -3,10 +3,12 @@ #include "audio/audio.h" #include "audio/audio_engine.h" +#include "audio/ring_buffer.h" #include "audio/wav_dump_backend.h" #include #include #include +#include #if !defined(STRIP_ALL) @@ -32,31 +34,48 @@ void test_wav_format_matches_live_audio() { const char* test_file = "test_format.wav"; + // Initialize audio system + audio_init(); + + // Initialize AudioEngine + AudioEngine engine; + engine.init(); + // Create WAV dump backend WavDumpBackend wav_backend; wav_backend.set_output_file(test_file); - wav_backend.set_duration(2.0f); // Only 2 seconds for quick testing - audio_set_backend(&wav_backend); + wav_backend.init(); + wav_backend.start(); - // Initialize audio system (calls synth_init internally) - audio_init(); + // Simulate 2 seconds of audio rendering (frontend-driven) + const float duration = 2.0f; + const float update_dt = 1.0f / 60.0f; + const int frames_per_update = (int)(32000 * update_dt); + const int samples_per_update = frames_per_update * 2; // Stereo - // Initialize AudioEngine (replaces direct synth_init/tracker_init) - AudioEngine engine; - engine.init(); + AudioRingBuffer* ring_buffer = audio_get_ring_buffer(); + std::vector chunk_buffer(samples_per_update); - // Manually trigger some audio for testing - engine.update(0.0f); // Trigger patterns at t=0 + float music_time = 0.0f; + for (float t = 0.0f; t < duration; t += update_dt) { + // Update audio engine (triggers patterns) + engine.update(music_time); + music_time += update_dt; - // Render short duration (1 second = 60 updates @ 60Hz) - for (int i = 0; i < 60; ++i) { - float t = i / 60.0f; - engine.update(t); + // Render audio ahead + audio_render_ahead(music_time, update_dt); + + // Read from ring buffer + if (ring_buffer != nullptr) { + ring_buffer->read(chunk_buffer.data(), samples_per_update); + } - // Simulate audio render (WavDumpBackend will handle this in start()) + // Write to WAV file + wav_backend.write_audio(chunk_buffer.data(), samples_per_update); } - audio_start(); // This triggers the actual WAV rendering + // Shutdown + wav_backend.shutdown(); engine.shutdown(); audio_shutdown(); @@ -146,6 +165,7 @@ void test_wav_format_matches_live_audio() { printf(" ✓ WAV format verified: stereo, 32kHz, 16-bit PCM\n"); printf(" ✓ Matches live audio output configuration\n"); + printf(" ✓ Backend is passive (frontend-driven)\n"); } void test_wav_stereo_buffer_size() { -- cgit v1.2.3