Audio Backends
The engine's audio is an API plus a backend. The API (renzora_audio, on the contract layer renzora_audio_api) owns everything a scene and a script see; the backend behind the renzora::audio_backend::Backend trait is what actually makes sound. The engine ships one backend, renzora_audio_backend, linked into the binary.
Why the backend is separate
Platforms need genuinely different implementations. The native backend is cpal plus Renzora's own mixer. A browser wants WebAudio instead, where the graph, the panner and the decoders come from the browser and nothing is compiled into the wasm. The two share a contract, not code.
A game that makes no sound should not carry a mixer. renzora_runtime's audio feature pulls in both renzora_audio and renzora_audio_backend; a lean export without it contains no device layer, no decoders and no DSP.
The bundled backend is a cargo feature rather than an installable file because the whole audio API is inert without a backend: a game missing a loose library would run perfectly and make no sound, with no error to explain it. A feature strips the mixer just as completely and cannot go missing by accident.
What each side owns
The engine (renzora_audio) | the bus graph, the components scenes save, the command queue, emitter bookkeeping, and all file I/O |
| The backend | decoding, mixing, panning, distance attenuation, effects, the device, capture |
The backend speaks in handles, samples and bus keys, and knows nothing about entities, asset paths, transforms or the editor.
The engine keeps file I/O
A backend never opens a path: load_clip is handed the bytes and an extension hint. Exported games read assets out of an .rpak archive through a loader the engine owns, so a backend calling std::fs would work in the editor and fail in every shipped game, the worst place for that difference to appear.
Capabilities are answered, not assumed
init returns a BackendInfo carrying a Caps bitfield: CAPTURE, SPATIAL, FEEDS, DEVICE_LIST. The engine never asks a backend for something it did not claim, so claim honestly: a backend that says it captures and then returns nothing makes a game that is silently wrong.
A device that will not open
If init returns an error, the engine keeps the backend and calls init again about every two seconds until it succeeds. An output device is often briefly missing (while the OS switches the default to a headset that has just connected, say), and dropping the backend would leave the session silent. So init must be safe to call again after a failure: open the device first and change nothing else until that has worked.
init runs on its own thread (audio-open-device), not the main thread, and the backend is adopted on the first frame after it returns: opening a device can take tens of milliseconds, and with no device present that would be a hitch every two seconds. Nothing else is called on the backend until init has returned.
Writing one
use bevy::prelude::*;
use renzora::audio_backend::*;
#[derive(Default)]
struct MyMixer {
// ...
}
impl Backend for MyMixer {
fn name(&self) -> &str {
"my_mixer"
}
fn init(&mut self) -> Result<BackendInfo, String> {
todo!("open a device")
}
fn load_clip(&mut self, clip: u64, extension: &str, bytes: &[u8]) -> Result<ClipInfo, String> {
todo!()
}
fn play(&mut self, request: &PlayRequest) -> Result<(), String> {
todo!()
}
fn update(&mut self, request: &UpdateRequest) -> UpdateReply {
todo!()
}
// everything else has a default
}
pub struct MyAudioPlugin;
impl Plugin for MyAudioPlugin {
fn build(&self, app: &mut App) {
app.add_audio_backend(MyMixer::default());
}
}
renzora::plugin!(MyAudioPlugin, Runtime);
Five methods are required. shutdown, set_buses, unload_clip, stop, the capture trio (open_capture, close_capture, read_capture), push_frames, list_devices and set_output_device all have defaults, so a backend that only plays clips implements the five and reports the capabilities it has.
A backend claiming DEVICE_LIST should also implement set_output_device, which a game calls to play through a device the player picked. It returns a BackendInfo for the device now playing, since another device can run at another sample rate. Keep the choice across a later init, so a device that drops out and returns reopens on the one chosen, and leave the current device playing when the name is not found.
Backend is Send but not Sync: a mixer usually holds a lock-free producer that cannot be shared across threads, and the engine reaches it through &mut, so there is no lock on the path.
One backend is used. A second would open the same output device and mix over the first, so add_audio_backend keeps the first registration and logs an error for any other. On a build with the audio feature the bundled backend already holds that slot: replacing it means building with the feature off (or a lean export that leaves it out), not adding a plugin.
The bundled backend
crates/renzora_audio_backend (RenzoraAudioPlugin) is cpal for the device, symphonia for decoding, and Renzora's own mixer, spatialiser and effects. Decoders are cargo features: ogg, wav and mp3 are on by default and flac is opt-in.
- Clips are decoded up front. A three-minute stereo track costs roughly 60 MB resident as
f32. In exchange the mixer has no I/O in it (no decode thread, no underrun path), and it is the shape that works unchanged where there are no threads to decode on. - The mixer runs on the audio thread, fed through a lock-free queue, and finished voices are handed back to be freed on the game thread. A mutex would let the device callback wait on a descheduled game thread, which is not a slow frame but an audible click.
See also
- Audio: using sound in a game
- Script Backends: the same trait-plus-registration shape, for languages