Expand description
Sound effects and music — the audio analogue of haptics.
The shape mirrors the rest of the service registry: a capability trait, an
Rc handle, a thread-local platform slot a backend installs into, and a
CompositionLocal descendants read. The compiled-in default is a no-op that
still hands out real SoundIds, so an app composes and runs unchanged on
a target with no audio backend.
ⓘ
const CUES: &[SoundSpec] = &[
SoundSpec::new("hit", include_bytes!("hit.wav")),
SoundSpec::new("music", include_bytes!("theme.wav")),
];
#[composable]
fn Game() {
let bank = rememberSoundBank(CUES);
let combo = useState(|| 0u32);
// A combo counter raises the pitch of the same cue.
bank.play_with(0, PlaybackParams::new().pitch_semitones(combo.value() as f32));
}§Threading
AudioPlayer is a UI-thread handle (Rc, like every other service here).
A real backend owns a real-time audio thread; the handle only enqueues
commands for it. Decoding happens in AudioPlayer::load, never during
playback, so a cue that fires every few frames costs one queue push.
Structs§
- Audio
Clip - Decoded PCM held in memory, ready to play with no further work.
- Noop
Audio Player - The player used when no backend is installed.
- Playback
Params - How one voice should sound.
- Sound
Bank - A set of cues loaded once and kept alive for as long as the composable that remembered it stays in the composition.
- Sound
Bank Entry - A loaded cue inside a
SoundBank. - Sound
Bank Failure - A cue that did not load, reported instead of silently vanishing.
- SoundId
- Identifies a decoded clip held by the audio engine.
- Sound
Spec - One entry in a
SoundBank: where the bytes come from and how the cue should sit in the mix before per-call parameters are applied. - VoiceId
- Identifies one playing voice, so a looping cue can be stopped or retuned while it runs.
Enums§
- Audio
Bus - The two mix buses every app gets, so “sound on” and “music on” are one call each rather than bookkeeping over individual voices.
- Audio
Error - Why an audio call could not be honoured.
Traits§
- Audio
Player - Plays sound. Installed by the platform backend; the default is a no-op.
Functions§
- Provide
Audio - Provides the installed audio player to
content. - clear_
platform_ audio - Removes any registered platform audio player (tests and teardown).
- default_
audio - The installed platform player, or a no-op one.
- local_
audio - The CompositionLocal descendants read to reach the audio player.
- remember
Sound Bank - Loads a set of cues once and keeps them alive across recompositions.
- set_
platform_ audio - Installs a platform audio player, replacing any previous one.
Type Aliases§
- Audio
Player Ref - Shared handle to the installed
AudioPlayer.