audio-scene
Garnet Audio Scene
Positional audio for a Garnet::World: listeners and emitters are components, one-shot sounds are events.
With Audio::Plugin, whose mixer runs on its own thread, create the plugin without a mixer. The World sends listener and emitter transforms and cue commands through Audio::Output; placement runs on the mixer thread.
world.add_plugin(Garnet::Audio::Scene::Plugin.new(three_dimensional: true)) # adds Audio::Plugin
camera << Garnet::Audio::Scene::Listener.new
speaker << Garnet::Audio::Scene::LoopSource.new(hum, range: 24.0, gain: 0.6_f32)
output = world.resource(Garnet::Audio::Output)
engine = output.play_at(engine_loop, Garnet::Audio::Position.new(0, 0, 0), loop: true)
car << Garnet::Audio::Scene::SpatialEmitter.new(engine, range: 30.0, owned: true)
world.emit(Garnet::Audio::Scene::Cue.new(thud, at: crate, gain: 0.8, delay: 0.05, voice_limit: 96))
In this mode loops play on the ambience channel and cues on channel: (effects by default). Inaudible placed cues finish as CompletionKind::Skipped.
With a mixer on the World thread (offline rendering, or a custom device pump), pass it in:
audio = Garnet::Audio::Mixer.new
world.add_plugin(Garnet::Audio::Scene::Plugin.new(audio, three_dimensional: true))
camera << Garnet::Audio::Scene::Listener.new
speaker << Garnet::Audio::Scene::LoopSource.new(hum, range: 24.0, gain: 0.6_f32)
voice = audio.play(engine_loop, gain: 0_f32, loop_frames: {0, engine_loop.frames})
car << Garnet::Audio::Scene::SpatialEmitter.new(voice, range: 30.0)
# Anywhere in gameplay:
world.emit(Garnet::Audio::Scene::Cue.new(thud, at: crate, gain: 0.8))
world.emit(Garnet::Audio::Scene::Cue.new(click)) # unplaced UI sound
world.schedule.add(:audio, order: 10, name: "audio.pump") do
audio_device.pump(audio)
audio.drain_events
end
Plugin
The "audio.spatial" system runs in the Audio phase, after simulation and before render preparation. Each frame it:
- reads every
Listenerentity's world transform (2D or 3D, chosen bythree_dimensional:). A listener faces its local forward axis and hears its local positive X axis on the right. Several listeners mix by the loudest level; - places every
SpatialEmittervoice (squared falloff overrangescene units). The game owns those voices and decides when they stop; - reconciles
LoopSourceloops: audible sources loop whole clips, keeping their position as listeners move; inaudible, removed or replaced sources stop; newly audible sources start at frame zero; full voice capacity is retried next frame; - plays this frame's
Cueevents.Cue.new(sound, at: entity_or_position, gain:, pitch:, range:, delay:, voice_limit:, bus:, channel:)places the sound at an entity's current world position or an explicitAudio::Position; withoutat:it plays centered.delay:starts it later on the mixer clock;voice_limit:skips it while that many voices play. Inaudible cues, and cues with no free voice, are skipped.
With a world-thread mixer, the Audio::Mixer and Audio::SpatialMixer are world resources; pump the device after the plugin (a higher order in the Audio phase). With Audio::Output the system runs at order -10, before Audio::Plugin hands the frame's commands to the mixer thread. Removing the plugin stops its loops and leaves caller-owned sounds alone.
One spatial emitter API
SpatialEmitter is the spatial emitter. It holds a mixer Voice (world-thread mixer) or an Audio::Cue (Audio::Output). The older Emitter component (linear falloff, one 2D listener) and Scene.sync(mixer, listener) are deprecated and kept only for existing callers. To migrate, replace Emitter.new(voice, distance: 320_f32) with SpatialEmitter.new(voice, range: 320.0), add Listener to the listener entity and install the plugin.
Without the plugin
Scene.bind(world, spatial, [camera]) and Scene.sync(world, spatial, [camera], three_dimensional: true) update SpatialEmitter voices for an explicit listener list. Playback.new(world, spatial, [camera]) with playback.bind or playback.update also reconciles LoopSource loops; close it at scene teardown on the mixer owner thread. For sources outside the ECS, call SpatialMixer#play or SpatialMixer#place with explicit positions.
audio-scene
- 0
- 0
- 0
- 0
- 2
- about 1 hour ago
- November 30, 2026
MIT License
Sun, 06 Dec 2026 03:15:31 GMT