← Pixel Dreamer Studio

A song position that moves on every frame

Add the time since Godot last mixed audio, take away the output latency, and never let the result go backwards.

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.

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

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