Skip to main content

wavegraph_audio/
lib.rs

1#![no_std]
2#![forbid(unsafe_code)]
3#![doc = include_str!("../README.md")]
4
5#[cfg(all(feature = "coreaudio", target_os = "macos"))]
6extern crate std;
7
8#[cfg(all(feature = "coreaudio", target_os = "macos"))]
9pub mod coreaudio;
10#[cfg(feature = "fm1")]
11pub mod fm1;
12
13/// Frames per graph render and FM-1 DAC half.
14pub const FRAMES: usize = 128;
15/// Stereo Q23 samples in i32 accumulators, with headroom until the output boundary.
16pub type Block = [[i32; 2]; FRAMES];
17
18/// Role of a hardware endpoint.
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20pub enum Direction {
21    /// Deliver captured samples to the application.
22    Input,
23    /// Play samples produced by the application.
24    Output,
25}
26
27/// An enumerated physical endpoint. Names are for display. Select by identity.
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29pub struct Endpoint<Id, Name> {
30    /// Backend identity of this device instance.
31    pub id: Id,
32    /// Display name, possibly shared with another device.
33    pub name: Name,
34    /// Signal flow.
35    pub direction: Direction,
36    /// Channels exposed by the backend.
37    pub channels: u16,
38    /// Backend-reported frame rate in frames per second, numerator and denominator.
39    pub rate: (u32, u32),
40}
41
42/// Enumerate an owned snapshot on the control path. Snapshots can outlive a device.
43pub trait DeviceHost {
44    /// Device identity, suitable for distinguishing duplicate display names.
45    type Id: Copy + Eq;
46    /// Display label.
47    type Name: AsRef<str>;
48    /// Storage chosen by the backend, such as a fixed array or a desktop catalog.
49    type Devices: AsRef<[Endpoint<Self::Id, Self::Name>]>;
50    /// Failure to obtain an inventory.
51    type Error;
52    /// Observe the currently available inputs and outputs.
53    ///
54    /// # Errors
55    /// Returns backend enumeration errors.
56    fn devices(&self) -> Result<Self::Devices, Self::Error>;
57}
58
59/// Outcome of a single refill attempt.
60#[derive(Clone, Copy, Debug, Eq, PartialEq)]
61pub enum Submission {
62    /// Backend accepted the rendered block. Queued playback can still be interrupted.
63    Written,
64    /// Hardware deadline passed during this render.
65    Late,
66    /// No space or no open output. The renderer did not run.
67    WouldBlock,
68}
69
70/// Sink for a stereo graph master. Service it on the backend's owning thread.
71pub trait Playback {
72    /// Backend error.
73    type Error;
74    /// Evidence available before selecting samples. FM-1 supplies its measured DAC interval.
75    type Timing;
76    /// Call the renderer at most once, with a cleared block. On submission failure
77    /// after rendering, the caller's graph may already have advanced.
78    ///
79    /// # Errors
80    /// Returns transport errors from the backend.
81    fn render(
82        &mut self,
83        render: impl FnOnce(&mut Block, Self::Timing),
84    ) -> Result<Submission, Self::Error>;
85}
86
87/// Capture one block from an input route.
88pub trait Capture {
89    /// Backend error.
90    type Error;
91    /// Return true for a full block. On false, the destination retains its contents.
92    ///
93    /// # Errors
94    /// Returns transport errors from the backend.
95    fn read(&mut self, out: &mut Block) -> Result<bool, Self::Error>;
96}