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
use std::collections::HashMap;
use std::io::Write;
use enum_map::{Enum, EnumMap};
use strum::{EnumIter, EnumProperty, IntoEnumIterator};
use xml::attribute::OwnedAttribute;
use xml::writer::events::StartElementBuilder;
use xml::writer::XmlEvent as XmlWriterEvent;
use xml::EventWriter;
use crate::components::colours::ColourMap;
#[derive(thiserror::Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum ParseError {
#[error("Expected int: {0}")]
ExpectedInt(#[from] std::num::ParseIntError),
#[error("Expected float: {0}")]
ExpectedFloat(#[from] std::num::ParseFloatError),
#[error("Expected enum: {0}")]
ExpectedEnum(#[from] strum::ParseError),
#[error("Invalid colours: {0}")]
InvalidColours(#[from] crate::components::colours::ParseError),
}
#[derive(Debug)]
pub struct Mixers {
mixer_table: EnumMap<InputChannels, EnumMap<OutputChannels, u16>>,
volume_table: EnumMap<FullChannelList, u8>,
colour_map: ColourMap,
}
impl Mixers {
pub fn new() -> Self {
Self {
mixer_table: EnumMap::default(),
volume_table: EnumMap::default(),
colour_map: ColourMap::new("mixerTree".to_string()),
}
}
pub fn parse_mixers(&mut self, attributes: &[OwnedAttribute]) -> Result<(), ParseError> {
for attr in attributes {
if attr.name.local_name.ends_with("Level") {
let mut found = false;
let channel = attr.name.local_name.as_str();
let channel = &channel[0..channel.len() - 5];
let value: u8 = attr.value.parse()?;
for volume in FullChannelList::iter() {
if volume.get_str("Name").unwrap() == channel {
self.volume_table[volume] = value;
found = true;
}
}
if !found {
println!("Unable to find Channel: {}", channel);
}
continue;
}
if attr.name.local_name.contains("To") {
let name = attr.name.local_name.as_str();
if let Some(middle_index) = name.find("To") {
let input = &name[0..middle_index];
let output = &name[middle_index + 2..];
let value: u16 = attr.value.parse()?;
for input_channel in InputChannels::iter() {
if input_channel.get_str("Name").unwrap() == input {
let table = &mut self.mixer_table[input_channel];
for output_channel in OutputChannels::iter() {
if output_channel.get_str("Name").unwrap() == output {
table[output_channel] = value;
}
}
}
}
}
continue;
}
if !self.colour_map.read_colours(attr)? {
println!("[MIXER] Unparsed Attribute: {}", attr.name);
}
}
Ok(())
}
pub fn write_mixers<W: Write>(
&self,
writer: &mut EventWriter<&mut W>,
) -> Result<(), xml::writer::Error> {
let mut element: StartElementBuilder = XmlWriterEvent::start_element("mixerTree");
let mut attributes: HashMap<String, String> = HashMap::default();
for volume in FullChannelList::iter() {
let key = format!("{}Level", volume.get_str("Name").unwrap());
let value = format!("{}", self.volume_table[volume]);
attributes.insert(key, value);
}
for input in InputChannels::iter() {
let input_text = input.get_str("Name").unwrap();
let table = self.mixer_table[input];
for output in OutputChannels::iter() {
let key = format!("{}To{}", input_text, output.get_str("Name").unwrap());
let value = format!("{}", table[output]);
attributes.insert(key, value);
}
}
self.colour_map.write_colours(&mut attributes);
for (key, value) in &attributes {
element = element.attr(key.as_str(), value.as_str());
}
writer.write(element)?;
writer.write(XmlWriterEvent::end_element())?;
Ok(())
}
pub fn mixer_table(&self) -> &EnumMap<InputChannels, EnumMap<OutputChannels, u16>> {
&self.mixer_table
}
pub fn mixer_table_mut(&mut self) -> &mut EnumMap<InputChannels, EnumMap<OutputChannels, u16>> {
&mut self.mixer_table
}
pub fn channel_volume(&self, channel: FullChannelList) -> u8 {
self.volume_table[channel]
}
pub fn set_channel_volume(&mut self, channel: FullChannelList, volume: u8) {
self.volume_table[channel] = volume;
}
}
#[derive(Debug, EnumIter, Enum, EnumProperty, Clone, Copy)]
pub enum InputChannels {
#[strum(props(Name = "mic"))]
Mic,
#[strum(props(Name = "chat"))]
Chat,
#[strum(props(Name = "music"))]
Music,
#[strum(props(Name = "game"))]
Game,
#[strum(props(Name = "console"))]
Console,
#[strum(props(Name = "lineIn"))]
LineIn,
#[strum(props(Name = "system"))]
System,
#[strum(props(Name = "sample"))]
Sample,
}
#[derive(Debug, EnumIter, Enum, EnumProperty)]
pub enum OutputChannels {
#[strum(props(Name = "HP"))]
Headphones,
#[strum(props(Name = "Stream"))]
Broadcast,
#[strum(props(Name = "LineOut"))]
LineOut,
#[strum(props(Name = "Chat"))]
ChatMic,
#[strum(props(Name = "Sampler"))]
Sampler,
}
#[derive(Copy, Clone, Debug, Enum, EnumIter, EnumProperty)]
pub enum FullChannelList {
#[strum(props(Name = "mic", faderIndex = "0"))]
Mic,
#[strum(props(Name = "chat", faderIndex = "1"))]
Chat,
#[strum(props(Name = "music", faderIndex = "2"))]
Music,
#[strum(props(Name = "game", faderIndex = "3"))]
Game,
#[strum(props(Name = "console", faderIndex = "4"))]
Console,
#[strum(props(Name = "lineIn", faderIndex = "5"))]
LineIn,
#[strum(props(Name = "system", faderIndex = "6"))]
System,
#[strum(props(Name = "sample", faderIndex = "7"))]
Sample,
#[strum(props(Name = "headphone", faderIndex = "8"))]
Headphones,
#[strum(props(Name = "mic2headphoneSub", faderIndex = "-1"))]
MicMonitor,
#[strum(props(Name = "lineOut", faderIndex = "9"))]
LineOut,
}