# weirdnet-audio-engine
[](https://crates.io/crates/weirdnet-audio-engine)
[](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.
- `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.
- `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.1", 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).