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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use std::collections::HashMap;
use std::os::raw::c_float;
use std::str::FromStr;
use xml::attribute::OwnedAttribute;
#[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),
}
#[derive(Debug)]
pub struct Equalizer {
eq_31h_gain: i8,
eq_63h_gain: i8,
eq_125h_gain: i8,
eq_250h_gain: i8,
eq_500h_gain: i8,
eq_1k_gain: i8,
eq_2k_gain: i8,
eq_4k_gain: i8,
eq_8k_gain: i8,
eq_16k_gain: i8,
eq_31h_freq: f32,
eq_63h_freq: f32,
eq_125h_freq: f32,
eq_250h_freq: f32,
eq_500h_freq: f32,
eq_1k_freq: f32,
eq_2k_freq: f32,
eq_4k_freq: f32,
eq_8k_freq: f32,
eq_16k_freq: f32,
}
impl Equalizer {
pub fn new() -> Self {
Self {
eq_31h_gain: 0,
eq_63h_gain: 0,
eq_125h_gain: 0,
eq_250h_gain: 0,
eq_500h_gain: 0,
eq_1k_gain: 0,
eq_2k_gain: 0,
eq_4k_gain: 0,
eq_8k_gain: 0,
eq_16k_gain: 0,
eq_31h_freq: 31.5,
eq_63h_freq: 63.0,
eq_125h_freq: 125.0,
eq_250h_freq: 250.0,
eq_500h_freq: 500.0,
eq_1k_freq: 1000.0,
eq_2k_freq: 2000.0,
eq_4k_freq: 4000.0,
eq_8k_freq: 8000.0,
eq_16k_freq: 16000.0,
}
}
pub fn parse_equaliser(&mut self, attributes: &[OwnedAttribute]) -> Result<(), ParseError> {
for attr in attributes {
if attr.name.local_name == "MIC_EQ_31.5HZ_GAIN" {
self.eq_31h_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_63HZ_GAIN" {
self.eq_63h_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_125HZ_GAIN" {
self.eq_125h_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_250HZ_GAIN" {
self.eq_250h_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_500HZ_GAIN" {
self.eq_500h_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_1KHZ_GAIN" {
self.eq_1k_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_2KHZ_GAIN" {
self.eq_2k_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_4KHZ_GAIN" {
self.eq_4k_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_8KHZ_GAIN" {
self.eq_8k_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_16KHZ_GAIN" {
self.eq_16k_gain = attr.value.parse::<c_float>()? as i8
}
if attr.name.local_name == "MIC_EQ_31.5HZ_F" {
self.eq_31h_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_63HZ_F" {
self.eq_63h_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_125HZ_F" {
self.eq_125h_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_250HZ_F" {
self.eq_250h_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_500HZ_F" {
self.eq_500h_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_1KHZ_F" {
self.eq_1k_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_2KHZ_F" {
self.eq_2k_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_4KHZ_F" {
self.eq_4k_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_8KHZ_F" {
self.eq_8k_freq = f32::from_str(attr.value.as_str())?;
}
if attr.name.local_name == "MIC_EQ_16KHZ_F" {
self.eq_16k_freq = f32::from_str(attr.value.as_str())?;
}
}
Ok(())
}
pub fn write_equaliser(&self, attributes: &mut HashMap<String, String>) {
attributes.insert(
"MIC_EQ_31.5HZ_GAIN".to_string(),
format!("{}", self.eq_31h_gain),
);
attributes.insert(
"MIC_EQ_63HZ_GAIN".to_string(),
format!("{}", self.eq_63h_gain),
);
attributes.insert(
"MIC_EQ_125HZ_GAIN".to_string(),
format!("{}", self.eq_125h_gain),
);
attributes.insert(
"MIC_EQ_250HZ_GAIN".to_string(),
format!("{}", self.eq_250h_gain),
);
attributes.insert(
"MIC_EQ_500HZ_GAIN".to_string(),
format!("{}", self.eq_500h_gain),
);
attributes.insert(
"MIC_EQ_1KHZ_GAIN".to_string(),
format!("{}", self.eq_1k_gain),
);
attributes.insert(
"MIC_EQ_2KHZ_GAIN".to_string(),
format!("{}", self.eq_2k_gain),
);
attributes.insert(
"MIC_EQ_4KHZ_GAIN".to_string(),
format!("{}", self.eq_4k_gain),
);
attributes.insert(
"MIC_EQ_8KHZ_GAIN".to_string(),
format!("{}", self.eq_8k_gain),
);
attributes.insert(
"MIC_EQ_16KHZ_GAIN".to_string(),
format!("{}", self.eq_16k_gain),
);
attributes.insert(
"MIC_EQ_31.5HZ_F".to_string(),
format!("{}", self.eq_31h_freq),
);
attributes.insert("MIC_EQ_63HZ_F".to_string(), format!("{}", self.eq_63h_freq));
attributes.insert(
"MIC_EQ_125HZ_F".to_string(),
format!("{}", self.eq_125h_freq),
);
attributes.insert(
"MIC_EQ_250HZ_F".to_string(),
format!("{}", self.eq_250h_freq),
);
attributes.insert(
"MIC_EQ_500HZ_F".to_string(),
format!("{}", self.eq_500h_freq),
);
attributes.insert("MIC_EQ_1KHZ_F".to_string(), format!("{}", self.eq_1k_freq));
attributes.insert("MIC_EQ_2KHZ_F".to_string(), format!("{}", self.eq_2k_freq));
attributes.insert("MIC_EQ_4KHZ_F".to_string(), format!("{}", self.eq_4k_freq));
attributes.insert("MIC_EQ_8KHZ_F".to_string(), format!("{}", self.eq_8k_freq));
attributes.insert(
"MIC_EQ_16KHZ_F".to_string(),
format!("{}", self.eq_16k_freq),
);
}
pub fn eq_31h_gain(&self) -> i8 {
self.eq_31h_gain
}
pub fn eq_63h_gain(&self) -> i8 {
self.eq_63h_gain
}
pub fn eq_125h_gain(&self) -> i8 {
self.eq_125h_gain
}
pub fn eq_250h_gain(&self) -> i8 {
self.eq_250h_gain
}
pub fn eq_500h_gain(&self) -> i8 {
self.eq_500h_gain
}
pub fn eq_1k_gain(&self) -> i8 {
self.eq_1k_gain
}
pub fn eq_2k_gain(&self) -> i8 {
self.eq_2k_gain
}
pub fn eq_4k_gain(&self) -> i8 {
self.eq_4k_gain
}
pub fn eq_8k_gain(&self) -> i8 {
self.eq_8k_gain
}
pub fn eq_16k_gain(&self) -> i8 {
self.eq_16k_gain
}
pub fn eq_31h_freq(&self) -> f32 {
self.eq_31h_freq
}
pub fn eq_63h_freq(&self) -> f32 {
self.eq_63h_freq
}
pub fn eq_125h_freq(&self) -> f32 {
self.eq_125h_freq
}
pub fn eq_250h_freq(&self) -> f32 {
self.eq_250h_freq
}
pub fn eq_500h_freq(&self) -> f32 {
self.eq_500h_freq
}
pub fn eq_1k_freq(&self) -> f32 {
self.eq_1k_freq
}
pub fn eq_2k_freq(&self) -> f32 {
self.eq_2k_freq
}
pub fn eq_4k_freq(&self) -> f32 {
self.eq_4k_freq
}
pub fn eq_8k_freq(&self) -> f32 {
self.eq_8k_freq
}
pub fn eq_16k_freq(&self) -> f32 {
self.eq_16k_freq
}
pub fn eq_31h_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_31h_freq)
}
pub fn eq_63h_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_63h_freq)
}
pub fn eq_125h_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_125h_freq)
}
pub fn eq_250h_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_250h_freq)
}
pub fn eq_500h_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_500h_freq)
}
pub fn eq_1k_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_1k_freq)
}
pub fn eq_2k_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_2k_freq)
}
pub fn eq_4k_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_4k_freq)
}
pub fn eq_8k_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_8k_freq)
}
pub fn eq_16k_freq_as_goxlr(&self) -> i32 {
self.freq_value(self.eq_16k_freq)
}
pub fn set_eq_31h_gain(&mut self, eq_31h_gain: i8) {
self.eq_31h_gain = eq_31h_gain;
}
pub fn set_eq_63h_gain(&mut self, eq_63h_gain: i8) {
self.eq_63h_gain = eq_63h_gain;
}
pub fn set_eq_125h_gain(&mut self, eq_125h_gain: i8) {
self.eq_125h_gain = eq_125h_gain;
}
pub fn set_eq_250h_gain(&mut self, eq_250h_gain: i8) {
self.eq_250h_gain = eq_250h_gain;
}
pub fn set_eq_500h_gain(&mut self, eq_500h_gain: i8) {
self.eq_500h_gain = eq_500h_gain;
}
pub fn set_eq_1k_gain(&mut self, eq_1k_gain: i8) {
self.eq_1k_gain = eq_1k_gain;
}
pub fn set_eq_2k_gain(&mut self, eq_2k_gain: i8) {
self.eq_2k_gain = eq_2k_gain;
}
pub fn set_eq_4k_gain(&mut self, eq_4k_gain: i8) {
self.eq_4k_gain = eq_4k_gain;
}
pub fn set_eq_8k_gain(&mut self, eq_8k_gain: i8) {
self.eq_8k_gain = eq_8k_gain;
}
pub fn set_eq_16k_gain(&mut self, eq_16k_gain: i8) {
self.eq_16k_gain = eq_16k_gain;
}
pub fn set_eq_31h_freq(&mut self, eq_31h_freq: f32) {
self.eq_31h_freq = eq_31h_freq;
}
pub fn set_eq_63h_freq(&mut self, eq_63h_freq: f32) {
self.eq_63h_freq = eq_63h_freq;
}
pub fn set_eq_125h_freq(&mut self, eq_125h_freq: f32) {
self.eq_125h_freq = eq_125h_freq;
}
pub fn set_eq_250h_freq(&mut self, eq_250h_freq: f32) {
self.eq_250h_freq = eq_250h_freq;
}
pub fn set_eq_500h_freq(&mut self, eq_500h_freq: f32) {
self.eq_500h_freq = eq_500h_freq;
}
pub fn set_eq_1k_freq(&mut self, eq_1k_freq: f32) {
self.eq_1k_freq = eq_1k_freq;
}
pub fn set_eq_2k_freq(&mut self, eq_2k_freq: f32) {
self.eq_2k_freq = eq_2k_freq;
}
pub fn set_eq_4k_freq(&mut self, eq_4k_freq: f32) {
self.eq_4k_freq = eq_4k_freq;
}
pub fn set_eq_8k_freq(&mut self, eq_8k_freq: f32) {
self.eq_8k_freq = eq_8k_freq;
}
pub fn set_eq_16k_freq(&mut self, eq_16k_freq: f32) {
self.eq_16k_freq = eq_16k_freq;
}
fn freq_value(&self, freq: f32) -> i32 {
return ((24.0 * (freq / 20.0).log2()) as f32).round() as i32;
}
}