Skip to main content

rustriff_lib/domain/dto/
buffer_latency_dto.rs

1//! DTO for the I/O buffer latency of the current CPAL stream configuration.
2//!
3//! Produced by [`AudioLatencyMeasurementService::measure_buffer_latency`] and
4//! serialised across the Tauri IPC boundary to the developer-mode UI overlay.
5//!
6//! [`AudioLatencyMeasurementService::measure_buffer_latency`]: crate::services::audio_latency_measurement_service::AudioLatencyMeasurementService::measure_buffer_latency
7
8use serde::{Deserialize, Serialize};
9
10/// I/O buffer latency for the current audio configuration.
11///
12/// Buffer latency is the delay imposed by the hardware frame buffers: the driver
13/// accumulates a full block of samples before delivering them to (or accepting them
14/// from) the application.  The formula is:
15///
16/// ```text
17/// latency_ms = (buffer_frames / sample_rate_hz) × 1000
18/// ```
19///
20/// When [`cpal::BufferSize::Default`] is in use the actual frame count is unknown at
21/// runtime; a conservative fallback of **256 frames** is substituted so the UI can
22/// always display a practical estimate.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(crate = "serde")]
25pub struct BufferLatencyDto {
26    /// Input-side buffering delay in milliseconds.
27    ///
28    /// Time the ADC driver holds samples in its internal buffer before delivering
29    /// them to the application ring buffer.
30    pub input_buffer_latency_ms: f64,
31
32    /// Output-side buffering delay in milliseconds.
33    ///
34    /// Time the application must fill the DAC driver buffer before the first sample
35    /// is actually played.
36    pub output_buffer_latency_ms: f64,
37
38    /// Total I/O buffer delay: `input_buffer_latency_ms + output_buffer_latency_ms`.
39    ///
40    /// This is the minimum hardware-buffer component of the full round-trip latency.
41    pub total_buffer_latency_ms: f64,
42}
43
44impl BufferLatencyDto {
45    /// Creates a new `BufferLatencyDto`, computing `total_buffer_latency_ms` automatically.
46    ///
47    /// # Arguments
48    ///
49    /// * `input_buffer_latency_ms` — Input buffering delay in ms.
50    /// * `output_buffer_latency_ms` — Output buffering delay in ms.
51    pub fn new(input_buffer_latency_ms: f64, output_buffer_latency_ms: f64) -> Self {
52        Self {
53            input_buffer_latency_ms,
54            output_buffer_latency_ms,
55            total_buffer_latency_ms: input_buffer_latency_ms + output_buffer_latency_ms,
56        }
57    }
58}