usbdac-core 0.1.3

Direct USB DAC playback for Rust audio applications on Android and Linux, with PCM, DoP, native DSD and bit-perfect reporting.
Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! The output stream.
//!
//! The model is *push*: the library owns the ring and the USB output thread,
//! the host writes audio into the ring from its own thread. There is no render
//! thread and no callback into the host on the audio path.

use crate::dac::DacShared;
use crate::error::{Error, Result};
use crate::events::StreamState;
use crate::format::StreamFormat;
use crate::transport::session::{bit_perfect_now, current_bit_perfect_blocker, stream_verdicts};
use crate::transport::shared::StreamShared;
use crate::transport::StreamHandle;
use parking_lot::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

/// What stands in the way of [`DacStream::is_bit_perfect`], most significant
/// first. `None` from [`DacStream::bit_perfect_blocker`] means nothing does.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BitPerfectBlocker {
    /// The stream is closed or not running.
    NoStream,
    /// At least one frame queued to the DAC was never taken (latched for the
    /// life of the stream).
    LostFrames,
    /// The DAC runs another rate or format than the one requested
    /// (`RatePolicy::NearestSupported`).
    FormatChanged,
    /// The DAC did not confirm its sampling frequency.
    ClockUnverified,
    /// Software gain other than 1.0 is applied.
    SoftwareGain,
    /// An asynchronous endpoint without a feedback endpoint, streamed at the
    /// nominal rate because `DacConfig::allow_unsynchronized_async` is on.
    UnsynchronizedAsync,
    /// The wire payload audit has not seen real audio (yet), or lost it.
    FeedbackUnlocked,
    PayloadUnverified,
}

impl BitPerfectBlocker {
    /// Stable identifier: `no_stream`, `lost_frames`, `format_changed`,
    /// `clock_unverified`, `software_gain`, `unsynchronized_async`,
    /// `payload_unverified`.
    pub fn code(self) -> &'static str {
        match self {
            Self::NoStream => "no_stream",
            Self::LostFrames => "lost_frames",
            Self::FormatChanged => "format_changed",
            Self::ClockUnverified => "clock_unverified",
            Self::SoftwareGain => "software_gain",
            Self::UnsynchronizedAsync => "unsynchronized_async",
            Self::FeedbackUnlocked => "feedback_unlocked",
            Self::PayloadUnverified => "payload_unverified",
        }
    }

    pub(crate) fn description(self) -> &'static str {
        match self {
            Self::NoStream => "stream inactive",
            Self::LostFrames => "audio lost on the USB transport",
            Self::FormatChanged => "requested/effective format mismatch",
            Self::ClockUnverified => "clock verification reset",
            Self::SoftwareGain => "software gain active",
            Self::UnsynchronizedAsync => {
                "asynchronous endpoint without feedback, host-paced at the nominal rate"
            }
            Self::FeedbackUnlocked => "feedback pacing has not locked",
            Self::PayloadUnverified => "wire payload audit failed",
        }
    }
}

