From 9b738c359d42c984f5ac3987604d455ad0a4ed9f Mon Sep 17 00:00:00 2001 From: illyum <90023277+itzilly@users.noreply.github.com> Date: Fri, 9 Aug 2024 02:44:08 -0600 Subject: [PATCH] Try to fix bpm desync --- main.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index 90bdc78..ec1032a 100644 --- a/main.cpp +++ b/main.cpp @@ -583,8 +583,6 @@ void renderImGui() { } void renderImGuiBpm() { - // Existing DMX Controller window code here... - // New BPM Control Window ImGui::Begin("BPM Controller"); @@ -612,16 +610,22 @@ void renderImGuiBpm() { // Start/Stop button static bool is_playing_bpm = false; - static bool has_applied_offset = false; // New variable to track if the offset has been applied + static bool has_applied_offset = false; static float lastBeatTime = 0.0f; + static float pauseTime = 0.0f; // Store the time when BPM is paused + if (ImGui::Button(is_playing_bpm ? "Stop" : "Start")) { - is_playing_bpm = !is_playing_bpm; if (is_playing_bpm) { + // Stop BPM playback + pauseTime = timeline_position; + is_playing_bpm = false; + } + else { + // Start BPM playback if (!is_playing && audioLoaded) { if (!has_applied_offset) { - // Apply offset only the first time playback starts playback_start_time = static_cast(glfwGetTime()) - timeline_position - offset_seconds; - has_applied_offset = true; // Mark that offset has been applied + has_applied_offset = true; } else { playback_start_time = static_cast(glfwGetTime()) - timeline_position; @@ -635,13 +639,13 @@ void renderImGuiBpm() { ma_sound_start(&sound); is_playing = true; } - lastBeatTime = timeline_position; - } - else { - if (audioLoaded) { - ma_sound_stop(&sound); - is_playing = false; + + if (!is_playing_bpm) { + // Adjust lastBeatTime based on the pause time + lastBeatTime += (timeline_position - pauseTime); } + + is_playing_bpm = true; } }