Skip to content

Engine → Audio → audio_engine.h

audio_engine

Syntax

class CONJURE_API audio_engine

Constructors

AccessSignatureDescription
publicexplicit 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

AccessSignatureDescription
public~audio_engine();Stops every voice, stops the backend and drains the acknowledgements.

Variables

AccessNameTypeDefaultDescription
privatemStatestd::unique_ptr<state>{}Private implementation state owning the mixer, command/event rings, voice and group tables, and backend.

Functions

Public

active_voice_count

[[nodiscard]] u32 active_voice_count() const noexcept;

Voices in PRIMING, PLAYING, PAUSED or STOPPING.

apply_params

void apply_params(
    voice_id voice,
    const play_params &params);

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

void 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

[[nodiscard]] audio_backend_kind backend_kind() const noexcept;

The backend in use (never AUTO).

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

void end_frame();

Erases every acknowledged one-shot and publishes the ids that ended this frame through finished_voices(). Main thread.

enumerate_devices

[[nodiscard]] core::result<std::vector<audio_device_info>> enumerate_devices() const;

Playback devices of the current backend.

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

[[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

[[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

[[nodiscard]] std::span<const group_info> groups() const noexcept;

Every live group, master first, in slot order. Valid until the next group change.

is_paused

[[nodiscard]] bool is_paused() const noexcept;

True after set_paused(true). Main thread.

is_playing

[[nodiscard]] bool is_playing(
    voice_id voice) const noexcept;

True for PRIMING, PLAYING or PAUSED. Any thread.

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

[[nodiscard]] const core::error *last_backend_error() const noexcept;

Why the requested backend was replaced by null, or nullptr when it was not.

listener

[[nodiscard]] const listener_state &listener() const noexcept;

The listener as last set (after clamping). Main thread.

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

[[nodiscard]] audio_format output_format() const noexcept;

The format the mixer renders at.

pause

void 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

[[nodiscard]] core::result<voice_id> play(
    const assets::asset_ref<audio_clip> &clip,
    const play_params &params = {});

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 &params = {},
    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 &params = {});

Starts a voice on raw PCM (tests, tools: no asset manager). INVALID_CLIP for a null or empty buffer.

release

void 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

[[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

void 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

void seek(
    voice_id voice,
    f64 seconds);

Moves 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

[[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

void set_attenuation(
    voice_id voice,
    const attenuation_params &params);

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

void set_group(
    voice_id voice,
    group_id group);

Moves 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

void set_group_gain(
    group_id group,
    f32 gain);

Gain 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

void 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

void 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

void set_loop(
    voice_id voice,
    bool loop);

Loops at the clip end (streamed voices rewind their decoder). No-op on a stale id.

set_master_gain

void set_master_gain(
    f32 gain);

set_group_gain(MASTER_GROUP, gain).

set_pan

void set_pan(
    voice_id voice,
    f32 pan);

Pan 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

void 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

void set_pitch(
    voice_id voice,
    f32 pitch);

Pitch 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

void 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

void set_positions(
    std::span<const voice_transform> transforms);

set_position() for every entry; one call per frame from engine.audio_sync.

set_priority

void set_priority(
    voice_id voice,
    u8 priority);

Voice-stealing priority; lower is stolen first. No-op on a stale id.

set_spatial

void 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

void set_volume(
    voice_id voice,
    f32 volume);

Volume 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

[[nodiscard]] const audio_settings &settings() const noexcept;

The settings the engine was constructed from.

stats

[[nodiscard]] audio_engine_stats stats() const noexcept;

Counters; lock-free, any thread (outputFormat is fixed at construction, the rest are relaxed atomics).

stop

void stop(
    voice_id voice,
    f32 fadeSeconds = DEFAULT_RAMP_SECONDS);

Fades 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.

stop_all

void stop_all(
    f32 fadeSeconds = DEFAULT_RAMP_SECONDS);

stop() for every live voice.

update

void update(
    f32 deltaSeconds);

Once per frame: backend update, event drain, spatial pass, streaming pass, spill flush, stats. Main thread.

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.