Skip to main content

rill_io/processor/
basic.rs

1//! Базовые процессоры для тестирования и отладки
2
3use crate::engine::AudioProcessor;
4
5/// Процессор, пропускающий входной сигнал без изменений
6pub struct PassThroughProcessor;
7
8impl AudioProcessor for PassThroughProcessor {
9    fn process(&mut self, input: &[f32], output: &mut [f32]) {
10        output.copy_from_slice(input);
11    }
12
13    fn reset(&mut self) {}
14
15    fn set_sample_rate(&mut self, _sample_rate: f32) {}
16}
17
18/// Процессор, генерирующий тишину (все нули)
19pub struct SilenceProcessor;
20
21impl AudioProcessor for SilenceProcessor {
22    fn process(&mut self, _input: &[f32], output: &mut [f32]) {
23        output.fill(0.0);
24    }
25
26    fn reset(&mut self) {}
27
28    fn set_sample_rate(&mut self, _sample_rate: f32) {}
29}
30
31/// Процессор, усиливающий сигнал с заданным коэффициентом
32pub struct GainProcessor {
33    gain: f32,
34}
35
36impl GainProcessor {
37    /// Создать новый процессор с коэффициентом усиления
38    pub fn new(gain: f32) -> Self {
39        Self {
40            gain: gain.max(0.0),
41        }
42    }
43
44    /// Установить коэффициент усиления
45    pub fn set_gain(&mut self, gain: f32) {
46        self.gain = gain.max(0.0);
47    }
48}
49
50impl AudioProcessor for GainProcessor {
51    fn process(&mut self, input: &[f32], output: &mut [f32]) {
52        for (i, sample) in input.iter().enumerate() {
53            if i < output.len() {
54                output[i] = sample * self.gain;
55            }
56        }
57    }
58
59    fn reset(&mut self) {}
60
61    fn set_sample_rate(&mut self, _sample_rate: f32) {}
62}
63
64/// Процессор, смешивающий два канала в моно
65pub struct MonoMixerProcessor;
66
67impl AudioProcessor for MonoMixerProcessor {
68    fn process(&mut self, input: &[f32], output: &mut [f32]) {
69        let stereo_samples = input.len() / 2;
70        for i in 0..stereo_samples.min(output.len()) {
71            output[i] = (input[i * 2] + input[i * 2 + 1]) * 0.5;
72        }
73    }
74
75    fn reset(&mut self) {}
76
77    fn set_sample_rate(&mut self, _sample_rate: f32) {}
78}
79
80/// Процессор, записывающий входной сигнал в кольцевой буфер (для отладки)
81#[cfg(feature = "examples")]
82pub struct CaptureProcessor {
83    buffer: Vec<f32>,
84    position: usize,
85}
86
87#[cfg(feature = "examples")]
88impl CaptureProcessor {
89    pub fn new(capacity: usize) -> Self {
90        Self {
91            buffer: vec![0.0; capacity],
92            position: 0,
93        }
94    }
95
96    pub fn get_buffer(&self) -> &[f32] {
97        &self.buffer
98    }
99}
100
101#[cfg(feature = "examples")]
102impl AudioProcessor for CaptureProcessor {
103    fn process(&mut self, input: &[f32], output: &mut [f32]) {
104        // Просто копируем вход в выход
105        output.copy_from_slice(input);
106
107        // И записываем в буфер
108        for sample in input {
109            self.buffer[self.position] = *sample;
110            self.position = (self.position + 1) % self.buffer.len();
111        }
112    }
113
114    fn reset(&mut self) {
115        self.buffer.fill(0.0);
116        self.position = 0;
117    }
118
119    fn set_sample_rate(&mut self, _sample_rate: f32) {}
120}