/// Counters of a stream. Frames are in the unit
/// [`StreamFormat::counter_rate_hz`] gives the rate of.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct StreamStats {
    pub state: StreamState,
    pub bit_perfect: bool,
    /// Why `bit_perfect` is false, if it is.
    pub bit_perfect_blocker: Option<BitPerfectBlocker>,
    pub software_gain: f32,
    /// Runs of padding because the ring ran dry (counted per transfer).
    pub underruns: u64,
    /// Frames queued to the DAC that it never took.
    pub lost_frames: u64,
    pub short_packets: u64,
    pub zero_packets: u64,
    /// Isochronous transfers libusb reported as timed out.
    pub urb_timeouts: u64,
    pub completed_transfers: u64,
    /// Frames the host has written.
    pub frames_written: u64,
    /// Frames the DAC has taken, music and padding alike.
    pub frames_completed: u64,
    /// Music frames completed on the bus since the start or the last flush.
    pub played_frames: u64,
    /// Music frames between the host and the DAC: ring plus bus.
    pub latency_frames: u64,
    pub latency_ms: f64,
    /// Frames in the ring.
    pub queued_frames: u64,
    pub buffer_ms: u32,
    /// The deep ring depth (`DacConfig::buffer_target_ms`).
    pub buffer_target_ms: u32,
    /// The depth `writable_bytes()` asks for now: `buffer_target_ms`, or the
    /// shallower processing depth while software gain or host processing is on.
    pub active_target_ms: u32,
    pub buffer_capacity_ms: u32,
    /// Transfers currently submitted to the bus.
    pub transfer_queue_depth: u32,
    pub last_turnaround_us: u64,
    pub max_turnaround_us: u64,
    pub last_submit_gap_us: u64,
    pub max_submit_gap_us: u64,
    /// Rate the DAC has actually consumed frames at since the start.
    pub effective_rate_hz: f64,
    /// Live feedback is locked. Equivalent to `feedback_locked` in 0.1.1.
    pub feedback_active: bool,
    pub feedback_reports: u64,
    pub feedback_timeouts: u64,
    pub feedback_errors: u64,
    pub feedback_rejected: u64,
    /// Time since the last valid feedback report.
    pub feedback_age_ms: Option<u64>,
    /// The sample rate the last feedback report implies.
    pub feedback_rate_hz: Option<f64>,
    pub feedback_stale: bool,
    /// The selected explicit or implicit source has established usable timing.
    pub feedback_locked: bool,
    /// Empty explicit polls or empty implicit IN patterns.
    pub feedback_empty: u64,
    pub implicit_fifo_depth: u32,
    pub implicit_fifo_high_water: u32,
    pub implicit_waits: u64,
    pub implicit_empty_bus: u64,
    pub implicit_overruns: u64,
    pub implicit_in_transfers: u64,
    pub implicit_out_of_order: u64,
    pub implicit_invalid_packets: u64,
    pub implicit_measured_frames: u64,
    pub implicit_scheduled_frames: u64,

    /// The transfers have been lossless for long enough (about 256 ms) with no
    /// underrun and no lost audio. A stream that lost audio is never stable
    /// again.
    pub stable: bool,
    /// The DAC confirmed the sampling frequency (or the device quirk trusts
    /// its `SET_CUR`). False before the clock is verified and after it is lost.
    pub clock_verified: bool,
    /// The wire payload audit passes (see `is_bit_perfect`).
    pub payload_verified: bool,
    /// How long, in milliseconds, the wire payload has been failing the audit,
    /// while it does.
    pub payload_constant_for_ms: Option<u64>,
}

pub(crate) struct StreamInner {
    dac: Arc<DacShared>,
    handle: Mutex<Option<StreamHandle>>,
    pub(crate) shared: Arc<StreamShared>,
    format: StreamFormat,
    closed: AtomicBool,
}

impl StreamInner {
    pub(crate) fn new(dac: Arc<DacShared>, handle: StreamHandle, format: StreamFormat) -> Arc<Self> {
        Arc::new(Self {
            dac,
            shared: Arc::clone(&handle.shared),
            handle: Mutex::new(Some(handle)),
            format,
            closed: AtomicBool::new(false),
        })
    }

    pub(crate) fn is_closed(&self) -> bool {
        self.closed.load(Ordering::Acquire)
    }

    /// Publishes the initial state once the listener is in place.
    pub(crate) fn start_events(&self) {
        self.shared.refresh_state();
    }

    pub(crate) fn close(&self) -> Result<()> {
        if self.closed.swap(true, Ordering::AcqRel) {
            return Ok(());
        }
        let result = match self.handle.lock().take() {
            Some(mut handle) => handle.stop().map_err(Error::Transfer),
            None => Ok(()),
        };
        self.shared.mark_closed();
        // The Closed event is queued with the listener it belongs to; the slot
        // can be cleared now.
        self.dac.events.set_stream_listener(None);
        let mut slot = self.dac.stream.lock();
        if slot
            .as_ref()
            .and_then(std::sync::Weak::upgrade)
            .is_some_and(|current| std::ptr::eq(Arc::as_ptr(&current), self))
        {
            *slot = None;
        }
        result
    }
}

