rill_router/mixer/
channel.rs1#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum ChannelMode {
6 Mono,
8 Stereo,
10}
11
12#[derive(Debug, Clone)]
17pub struct ChannelConfig {
18 pub name: String,
20 pub mode: ChannelMode,
22 pub volume: f32,
24 pub pan: f32,
26 pub muted: bool,
28 pub soloed: bool,
30}
31
32impl Default for ChannelConfig {
33 fn default() -> Self {
34 Self {
35 name: "Channel".to_string(),
36 mode: ChannelMode::Mono,
37 volume: 1.0,
38 pan: 0.0,
39 muted: false,
40 soloed: false,
41 }
42 }
43}
44
45#[derive(Debug, Clone)]
47pub struct ChannelState {
48 config: ChannelConfig,
49 current_volume: f32,
51 current_pan: f32,
53 smoothing: f32,
55}
56
57impl ChannelState {
58 pub fn new(config: ChannelConfig) -> Self {
60 let current_volume = config.volume;
61 let current_pan = config.pan;
62 Self {
63 config,
64 current_volume,
65 current_pan,
66 smoothing: 0.1, }
68 }
69
70 pub fn process_mono(&mut self, input: f32) -> (f32, f32) {
72 if self.config.muted {
73 return (0.0, 0.0);
74 }
75
76 self.current_volume += (self.config.volume - self.current_volume) * self.smoothing;
78 self.current_pan += (self.config.pan - self.current_pan) * self.smoothing;
79
80 let (left_gain, right_gain) = if self.current_pan <= 0.0 {
82 (1.0, 1.0 + self.current_pan)
83 } else {
84 (1.0 - self.current_pan, 1.0)
85 };
86
87 let left_out = input * self.current_volume * left_gain;
88 let right_out = input * self.current_volume * right_gain;
89
90 (left_out, right_out)
91 }
92
93 pub fn process_stereo(&mut self, left: f32, right: f32) -> (f32, f32) {
95 if self.config.muted {
96 return (0.0, 0.0);
97 }
98
99 self.current_volume += (self.config.volume - self.current_volume) * self.smoothing;
101 self.current_pan += (self.config.pan - self.current_pan) * self.smoothing;
102
103 let (left_gain, right_gain) = if self.current_pan <= 0.0 {
105 (1.0, 1.0 + self.current_pan)
106 } else {
107 (1.0 - self.current_pan, 1.0)
108 };
109
110 let left_out = left * self.current_volume * left_gain;
111 let right_out = right * self.current_volume * right_gain;
112
113 (left_out, right_out)
114 }
115
116 pub fn set_config(&mut self, config: ChannelConfig) {
118 self.config = config;
119 }
120
121 pub fn config(&self) -> &ChannelConfig {
123 &self.config
124 }
125
126 pub fn set_smoothing(&mut self, factor: f32) {
128 self.smoothing = factor.clamp(0.0, 1.0);
129 }
130
131 pub fn current_volume(&self) -> f32 {
133 self.current_volume
134 }
135
136 pub fn current_pan(&self) -> f32 {
138 self.current_pan
139 }
140}