diff options
Diffstat (limited to 'src/test_demo.cc')
| -rw-r--r-- | src/test_demo.cc | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/test_demo.cc b/src/test_demo.cc index 8209eca..bb92446 100644 --- a/src/test_demo.cc +++ b/src/test_demo.cc @@ -219,20 +219,18 @@ int main(int argc, char** argv) { // Music time tracking with optional tempo variation static float g_music_time = 0.0f; - static double g_last_physical_time = 0.0; + static float g_last_audio_time = 0.0f; static float g_tempo_scale = 1.0f; - auto fill_audio_buffer = [&](double t) { - const float dt = (float)(t - g_last_physical_time); - g_last_physical_time = t; - + auto fill_audio_buffer = [&](float audio_dt, double physical_time) { // Calculate tempo scale if --tempo flag enabled if (tempo_test_enabled) { // Each bar = 2 seconds at 120 BPM (4 beats) const float bar_duration = 2.0f; - const int bar_number = (int)(t / bar_duration); + const int bar_number = (int)(physical_time / bar_duration); const float bar_progress = - fmodf((float)t, bar_duration) / bar_duration; // 0.0-1.0 within bar + fmodf((float)physical_time, bar_duration) / + bar_duration; // 0.0-1.0 within bar if (bar_number % 2 == 0) { // Even bars: Ramp from 1.0x → 1.5x @@ -245,16 +243,17 @@ int main(int argc, char** argv) { g_tempo_scale = 1.0f; // No tempo variation } - g_music_time += dt * g_tempo_scale; + g_music_time += audio_dt * g_tempo_scale; - g_audio_engine.update(g_music_time); - audio_render_ahead(g_music_time, 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); }; // Pre-fill audio buffer - g_audio_engine.update(g_music_time); + g_audio_engine.update(g_music_time, 1.0f / 60.0f); audio_render_ahead(g_music_time, 1.0f / 60.0f); audio_start(); + g_last_audio_time = audio_get_playback_time(); int last_width = platform_state.width; int last_height = platform_state.height; @@ -312,13 +311,17 @@ int main(int argc, char** argv) { break; } - fill_audio_buffer(physical_time); + // Calculate stable audio dt for jitter-free tracker updates + const float audio_time = audio_get_playback_time(); + const float audio_dt = audio_time - g_last_audio_time; + g_last_audio_time = audio_time; + + fill_audio_buffer(audio_dt, physical_time); // Audio-visual synchronization: Use audio playback time (not physical // time!) This accounts for ring buffer latency automatically (no hardcoded // constants) - const float audio_time = audio_get_playback_time(); - + // Audio/visual sync parameters const float aspect_ratio = platform_state.aspect_ratio; // Peak is measured at audio playback time, so it matches audio_time |
