weirdnet-audio-engine 0.1.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.
  • 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.1", 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.