Skip to main content

Module audio

Module audio 

Source
Expand description

The audio threaded world: a callback-driven output stream fed by a ring.

The audio thread owns the stream and a lock-free ring buffer; the operating system’s callback pops whatever is newest and silences the rest, so a late simulation degrades to silence instead of stuttering. What sound plays is decided by the world’s OnAudioFill components: the thread runs them once per filled chunk and sums their samples into the ring. The output’s format is not known until its stream opens, so the thread spawns it as an AudioFormat component, marks the world with AudioOutputOpen, and a caller subscribes to either — the format to build its sources at the rate the device really uses, the marker to know when the world can sound at all.

Re-exports§

use std::cell::RefCell; 🔒
use std::time::Duration; 🔒
use cpal::traits::DeviceTrait; 🔒
use cpal::traits::HostTrait; 🔒
use cpal::traits::StreamTrait; 🔒
use ringbuf::traits::Consumer; 🔒
use ringbuf::traits::Observer; 🔒
use ringbuf::traits::Producer; 🔒
use ringbuf::traits::Split; 🔒
use ringbuf::HeapProd; 🔒
use ringbuf::HeapRb; 🔒
use unlit_ecs::Entity; 🔒
use unlit_ecs::World; 🔒
use unlit_ecs::bundle; 🔒
use crate::handle::WorldHandle; 🔒
use crate::job::JobSender; 🔒
use crate::job::SendError; 🔒
use crate::thread::FixedClock; 🔒
use crate::world::WorldRunner; 🔒

Structs§

AudioChunk
One chunk of interleaved audio a fill behaviour contributes to.
AudioConfig
How the audio world is set up before its first fill.
AudioFillOrder
Where a fill behaviour runs among the world’s other sources.
AudioFillSlot 🔒
The state every fill behaviour shares: its callback and its order.
AudioFormat
The format the audio world’s output stream opened at.
AudioOutputOpen
The marker the audio world carries once its output is open.
AudioOutputRequest
The marker an AudioOutputMode::OnRequest audio world carries once a request_audio_output job has reached it. It stays for the world’s life, so a subscriber can tell a world still waiting for its page’s first click from one that has been asked and is only missing a device.
GlobalAudioGain
The factor the mixed samples scale by, read from the audio world.
OnAudioFill
A source of audio samples: a component the audio thread runs once per filled chunk.
OpenOutput 🔒
Everything the open output stream needs to keep running.

Enums§

AudioOutputMode
How an audio world gets its output, whichever way it is driven.

Constants§

AUDIO_FILL_FRAMES 🔒
How many frames the filler generates per pass, the unit the pacing and the ring’s headroom are measured in.
SOURCES 🔒
The source list dispatch_audio_fill reuses across fills.

Functions§

dispatch_audio_fill
Run every OnAudioFill in world once, mixing their samples into out.
open_output 🔒
Open the default output stream and the ring feeding it, preferring a stereo f32 stream at the configured rate.
request_audio_output
Ask sender’s audio world to open its output device.
spawn_audio_world_with
Spawn the audio world driven by R: on its own thread (Threaded), or stepped by the host (Stepped).

Type Aliases§

AudioFillCallback 🔒
The callback a fill behaviour runs: it adds its source’s samples to the chunk it is handed.