Namespace OmegaEngine.Audio
The audio subsystem plays back sound effects and background music.
Output device and mixer
Each Engine owns one AudioManager (exposed as Audio). It holds the shared NAudio output device and a mixer that all sounds and music feed into.
All inputs are mixed as 32-bit IEEE-float, stereo, at SampleRate (44.1 kHz). Sources in other formats are resampled and converted to match before they reach the mixer.
Sound effects
Sound effects are decoded up-front into an in-memory buffer of samples, so they can be triggered repeatedly and overlapped without re-reading the file.
- XSound is the cached asset holding the decoded samples. Load it via Get(Engine, string?).
- Sound plays a sound non-positionally. Each call to StartPlayback(bool) feeds an independent playback into the mixer, so the same asset can play multiple times at once.
- Sound3D pans and attenuates a sound in stereo based on its Position in world space relative to a listener.
The listener for 3D playback is whatever IViewpoint is assigned to Listener. Both Camera and View implement that interface, so you can assign either; assigning the View is preferred because it delegates to whichever Camera is currently active, keeping the listener correct across camera swaps and transitions.
Games built on PresenterBase<TUniverse> get this wiring for free. The presenter points the listener at its View when hooked in.
Background music
Song streams a music file from disk rather than decoding it fully into memory. MusicManager (exposed as Music) organizes songs into named themes and handles selection and cross-fading:
- AddSong(string, params string[]) registers a song with one or more themes.
- PlayTheme(string) starts a random song from a theme, fading out any currently playing song.
- Update() advances to the next song in the current theme once the previous one finishes.
If a file named list.txt is located in the Music content directory, it will automatically be parsed and used to populate the list of themes.
Supported formats
Both sounds and songs infer the file format from the file ending:
.wav.mp3.m4a.wma
Signal chain
flowchart LR
subgraph Sources
XSound["XSound (decoded samples)"]
SongFile["Song (streamed file)"]
end
Sound --> Volume
XSound --> Sound
XSound --> Sound3D
Sound3D --> Pan["Pan / attenuate"]
SongFile --> Resample --> Stereo --> Volume
Volume --> Mixer
Pan --> Mixer
Mixer --> Output["Output device"]
API
Classes
- AudioManager
Owns the shared NAudio output device and mixer that all Sounds and Songs feed into.
- MusicManager
Manages the playback of Song in the background controlled by themes.
- Song
A streamed sound that is played in the background as music.
- Sound
A memory-cached sound that is played on-demand.
- Sound3D
A memory-cached sound that is played on-demand simulating a position in 3D-space.
Interfaces
- IAudio
Represents an asset that can playback audio.
Enums
- AudioCategory
Categorizes audio playback so it can be routed through the matching global volume bus in AudioManager.