Skip to main content

StreamFormat

Struct StreamFormat 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§request: StreamRequest

The request this format answers.

§encoding: Encoding§sample_rate: u32

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)).

§channels: u16§channel_map: ChannelMap

Descriptor-backed identities in interleaved USB wire order; unnamed when unavailable.

§valid_bits: u8

Significant bits of the alternate setting (PCM and DoP carrier). 1 for native DSD.

§subslot_bytes: u8

Bytes 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: Pacing

How 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

Source

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?
examples/pull_adapter.rs (line 124)
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}
Source

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?
examples/pull_adapter.rs (line 105)
102    fn position_seconds(&self) -> f64 {
103        let base = lock(&self.state).base_seconds;
104        base + self.stream.played_frames() as f64
105            / f64::from(self.stream.format().counter_rate_hz())
106    }
More examples
Hide additional examples
examples/guide_player.rs (line 122)
117    fn from(format: &StreamFormat) -> Self {
118        Self {
119            channels: usize::from(format.channels),
120            bytes_per_sample: usize::from(format.subslot_bytes),
121            sample_rate: format.sample_rate,
122            counter_rate_hz: format.counter_rate_hz(),
123        }
124    }
Source

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?
examples/guide_open.rs (line 101)
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}

Trait Implementations§

Source§

impl Clone for StreamFormat

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StreamFormat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for StreamFormat

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for StreamFormat

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.