Skip to main content

Crate wavegraph_audio

Crate wavegraph_audio 

Source
Expand description

§wavegraph-audio

crates.io docs.rs Build status Project Chat

Route a WaveGraph master to an audio device, or read a selected input into a graph. The portable interface uses the FM-1’s native render size, 128 stereo frames of signed Q23 PCM in i32 accumulators. A graph can keep its mono buses and processor state across device changes. It only borrows the output block during a refill.

§Desktop playback

The cpal feature enables CoreAudio on macOS 14.2 or later, ALSA on 64-bit Linux, and WASAPI on Windows. On Debian and Ubuntu, install libasound2-dev and pkg-config before building. On macOS, the Xcode command line tools suffice. Existing macOS callers can keep the coreaudio feature and module path.

cargo run --release --locked -p wavegraph-audio --features cpal --example play -- --list
cargo run --release --locked -p wavegraph-audio --features cpal --example play

The second command plays a four-note monosynth phrase through a WaveGraph bytecode patch with a gain node. Change the default output in the system audio settings while it plays. To pin to channels 3/4 of an interface, append -- --output "Interface Name" 3. The CLI uses one-based numbers. The Rust API uses zero-based channel indices. Ctrl-C stops playback.

§Where mixing belongs

OperationDesktopFM-1
Instrument levels, inserts, post-fader sends, return sum, master dynamicsWaveGraph owns the signal order and sample-timed controls. Use the existing wavegraph-fx nodes.Run the same graph, including ChannelMixer and MasterBus where the patch uses them.
Mix this application’s output with other clientsSubmit one WaveGraph master to the selected output. The native audio system handles device sharing.Firmware owns the DAC. Sum additional local sources in the staging mix before MASTER. fm1_audio::mix_stereo already adds planar I24 sources with i32 headroom.
Final level and device PCMThe adapter clips the stereo master at the output boundary and converts Q23 to the device’s PCM format.fm1_audio::Output applies the panel MASTER ramp and final Q23 clip once before copying to the released DMA half.
Clock and bufferingThe graph chooses its rate. When the device clock differs, a preallocated sinc resampler converts on the graph side of the queue. Matching rates bypass it.Tune to the exact 24_000_000 / 544 clock. CPU0 services timed refills. CPU1 keeps exclusive ownership of outstanding DSP work.
Device changesThe application scans endpoints by identity and reconciles the selected route.The local adapter exposes one fixed stereo DAC. WaveGraph FM-1 also sends the pre-MASTER stereo mix to a connected computer over USB.

Apple also ships mixer Audio Units. Substituting one for a WaveGraph track mixer would change who implements the patch’s gain law, headroom and automation. The desktop example renders the patch in Rust. The backend receives the resulting master.

§Routing and lifetime

Import DeviceHost and use host.devices() on the control thread for input and output selectors. The snapshot carries display names, session-local IDs, channel counts, backend-reported rates, and the current default names. Keep the selected Route as application state. Call Input::sync or Output::sync at startup, after a route selection and on a periodic scan. The example scans every 250 ms and services PCM between scans.

  • Route::Default follows the system default and uses its first stereo pair or mono channel.
  • Route::Device(id, Channels::Pair(2)) selects the third and fourth channels in the endpoint’s channel layout. A mono input is duplicated and a mono output averages L/R. An explicit port goes offline when its device or channels disappear. A replug can get a new instance ID and requires reselection. Display names can collide.
  • Playback::render calls the renderer only when there is room. The desktop queue targets two device periods with a floor of four graph blocks. Written means the queue accepted PCM. It provides no DAC timestamp. Changing the selected route can discard queued samples. Keep synth state outside the port.
  • Capture::read yields a complete stereo block or leaves the destination alone. Open an input only when recording is desired, drain it regularly and drop it when finished. When the input queue fills, new frames are dropped. Opening a microphone requires macOS permission. Input and output can have independent clocks. This release does not provide drift-compensated monitoring between two devices.
  • On FM-1, service Dac(output, master).render(...) in the ALNK0 task. It passes the BSP’s exact RefillInterval before sample selection, preserves Written, Late and WouldBlock, and borrows the existing staging block. The firmware’s handoff rejects stale CPU1 completions, leaves the cleared half silent on a miss, and acknowledges edits only for a current, written block.

To connect a capture port to a graph source, keep the block with the host and hand it to the source node only after a full read. A desktop application can enable the cpal feature and service the input as follows.

use core::num::NonZeroU32;
use wavegraph_audio::{Capture, DeviceHost, FRAMES, cpal::{Host, Input, Route}};

let host = Host::new()?;
let mut input = Input::new(NonZeroU32::new(48_000).unwrap());
input.sync(&host, &host.devices()?, Route::Default)?;
let mut block = [[0; 2]; FRAMES];
// In the application's audio service loop:
if input.read(&mut block)? {
    let left = block.map(|[left, _]| left);
    // The graph's input node can now consume the complete left plane.
}

The crate forbids unsafe Rust. Each desktop port owns its stream and a bounded single-producer, single-consumer queue. The CPAL data callback transfers frames between the queue and the native buffer. It writes silence on underrun and on unselected output channels. The application owns the renderer and its buffers. The FM-1 adapter uses the CPU0 refill API in fm1-audio.

§Backend references

§License

The Rust code is licensed under either of

at your option.

§Contribution

Unless you explicitly state otherwise, you license any contribution you intentionally submit for inclusion in this work (as defined in the Apache-2.0 license) under both licenses above, without additional terms or conditions.

Structs§

Endpoint
An enumerated physical endpoint. Names are for display. Select by identity.

Enums§

Direction
Role of a hardware endpoint.
Submission
Outcome of a single refill attempt.

Constants§

FRAMES
Frames per graph render and FM-1 DAC half.

Traits§

Capture
Capture one block from an input route.
DeviceHost
Enumerate an owned snapshot on the control path. Snapshots can outlive a device.
Playback
Sink for a stereo graph master. Service it on the backend’s owning thread.

Type Aliases§

Block
Stereo Q23 samples in i32 accumulators, with headroom until the output boundary.