Work in progress
The content of this page was not yet updated for Godot
4.0
and may be outdated. If you know how to improve this page or you can confirm
that it's up to date, feel free to open a pull request.
Sync the gameplay with audio and music¶
Introduction¶
In any application or game, sound and music playback will have a slight delay. For games, this delay is often so small that it is negligible. Sound effects will come out a few milliseconds after any play() function is called. For music this does not matter as in most games it does not interact with the gameplay.
Still, for some games (mainly, rhythm games), it may be required to synchronize player actions with something happening in a song (usually in sync with the BPM). For this, having more precise timing information for an exact playback position is useful.
Achieving very low playback timing precision is difficult. This is because many factors are at play during audio playback:
Audio is mixed in chunks (not continuously), depending on the size of audio buffers used (check latency in project settings).
Mixed chunks of audio are not played immediately.
Graphics APIs display two or three frames late.
When playing on TVs, some delay may be added due to image processing.
The most common way to reduce latency is to shrink the audio buffers (again, by editing the latency setting in the project settings). The problem is that when latency is too small, sound mixing will require considerably more CPU. This increases the risk of skipping (a crack in sound because a mix callback was lost).
This is a common tradeoff, so Godot ships with sensible defaults that should not need to be altered.
The problem, in the end, is not this slight delay but synchronizing graphics and audio for games that require it. Beginning with Godot 3.2, some helpers were added to obtain more precise playback timing.
Using the system clock to sync¶
As mentioned before, If you call AudioStreamPlayer.play(), sound will not begin immediately, but when the audio thread processes the next chunk.
This delay can't be avoided but it can be estimated by calling AudioServer.get_time_to_next_mix().
The output latency (what happens after the mix) can also be estimated by calling AudioServer.get_output_latency().
Add these two and it's possible to guess almost exactly when sound or music will begin playing in the speakers during _process():
var time_begin
var time_delay
func _ready():
time_begin = Time.get_ticks_usec()
time_delay = AudioServer.get_time_to_next_mix() + AudioServer.get_output_latency()
$Player.play()
func _process(delta):
# Obtain from ticks.
var time = (Time.get_ticks_usec() - time_begin) / 1000000.0
# Compensate for latency.
time -= time_delay