get_playback_position() only moves when Godot mixes the next chunk of audio, so between chunks it repeats the same time and then jumps. Anything you move by the song's time, like rhythm notes, a beat pulse or a firework cue, freezes and then skips. In our Godot 4.7 test on Windows at 144 fps it repeated on 158 of 371 frames and jumped up to 35 ms. With Godot's Dummy audio driver, which mixes 93 ms at a time, it repeated on 135 of 166 frames at 60 fps. The script below adds AudioServer.get_time_since_last_mix() and takes away AudioServer.get_output_latency(). That alone went backwards on 19 to 34 frames at 144 fps, so the script also never lets the time go down. Result: it never went backwards, and with 93 ms chunks it moved on every frame.
The code
Save it as song_clock.gd and load it with const SongClock := preload("res://song_clock.gd"). Make one with SongClock.new($Music) when the scene starts, and read clock.position() every frame instead of $Music.get_playback_position(). clock.beats(bpm) gives the beats since the first downbeat: when its whole number goes up, a new beat has started.
extends RefCounted
## Where the song is, in seconds, smooth on every frame and matched to what the player hears.
## get_playback_position() only moves when Godot mixes the next chunk of audio,
## so on many frames it repeats the last value and then jumps.
## const SongClock := preload("res://song_clock.gd")
## var clock := SongClock.new($Music) # once, in _ready()
## var t := clock.position() # every frame: seconds into the song
## var b := clock.beats(150.0) # every frame: beats since the first downbeat
var player: AudioStreamPlayer
var _last := 0.0
func _init(music: AudioStreamPlayer) -> void:
player = music
func position() -> float:
var raw := player.get_playback_position()
var t := raw
if player.playing:
t += AudioServer.get_time_since_last_mix() - AudioServer.get_output_latency()
t = maxf(t, 0.0)
if raw + 0.25 < _last: # the song was restarted or seeked back: follow it
_last = t
_last = maxf(_last, t) # the estimate can wobble back a few ms between mixes: never go backwards
return _last
## Beats since `first_beat` seconds into the song. floor() it for the beat number.
func beats(bpm: float, first_beat := 0.0) -> float:
return (position() - first_beat) * bpm / 60.0
What we measured
8 seconds of silence playing on an AudioStreamPlayer, read on every frame for 3 seconds, leaving out the first quarter second. We counted frames that showed the same time as the frame before, frames where the time went down, and the biggest step, and how evenly beats at 150 BPM were spotted. Two runs of each.
- Windows audio (WASAPI, 10 ms output latency) at 144 fps:
get_playback_position()repeated on 158 of 371 and 158 of 390 frames, with steps up to 35 and 23 ms. At 60 fps it repeated on only 6 of 166 frames, because one frame is longer than one chunk of audio. - Adding the time since the last mix and taking away the latency, at 144 fps: no repeats, but the time went backwards on 34 and 19 frames.
- The script at 144 fps: never backwards. It held still on 28 and 37 frames instead, while the estimate caught up.
- Godot's Dummy audio driver at 60 fps, 93 ms chunks:
get_playback_position()repeated on 135 and 136 of 166 frames, in steps of 92.9 ms. The moment each beat was spotted wobbled by 66 ms from beat to beat. The script: no repeats, never backwards, steps of about one frame, and beats spotted within 17 ms of each other, one frame.
Why it happens
Godot does not play audio sample by sample. It mixes a chunk, hands it to the sound card, and only then moves the playback position. A frame that comes between two chunks reads the same position as the last one. Time since the last mix fills in how far the song has got since that chunk, and the output latency is how long the sound takes to reach the speakers, so taking it away matches what the player hears. The two are measured at slightly different moments, so the sum can wobble back by a few milliseconds. Keeping the highest value so far hides that.
What to change
- Seeking or restarting the song: the script follows when the position drops by more than a quarter of a second. Make a new clock if you switch songs.
- The first downbeat after an intro: pass its time in seconds as
first_beat. - Beats from a start time, with no music node: fireworks on the beat without a drifting timer. Recording and replaying by time: a ghost replay by time, not frames.
A whole show on the music: Fireworks Show Kit for Godot 4 plays cues written in seconds or in beats, counted from a BPM and a beat offset, and can build a show for you with an opening, a climb, a pause and a finale.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Fireworks Show Kit for Godot 4 plays firework cues written in seconds or in beats from a BPM, and can build a whole show with an opening, a climb and a finale.
More how-tos for Fireworks Show Kit for Godot 4: Effects on the beat that stay on the beat · Make a firework rocket burst at the top of its arc at any launch speed
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- Nodes your editor tool adds that stay in the scene