AudioSystem: Architecture and Internals
Use this skill as an architectural playbook for AudioSystem :)
Keep your focus on fresh code and check its relevance through git blame (cutoff: 2024-02-19).
What to download first
references/fresh-pattern-catalog.md- confirmed fresh patterns ✅references/rejected-snippets.md- old and risky zones that cannot be copied ⚠️references/docs-context.md- what to take from docs and where docs can become outdated
Source of truth
- The codebase is the primary source of truth.
- Documentation - secondary: for terms, intent and hypothesis testing.
- Any site older than two years or with obvious TODO/problematic comments should not be included in the rules as a “standard”.
AudioSystem mental model
SharedAudioSystemspecifies the contract: creation/stopping, state, positioning, audience routing.AudioComponent— network state of the audio stream (state + params + filtering + binding to auxiliary).- The server creates audio entities and decides who to replicate them to (filter/PVS/override), but does not play the actual audio.
- The client creates a real
IAudioSource, appliesAudioParams, updates the position/occlusion and manages the playback. - For positional sound, the key loop is: calculate position -> check distance -> evaluate occlusion -> update gain/velocity.
- Effects come in a separate chain:
AudioSource -> Auxiliary Slot -> AudioEffect (EAX Reverb). - When EFX is enabled, occlusion works through a lowpass filter; without EFX - through fallback by gain.
- For streams, “instant processing after start” is important so that the sound does not live for one tick with an incorrect position/occlusion.
Layer scheme
- Shared layer:
ResolveSound, SetupAudio, SetState, SetPlaybackPosition, SetMapAudio, SetGridAudio, Stop.
- Server layer:
selection of recipients, PlayPvs/PlayEntity/PlayStatic/PlayGlobal, PVS override for map/grid audio.
- Client layer:
FrameUpdate, ProcessStream, GetOcclusion, audio-limit, stream playback.
- OpenAL layer:
AudioManager, BaseAudioSource, AudioEffect, AuxiliaryAudio.
Patterns
- For locally predicted interactions, use
PlayPredicted(..., user)instead of “pure”PlayPvs. - For map/grid special sounds, use
SetMapAudio/SetGridAudio, and not manual emulation through random filters. - For streams launched in the middle of a tick, immediately recalculate the spatial state (position/occlusion/velocity).
- For complex custom acoustics, connect
ProcessStreamOverride/GetOcclusionOverridewith one subscriber. - For OpenAL effects, assemble a bunch:
CreateEffect -> CreateAuxiliary -> SetEffect -> SetAuxiliary. - For repeated short SFX, consider the limit concurrent sound, otherwise you can “eat” the source budget.
- For long-running music/event sound, store stream
EntityUid?and manage the state viaSetState/Stop. - For global station/card music, create an audience through a server filter, not on the client.
Anti-patterns
- Rely on the
LoadStreamsections in the server implementation as a working API path. - Copy old zones with TODO about range/PVS/Transform as a template for new logic.
- Attach an effect through a race timer without source life cycle guarantees (fragile race 😬).
- Mutate the volume/gain into a tight loop without guard checks and without understanding EFX fallback behavior.
- Use the same implementation for predicted and non-predicted cases without explicit user.
- Consider docs as the “truth of behavior” where the code has already gone ahead.
OpenAL/EFX: what is important to understand
AudioManageropens the OpenAL device/context and defines support forALC_EXT_EFX.AudioSourcecontrols the AL source (Gain,Pitch,Position,Velocity,SecOffset).AuxiliaryAudio— effect slot into whichAudioEffectis attached.AudioEffectmaps reverb fields (Density,Decay*,Echo*,Modulation*,HF/LF references) in EFX parameters.- For EFX occlusion, lowpass (
LowpassGain,LowpassGainHF) is used, otherwise gain-fallback.
Code examples
1) Stream + immediate spatial recalculation
// After the start of the stream in the middle of a tick, we immediately recalculate spatial,
// otherwise one tick you can hear the sound in the wrong position.
var playing = CreateAndStartPlayingStream(audioParams, specifier, stream);
SetCoordinates(playing.Entity, new EntityCoordinates(sourceEntity, Vector2.Zero));
ProcessStream(
playing.Entity,
playing.Component,
Transform(playing.Entity),
GetListenerCoordinates());
2) Grid audio for large moving objects
// We mark the sound as grid-audio: grid center + extended range + no occlusion.
var stream = PlayPvs(startupSound, shuttleUid);
SetGridAudio(stream);
3) Ping compensation for playback position
// To synchronize the music stream, we take into account the network delay of the session.
var offset = session.Channel.Ping * 1.5f / 1000f;
SetPlaybackPosition(jukeboxStream, requestedSongTime + offset);
4) EFX reverb chain
// Create an effect + auxiliary, apply a preset and connect to the source.
var effect = CreateEffect();
var aux = CreateAuxiliary();
SetEffectPreset(effect.Entity, effect.Component, presetProto);
SetEffect(aux.Entity, aux.Component, effect.Entity);
SetAuxiliary(audioUid, audioComp, aux.Entity);
5) Server predicted-flow without duplicates for the initiator
// The server sends audio via PVS, but excludes the initiator,
// because the initiator has already predicted locally.
var audio = PlayPvs(ResolveSound(sound), sourceUid, audioParams);
if (audio != null)
audio.Value.Component.ExcludedEntity = user;
Extension rule
- First build new mechanics on fresh entry points (
stream overloads,SetGridAudio/SetMapAudio, override hooks). - If you have to touch the legacy zone, first record the risk in
references/rejected-snippets.md. - Confirm any audio optimization by profiling and listening in a real round.
- For effects, first check EFX support, then design fallback behavior.
Think of AudioSystem as a network contract + client DSP pipeline, not as a “play file” 🎧