Skip to main content

StreamFormat

Struct StreamFormat 

Source
#[non_exhaustive]
pub struct StreamFormat {
Show 15 fields pub request: StreamRequest, pub encoding: Encoding, pub sample_rate: u32, pub channels: u16, 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>,
}
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§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 120)
119fn write_loop(stream: &DacStream, state: &Mutex<State>, stop: &AtomicBool) {
120    let frame = stream.format().bytes_per_frame();
121    // About 10 ms per render call.
122    let chunk_frames = (stream.format().sample_rate as usize / 100).max(1);
123    let mut scratch = vec![0u8; chunk_frames * frame];
124    while !stop.load(Ordering::Acquire) {
125        let mut state = lock(state);
126        if state.ended {
127            drop(state);
128            thread::sleep(Duration::from_millis(10));
129            continue;
130        }
131        // Render only when the ring wants more, so the decoder is never ahead of
132        // the configured buffer depth (and a seek never has much to throw away).
133        if state.taken == state.chunk.len() {
134            if stream.writable_bytes() < scratch.len() {
135                drop(state);
136                thread::sleep(Duration::from_millis(2));
137                continue;
138            }
139            let rendered = state.source.render(&mut scratch);
140            if rendered == 0 {
141                // No more audio: let the ring play out, even a short tail.
142                if stream.drain().is_ok() {
143                    state.ended = true;
144                }
145                continue;
146            }
147            state.chunk.clear();
148            state.chunk.extend_from_slice(&scratch[..rendered]);
149            state.taken = 0;
150        }
151        match stream.write(&state.chunk[state.taken..]) {
152            Ok(0) => {
153                drop(state);
154                thread::sleep(Duration::from_millis(2));
155            }
156            Ok(taken) => state.taken += taken,
157            Err(error) => {
158                // The stream closed or failed; the listener has the details.
159                eprintln!("writer stopping: {error}");
160                return;
161            }
162        }
163    }
164}
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 101)
99    fn position_seconds(&self) -> f64 {
100        let base = lock(&self.state).base_seconds;
101        base + self.stream.played_frames() as f64 / f64::from(self.stream.format().counter_rate_hz())
102    }
Source

pub fn rate_differs_from_request(&self) -> bool

Whether the DAC runs a different rate than requested (only possible with RatePolicy::NearestSupported).

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.