/// An open output stream. Dropping it closes it.
///
/// All methods take `&self`, so a stream can be shared between the host's
/// writer thread and its control thread behind an `Arc`.
///
/// # Sample layout
///
/// [`write`](Self::write) takes **wire-layout bytes**: the bytes exactly as
/// they go into the USB packets, in whole *wire frames*
/// (`format.bytes_per_frame()` = `subslot_bytes * channels` bytes, channels
/// interleaved in the DAC's channel order). Samples are little-endian.
///
/// * **PCM** — each sample is `subslot_bytes` bytes, a signed two's-complement
///   integer, *left-justified*: the sign bit is the top bit of the subslot and
///   unused low bits are zero. 16-bit audio in a 2-byte subslot is a plain
///   `i16`; 24-bit audio in a 3-byte subslot is a packed `i24`; 24-bit audio
///   in a 4-byte subslot is the `i24` shifted left by 8 (low byte zero);
///   32-bit audio is an `i32`. With the layout the DAC advertises, the bytes
///   written are the bytes on the wire.
/// * **DoP** — each sample is the DoP word in the same subslot layout, the 16
///   DSD bits in the top two bytes below the marker: for a 3-byte subslot
///   `[dsd_low, dsd_high, marker]`, for a 4-byte subslot `[0, dsd_low,
///   dsd_high, marker]`. `dsd_high` holds the older 8 DSD bits (MSB first). The
///   marker byte is **ignored and overwritten**: the library owns the
///   `0x05`/`0xFA` alternation so the phase survives pause, flush and underrun.
/// * **Native DSD** — raw DSD bytes, MSB first (the oldest bit in bit 7),
///   interleaved one byte per channel: `[ch0, ch1, ch0, ch1, ...]` in
///   stream order. A wire frame is `subslot_bytes * channels` such bytes (one
///   wire word per channel); the library groups consecutive bytes of a channel
///   into the wire word and orders them as the DAC needs (big-endian or
///   little-endian, optional bit reversal, per quirk or config).
///
/// [`write_i32`](Self::write_i32) takes the ring's own sample unit instead:
/// PCM as full-scale 32-bit left-justified `i32` (the encoder keeps the top
/// `valid_bits` bits), DoP as `i32` words with the DSD bits in bits 23..8 (bits
/// 31..24 are overwritten with the marker), native DSD as one DSD byte per
/// `i32` in bits 7..0.
pub struct DacStream {
    inner: Arc<StreamInner>,
}

impl std::fmt::Debug for DacStream {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DacStream")
            .field("format", &self.inner.format)
            .finish_non_exhaustive()
    }
}

impl DacStream {
    pub(crate) fn from_inner(inner: Arc<StreamInner>) -> Self {
        Self { inner }
    }

    fn shared(&self) -> &StreamShared {
        &self.inner.shared
    }

    fn ensure_open(&self) -> Result<()> {
        if self.inner.is_closed() {
            return Err(Error::InvalidState("the stream is closed".to_string()));
        }
        Ok(())
    }

    /// The wire format this stream runs.
    pub fn format(&self) -> &StreamFormat {
        &self.inner.format
    }

    /// Appends wire-layout bytes (see the type-level documentation). Never
    /// blocks. Returns the number of bytes taken: a whole number of wire frames,
    /// as many as fit in the ring; a partial or zero count means the ring is
    /// full, so retry with the remainder later. Input shorter than one wire
    /// frame is not taken.
    ///
    /// [`Error::InvalidState`] after [`drain`](Self::drain) (until
    /// [`flush`](Self::flush)), or once the stream has closed or failed.
    pub fn write(&self, data: &[u8]) -> Result<usize> {
        self.ensure_open()?;
        self.shared().write_bytes(data)
    }

