#[non_exhaustive]pub struct StreamFormat {Show 16 fields
pub request: StreamRequest,
pub encoding: Encoding,
pub sample_rate: u32,
pub channels: u16,
pub channel_map: ChannelMap,
pub valid_bits: u8,
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,
pub pacing: Pacing,
pub clock: Option<ClockChoice>,
/* private fields */
}Expand description
What will go on the wire, as chosen by
Dac::negotiate.
A StreamFormat cannot be built by hand; pass the one negotiate returned
to Dac::open_stream.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.request: StreamRequestThe request this format answers.
encoding: Encoding§sample_rate: u32Frame 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)).
channels: u16§channel_map: ChannelMapDescriptor-backed identities in interleaved USB wire order; unnamed when unavailable.
valid_bits: u8Significant bits of the alternate setting (PCM and DoP carrier). 1 for native DSD.
subslot_bytes: u8Bytes per sample on the wire.
interface_number: u8§alt_setting: u8§endpoint_address: u8§sync_type: SyncKind§explicit_feedback: bool§link_speed: LinkSpeed§service_interval_us: u32§pacing: PacingHow the stream is paced on the endpoint: what the transport does, named
(see Pacing). Consistent with sync_type and explicit_feedback.
clock: Option<ClockChoice>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. None when the DAC has no
clock entities to set (UAC 1.0 sets the rate on the endpoint).
Implementations§
Source§impl StreamFormat
impl StreamFormat
Sourcepub fn bytes_per_frame(&self) -> usize
pub fn bytes_per_frame(&self) -> usize
Bytes of one wire frame (one sample for every channel): the unit
DacStream::write counts in. Also the
granularity of everything the stream accepts.
Examples found in repository?
123fn write_loop(stream: &DacStream, state: &Mutex<State>, stop: &AtomicBool) {
124 let frame = stream.format().bytes_per_frame();
125 // About 10 ms per render call.
126 let chunk_frames = (stream.format().sample_rate as usize / 100).max(1);
127 let mut scratch = vec![0u8; chunk_frames * frame];
128 while !stop.load(Ordering::Acquire) {
129 let mut state = lock(state);
130 if state.ended {
131 drop(state);
132 thread::sleep(Duration::from_millis(10));
133 continue;
134 }
135 // Render only when the ring wants more, so the decoder is never ahead of
136 // the configured buffer depth (and a seek never has much to throw away).
137 if state.taken == state.chunk.len() {
138 if stream.writable_bytes() < scratch.len() {
139 drop(state);
140 thread::sleep(Duration::from_millis(2));
141 continue;
142 }
143 let rendered = state.source.render(&mut scratch);
144 if rendered == 0 {
145 // No more audio: let the ring play out, even a short tail.
146 if stream.drain().is_ok() {
147 state.ended = true;
148 }
149 continue;
150 }
151 state.chunk.clear();
152 state.chunk.extend_from_slice(&scratch[..rendered]);
153 state.taken = 0;
154 }
155 match stream.write(&state.chunk[state.taken..]) {
156 Ok(0) => {
157 drop(state);
158 thread::sleep(Duration::from_millis(2));
159 }
160 Ok(taken) => state.taken += taken,
161 Err(error) => {
162 // The stream closed or failed; the listener has the details.
163 eprintln!("writer stopping: {error}");
164 return;
165 }
166 }
167 }
168}Sourcepub fn counter_rate_hz(&self) -> u32
pub fn counter_rate_hz(&self) -> u32
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).
Examples found in repository?
More examples
Sourcepub fn rate_differs_from_request(&self) -> bool
pub fn rate_differs_from_request(&self) -> bool
Whether the DAC runs a different rate than requested (only possible
with RatePolicy::NearestSupported).
Examples found in repository?
91pub fn negotiate(dac: &Dac, track: TrackFormat, allow_nearest: bool) -> Result<Negotiated> {
92 let policy = if allow_nearest {
93 RatePolicy::NearestSupported
94 } else {
95 RatePolicy::Exact
96 };
97 let request =
98 StreamRequest::pcm(track.sample_rate, track.channels, track.bits).with_rate_policy(policy);
99 let format = dac.negotiate(&request)?;
100 let resample_to = format
101 .rate_differs_from_request()
102 .then_some(format.sample_rate);
103 Ok(Negotiated {
104 format,
105 resample_to,
106 })
107}