Skip to main content

usbdac_core/
lib.rs

1//! Bit-perfect direct USB DAC output for Android and Linux.
2//!
3//! `usbdac-core` drives a USB Audio Class 1.0/2.0 DAC through usbfs, without the
4//! platform audio stack: PCM at the DAC's native depth, DSD over PCM (DoP) and
5//! native DSD, feedback-paced isochronous transfers, loss accounting, and the
6//! DAC's own hardware volume. Asynchronous streams can follow explicit rate
7//! reports or frame counts from a compatible UAC2 input stream. Use
8//! [`StreamFormat::pacing`] for the selected mechanism and
9//! [`DacStream::stats`] for live lock and feedback counters.
10//!
11//! The library never resamples, dithers or scales samples unless the host
12//! explicitly enables software gain, and it reports honestly whether the audio
13//! on the wire is bit-perfect ([`DacStream::is_bit_perfect`]). (The opt-in
14//! [`convert`] module, which the stream never calls, is for a host that wants
15//! to convert before writing.)
16//!
17//! ```no_run
18//! use std::sync::Arc;
19//! use usbdac_core::{Dac, DacConfig, DacEvent, DeviceInfo, StreamRequest};
20//!
21//! # fn demo(fd: i32) -> usbdac_core::Result<()> {
22//! // `fd` comes from UsbDeviceConnection.getFileDescriptor(); the host has
23//! // claimed the audio interfaces and lists them here.
24//! let info = DeviceInfo::new(0x2fc6, 0xf09a, "Some DAC").with_claimed_interfaces(vec![0u8, 1]);
25//! let dac = Dac::open(fd, info, DacConfig::default())?;
26//! let format = dac.negotiate(&StreamRequest::pcm(44_100, 2, 16))?;
27//! let stream = dac.open_stream(&format, Arc::new(|event: DacEvent| log::info!("{event:?}")))?;
28//! let silence = vec![0u8; stream.writable_bytes()];
29//! stream.write(&silence)?;
30//! # Ok(()) }
31//! ```
32//!
33//! v0.1 supports one open [`Dac`] per process.
34
35#[macro_use]
36mod macros;
37
38pub mod convert;
39pub mod descriptors;
40
41mod capabilities;
42mod clock_resolution;
43mod config;
44mod dac;
45mod endpoint;
46mod error;
47mod events;
48mod feedback_diagnostics;
49mod format;
50mod flush_cancel;
51mod iso_packet_scheduler;
52mod iso_packet_validation;
53mod iso_transport_loss;
54mod output_position;
55mod payload_audit_latch;
56mod priority;
57mod quirk;
58mod ring_target;
59mod settings;
60mod stream;
61mod transport;
62mod volume;
63mod volume_control;
64
65#[cfg(test)]
66mod tests;
67
68pub use capabilities::{
69    AltFormat, AltSetting, Capabilities, DeviceInfo, EndpointUsage, LinkSpeed, Pacing, RateRange,
70    SyncKind, UacVersion,
71};
72pub use clock_resolution::{ClockChoice, ClockHop, ClockHopKind, ClockResolution};
73pub use config::{CloseVolume, DacConfig, HandBack, QuirkOverride, StartupVolume};
74pub use dac::Dac;
75pub use error::{Error, Result};
76pub use events::{DacEvent, DacListener, StreamState};
77pub use feedback_diagnostics::{
78    ExplicitFeedbackDiagnostics, ExplicitFeedbackOutcome, ExplicitFeedbackPacket,
79    FeedbackTrace, FeedbackTraceEntry, FeedbackTraceEvent,
80};
81pub use format::{Encoding, RatePolicy, StreamFormat, StreamRequest};
82pub use quirk::Quirk;
83pub use stream::{BitPerfectBlocker, DacStream, StreamStats};
84pub use volume::{
85    StartupVolumeReport, VolumeControl, VolumeRange, VolumeState, DEFAULT_VOLUME_STEPS,
86};
87
88/// Compiles the README's example as a doctest so it cannot rot.
89#[cfg(doctest)]
90#[doc = include_str!("../../../README.md")]
91struct ReadmeDoctests;