1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! Stream formats: what the host asks for and what goes on the wire.
use crate::capabilities::{LinkSpeed, Pacing, SyncKind};
use crate::clock_resolution::ClockChoice;
/// How the audio is carried.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encoding {
/// Linear PCM.
Pcm,
/// Type I unsigned 8-bit PCM; silence is 0x80, one byte per sample.
PcmU8,
/// Type I IEEE-754 binary32 PCM; exact little-endian bits, including NaN payloads.
PcmFloat32,
/// DSD over PCM: 16 DSD bits per channel per PCM frame, with the 0x05/0xFA
/// marker byte owned by the library. `sample_rate` of the request is the
/// carrier rate (DSD bit rate / 16: 176400 for DSD64).
DoP,
/// Native DSD alternate setting; `bit_rate` is the 1-bit rate per channel
/// (2822400 for DSD64).
DsdNative { bit_rate: u32 },
}
/// What to do when the DAC cannot run the requested rate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RatePolicy {
/// Fail with [`Error::Unsupported`](crate::Error::Unsupported). Nothing is
/// ever resampled by the library, so a host that cannot resample itself
/// wants this.
#[default]
Exact,
/// Use the closest rate the DAC runs. [`StreamFormat::sample_rate`] then
/// differs from the request; the host must resample, and the stream is not
/// reported bit-perfect.
NearestSupported,
}
/// A format the host would like.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StreamRequest {
/// PCM rate, or the DoP carrier rate. Ignored for native DSD (the wire
/// rate follows from `bit_rate` and the subslot size).
pub sample_rate: u32,
pub channels: u16,
/// Significant bits per sample: 16, 24 or 32 for PCM; 24 or 32 for DoP;
/// ignored for native DSD.
pub bit_depth: u8,
pub encoding: Encoding,
pub rate_policy: RatePolicy,
}
impl StreamRequest {
/// PCM at `sample_rate` with `bit_depth` significant bits.
pub fn pcm(sample_rate: u32, channels: u16, bit_depth: u8) -> Self {
Self {
sample_rate,
channels,
bit_depth,
encoding: Encoding::Pcm,
rate_policy: RatePolicy::Exact,
}
}
/// Unsigned Type I PCM with one byte per channel sample.
pub fn pcm_u8(sample_rate: u32, channels: u16) -> Self {
Self {
encoding: Encoding::PcmU8,
..Self::pcm(sample_rate, channels, 8)
}
}
/// IEEE-754 binary32 Type I PCM, retained as exact wire bits.
pub fn pcm_float32(sample_rate: u32, channels: u16) -> Self {
Self {
encoding: Encoding::PcmFloat32,
..Self::pcm(sample_rate, channels, 32)
}
}
/// DoP for a DSD stream of `dsd_bit_rate` (2822400 for DSD64) in a 24-bit
/// carrier.
pub fn dop(dsd_bit_rate: u32, channels: u16) -> Self {
Self {
sample_rate: dsd_bit_rate / 16,
channels,
bit_depth: 24,
encoding: Encoding::DoP,
rate_policy: RatePolicy::Exact,
}
}
/// Native DSD of `dsd_bit_rate` (2822400 for DSD64).
pub fn dsd_native(dsd_bit_rate: u32, channels: u16) -> Self {
Self {
sample_rate: 0,
channels,
bit_depth: 0,
encoding: Encoding::DsdNative {
bit_rate: dsd_bit_rate,
},
rate_policy: RatePolicy::Exact,
}
}
pub fn with_rate_policy(mut self, rate_policy: RatePolicy) -> Self {
self.rate_policy = rate_policy;
self
}
}
/// What will go on the wire, as chosen by
/// [`Dac::negotiate`](crate::Dac::negotiate).
///
/// A `StreamFormat` cannot be built by hand; pass the one `negotiate` returned
/// to [`Dac::open_stream`](crate::Dac::open_stream).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct StreamFormat {
/// The request this format answers.
pub request: StreamRequest,
pub encoding: Encoding,
/// Frame rate on the wire: the PCM rate, the DoP carrier rate, or for
/// native DSD the rate of subslot-sized words (`bit_rate / (8 *
/// subslot_bytes)`).
pub sample_rate: u32,
pub channels: u16,
/// Descriptor-backed identities in interleaved USB wire order; unnamed when unavailable.
pub channel_map: crate::capture::ChannelMap,
/// Significant bits of the alternate setting (PCM and DoP carrier). 1 for
/// native DSD.
pub valid_bits: u8,
/// Bytes per sample on the wire.
pub subslot_bytes: u8,
pub interface_number: u8,
pub alt_setting: u8,
pub endpoint_address: u8,
pub sync_type: SyncKind,
pub explicit_feedback: bool,
pub link_speed: LinkSpeed,
pub service_interval_us: u32,
/// How the stream is paced on the endpoint: what the transport does, named
/// (see [`Pacing`]). Consistent with `sync_type` and `explicit_feedback`.
pub pacing: Pacing,
/// The Clock Source the sample rate was set and verified on, and how it was
/// reached from the stream's terminal (see [`ClockChoice`]), as resolved by
/// [`Dac::negotiate`](crate::Dac::negotiate). `None` when the DAC has no
/// clock entities to set (UAC 1.0 sets the rate on the endpoint).
pub clock: Option<ClockChoice>,
/// Native word width, byte order and bit order frozen at negotiation.
pub(crate) native_dsd_signature: Option<(u8, bool, bool)>,
}
impl StreamFormat {
/// Bytes of one wire frame (one sample for every channel): the unit
/// [`DacStream::write`](crate::DacStream::write) counts in. Also the
/// granularity of everything the stream accepts.
pub fn bytes_per_frame(&self) -> usize {
usize::from(self.subslot_bytes) * usize::from(self.channels)
}
/// Rate of the unit that `played_frames()`, `latency_frames()` and the
/// stream statistics count in. For PCM and DoP it is the wire frame rate.
/// For native DSD it is `bit_rate / 8`: one frame is one DSD byte per
/// channel (8 DSD bits per channel).
pub fn counter_rate_hz(&self) -> u32 {
match self.encoding {
Encoding::DsdNative { bit_rate } => bit_rate / 8,
_ => self.sample_rate,
}
}
/// Whether the DAC runs a different rate than requested (only possible
/// with [`RatePolicy::NearestSupported`]).
pub fn rate_differs_from_request(&self) -> bool {
match self.encoding {
Encoding::DsdNative { .. } => false,
_ => self.sample_rate != self.request.sample_rate,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn request_constructors_derive_the_carrier_rate() {
assert_eq!(StreamRequest::dop(2_822_400, 2).sample_rate, 176_400);
assert_eq!(StreamRequest::dop(5_644_800, 2).sample_rate, 352_800);
let native = StreamRequest::dsd_native(2_822_400, 2);
assert_eq!(native.sample_rate, 0);
assert_eq!(
native.encoding,
Encoding::DsdNative {
bit_rate: 2_822_400
}
);
assert_eq!(
StreamRequest::pcm(44_100, 2, 16).rate_policy,
RatePolicy::Exact
);
}
}