From b3837e21d522650d1e09ace190526c2b8435081a Mon Sep 17 00:00:00 2001 From: illyum <90023277+itzilly@users.noreply.github.com> Date: Fri, 9 Aug 2024 02:16:34 -0600 Subject: [PATCH] Fix offset misalinment (untested) --- main.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 67289bd..90bdc78 100644 --- a/main.cpp +++ b/main.cpp @@ -583,6 +583,7 @@ void renderImGui() { } void renderImGuiBpm() { + // Existing DMX Controller window code here... // New BPM Control Window ImGui::Begin("BPM Controller"); @@ -611,26 +612,30 @@ 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 float lastBeatTime = 0.0f; if (ImGui::Button(is_playing_bpm ? "Stop" : "Start")) { is_playing_bpm = !is_playing_bpm; if (is_playing_bpm) { - // Start playing the audio if not already playing if (!is_playing && audioLoaded) { - // Apply offset to playback start time - playback_start_time = static_cast(glfwGetTime()) - timeline_position - offset_seconds; + 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 + } + else { + playback_start_time = static_cast(glfwGetTime()) - timeline_position; + } ma_uint64 frameCount; ma_sound_get_length_in_pcm_frames(&sound, &frameCount); - // Calculate the frame to seek to based on the timeline position and offset float playback_time = timeline_position + offset_seconds; ma_uint64 targetFrame = static_cast((playback_time / timeline_duration) * frameCount); ma_sound_seek_to_pcm_frame(&sound, targetFrame); ma_sound_start(&sound); is_playing = true; } - // Initialize the beat timer with offset - lastBeatTime = timeline_position; // Start the beat timer from the timeline position + lastBeatTime = timeline_position; } else { if (audioLoaded) { @@ -642,21 +647,17 @@ void renderImGuiBpm() { // Non-blocking BPM-based Random Color Changes if (is_playing_bpm && audioLoaded) { - float currentTime = timeline_position + offset_seconds; // Apply offset to current time + float currentTime = timeline_position; float timePerBeat = 60.0f / bpm; - // Only update the color when the current time exceeds the next beat time if (currentTime - lastBeatTime >= timePerBeat) { - // Calculate how many beats we have skipped and adjust `lastBeatTime` int beatsSkipped = static_cast((currentTime - lastBeatTime) / timePerBeat); lastBeatTime += beatsSkipped * timePerBeat; - // Generate new random colors every beat unsigned char r = rand() % 256; unsigned char g = rand() % 256; unsigned char b = rand() % 256; - // Set the DMX buffer with the random colors buffer[0] = r; buffer[1] = g; buffer[2] = b; @@ -664,4 +665,5 @@ void renderImGuiBpm() { } ImGui::End(); + }