Skip to main content

weirdnet_audio_engine/
lib.rs

1//! The audio output boundary of the engine.
2//!
3//! * [`AudioSink`]: starts a stream and calls a render function from the device's audio thread. Everything above the sink is platform-free;
4//!   a device backend is one implementation of this trait.
5//! * [`ManualSink`]: a sink with no device, driven by the caller (`render_block`). It makes engine code deterministic to test.
6//! * `CpalSink` (feature `cpal`): the real device.
7//! * [`AudioSource`], [`ManualSource`] and `CpalSource` (feature `cpal`): the input boundary, a capture function called from the device's input thread.
8//! * [`talkback`]: voices between people on a bus of their own: the mic switch (starts muted), a level gate, a speaker mixer with jitter
9//!   buffers, volume and mute, and a ducker for the music.
10//! * [`Scene`], [`SceneRenderer`], [`render_offline`]: sounds and sorted events in frames, and the one mixer that plays them: block by block from
11//!   a device callback, or offline for an export that matches live playback sample for sample.
12//! * [`WaveformPlayer`]: renders one [`weirdnet_media::waveform::Waveform`] with play, pause, seek, loop and gain, as the simplest source to play and the
13//!   reference for what any other source should do.
14//!
15//! ```
16//! use weirdnet_audio_engine::{AudioSink, ManualSink, SinkConfig, WaveformPlayer};
17//! use weirdnet_media::waveform::Waveform;
18//!
19//! let config = SinkConfig::new(48_000, 1);
20//! let (player, control) = WaveformPlayer::new(Waveform::tone(48_000, 440.0, 480, 0.5)?, &config)?;
21//! let mut sink = ManualSink::new();
22//! let _running = sink.start(config, player.into_render())?;
23//! control.play();
24//! let block = sink.render_block(480).expect("the sink is running");
25//! assert!(block.iter().any(|s| s.abs() > 0.1));
26//! # Ok::<(), Box<dyn std::error::Error>>(())
27//! ```
28//!
29//! The render function runs on the audio thread: it must not block, lock, or allocate. [`WaveformPlayer`] follows that rule and its tests
30//! check what can be checked (the callback only reads shared atomics and the sample buffer it owns).
31
32mod engine;
33mod input;
34mod manual;
35mod player;
36mod scene;
37mod sink;
38pub mod talkback;
39
40#[cfg(feature = "cpal")]
41mod cpal_sink;
42
43pub use engine::{Command, Engine, EngineAudio, EngineError, EngineOptions, Event, Timing, engine};
44pub use input::{AudioSource, CaptureFn, ManualSource};
45pub use manual::ManualSink;
46pub use player::{PlayerControl, PlayerError, WaveformPlayer};
47pub use scene::{MAX_VOICES, NO_TAG, Scene, SceneError, SceneEvent, SceneRenderer, render_offline, render_to_end};
48pub use sink::{AudioSink, RenderFn, SinkConfig, SinkError, SinkHandle, SinkStats};
49
50#[cfg(feature = "cpal")]
51pub use cpal_sink::CpalSink;
52#[cfg(feature = "cpal")]
53pub use input::CpalSource;