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