    /// Like [`write`](Self::write) for the ring's native `i32` samples. Counts
    /// and returns samples (a whole number of wire frames: multiples of
    /// `channels`, or of `subslot_bytes * channels` for native DSD).
    pub fn write_i32(&self, samples: &[i32]) -> Result<usize> {
        self.ensure_open()?;
        self.shared().write_samples(samples)
    }

    /// How many bytes the host should write now to bring the ring to its
    /// target depth ([`DacConfig::buffer_target_ms`], or the shallower
    /// `processing_buffer_target_ms` while software gain is on). A whole number
    /// of wire frames. `write` still accepts more, up to the ring's capacity.
    ///
    /// [`DacConfig::buffer_target_ms`]: crate::DacConfig::buffer_target_ms
    pub fn writable_bytes(&self) -> usize {
        if self.inner.is_closed() {
            return 0;
        }
        self.shared().writable_bytes()
    }

    /// Resumes after [`pause`](Self::pause). A new stream plays by itself once
    /// the ring holds the start threshold; this is only needed after a pause.
    pub fn play(&self) -> Result<()> {
        self.ensure_open()?;
        self.shared().set_paused(false);
        Ok(())
    }

    /// Holds the stream: wire-correct silence goes out (zero for PCM, the
    /// idle pattern for DSD, DoP markers keep alternating) so the DAC stays
    /// locked, and the ring is not consumed. Audio already submitted to the bus
    /// (about [`DacConfig::bus_queue_ms`](crate::DacConfig::bus_queue_ms), 16 ms
    /// by default, at every service interval) still plays out first; the ring
    /// resumes exactly where it was.
    pub fn pause(&self) -> Result<()> {
        self.ensure_open()?;
        self.shared().set_paused(true);
        Ok(())
    }

    /// Drops everything queued in the ring and resets
    /// [`played_frames`](Self::played_frames) to 0. The stream holds wire
    /// silence until the ring holds the start threshold again. Also clears
    /// [`drain`](Self::drain), so the stream can be reused.
    ///
    /// Returns at once. With
    /// [`DacConfig::cancel_in_flight_on_flush`](crate::DacConfig::cancel_in_flight_on_flush)
    /// (the default) the output thread then cancels the transfers already on
    /// the bus, within about one transfer (2 ms), waits for every one to be
    /// handed back and refills the slots with silence; those transfers are not
    /// lost audio, not underruns and not counted as played. Off, or when only
    /// silence is queued (a flush while paused), the queue plays out
    /// ([`DacConfig::bus_queue_ms`](crate::DacConfig::bus_queue_ms), 16 ms by
    /// default) and is not counted as played.
    pub fn flush(&self) -> Result<()> {
        self.ensure_open()?;
        self.shared().flush();
        Ok(())
    }

    /// Declares that no more audio will be written: the ring plays out, even a
    /// tail shorter than the start threshold, and the stream then reports
    /// [`is_drained`](Self::is_drained) and the `Drained` state. The padded
    /// tail is not an underrun. Further writes fail until
    /// [`flush`](Self::flush).
    pub fn drain(&self) -> Result<()> {
        self.ensure_open()?;
        self.shared().drain();
        Ok(())
    }

    /// `drain()` was called and everything written has been played.
    pub fn is_drained(&self) -> bool {
        self.shared().is_drained()
    }

    /// Closes the stream and waits for the output thread. The DAC stays open.
    pub fn close(self) -> Result<()> {
        self.inner.close()
    }

    pub fn state(&self) -> StreamState {
        self.shared().current_state()
    }

    /// Music frames the DAC has completed on the bus since the stream started
    /// or the last [`flush`](Self::flush). Padding, held silence and lost
    /// frames are not counted. The unit is `format().counter_rate_hz()`.
    pub fn played_frames(&self) -> u64 {
        self.shared().stats.played_frames()
    }

