Fix offset misalinment (untested)
This commit is contained in:
parent
6befaf5eea
commit
b3837e21d5
24
main.cpp
24
main.cpp
@ -583,6 +583,7 @@ 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");
|
||||||
@ -611,26 +612,30 @@ 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 float lastBeatTime = 0.0f;
|
static float lastBeatTime = 0.0f;
|
||||||
if (ImGui::Button(is_playing_bpm ? "Stop" : "Start")) {
|
if (ImGui::Button(is_playing_bpm ? "Stop" : "Start")) {
|
||||||
is_playing_bpm = !is_playing_bpm;
|
is_playing_bpm = !is_playing_bpm;
|
||||||
if (is_playing_bpm) {
|
if (is_playing_bpm) {
|
||||||
// Start playing the audio if not already playing
|
|
||||||
if (!is_playing && audioLoaded) {
|
if (!is_playing && audioLoaded) {
|
||||||
// Apply offset to playback start time
|
if (!has_applied_offset) {
|
||||||
playback_start_time = static_cast<float>(glfwGetTime()) - timeline_position - offset_seconds;
|
// 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_uint64 frameCount;
|
||||||
ma_sound_get_length_in_pcm_frames(&sound, &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;
|
float playback_time = timeline_position + offset_seconds;
|
||||||
ma_uint64 targetFrame = static_cast<ma_uint64>((playback_time / timeline_duration) * frameCount);
|
ma_uint64 targetFrame = static_cast<ma_uint64>((playback_time / timeline_duration) * frameCount);
|
||||||
ma_sound_seek_to_pcm_frame(&sound, targetFrame);
|
ma_sound_seek_to_pcm_frame(&sound, targetFrame);
|
||||||
ma_sound_start(&sound);
|
ma_sound_start(&sound);
|
||||||
is_playing = true;
|
is_playing = true;
|
||||||
}
|
}
|
||||||
// Initialize the beat timer with offset
|
lastBeatTime = timeline_position;
|
||||||
lastBeatTime = timeline_position; // Start the beat timer from the timeline position
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (audioLoaded) {
|
if (audioLoaded) {
|
||||||
@ -642,21 +647,17 @@ void renderImGuiBpm() {
|
|||||||
|
|
||||||
// Non-blocking BPM-based Random Color Changes
|
// Non-blocking BPM-based Random Color Changes
|
||||||
if (is_playing_bpm && audioLoaded) {
|
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;
|
float timePerBeat = 60.0f / bpm;
|
||||||
|
|
||||||
// Only update the color when the current time exceeds the next beat time
|
|
||||||
if (currentTime - lastBeatTime >= timePerBeat) {
|
if (currentTime - lastBeatTime >= timePerBeat) {
|
||||||
// Calculate how many beats we have skipped and adjust `lastBeatTime`
|
|
||||||
int beatsSkipped = static_cast<int>((currentTime - lastBeatTime) / timePerBeat);
|
int beatsSkipped = static_cast<int>((currentTime - lastBeatTime) / timePerBeat);
|
||||||
lastBeatTime += beatsSkipped * timePerBeat;
|
lastBeatTime += beatsSkipped * timePerBeat;
|
||||||
|
|
||||||
// Generate new random colors every beat
|
|
||||||
unsigned char r = rand() % 256;
|
unsigned char r = rand() % 256;
|
||||||
unsigned char g = rand() % 256;
|
unsigned char g = rand() % 256;
|
||||||
unsigned char b = rand() % 256;
|
unsigned char b = rand() % 256;
|
||||||
|
|
||||||
// Set the DMX buffer with the random colors
|
|
||||||
buffer[0] = r;
|
buffer[0] = r;
|
||||||
buffer[1] = g;
|
buffer[1] = g;
|
||||||
buffer[2] = b;
|
buffer[2] = b;
|
||||||
@ -664,4 +665,5 @@ void renderImGuiBpm() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user