Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stream sync play and seek setting wrong position on certain cases #100292

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions modules/interactive_music/audio_stream_synchronized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,42 @@ void AudioStreamPlaybackSynchronized::stop() {
}

void AudioStreamPlaybackSynchronized::start(double p_from_pos) {
if (active) {
_seek_or_start(p_from_pos, true);
}

void AudioStreamPlaybackSynchronized::seek(double p_time) {
_seek_or_start(p_time, false);
}

void AudioStreamPlaybackSynchronized::_seek_or_start(double p_from_pos, bool is_start) {
if (is_start && active) {
stop();
}

for (int i = 0; i < stream->stream_count; i++) {
if (playback[i].is_valid()) {
playback[i]->start(p_from_pos);
active = true;
}
}
}
bool is_pos_within_range = p_from_pos >= 0 && p_from_pos < stream->get_length();

void AudioStreamPlaybackSynchronized::seek(double p_time) {
for (int i = 0; i < stream->stream_count; i++) {
if (playback[i].is_valid()) {
playback[i]->seek(p_time);
if (playback[i].is_valid() && stream->audio_streams[i].is_valid()) {
double audio_stream_length = stream->audio_streams[i]->get_length();
double playback_pos = p_from_pos;

if (is_pos_within_range && audio_stream_length < playback_pos) {
if (!stream->audio_streams[i]->has_loop()) {
continue;
}

while (audio_stream_length < playback_pos) {
playback_pos -= audio_stream_length;
}
}

if (is_start) {
playback[i]->start(playback_pos);
} else {
playback[i]->seek(playback_pos);
}

active = true;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/interactive_music/audio_stream_synchronized.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class AudioStreamPlaybackSynchronized : public AudioStreamPlayback {
bool active = false;

void _update_playback_instances();
void _seek_or_start(double p_from_pos, bool is_start);

public:
virtual void start(double p_from_pos = 0.0) override;
Expand Down