← Pixel Dreamer Studio

Effects on the beat that stay on the beat

Work out when beat n is due from the start time. Do not chain one timer after another.

Work out when each beat is due from the moment the music started, n * 60 / bpm seconds, and fire every beat whose time has passed. Starting a new 0.4-second timer after each beat adds up the lateness: in our Godot 4.7 test at 150 BPM it was 0.13 s late by beat 10 and 0.56 s late after 15 seconds, one beat short. The script below stayed within 0.015 s, one frame, for all 38 beats.

The code

Add this node, set bpm, and connect its beat signal to whatever should happen on the beat: launch a firework, flash the lights, pulse the UI. Call start() on the same frame you start the music. The signal passes the beat number, so you can launch something bigger every 4th or 16th beat.

extends Node
## Calls on_beat on every beat of a song or show without drifting late.
## Set bpm, connect the beat signal to whatever should launch, then call start() when the music starts.

signal beat(n: int)

@export var bpm := 120.0

var _start_usec := 0
var _next := 0
var _running := false


func start() -> void:
	_start_usec = Time.get_ticks_usec()
	_next = 0
	_running = true


func stop() -> void:
	_running = false


func _process(_delta: float) -> void:
	if not _running:
		return
	var elapsed := (Time.get_ticks_usec() - _start_usec) / 1000000.0
	var interval := 60.0 / bpm
	# beat n is due at n * interval from the start, so a late frame never pushes the next beats back
	while elapsed >= _next * interval:
		beat.emit(_next)
		_next += 1

What we measured

150 BPM, a beat every 0.4 s, for 15 seconds at 60 frames per second. For each beat we measured how late it fired against n x 0.4 s from the start. Both methods ran at the same time. Two runs gave the same numbers to within 0.001 s.

Why chained timers fall behind

A timer can only fire on a frame, so it fires up to one frame after it runs out. The next timer starts from that late moment, so each beat inherits the lateness of every beat before it. At 60 frames per second that was about 1/60 of a second per beat. Counting every beat from the same start time means a late frame delays only that one beat.

What to change

Fireworks that do not need sub-emitters: fireworks without sub-emitters.

A whole show on the beat: 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. It comes with launch racks, fuses and a harbor demo.

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: A song position that moves on every frame

More how-tos: Fire, smoke and explosions