weirdnet-audio-engine 0.4.0

Audio engine pieces for Weirdnet: the AudioSink trait, a manual sink for tests, a cpal device sink (feature `cpal`) and a waveform player.
Documentation

weirdnet-audio-engine

crates.io docs.rs

Audio output for Weirdnet by Weird Brains: the boundary between audio code and a device, and a player to put the first sound through it. Platform code lives behind one trait; everything above it is platform-free and testable without a sound card.

  • AudioSink: default_config and start(SinkConfig, RenderFn). The render function fills an interleaved f32 buffer on the audio thread and must not block, lock or allocate. A sink that cannot run the exact config returns SinkError::UnsupportedConfig (with the device default attached) instead of silently running at another rate, so the render function never guesses its format. The stream stops when the SinkHandle is dropped; SinkStats counts callbacks, xruns and errors.
  • ManualSink: no device. The caller pulls blocks with render_block and render_blocks, so engine code is deterministic to test. Buffers start filled with NaN, so a render function that forgets to write shows up.
  • Scene and SceneRenderer: a scene is sounds (Waveforms) plus events sorted by frame (SceneEvent: a sound, a gain, an optional gate length). SceneRenderer::render mixes it block by block, splitting each block at event boundaries so an event starts on its exact frame, from a fixed pool of 64 voices (the oldest is stolen when the pool is full). A gated voice fades out over the scene's release. The render path does not allocate (tests/no_alloc.rs), and the block size never changes a sample.
  • Scene::with_loop, live voices and the live engine: engine(config, options) returns an Engine for the control thread and an EngineAudio for the device callback. Commands (Press with a tag, Release, Play, Pause, Locate, SetScene, SetGain, Panic) go in through a bounded lock-free queue and apply at the start of the next block; Events (SceneRetired, VoiceStarted, VoiceEnded, Rejected) come back. A new scene swaps in at a block boundary with the cursor kept, voices of the old scene finish on its own sounds, and the old scene is handed back to be dropped off the audio thread. A looping scene wraps on its exact frame, and a loop played live matches render_offline sample for sample at any block size. The dry bus (the voices) has an optional tap, the monitor bus is what the device plays, and Timing maps output frames to a monotonic clock. The live render path does not allocate.
  • render_offline and render_to_end: the same mixer without a device, so an export matches live playback sample for sample. Scene::end_frame is the length that holds every tail.
  • WaveformPlayer: renders one Waveform from weirdnet-media with play, pause, seek, loop and gain, controlled from any thread through a PlayerControl (shared atomics). Its render path does not allocate, which tests/no_alloc.rs proves with a counting allocator.
  • CpalSink (feature cpal): the real device through cpal: ALSA (and so PulseAudio-compatible and PipeWire servers) on Linux, AAudio on Android, and the other platforms cpal supports. The feature pipewire also builds cpal's native PipeWire backend, selectable with CpalSink::pipewire().
[dependencies]
weirdnet-audio-engine = { version = "0.3", features = ["cpal"] }
weirdnet-media = "0.2"
use weirdnet_audio_engine::{AudioSink, CpalSink, WaveformPlayer};
use weirdnet_media::waveform::Waveform;

let mut sink = CpalSink::new();
let config = sink.default_config()?;
let wave = Waveform::tone(config.sample_rate, 440.0, config.sample_rate as usize, 0.2)?;
let wave = if config.channels == 1 { wave } else { wave.to_stereo()? };
let (player, control) = WaveformPlayer::new(wave, &config)?;
let _running = sink.start(config, player.into_render())?;
control.play();
std::thread::sleep(std::time::Duration::from_secs(1));

Testing

cargo test never opens an audio device. The device tests are ignored by default and refuse to run unless the audio server is a private one: scripts/private-pipewire.sh starts a PipeWire and WirePlumber of its own, under env -i with its own runtime directory, whose only output is a null sink (nothing reaches real speakers and a running desktop session is never touched), and runs a command against it:

scripts/private-pipewire.sh cargo test --features cpal -- --ignored --nocapture

It needs pipewire, wireplumber and pw-cli installed. The tests also check that XDG_RUNTIME_DIR is the script's private directory and that no DISPLAY, WAYLAND_DISPLAY, PULSE_SERVER or session bus is set.

Rust 1.99 or newer, edition 2024. BSD-3-Clause, Copyright (c) 2026, Weird Brains LLC.