Try to fix bpm desync

This commit is contained in:
illyum 2024-08-09 02:44:08 -06:00
parent b3837e21d5
commit 9b738c359d

View File

@ -583,8 +583,6 @@ void renderImGui() {
} }
void renderImGuiBpm() { void renderImGuiBpm() {
// Existing DMX Controller window code here...
// New BPM Control Window // New BPM Control Window
ImGui::Begin("BPM Controller"); ImGui::Begin("BPM Controller");
@ -612,16 +610,22 @@ void renderImGuiBpm() {
// Start/Stop button // Start/Stop button
static bool is_playing_bpm = false; 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 lastBeatTime = 0.0f;
static float pauseTime = 0.0f; // Store the time when BPM is paused
if (ImGui::Button(is_playing_bpm ? "Stop" : "Start")) { if (ImGui::Button(is_playing_bpm ? "Stop" : "Start")) {
is_playing_bpm = !is_playing_bpm;
if (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 (!is_playing && audioLoaded) {
if (!has_applied_offset) { if (!has_applied_offset) {
// Apply offset only the first time playback starts
playback_start_time = static_cast<float>(glfwGetTime()) - timeline_position - offset_seconds; playback_start_time = static_cast<float>(glfwGetTime()) - timeline_position - offset_seconds;
has_applied_offset = true; // Mark that offset has been applied has_applied_offset = true;
} }
else { else {
playback_start_time = static_cast<float>(glfwGetTime()) - timeline_position; playback_start_time = static_cast<float>(glfwGetTime()) - timeline_position;
@ -635,13 +639,13 @@ void renderImGuiBpm() {
ma_sound_start(&sound); ma_sound_start(&sound);
is_playing = true; is_playing = true;
} }
lastBeatTime = timeline_position;
} if (!is_playing_bpm) {
else { // Adjust lastBeatTime based on the pause time
if (audioLoaded) { lastBeatTime += (timeline_position - pauseTime);
ma_sound_stop(&sound);
is_playing = false;
} }
is_playing_bpm = true;
} }
} }