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
use std::collections::HashMap;
use std::io::Write;
use std::str::FromStr;
use xml::attribute::OwnedAttribute;
use xml::writer::events::StartElementBuilder;
use xml::writer::XmlEvent as XmlWriterEvent;
use xml::EventWriter;

#[derive(thiserror::Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum ParseError {
    #[error("Expected int: {0}")]
    ExpectedInt(#[from] std::num::ParseIntError),
}

#[derive(Debug)]
pub struct MicSetup {
    mic_type: u8,

    // These are super weird, in the config they're stored as dB * 65536!
    dynamic_mic_gain: u16,
    condenser_mic_gain: u16,
    trs_mic_gain: u16,
}

impl MicSetup {
    pub fn new() -> Self {
        Self {
            mic_type: 0,
            dynamic_mic_gain: 0,
            condenser_mic_gain: 0,
            trs_mic_gain: 0,
        }
    }

    pub fn parse_config(&mut self, attributes: &[OwnedAttribute]) -> Result<(), ParseError> {
        for attr in attributes {
            if attr.name.local_name == "MIC_TYPE" {
                self.mic_type = u8::from_str(attr.value.as_str())?;
                continue;
            }

            if attr.name.local_name == "DYNAMIC_MIC_GAIN" {
                self.dynamic_mic_gain = (u32::from_str(attr.value.as_str())? / 65536) as u16;
                continue;
            }

            if attr.name.local_name == "CONDENSER_MIC_GAIN" {
                self.condenser_mic_gain = (u32::from_str(attr.value.as_str())? / 65536) as u16;
                continue;
            }

            if attr.name.local_name == "TRS_MIC_GAIN" {
                self.trs_mic_gain = (u32::from_str(attr.value.as_str())? / 65536) as u16;
                continue;
            }
        }

        Ok(())
    }

    pub fn write_config<W: Write>(
        &self,
        writer: &mut EventWriter<&mut W>,
    ) -> Result<(), xml::writer::Error> {
        let mut element: StartElementBuilder = XmlWriterEvent::start_element("setupTreeMicProfile");

        let mut attributes: HashMap<String, String> = HashMap::default();
        attributes.insert("MIC_TYPE".to_string(), format!("{}", self.mic_type));
        attributes.insert(
            "DYNAMIC_MIC_GAIN".to_string(),
            format!("{}", (self.dynamic_mic_gain as u32 * 65536)),
        );
        attributes.insert(
            "CONDENSER_MIC_GAIN".to_string(),
            format!("{}", (self.condenser_mic_gain as u32 * 65536)),
        );
        attributes.insert(
            "TRS_MIC_GAIN".to_string(),
            format!("{}", (self.trs_mic_gain as u32 * 65536)),
        );

        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 mic_type(&self) -> u8 {
        self.mic_type
    }

    pub fn dynamic_mic_gain(&self) -> u16 {
        self.dynamic_mic_gain
    }

    pub fn condenser_mic_gain(&self) -> u16 {
        self.condenser_mic_gain
    }

    pub fn trs_mic_gain(&self) -> u16 {
        self.trs_mic_gain
    }

    pub fn set_mic_type(&mut self, mic_type: u8) {
        self.mic_type = mic_type;
    }

    pub fn set_dynamic_mic_gain(&mut self, dynamic_mic_gain: u16) {
        self.dynamic_mic_gain = dynamic_mic_gain;
    }
    pub fn set_condenser_mic_gain(&mut self, condenser_mic_gain: u16) {
        self.condenser_mic_gain = condenser_mic_gain;
    }
    pub fn set_trs_mic_gain(&mut self, trs_mic_gain: u16) {
        self.trs_mic_gain = trs_mic_gain;
    }
}