Skip to main content

rill_io/backends/
null.rs

1//! Null бэкенд для тестирования
2
3use std::time::Duration;
4
5use crate::backend::{AudioBackend, BackendType};
6use crate::config::AudioConfig;
7use crate::error::IoResult;
8
9/// Null бэкенд - не производит реального аудио ввода-вывода
10#[derive(Debug)]
11pub struct NullBackend {
12    config: AudioConfig,
13    is_running: bool,
14    xruns: u32,
15}
16
17impl NullBackend {
18    /// Создать новый Null бэкенд
19    pub fn new(config: AudioConfig) -> Self {
20        Self {
21            config,
22            is_running: false,
23            xruns: 0,
24        }
25    }
26}
27
28impl AudioBackend for NullBackend {
29    fn backend_type(&self) -> BackendType {
30        BackendType::Null
31    }
32
33    fn config(&self) -> &AudioConfig {
34        &self.config
35    }
36
37    fn config_mut(&mut self) -> &mut AudioConfig {
38        &mut self.config
39    }
40
41    fn init(&mut self) -> IoResult<()> {
42        Ok(())
43    }
44
45    fn start(&mut self) -> IoResult<()> {
46        self.is_running = true;
47        Ok(())
48    }
49
50    fn stop(&mut self) -> IoResult<()> {
51        self.is_running = false;
52        Ok(())
53    }
54
55    fn read(&mut self, buffer: &mut [f32]) -> IoResult<usize> {
56        buffer.fill(0.0);
57        Ok(buffer.len())
58    }
59
60    fn write(&mut self, buffer: &[f32]) -> IoResult<usize> {
61        Ok(buffer.len())
62    }
63
64    fn xruns(&self) -> u32 {
65        self.xruns
66    }
67
68    fn latency(&self) -> Duration {
69        Duration::from_micros(
70            (1_000_000.0 * self.config.buffer_size as f64 / self.config.sample_rate as f64) as u64,
71        )
72    }
73
74    fn list_input_devices(&self) -> Vec<String> {
75        vec!["Null Input".to_string()]
76    }
77
78    fn list_output_devices(&self) -> Vec<String> {
79        vec!["Null Output".to_string()]
80    }
81}