Engine → Audio → audio_engine.h
audio_engine
| Type | Class |
| Namespace | conjure::audio |
| Include File Path | engine/include/conjure/audio/audio_engine.h |
| Source File Path | engine/src/audio/audio_engine.cpp |
| API ↗ | Clips and decoding, Audio, Playing sounds |
Syntax
Constructors
| Access | Signature | Description |
|---|---|---|
| public | explicit audio_engine(
audio_settings settings); | Never fails: an invalid setting or a backend that cannot start leaves the engine on the null backend with last_backend_error() set. |
Important
Copy constructor and assignment has been deleted.
Destructors
| Access | Signature | Description |
|---|---|---|
| public | ~audio_engine(); | Stops every voice, stops the backend and drains the acknowledgements. |
Variables
| Access | Name | Type | Default | Description |
|---|---|---|---|---|
| private | mState | std::unique_ptr<state> | {} | Private implementation state owning the mixer, command/event rings, voice and group tables, and backend. |
Functions
Public
active_voice_count
active_voice_count[[nodiscard]] u32 active_voice_count() const noexcept;Voices in PRIMING, PLAYING, PAUSED or STOPPING.
apply_params
apply_paramsvoid apply_params(
voice_id voice,
const play_params ¶ms);Applies volume, pitch, pan, loop, spatial flag, priority, group and attenuation of params at once (what apply_source forwards to). An invalid group leaves the voice's group unchanged.
apply_source
apply_sourcevoid apply_source(
voice_id voice,
const audio_source &source,
group_id group);Applies every authored field of an audio_source component to its voice at once (engine.audio_sync calls it for changed<audio_source> chunks): apply_params(voice, play_params_from(source, group)) with group the resolution of source.group through find_group(). No-op on a stale voice id.
backend_kind
backend_kind[[nodiscard]] audio_backend_kind backend_kind() const noexcept;The backend in use (never AUTO).
create_group
create_group[[nodiscard]] core::result<group_id> create_group(
const group_desc &desc);Creates a group under desc.parent (or desc.parentName). Its own paused flag starts clear, but a group created under a paused ancestor is frozen from the start. Errors: GROUP_LIMIT_REACHED, DUPLICATE_GROUP (name taken or null), INVALID_GROUP (unknown parent id or name), GROUP_CYCLE (depth would exceed MAX_GROUP_DEPTH).
end_frame
end_framevoid end_frame();Erases every acknowledged one-shot and publishes the ids that ended this frame through finished_voices(). Main thread.
enumerate_devices
enumerate_devices[[nodiscard]] core::result<std::vector<audio_device_info>> enumerate_devices() const;Playback devices of the current backend.
find_group
find_group[[nodiscard]] group_id find_group(
core::string_id name) const noexcept;The group named name; MASTER_GROUP for the null name; the null id when unknown.
finished_voices
finished_voices[[nodiscard]] std::span<const voice_id> finished_voices() const noexcept;Voices that reached FINISHED or STOPPED during the last frame, each reported once. Valid until the next end_frame().
group
group[[nodiscard]] std::optional<group_info> group(
group_id group) const noexcept;Name, parent, gain, effective gain, own paused flag and effective paused flag (own or any ancestor's) of a live group; nullopt for a stale id. Main thread.
groups
groups[[nodiscard]] std::span<const group_info> groups() const noexcept;Every live group, master first, in slot order. Valid until the next group change.
is_playing
is_playing[[nodiscard]] bool is_playing(
voice_id voice) const noexcept;True for PRIMING, PLAYING or PAUSED. Any thread.
is_valid
is_valid[[nodiscard]] bool is_valid(
voice_id voice) const noexcept;True while the id addresses a voice the engine still holds (any state but FREE). Any thread.
last_backend_error
last_backend_error[[nodiscard]] const core::error *last_backend_error() const noexcept;Why the requested backend was replaced by null, or nullptr when it was not.
listener
listener[[nodiscard]] const listener_state &listener() const noexcept;The listener as last set (after clamping). Main thread.
null_backend_instance
null_backend_instance[[nodiscard]] null_backend *null_backend_instance() noexcept;The null backend when it is the one in use, nullptr on miniaudio.
output_format
output_format[[nodiscard]] audio_format output_format() const noexcept;The format the mixer renders at.
pause
pausevoid pause(
voice_id voice);Holds the cursor (the voice stays is_playing()); no-op on an invalid id or a voice that is not PLAYING. A PRIMING voice starts paused.
play
play[[nodiscard]] core::result<voice_id> play(
const assets::asset_ref<audio_clip> &clip,
const play_params ¶ms = {});Starts a voice on a loaded clip asset (CLIP_NOT_LOADED while get() is null), recording the ref's version for reload detection. Other errors as play(const audio_clip &).
[[nodiscard]] core::result<voice_id> play(
const audio_clip &clip,
const play_params ¶ms = {},
u32 clipVersion = 0);Starts a voice on a clip payload, sharing its PCM or encoded bytes. clipVersion is recorded in voice_info for reload detection. Errors: INVALID_CLIP (clip without its buffer), INVALID_GROUP (params.group stale or unknown), STREAM_SLOT_LIMIT, VOICE_LIMIT_REACHED, DECODE_FAILED (streamed clip that does not open; its stream slot is released again).
[[nodiscard]] core::result<voice_id> play(
std::shared_ptr<const pcm_buffer> pcm,
const play_params ¶ms = {});Starts a voice on raw PCM (tests, tools: no asset manager). INVALID_CLIP for a null or empty buffer.
release
releasevoid release(
voice_id voice);Gives a retained voice back: stops it if it is still playing and erases it once the mixer acknowledged. One-shot voices are released by end_frame() instead (calling release on one is harmless).
remove_group
remove_group[[nodiscard]] core::result<void> remove_group(
group_id group);Reparents children and moves voices to the parent before retiring the id. A pause the group held goes with it: its children and voices follow the parent's effective paused flag from then on. INVALID_GROUP for MASTER_GROUP or a stale id.
resume
resumevoid resume(
voice_id voice);Continues a paused voice from its cursor; no-op on an invalid id or a voice that is not PAUSED. During a pending streamed seek the resume takes effect once it lands. On a voice still PRIMING it cancels startPaused or an earlier pause(): the voice starts playing.
seek
seekMoves the cursor to seconds (clamped to the clip length). A PCM voice already in the mixer jumps at the next block and voice() reports the target at once; a streamed voice pauses, refills its ring from the new position and resumes a few frames later (a later seek before that lands retargets it), and voice() reports the target once the ring is reset and the seek sent to the mixer. A stop() before that drops the seek. When the decoder cannot reach the position, the voice continues from where its decoder stands and voice() reports that frame. No-op on a stale, stopping or ended voice.
select_device
select_device[[nodiscard]] core::result<void> select_device(
std::string_view deviceName);Restarts the backend on the named device (empty = default). Voices are frozen during the restart and continue afterwards. On failure the previous device is kept. An unknown non-empty name falls back to the default device with a WARN and counts as success. If neither the new nor the previous device starts, the engine continues on the null backend with last_backend_error() set and stats().fellBackToNull true; the error of the new device is returned.
set_attenuation
set_attenuationvoid set_attenuation(
voice_id voice,
const attenuation_params ¶ms);Attenuation model and distances of a spatial voice, clamped to minDistance >= 0.01, maxDistance >= minDistance and a rolloff in [0, 10]; applied by the next update(). No-op on a stale id.
set_group
set_groupMoves the voice to a live group; an invalid or stale group id is a no-op with a TRACE, as is a stale voice id.
set_group_gain
set_group_gainGain clamped to [0, 2]; the effective gains of the group and its descendants are recomputed and sent. NaN asserts and becomes 1. Stale id: no-op with a TRACE.
set_group_paused
set_group_pausedvoid set_group_paused(
group_id group,
bool paused);Sets the group's own paused flag. Pause is hierarchical, like gain: a paused group freezes its own voices and those of every descendant group (cursors hold, output is silence), and a group plays again only once neither it nor any ancestor is paused, so releasing a child under a paused parent changes group_info::paused but not group_info::effectivePaused. set_group_paused(MASTER_GROUP, true) therefore freezes every voice; it is a flag of its own, independent of set_paused() and is_paused(). The flags reach the mixer as one command per group, so groups of one subtree may freeze or resume at different times: normally at most one MIX_BLOCK_FRAMES block apart, and with no upper bound when the main thread is interrupted in the middle of the call or the command ring spills. Voices that must stay sample-synchronised belong in the same group, or are held with set_paused(). Stale id: no-op with a TRACE.
set_listener
set_listenervoid set_listener(
const listener_state &listener);The single listener (clamp_listener applied: basis normalised, gain in [0, 2], non-finite values sanitised). Spatial voices pick it up at the next update().
set_loop
set_loopvoid set_loop(
voice_id voice,
bool loop);Loops at the clip end (streamed voices rewind their decoder). No-op on a stale id.
set_pan
set_panPan clamped to [-1, 1], used only while the voice is not spatial (a spatial voice pans from its position). NaN asserts and becomes 0. No-op with a TRACE on a stale id.
set_paused
set_pausedvoid set_paused(
bool paused);Pauses or resumes the whole mixer; cursors hold, output is silence. Independent of the group flags: group_info::effectivePaused does not report it.
set_pitch
set_pitchPitch clamped to [0.25, 4]; the mixer step changes at the next block. NaN asserts and becomes 1. No-op with a TRACE on a stale id.
set_position
set_positionvoid set_position(
voice_id voice,
math::vec3 position,
math::vec3 velocity = math::vec3(0.0f));World position and velocity of a spatial voice; applied by the next update().
set_positions
set_positionsvoid set_positions(
std::span<const voice_transform> transforms);set_position() for every entry; one call per frame from engine.audio_sync.
set_priority
set_priorityVoice-stealing priority; lower is stolen first. No-op on a stale id.
set_spatial
set_spatialvoid set_spatial(
voice_id voice,
bool spatial);Switches between flat playback (pan, unity gain) and 3D playback: attenuation and pan from set_position(), with a stereo clip downmixed to mono. No-op on a stale id.
set_volume
set_volumeVolume clamped to [0, 2], ramped over DEFAULT_RAMP_SECONDS in the mixer. NaN asserts and becomes 1. No-op with a TRACE on a stale id. Main thread, like every setter.
settings
settings[[nodiscard]] const audio_settings &settings() const noexcept;The settings the engine was constructed from.
stats
stats[[nodiscard]] audio_engine_stats stats() const noexcept;Counters; lock-free, any thread (outputFormat is fixed at construction, the rest are relaxed atomics).
stop
stopFades the voice out over fadeSeconds, then it reports STOPPED. No-op for an invalid id or an already ended voice. A voice that is paused, or whose group is frozen (paused itself or through an ancestor group, group_info::effectivePaused), or while the master is paused, ends at once without a fade.
update
updatevoid update(
f32 deltaSeconds);Once per frame: backend update, event drain, spatial pass, streaming pass, spill flush, stats. Main thread.
voice
voice[[nodiscard]] std::optional<voice_info> voice(
voice_id voice) const noexcept;The voice's mirror, or nullopt for an invalid id. Any thread: state and cursor come from atomics the mixer writes (the cursor once the mixer has applied the voice's last PLAY and seek, the start or seek target until then); the other fields are a snapshot the main thread publishes (seqlock) whenever play(), a setter or update() changes them.