Fix offset misalinment (untested)

This commit is contained in:
illyum 2024-08-09 02:16:34 -06:00
parent 6befaf5eea
commit b3837e21d5

View File

@ -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<float>(glfwGetTime()) - timeline_position - offset_seconds;
if (!has_applied_offset) {
// Apply offset only the first time playback starts
playback_start_time = static_cast<float>(glfwGetTime()) - timeline_position - offset_seconds;
has_applied_offset = true; // Mark that offset has been applied
}
else {
playback_start_time = static_cast<float>(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<ma_uint64>((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<int>((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();
}