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
use std::collections::HashMap;
use std::os::raw::c_float;
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 Gate {
    amount: u8,
    threshold: i8,
    attack: u8,
    release: u8,
    enabled: bool,
    attenuation: u8,
}

impl Gate {
    pub fn new() -> Self {
        Self {
            amount: 0,
            threshold: 0,
            attack: 0,
            release: 0,
            enabled: false,
            attenuation: 0,
        }
    }

    pub fn parse_gate(&mut self, attributes: &[OwnedAttribute]) -> Result<(), ParseError> {
        for attr in attributes {
            if attr.name.local_name == "MIC_GATE_MACRO_AMOUNT" {
                self.amount = attr.value.parse::<c_float>()? as u8;
                continue;
            }

            if attr.name.local_name == "MIC_GATE_THRESOLD" {
                self.threshold = attr.value.parse::<c_float>()? as i8;
                continue;
            }

            if attr.name.local_name == "MIC_GATE_ATTACK" {
                self.attack = attr.value.parse::<c_float>()? as u8;
                continue;
            }

            if attr.name.local_name == "MIC_GATE_RELEASE" {
                self.release = attr.value.parse::<c_float>()? as u8;
                continue;
            }

            if attr.name.local_name == "MIC_GATE_ENABLE" {
                if attr.value == "0" {
                    self.enabled = false;
                } else {
                    self.enabled = true;
                }
                continue;
            }

            // Read and handle as a percentage.
            if attr.name.local_name == "MIC_GATE_ATTEN" {
                self.attenuation = attr.value.parse::<c_float>()? as u8;
                continue;
            }
        }

        Ok(())
    }

    pub fn write_gate(&self, attributes: &mut HashMap<String, String>) {
        attributes.insert(
            "MIC_GATE_MACRO_AMOUNT".to_string(),
            format!("{}", self.amount),
        );
        attributes.insert(
            "MIC_GATE_THRESOLD".to_string(),
            format!("{}", self.threshold),
        );
        attributes.insert("MIC_GATE_ATTACK".to_string(), format!("{}", self.attack));
        attributes.insert("MIC_GATE_RELEASE".to_string(), format!("{}", self.release));
        attributes.insert(
            "MIC_GATE_ENABLE".to_string(),
            format!("{}", self.enabled as u8),
        );
        attributes.insert(
            "MIC_GATE_ATTEN".to_string(),
            //format!("{}", ((self.attenuation as f32 / -61 as f32) * 100 as f32) as u8),
            format!("{}", self.attenuation)
        );
    }

    pub fn amount(&self) -> u8 { self.amount }
    pub fn enabled(&self) -> bool { self.enabled }
    pub fn threshold(&self) -> i8 { self.threshold }
    pub fn attack(&self) -> u8 { self.attack }
    pub fn release(&self) -> u8 { self.release }
    pub fn attenuation(&self) -> u8 { self.attenuation }


    pub fn set_amount(&mut self, amount: u8) {
        self.amount = amount;
    }
    pub fn set_threshold(&mut self, threshold: i8) {
        self.threshold = threshold;
    }
    pub fn set_attack(&mut self, attack: u8) {
        self.attack = attack;
    }
    pub fn set_release(&mut self, release: u8) {
        self.release = release;
    }
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }
    pub fn set_attenuation(&mut self, attenuation: u8) {
        self.attenuation = attenuation;
    }
}