    /// Music frames between the host and the DAC: in the ring plus on the bus.
    /// What the audible position lags the written position by.
    pub fn latency_frames(&self) -> u64 {
        self.shared().stats.output_latency_frames()
    }

    /// Frames waiting in the ring, in the unit of
    /// [`StreamFormat::counter_rate_hz`]. Cheaper than [`stats`](Self::stats)
    /// (`StreamStats::queued_frames`), for a host that wants to know how full the
    /// ring is on every cycle of its writer loop.
    pub fn queued_frames(&self) -> u64 {
        if self.inner.is_closed() {
            return 0;
        }
        self.shared().stats.queued_frames()
    }

    /// Bit-perfect means: no software gain, a verified clock at the requested
    /// rate, no frame lost on the bus, and the wire payload is real audio.
    /// False as soon as any of that stops being true, and for good after a lost
    /// frame.
    pub fn is_bit_perfect(&self) -> bool {
        !self.inner.is_closed() && self.shared().note_bit_perfect()
    }

    /// Why [`is_bit_perfect`](Self::is_bit_perfect) is false: `None` when it is
    /// true, otherwise the first condition that fails.
    pub fn bit_perfect_blocker(&self) -> Option<BitPerfectBlocker> {
        if self.inner.is_closed() {
            return Some(BitPerfectBlocker::NoStream);
        }
        current_bit_perfect_blocker()
    }

    /// Scales PCM samples by `gain` (0.0 to 16.0) as each transfer is prepared,
    /// so it takes effect within the bus latency rather than the ring depth.
    /// Rounds to nearest and saturates; no dither. Any value other than 1.0
    /// ends bit-perfect output. Refused for DoP and native DSD (it would corrupt
    /// the bitstream).
    pub fn set_software_gain(&self, gain: f32) -> Result<()> {
        self.ensure_open()?;
        self.shared().set_software_gain(gain)
    }

    pub fn software_gain(&self) -> f32 {
        self.shared().stats.software_gain()
    }

    /// Tells the stream that the host processes the audio (EQ, DSP, crossfade,
    /// pitch) before it writes it. A change the host makes is then heard after
    /// the ring's fill, so [`writable_bytes`](Self::writable_bytes) asks for the
    /// shallow `processing_buffer_target_ms` instead of the deep target, as it
    /// does for software gain. Nothing is flushed when the depth changes (see
    /// `writable_bytes`).
    ///
    /// It is only a hint about buffering. It does not change a sample, and the
    /// library cannot see what the host did to the audio, so it does not touch
    /// [`is_bit_perfect`](Self::is_bit_perfect): a host that processes audio is
    /// responsible for not claiming bit-perfect output. It has no effect for DoP
    /// and native DSD, which are never processed.
    pub fn set_host_processing(&self, processing: bool) {
        self.shared()
            .stats
            .host_processing
            .store(processing, std::sync::atomic::Ordering::Relaxed);
    }

