Skip to main content

Crate weirdnet_audio_engine

Crate weirdnet_audio_engine 

Source
Expand description

The audio output boundary of the engine.

  • AudioSink: starts a stream and calls a render function from the device’s audio thread. Everything above the sink is platform-free; a device backend is one implementation of this trait.
  • ManualSink: a sink with no device, driven by the caller (render_block). It makes engine code deterministic to test.
  • CpalSink (feature cpal): the real device.
  • AudioSource, ManualSource and CpalSource (feature cpal): the input boundary, a capture function called from the device’s input thread.
  • talkback: voices between people on a bus of their own: the mic switch (starts muted), a level gate, a speaker mixer with jitter buffers, volume and mute, and a ducker for the music.
  • Scene, SceneRenderer, render_offline: sounds and sorted events in frames, and the one mixer that plays them: block by block from a device callback, or offline for an export that matches live playback sample for sample.
  • WaveformPlayer: renders one weirdnet_media::waveform::Waveform with play, pause, seek, loop and gain, as the simplest source to play and the reference for what any other source should do.
use weirdnet_audio_engine::{AudioSink, ManualSink, SinkConfig, WaveformPlayer};
use weirdnet_media::waveform::Waveform;

let config = SinkConfig::new(48_000, 1);
let (player, control) = WaveformPlayer::new(Waveform::tone(48_000, 440.0, 480, 0.5)?, &config)?;
let mut sink = ManualSink::new();
let _running = sink.start(config, player.into_render())?;
control.play();
let block = sink.render_block(480).expect("the sink is running");
assert!(block.iter().any(|s| s.abs() > 0.1));

The render function runs on the audio thread: it must not block, lock, or allocate. WaveformPlayer follows that rule and its tests check what can be checked (the callback only reads shared atomics and the sample buffer it owns).

Modules§

talkback
Talkback: voices between people, kept apart from the music.

Structs§

CpalSink
Plays through the default output device of a cpal host: ALSA on Linux (which a PipeWire or PulseAudio server serves through its ALSA plugin), AAudio on Android, and the native host elsewhere.
CpalSource
Captures from the default input device of a cpal host (ALSA on Linux, which PipeWire and PulseAudio serve; AAudio on Android).
Engine
The control half: send commands, read events and the clock. Cheap to share by reference between threads.
EngineAudio
The audio half: owned by the device callback.
EngineOptions
Options for engine.
ManualSink
A sink with no device. ManualSink::render_block plays the part of the device’s callback, so a test decides exactly when and how much audio is pulled. It accepts any valid config.
ManualSource
A source with no device: ManualSource::push hands a block to the running capture function.
PlayerControl
The control-thread side of a WaveformPlayer: cheap to clone, lock-free, every call takes effect at the start of the next block.
Scene
Sounds and the events that play them. Immutable; share it with Arc and hand it to the audio thread whole.
SceneEvent
One sound starting at a frame.
SceneRenderer
Mixes a Scene block by block.
SinkConfig
What a sink is asked for, and what a running one reports.
SinkHandle
A running stream. Dropping it stops the stream. It is not Send, because some platform streams are not.
SinkStats
Counters the backend updates from its threads. Cloning shares them.
Timing
A reading of the engine’s clock, taken at the start of an audio callback.
WaveformPlayer
Plays one Waveform into a sink’s buffers: mono and stereo to any sink with a channel mapping, no resampling. Create it with the sink’s config, keep the PlayerControl, and hand WaveformPlayer::into_render to AudioSink::start. It starts paused.

Enums§

Command
What the control thread asks of the audio thread. Applied at the start of the next device block.
EngineError
Why an engine could not be made.
Event
What the audio thread tells the control thread.
PlayerError
Why a waveform cannot be played on a sink.
SceneError
Why a scene could not be built.
SinkError
Why a sink could not start.

Constants§

MAX_VOICES
The size of the voice pool. When it is full, the oldest voice is stolen.
NO_TAG
A voice’s tag when it was started by a scene event rather than a live press.

Traits§

AudioSink
An audio output. Platform code lives behind this trait and nowhere else.
AudioSource
A capture device.

Functions§

engine
Makes an engine for config. It starts stopped, with an empty scene: presses sound once a scene with sounds is set.
render_offline
Renders frames frames of scene from its start, with the same mixer a device callback uses.
render_to_end
Renders the whole scene, ending on the frame the last voice ends on (Scene::end_frame).

Type Aliases§

CaptureFn
Called with each captured block of interleaved samples.
RenderFn
Fills one buffer of interleaved f32 samples (frames * channels long) for the device. Called on the audio thread.