Skip to content

Engine

Audio

The audio module mixes voices, routes them through a hierarchy of groups, spatialises them against a listener, and sends the resulting samples to a playback backend. It also supplies clip decoding, the audio clip asset type, and a deterministic null backend for headless applications and tests.

Dependency Graph

Audio module dependencies

Headers

File Description Provides
audio.h Umbrella header of the audio module.
audio_backend.h The backend interface: the piece that owns a device (or a virtual clock) and drives the mix callback. audio_backend
audio_device.h Device descriptions shared by every backend. audio_device_info
backend_desc
audio_engine.h Construction settings, batched voice transforms, diagnostic counters, and the audio engine facade. audio_engine
audio_engine_stats
audio_settings
voice_transform
audio_error.h Error codes of the audio module.
audio_types.h Formats, identifiers and constants shared by every audio header. audio_format
clip.h Clips. audio_clip
clip_info
pcm_buffer
clip_asset.h The conjure.audio.clip cooked blob, import settings, importer and loader. audio_clip_importer
audio_clip_loader
clip_blob_header
clip_import_settings
components.h Component data the engine's audio_subsystem registers into the ECS. audio_listener
audio_source
source_handle
decoder.h Decoding. decode_options
streaming_decoder
log_category.h The audio module's log category ("audio"); defined in engine/src/audio/log_category.cpp.
mix_groups.h Mix groups: a gain tree rooted at MASTER_GROUP. group_desc
group_id
group_info
null_backend.h The headless backend: no device, no thread, no OS calls. null_backend
null_backend_stats
null_group_stats
null_voice_stats
spatialisation.h Listener and spatial maths. attenuation_params
listener_state
spatial_result
voice.h Voice identity and parameters. play_params
voice_id
voice_info

Breakdown

Frame coordination

The audio_engine owns the backend and keeps the main-thread voice and group state separate from the mixer. Its private implementation lives in engine/src/audio/audio_engine.cpp. The application's audio subsystem connects it to scene components; the audio library itself does not depend on ECS.

Call update() once per frame to advance the backend, drain events, reconcile voice state, compute spatial parameters, and service streaming. end_frame() releases completed one-shots and publishes the finished-voice list. The following helper is a complete, compilable example for an already-created engine; call it on the main thread.

#include "conjure/audio/audio_engine.h"

void advance_audio_frame(conjure::audio::audio_engine &audio, conjure::f32 deltaSeconds) {
    audio.update(deltaSeconds);
    audio.end_frame();
}

One mixer, two drivers

engine/src/audio/mixer.cpp renders interleaved floating-point samples without allocating, locking, or logging inside the callback. The miniaudio backend drives the callback from its device thread.

Note

The null_backend drives the same mixer on the main thread using a virtual clock, so headless tests can inspect the output without opening a real device.

Memory and voice lifetime

Voice handles pair an index with a generation, so a reused slot does not make an old handle valid again. The main thread owns a table of 512 voice entries; the mixer has 256 voice slots. The additional entries let a new voice retain its own identity while it waits for a stolen slot to finish fading out.

One-shots are reclaimed by end_frame() after the mixer acknowledges completion. Retained voices stay owned until release() is called. Per-entry atomic state and seqlock snapshots support the documented read-only queries.

Commands, events, and recovery

A single-producer/single-consumer ring sends commands to the mixer. When that ring fills, commands spill into a main-thread vector and are flushed later. Mixer events travel through a second ring; if it overflows, the main thread reconciles terminal states from per-slot atomics. Both directions carry generations to reject stale work.

Device loss freezes progress while the engine retries the device using the configured backoff and attempt count. If recovery cannot restore it, playback continues on the null backend. Counters and the mirrored device state are available through audio_engine_stats.

Gain, spatialisation, and streaming

The main thread combines voice volume, attenuation, and listener gain. The mixer applies the group's effective gain and ramps changes to avoid discontinuities. Mono spatial sources use constant-power panning; pitch combines the clip-to-mix sample-rate ratio, requested pitch, and Doppler ratio.

Streaming voices decode into bounded SPSC rings. An optional job system performs decode work; without it, update() performs that work inline. Seeking coordinates the decoder and mixer through pause, reset, and prime states so a ring is not reset while either side is using it.

Threading Model

Important

The audio_engine is main-thread owned. Construction, destruction, mutators, device selection, group changes, update(), and end_frame() must run there.

is_valid(), is_playing(), voice(), stats(), and output_format() are the documented read-only exceptions.

The real device callback runs on the miniaudio thread. The null backend invokes the same callback on the main thread. Streaming decode jobs finish before their state is released; only one job may produce into a stream's ring at a time.

Warning

Callback work must not allocate, acquire locks, or log. The real device callback runs independently of the main thread, so main-thread-only operations must not be called from it.

Group pause changes are sent as individual commands. A subtree is therefore not paused atomically across different groups; voices requiring sample synchronisation should share a group or use the whole-mixer pause.

Extension Points

Implement audio_backend to supply another playback driver. Keep device handling behind that interface and preserve the callback's format and threading contract.

Caution

A custom backend must stop its callback thread before the engine state is destroyed. Calling the callback with a dangling user pointer has undefined behaviour.

Clip import and loading integrate with the asset database through the audio clip asset type. Scene integration uses audio_source and audio listeners, while the engine's audio subsystem owns the ECS scheduling. Applications can provide a job system through audio_settings to move streaming decode work off the main thread.

Testing

tests/audio/ exercises construction, settings validation, voice and group lifetimes, spatialisation, decoding, streaming, device recovery, and the null backend. Most cases are headless; real-device checks are separately gated.

Tip

For this header, start with tests/audio/test_audio_engine.cpp. It covers backend selection, invalid settings, playback state, bulk updates, and diagnostics.

Structs without a dedicated test file are also exercised by the engine and integration suites; do not infer a test file solely from a type name.

python tools/build.py --target conjure_audio_tests --test -L audio