Stop the first play of each sound paying for its own load

Reported from the game: sounds sometimes start a bit late. The samples were
the suspect, so they were measured first - and they are clean. True digital
silence at the head of each file:

  seven of nine start on sample 1
  sfx_gas_blast    0.2ms
  sfx_black_hole   1.5ms, then a deliberate swell

The black hole's slow attack is the sound it was written to be - make_sfx.py
says it "swells across most of its length instead of decaying from the first
sample" - so there is nothing to trim without breaking it.

The delay was in the playback path, and "sometimes" was the clue:

  play() called load() every time. A cache hit after the first, but the
  FIRST play of each sound was a synchronous read and decode on the frame
  the sound was asked for. Ten sounds, ten occasional late starts.

  _pool() built its twelve AudioStreamPlayer nodes lazily, on the first
  play() call - one bigger stall, every session.

The catalog is preloaded now, so play() never touches the disk, and
Sfx.warm() builds the voices next to Music.ensure() at startup.

Case F pins the thing that was originally suspected, since make_sfx.py
regenerates these and a lazy envelope would hide the delay somewhere nobody
would look. It reads the source .wav's RIFF chunks rather than the imported
stream: these import as QOA (compress/mode=2), so AudioStreamWAV.data is not
PCM. The first version of that check was vacuous for exactly that reason and
passed at a 0.5ms threshold; it now reports "9 samples checked, worst attack
sfx_black_hole.wav at 1.5ms" and fails when tightened.

Not touched: project.godot has no [audio] section, so output_latency is at
its default. On Android that buffer is usually the dominant latency, but
lowering it risks crackle on real hardware - worth doing with someone
listening, not blind.

172/172.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019AJMdB7iuHuLFZM1aBu3GD
This commit is contained in:
Claude 2026-09-09 11:40:28 +02:00
commit 37e2e89cd3
4 changed files with 130 additions and 14 deletions

View file

@ -34,17 +34,23 @@ const ASTEROID_BREAK: String = "asteroid_break"
const GAS_BLAST: String = "gas_blast"
const BLACK_HOLE: String = "black_hole"
# PRELOADED, not paths. play() used to load() on every call, which is a
# cache hit after the first time but a synchronous disk read and decode the
# first time - on the frame the sound was asked for. Ten sounds meant ten
# occasional late starts, one per sound, which is what "sometimes starting a
# bit late" was. Swapping in a different file is still just replacing the
# .wav; nothing here knows or cares which it is.
const SOUNDS: Dictionary = {
SHOT: "res://audio/sfx_shot.wav",
SHIP_DEATH: "res://audio/sfx_ship_death.wav",
ENEMY_DEATH: "res://audio/sfx_ship_death.wav",
BUILDING_DEATH: "res://audio/sfx_building_death.wav",
DIALOG: "res://audio/sfx_dialog.wav",
BOSS_SCREAM: "res://audio/sfx_boss_scream.wav",
PICKUP: "res://audio/sfx_pickup.wav",
ASTEROID_BREAK: "res://audio/sfx_asteroid_break.wav",
GAS_BLAST: "res://audio/sfx_gas_blast.wav",
BLACK_HOLE: "res://audio/sfx_black_hole.wav",
SHOT: preload("res://audio/sfx_shot.wav"),
SHIP_DEATH: preload("res://audio/sfx_ship_death.wav"),
ENEMY_DEATH: preload("res://audio/sfx_ship_death.wav"),
BUILDING_DEATH: preload("res://audio/sfx_building_death.wav"),
DIALOG: preload("res://audio/sfx_dialog.wav"),
BOSS_SCREAM: preload("res://audio/sfx_boss_scream.wav"),
PICKUP: preload("res://audio/sfx_pickup.wav"),
ASTEROID_BREAK: preload("res://audio/sfx_asteroid_break.wav"),
GAS_BLAST: preload("res://audio/sfx_gas_blast.wav"),
BLACK_HOLE: preload("res://audio/sfx_black_hole.wav"),
}
# Per-sound level. A shot is fired constantly and a death happens once, so
@ -81,6 +87,16 @@ const PITCH_SPREAD: Dictionary = {
}
# Builds the voice pool before anything needs it. _pool() is lazy, so without
# this the first sound in a session paid for twelve AudioStreamPlayer nodes
# being created on the frame it was asked for - the worst late start of all,
# and the one that happens every session.
static func warm(tree: SceneTree) -> void:
if tree == null:
return
_pool(tree)
static func _pool(tree: SceneTree) -> Node:
var existing: Node = tree.root.get_node_or_null(NODE_NAME)
if existing != null:
@ -103,7 +119,7 @@ static func _pool(tree: SceneTree) -> Node:
static func play(tree: SceneTree, id: String) -> void:
if tree == null or not SOUNDS.has(id):
return
var stream: AudioStream = load(String(SOUNDS[id]))
var stream: AudioStream = SOUNDS[id] as AudioStream
if stream == null:
return
var pool: Node = _pool(tree)