Skip to main content

Module playback

Module playback 

Source
Expand description

Play decoded PCM out a speaker, via cpal (CoreAudio / WASAPI / ALSA).

The playback counterpart to capture, and where a decode::Consumer ends up:

use moq_audio::playback;

let engine = playback::Engine::open(playback::Config::default()).await?;
let mut input = playback::Input::default();
input.sample_rate = audio.sample_rate();
input.layout = audio.layout();

let mut sink = engine.sink(input)?;

while let Some(event) = audio.read().await? {
    let moq_audio::decode::Read::Frame(frame) = event else { continue };
    let write = sink.write(&frame.data)?;
    if write.dropped_sample_frames > 0 {
        eprintln!("dropped {} live audio frames", write.dropped_sample_frames);
    }
}
sink.finish().await;

Engine owns the output device and Sink is one stream playing through it. That split is deliberate: a call with several participants mixes into one device stream rather than opening one per speaker, which is what the device wants and what echo cancellation will need a single mixed reference from.

The device is not the caller’s problem. Opening it, renegotiating its format, resampling to its rate, and reopening it after it disappears all happen on a driver thread behind Engine; a Sink keeps taking writes throughout, and its samples land wherever the device currently is.

Structs§

Config
Playback configuration.
Control
A cheap, clonable handle to one Sink’s volume and level.
Device
An audio output reported by devices.
Drain
A finished sink that stays in the mix until its queued samples have played.
Engine
An open output device, mixing up to 64 Sinks into it.
Gap
A run of consecutive quanta the mix could not fill.
Input
The PCM layout a Sink accepts.
Observation
What an Observer has seen: running totals, and the events since the last read.
Observer
Reads what an Engine played, quantum by quantum.
Sink
One stream of PCM being played, mixed with every other sink on the device.
Stall
A change in whether anything was playing, on Observation::clock.
Write
The sample frames accepted and dropped by one Sink::write.

Functions§

devices
List the audio outputs, across every host API the platform offers.