Skip to main content

Module audio

Module audio 

Source
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§

AudioClip
Decoded PCM held in memory, ready to play with no further work.
NoopAudioPlayer
The player used when no backend is installed.
PlaybackParams
How one voice should sound.
SoundBank
A set of cues loaded once and kept alive for as long as the composable that remembered it stays in the composition.
SoundBankEntry
A loaded cue inside a SoundBank.
SoundBankFailure
A cue that did not load, reported instead of silently vanishing.
SoundId
Identifies a decoded clip held by the audio engine.
SoundSpec
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§

AudioBus
The two mix buses every app gets, so “sound on” and “music on” are one call each rather than bookkeeping over individual voices.
AudioError
Why an audio call could not be honoured.

Traits§

AudioPlayer
Plays sound. Installed by the platform backend; the default is a no-op.

Functions§

ProvideAudio
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.
rememberSoundBank
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§

AudioPlayerRef
Shared handle to the installed AudioPlayer.