From 8cababb75f17c420518cc5e84b66f48a1a1b82d3 Mon Sep 17 00:00:00 2001 From: skal Date: Sun, 15 Feb 2026 12:38:48 +0100 Subject: investigating audio-drive bug --- src/app/test_demo.cc | 3 +-- src/audio/audio.cc | 5 +++++ src/audio/tracker.cc | 10 ++++++++++ src/tests/audio/test_jittered_audio.cc | 5 ++--- src/tests/audio/test_silent_backend.cc | 8 ++++---- src/tests/audio/test_wav_dump.cc | 2 +- 6 files changed, 23 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/app/test_demo.cc b/src/app/test_demo.cc index f45916a..5775e74 100644 --- a/src/app/test_demo.cc +++ b/src/app/test_demo.cc @@ -280,10 +280,9 @@ int main(int argc, char** argv) { g_tempo_scale = 1.0f; // No tempo variation } - g_music_time += audio_dt * g_tempo_scale; - g_audio_engine.update(g_music_time, audio_dt * g_tempo_scale); audio_render_ahead(g_music_time, audio_dt * g_tempo_scale); + g_music_time += audio_dt * g_tempo_scale; }; // Pre-fill ring buffer to target lookahead (prevents startup delay) diff --git a/src/audio/audio.cc b/src/audio/audio.cc index aaadd5a..ba76a28 100644 --- a/src/audio/audio.cc +++ b/src/audio/audio.cc @@ -97,6 +97,11 @@ void audio_render_ahead(float music_time, float dt, float target_fill) { // Render in small chunks to keep synth time synchronized with tracker // Chunk size: one frame's worth of audio (~16.6ms @ 60fps) + // TODO(timing): CRITICAL BUG - Truncation here may cause 180ms drift over 63 beats + // (int) cast loses fractional samples: 0.333 samples/frame * 2560 frames = 853 samples = 27ms + // But observed drift is 180ms, so this is not the only source (27ms < 180ms) + // NOTE: This is NOT a float vs double precision issue - floats handle <500s times fine + // See also: tracker.cc BPM timing calculation const int chunk_frames = (int)(dt * RING_BUFFER_SAMPLE_RATE); const int chunk_samples = chunk_frames * RING_BUFFER_CHANNELS; diff --git a/src/audio/tracker.cc b/src/audio/tracker.cc index 1c0a9b2..37f0683 100644 --- a/src/audio/tracker.cc +++ b/src/audio/tracker.cc @@ -234,6 +234,16 @@ static void trigger_note_event(const TrackerEvent& event, } void tracker_update(float music_time_sec, float dt_music_sec) { + // TODO(timing): CRITICAL BUG - Events trigger ~180ms early over 63 beats @ BPM=90 + // Observed: Beat 63 snare at 41.82s in WAV, should be at 42.00s (180ms drift) + // NOTE: This is NOT a float vs double precision issue - floats handle <500s times fine + // Root cause unknown - suspects: + // 1. Systematic bias in time calculation (not random accumulation) + // 2. Truncation in audio.cc:103 chunk_frames = (int)(dt * sample_rate) + // 3. BPM calculation precision below (unit_duration_sec) + // 4. Mismatch between tracker time and actual sample rendering + // See also: audio.cc sample rendering truncation + // Unit-less timing: 1 unit = 4 beats (by convention) const float BEATS_PER_UNIT = 4.0f; const float unit_duration_sec = diff --git a/src/tests/audio/test_jittered_audio.cc b/src/tests/audio/test_jittered_audio.cc index d8260ec..d7c6a7d 100644 --- a/src/tests/audio/test_jittered_audio.cc +++ b/src/tests/audio/test_jittered_audio.cc @@ -42,11 +42,10 @@ void test_jittered_audio_basic() { float music_time = 0.0f; for (float t = 0.0f; t < total_time; t += dt) { - music_time += dt; // Normal tempo - // Update tracker and fill buffer tracker_update(music_time, dt); audio_render_ahead(music_time, dt); + music_time += dt; // Sleep minimal time to let audio thread run std::this_thread::sleep_for(std::chrono::milliseconds(1)); @@ -114,7 +113,7 @@ void test_jittered_audio_with_acceleration() { // Update tracker and fill buffer tracker_update(music_time, dt * tempo_scale); - audio_render_ahead(music_time, dt); + (void)audio_render_ahead(music_time, dt); // Sleep minimal time to let audio thread run std::this_thread::sleep_for(std::chrono::milliseconds(1)); diff --git a/src/tests/audio/test_silent_backend.cc b/src/tests/audio/test_silent_backend.cc index 3dc1cd4..cecf72c 100644 --- a/src/tests/audio/test_silent_backend.cc +++ b/src/tests/audio/test_silent_backend.cc @@ -97,7 +97,7 @@ void test_silent_backend_tracking() { assert(backend.get_voice_trigger_count() == 1); // Render audio (calls on_frames_rendered) - audio_render_ahead(0.0f, 0.1f); // Render ~0.1 seconds + (void)audio_render_ahead(0.0f, 0.1f); // Render ~0.1 seconds assert(backend.get_frames_rendered() > 0); // Reset stats @@ -123,7 +123,7 @@ void test_audio_playback_time() { assert(t0 == 0.0f); // Render some audio - audio_render_ahead(0.5f, 0.1f); // Advance music time to 0.5s + (void)audio_render_ahead(0.5f, 0.1f); // Advance music time to 0.5s // Playback time should advance based on frames rendered // Note: audio_get_playback_time() tracks cumulative frames consumed @@ -131,7 +131,7 @@ void test_audio_playback_time() { assert(t1 >= 0.0f); // Should have advanced // Render more - audio_render_ahead(1.0f, 0.5f); + (void)audio_render_ahead(1.0f, 0.5f); float t2 = audio_get_playback_time(); assert(t2 >= t1); // Should continue advancing @@ -152,7 +152,7 @@ void test_audio_buffer_partial_writes() { // Note: With SilentBackend, frames_rendered won't increase because // there's no audio callback consuming from the ring buffer for (int i = 0; i < 10; ++i) { - audio_render_ahead((float)i * 0.1f, 0.1f); + (void)audio_render_ahead((float)i * 0.1f, 0.1f); } // Buffer should have handled multiple writes correctly (no crash) diff --git a/src/tests/audio/test_wav_dump.cc b/src/tests/audio/test_wav_dump.cc index a0f2a4a..ce161a4 100644 --- a/src/tests/audio/test_wav_dump.cc +++ b/src/tests/audio/test_wav_dump.cc @@ -60,10 +60,10 @@ void test_wav_format_matches_live_audio() { for (float t = 0.0f; t < duration; t += update_dt) { // Update audio engine (triggers patterns) fixture.engine().update(music_time, update_dt); - music_time += update_dt; // Render audio ahead audio_render_ahead(music_time, update_dt); + music_time += update_dt; // Read from ring buffer if (ring_buffer != nullptr) { -- cgit v1.2.3