Pre-Rendered Procedural Audio and Native Text Outlines on Mobile
Maze Point ships no music files. Every track and every sound effect is synthesized in GDScript from sine, triangle, square, sawtooth, and noise formulas, as 16-bit mono at 22,050 Hz. That math is expensive on a phone. What keeps it from stalling the game is when it runs: each sound is computed once, into a buffer sized up front, and then the engine plays that buffer with no script code involved.
Render once, then let the engine play it
A new music track is rendered when each run starts and again at each new level, seeded by the run seed at the start and by the run seed plus the level after each level-up. _build_music_stream picks a style, tempo, and note patterns from that seed, computes how many samples the loop needs, allocates one PackedByteArray of exactly that size, and writes every sample into it. The result is wrapped in an AudioStreamWAV marked to loop forward, so the music player repeats it without asking the script for more data:
# Simplified from scripts/procedural_audio.gd
func _build_music_stream(run_seed: int) -> AudioStreamWAV:
# ... style, tempo and note patterns from run_seed ...
var total_frames: int = maxi(1, int(round(total_duration * SAMPLE_RATE)))
var data: PackedByteArray = PackedByteArray()
data.resize(total_frames * 2) # 16-bit mono, sized once
for frame in range(total_frames):
var time: float = float(frame) / SAMPLE_RATE
# ... mix lead, bass, pad, drums, delay ...
data.encode_s16(frame * 2, int(round(clampf(final_mix * 0.82, -1.0, 1.0) * 32767.0)))
return AudioStreamFactoryScript.make_wav_stream(data, true, total_frames)
# scripts/audio/audio_stream_factory.gd
static func make_wav_stream(data: PackedByteArray, loop: bool = false, loop_end: int = 0) -> AudioStreamWAV:
var stream: AudioStreamWAV = AudioStreamWAV.new()
stream.mix_rate = SAMPLE_RATE
stream.format = AudioStreamWAV.FORMAT_16_BITS
stream.stereo = false
stream.data = data
if loop:
stream.loop_mode = AudioStreamWAV.LOOP_FORWARD
stream.loop_end = loop_end
return stream
Sound effects follow the same pattern. At startup, _build_sfx_cache renders all eight cues (move, success, failure, menu move, menu confirm, restart, player entry, invert) into a cached_sfx dictionary. Playing one means picking an idle player from a pool of six and assigning the cached stream. Nothing is synthesized on a tap.
The music still reacts to the puzzle, but not by re-rendering. sync_music_state sets a target volume from how close the player's total is to the goal, the level, and the retry count, and _process eases the player's volume toward that target each frame. Only the volume moves; the buffer does not.
That is the memory story. Allocation happens at known moments: once at startup for the effects, once per run and per level for the music. No script runs in the playback path, so there is no per-frame buffer to allocate and release while sound plays, and nothing for the frame loop to pause on.
The one circular buffer
The music has a feedback delay that echoes each sample three steps later. It needs a ring buffer, and that buffer follows the same rule: one PackedFloat32Array, resized and zeroed before the sample loop starts, then read and overwritten in place by an index that wraps.
# Simplified from _build_music_stream
var delay_samples: int = maxi(1, int(round(step_duration * 3.0 * SAMPLE_RATE)))
var delay_buffer: PackedFloat32Array = PackedFloat32Array()
delay_buffer.resize(delay_samples)
delay_buffer.fill(0.0)
var delay_index: int = 0
for frame in range(total_frames):
# ... compute mix ...
var delay_signal: float = delay_buffer[delay_index]
var final_mix: float = mix + delay_signal * 0.38
delay_buffer[delay_index] = mix + delay_signal * 0.38
delay_index = (delay_index + 1) % delay_samples
What broke first: rendering on the main thread (May 31, 2026)
The render-once design was there from the start. The early mistake was where it ran. _ready called _build_sfx_cache() directly, and starting a run assigned the result of _build_music_stream on the main thread. While those loops ran, the game stopped feeding Android's audio output, which timed out and crashed natively inside AudioTrack.
The fix moved both renders to background threads. The effect cache builds on a Thread at startup, and _play_sfx stays silent until _sfx_cache_ready is set. Each new track builds on its own thread, and _process swaps the finished stream in on the main thread:
# Simplified from scripts/procedural_audio.gd
func start_run_music(run_seed: int) -> void:
# ... skip if this seed's track is already playing ...
_music_thread = Thread.new()
_music_thread.start(_build_music_async.bind(run_seed))
func _process(delta: float) -> void:
if _music_thread != null and not _music_thread.is_alive():
var generated_stream = _music_thread.wait_to_finish()
_music_thread = null
if generated_stream != null:
music_player.stop()
music_player.stream = generated_stream
music_player.play()
_exit_tree joins both threads before teardown. The web build is the exception: since July 12, 2026 it renders the effects in _ready and the music inline in start_run_music, because the threaded path left it silent.
Making the render itself cheaper (June 21, 2026)
Threads took the render off the frame. The next change made the render smaller, in three ways.
- Per-step work moved out of the per-sample loop. Note frequencies, chord tones, drum envelope levels, and the section swell only change when the sequencer moves to a new step, but they were recomputed for every sample. They are now recalculated only when the step index changes.
- Wave helpers inlined. Calls like
AudioMathScript.triangle_wave(time * lead_note)became the arithmetic itself, andpow()envelopes became plain multiplications. In GDScript, a function call inside a loop that runs once per sample is a cost worth removing. - Shorter loops. The style presets went from 7 to 10 bars per track down to 4 or 5, roughly halving the samples each track has to compute.
# BEFORE: every sample recomputed per-step values
for frame in range(total_frames):
# ...
var lead_note: float = AudioMathScript.midi_to_frequency(root_note + maxi(lead_note_value, 0))
lead_sample = AudioMathScript.triangle_wave(time * lead_note) * 0.62 # ...
# AFTER: per-step values update only on a new step; waves are inline
for frame in range(total_frames):
# ...
if step_index != last_step_index:
last_step_index = step_index
lead_note = AudioMathScript.midi_to_frequency(root_note + maxi(lead_note_value, 0))
# ... bass, counter, pad, drum envelope bases ...
var decay: float = 1.0 - step_progress
var lead_env: float = (decay * decay) * section_lift
var lead_phase: float = time * lead_note
var lead_tri: float = 1.0 - 4.0 * absf((lead_phase - floorf(lead_phase)) - 0.5)
The same commit added a log line that reports how many milliseconds each track took to synthesize, so the cost stays visible in device logs.
Text outlines: one call instead of a loop (June 21, 2026)
The grid renderer (scripts/game/game_renderer.gd) outlines every number on the board. It used to fake the outline by drawing the string again at every pixel offset in a square around the text, with the corners clipped. At the thickness the board used, 4, that was 68 extra draw_string calls for each number, every time the board was drawn. It now makes one call to Godot's built-in outline renderer:
# BEFORE: redraw the text at each offset
for offset_y in range(-outline_thickness, outline_thickness + 1):
for offset_x in range(-outline_thickness, outline_thickness + 1):
if offset_x == 0 and offset_y == 0:
continue
if absf(float(offset_x)) + absf(float(offset_y)) > float(outline_thickness) * 1.6:
continue
game.draw_string(font, baseline + Vector2(offset_x, offset_y), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline)
# AFTER: one native outline pass
if outline_thickness > 0:
game.draw_string_outline(font, baseline, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_thickness, outline)
game.draw_string(font, baseline, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color)
Takeaways
- For procedural audio, decide when the math runs. Rendering to a buffer at known moments keeps allocation and synthesis out of the playback path.
- On Android, a long render on the main thread is a crash risk, not just a hitch. Run it on a thread and swap the result in on the main thread.
- In a per-sample loop, anything that changes per step belongs outside it.
- Before hand-rolling a visual effect, check whether the engine already draws it in one call.