    /// Whether [`set_host_processing`](Self::set_host_processing) is on.
    pub fn host_processing(&self) -> bool {
        self.shared()
            .stats
            .host_processing
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// A bounded snapshot of the explicit USB feedback path for this stream.
    ///
    /// Available after the worker has selected the endpoint, including after
    /// a feedback failure. Other pacing modes return `None`.
    pub fn explicit_feedback_diagnostics(&self) -> Option<crate::ExplicitFeedbackDiagnostics> {
        *self.shared().stats.explicit_feedback_diagnostics.lock()
    }

    pub fn stats(&self) -> StreamStats {
        use std::sync::atomic::Ordering::Relaxed;
        let shared = self.shared();
        let stats = &shared.stats;
        let feedback_last = stats.feedback_last_report_ms.load(Relaxed);
        let elapsed_ms = stats.started_at.elapsed().as_millis().min(u128::from(u64::MAX)) as u64;
        let verdicts = if self.inner.is_closed() {
            Default::default()
        } else {
            stream_verdicts()
        };
        StreamStats {
            state: shared.current_state(),
            bit_perfect: !self.inner.is_closed() && bit_perfect_now(),
            bit_perfect_blocker: self.bit_perfect_blocker(),
            software_gain: stats.software_gain(),
            underruns: stats.underrun_count.load(Relaxed),
            lost_frames: stats.lost_frames.load(Relaxed),
            short_packets: stats.short_packets.load(Relaxed),
            zero_packets: stats.zero_packets.load(Relaxed),
            urb_timeouts: stats.data_urb_timeouts.load(Relaxed),
            completed_transfers: stats.completed_transfers.load(Relaxed),
            frames_written: stats.producer_frames.load(Relaxed),
            frames_completed: stats.consumer_frames.load(Relaxed),
            played_frames: stats.played_frames(),
            latency_frames: stats.output_latency_frames(),
            latency_ms: stats.output_latency_ms(),
            queued_frames: stats.queued_frames(),
            buffer_ms: stats.buffer_fill_ms(),
            buffer_target_ms: stats.buffer_target_ms(),
            active_target_ms: stats.active_target_ms(),
            buffer_capacity_ms: stats.buffer_capacity_ms(),
            transfer_queue_depth: stats.transfer_queue_depth.load(Relaxed) as u32,
            last_turnaround_us: stats.last_transfer_turnaround_us.load(Relaxed),
            max_turnaround_us: stats.max_transfer_turnaround_us.load(Relaxed),
            last_submit_gap_us: stats.last_submit_gap_us.load(Relaxed),
            max_submit_gap_us: stats.max_submit_gap_us.load(Relaxed),
            effective_rate_hz: stats.effective_consumer_rate_milli_hz.load(Relaxed) as f64 / 1_000.0,
            feedback_active: stats.feedback_locked.load(Relaxed),
            feedback_reports: stats.feedback_reports.load(Relaxed),
            feedback_timeouts: stats.feedback_timeouts.load(Relaxed),
            feedback_errors: stats.feedback_errors.load(Relaxed),
            feedback_rejected: stats.feedback_rejected.load(Relaxed),
            feedback_age_ms: (feedback_last != u64::MAX)
                .then(|| elapsed_ms.saturating_sub(feedback_last)),
            feedback_rate_hz: (stats.feedback_reports.load(Relaxed) > 0)
                .then(|| stats.feedback_rate_milli_hz.load(Relaxed) as f64 / 1_000.0),
            feedback_stale: stats.feedback_stale.load(Relaxed),
            feedback_locked: stats.feedback_locked.load(Relaxed),
            feedback_empty: stats.feedback_empty.load(Relaxed),
            implicit_fifo_depth: stats.implicit_fifo_depth.load(Relaxed) as u32,
            implicit_fifo_high_water: stats.implicit_fifo_high_water.load(Relaxed) as u32,
            implicit_waits: stats.implicit_waits.load(Relaxed),
            implicit_empty_bus: stats.implicit_empty_bus.load(Relaxed),
            implicit_overruns: stats.implicit_overruns.load(Relaxed),
            implicit_in_transfers: stats.implicit_in_transfers.load(Relaxed),
            implicit_out_of_order: stats.implicit_out_of_order.load(Relaxed),
            implicit_invalid_packets: stats.implicit_invalid_packets.load(Relaxed),
            implicit_measured_frames: stats.implicit_measured_frames.load(Relaxed),
            implicit_scheduled_frames: stats.implicit_scheduled_frames.load(Relaxed),

            stable: verdicts.stable,
            clock_verified: verdicts.clock_verified,
            payload_verified: verdicts.payload_verified,
            payload_constant_for_ms: verdicts.payload_constant_for_ms,
        }
    }
}

impl Drop for DacStream {
    fn drop(&mut self) {
        let _ = self.inner.close();
    }
}