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

Make AudioStreamPlayer's get_playback_position more accurate #81873

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions doc/classes/AudioStreamPlayer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<return type="float" />
<description>
Returns the position in the [AudioStream] in seconds.
[b]Note:[/b] This method does not account for the small delay between mixing and hearing audio (see [method AudioServer.get_output_latency]).
</description>
</method>
<method name="get_stream_playback">
Expand Down
3 changes: 2 additions & 1 deletion scene/2d/audio_stream_player_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ bool AudioStreamPlayer2D::is_playing() const {
float AudioStreamPlayer2D::get_playback_position() {
// Return the playback position of the most recently started playback stream.
if (!stream_playbacks.is_empty()) {
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
float time_to_next_mix = get_stream_paused() ? 0.0 : MAX(AudioServer::get_singleton()->get_time_to_next_mix() * pitch_scale, 0.0);
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]) - time_to_next_mix;
}
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion scene/3d/audio_stream_player_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ bool AudioStreamPlayer3D::is_playing() const {
float AudioStreamPlayer3D::get_playback_position() {
// Return the playback position of the most recently started playback stream.
if (!stream_playbacks.is_empty()) {
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
float time_to_next_mix = get_stream_paused() ? 0.0 : MAX(AudioServer::get_singleton()->get_time_to_next_mix() * pitch_scale, 0.0);
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]) - time_to_next_mix;
}
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion scene/audio/audio_stream_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ bool AudioStreamPlayer::is_playing() const {
float AudioStreamPlayer::get_playback_position() {
// Return the playback position of the most recently started playback stream.
if (!stream_playbacks.is_empty()) {
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
float time_to_next_mix = get_stream_paused() ? 0.0 : MAX(AudioServer::get_singleton()->get_time_to_next_mix() * pitch_scale, 0.0);
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]) - time_to_next_mix;
}
return 0;
}
Expand Down
Loading