weirdnet-audio-engine 0.5.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](https://img.shields.io/crates/v/weirdnet-audio-engine.svg)](https://crates.io/crates/weirdnet-audio-engine)
[![docs.rs](https://docs.rs/weirdnet-audio-engine/badge.svg)](https://docs.rs/weirdnet-audio-engine)

Audio output for [Weirdnet](https://net.weirdbrains.com) by [Weird Brains](https://weirdbrains.com): 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 (`Waveform`s) 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. `Command`s (`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; `Event`s (`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`](https://docs.rs/weirdnet-media/latest/weirdnet_media/waveform/struct.Waveform.html) from [weirdnet-media](https://crates.io/crates/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.
- `router`: a fan-in router of named endpoints: sources rendered on the audio thread (an `EngineAudio`, a talkback bus, any render function), inputs fed from another thread (`RouterInput`), buses that sum their input and offer it as an output (so channel strips chain into a master), taps read from another thread (`RouterTap`) and the device. Routes carry an output to an input with a gain; fan-out and fan-in are free and a loop between buses is refused. Each destination sums in f32 in the order of its inputs' ids, so the same routes give the same bytes. Sources can be muted or soloed (solo is per destination), any destination can have a peak limiter, and each endpoint reports its last block's peak. Changes go to the audio side through lock-free queues; old plans and removed sources come back to be dropped off the audio thread, and the render path does not allocate (`tests/no_alloc.rs`). With the feature `route`, `RouterControl` implements [weirdnet-audio-route](https://crates.io/crates/weirdnet-audio-route)'s `Executor`: it routes a graph's connections whose two ports are endpoints here (port id = endpoint id) and lists the others for a remote executor.
- `CpalSink` (feature `cpal`): the real device through [cpal](https://crates.io/crates/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()`.

```toml
[dependencies]
weirdnet-audio-engine = { version = "0.5", features = ["cpal"] }
weirdnet-media = "0.2"
```

```rust
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:

```sh
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](LICENSE), Copyright (c) 2026, [Weird Brains LLC](https://weirdbrains.com).