diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-08 11:23:03 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-08 11:23:03 +0100 |
| commit | 47d738f36a3eb262456082e35369cb26485df575 (patch) | |
| tree | d83d8259f0d87df20861030f4fa544195e61d358 /src/main.cc | |
| parent | 392d03c0c05f24be3210a04d9a50cd9714d1e265 (diff) | |
feat(timing): Decouple graphics loop from audio clock for smooth performance
This commit addresses the choppy graphics issue reported after the audio synchronization fixes. The graphics rendering loop is now driven by the platform's independent clock () to ensure a consistent frame rate, while still using audio playback time for synchronization cues like beat detection and visual peak indicators.
Key changes include:
- In and :
- Introduced derived from for the main loop.
- Main loop exit conditions and calls now use .
- Audio timing (, ) remains separate and is used for audio processing and synchronization events.
- Debug output clarifies between graphics and audio time sources.
- Corrected scope issues for and in .
Diffstat (limited to 'src/main.cc')
| -rw-r--r-- | src/main.cc | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/src/main.cc b/src/main.cc index 51060ce..a4cc67d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -233,49 +233,59 @@ int main(int argc, char** argv) { gpu_resize(last_width, last_height); } - const double physical_time = - platform_state.time + seek_time; // Offset logic time + // Graphics frame time - derived from platform's clock + const double current_physical_time = platform_state.time + seek_time; + // Audio playback time - master clock for audio events + const float current_audio_time = audio_get_playback_time(); + // Delta time for audio processing, based on audio clock + const float audio_dt = current_audio_time - g_last_audio_time; + g_last_audio_time = current_audio_time; // Auto-exit when demo finishes (if duration is specified) - if (demo_duration > 0.0f && physical_time >= demo_duration) { + if (demo_duration > 0.0f && current_physical_time >= demo_duration) { #if !defined(STRIP_ALL) - printf("Demo finished at %.2f seconds. Exiting...\n", physical_time); + printf("Demo finished at %.2f seconds. Exiting...\n", current_physical_time); #endif break; } - // 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); + // This fill_audio_buffer call is crucial for audio system to process + // events based on the *current audio time* and *graphics physical time* context + fill_audio_buffer(audio_dt, current_physical_time); + // --- Graphics Update --- const float aspect_ratio = platform_state.aspect_ratio; - // Adjusted multiplier for visuals (preventing constant 1.0 saturation) - // Use real-time peak for proper audio-visual synchronization + // Peak value derived from audio, but used for visual effect intensity const float raw_peak = audio_get_realtime_peak(); const float visual_peak = fminf(raw_peak * 8.0f, 1.0f); - // Calculate beat information for synchronization - // MASTER CLOCK: Use audio playback time for perfect visual sync - const float sync_time = audio_get_playback_time(); - const float beat_time = sync_time * g_tracker_score.bpm / 60.0f; + // Beat calculation should use audio time to align with audio events. + // The graphics loop time (current_physical_time) is used for frame rate. + const float beat_time = current_audio_time * g_tracker_score.bpm / 60.0f; const int beat_number = (int)beat_time; const float beat = fmodf(beat_time, 1.0f); // Fractional part (0.0 to 1.0) -#if !defined(STRIP_ALL) // Print beat/time info periodically for identifying sync points - static float last_print_time = -1.0f; - if (sync_time - last_print_time >= 0.5f) { // Print every 0.5 seconds - printf("[T=%.2f, MusicT=%.2f, Beat=%d, Frac=%.2f, Peak=%.2f]\n", - sync_time, g_music_time, beat_number, beat, visual_peak); - last_print_time = sync_time; + // Use graphics time for the print interval to avoid excessive output if audio clock is slow + static float last_graphics_print_time = -1.0f; + if (current_physical_time - last_graphics_print_time >= 0.5f) { // Print every 0.5 seconds + if (tempo_test_enabled) { + printf( + "[GraphicsT=%.2f, AudioT=%.2f, MusicT=%.2f, Beat=%d, Frac=%.2f, Peak=%.2f, Tempo=%.2fx]\n", + current_physical_time, current_audio_time, g_music_time, beat_number, beat, + visual_peak, g_tempo_scale); + } else { + printf("[GraphicsT=%.2f, AudioT=%.2f, Beat=%d, Frac=%.2f, Peak=%.2f]\n", + current_physical_time, current_audio_time, beat_number, beat, visual_peak); + } + last_graphics_print_time = current_physical_time; } -#endif /* !defined(STRIP_ALL) */ - gpu_draw(visual_peak, aspect_ratio, sync_time, beat); + // Draw graphics using the graphics frame time and synchronized audio events + gpu_draw(visual_peak, aspect_ratio, graphics_frame_time, beat); + + // Update audio systems (tracker, synth, etc.) based on audio time progression audio_update(); } @@ -283,4 +293,4 @@ int main(int argc, char** argv) { gpu_shutdown(); platform_shutdown(&platform_state); return 0; -} +}
\ No newline at end of file |
