rytm_rs/object/kit/
comp.rs

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
use super::types::{FxCompAttack, FxCompRatio, FxCompRelease, FxCompSideChainEq};
use crate::error::{ConversionError, ParameterError, RytmError};
use rytm_rs_macro::parameter_range;
use rytm_sys::ar_kit_t;
use serde::{Deserialize, Serialize};

/// Compressor parameters for the kit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct FxCompressor {
    threshold: u8,
    attack: FxCompAttack,
    release: FxCompRelease,
    ratio: FxCompRatio,
    seq: FxCompSideChainEq,
    gain: u8,
    mix: u8,
    volume: u8,
}

impl Default for FxCompressor {
    fn default() -> Self {
        Self {
            threshold: 96,
            attack: FxCompAttack::default(),
            release: FxCompRelease::default(),
            ratio: FxCompRatio::default(),
            seq: FxCompSideChainEq::default(),
            gain: 0,
            mix: 0,
            volume: 64,
        }
    }
}

impl TryFrom<&ar_kit_t> for FxCompressor {
    type Error = ConversionError;
    fn try_from(raw_kit: &ar_kit_t) -> Result<Self, Self::Error> {
        Ok(Self {
            threshold: raw_kit.fx_comp_threshold,
            attack: raw_kit.fx_comp_attack.try_into()?,
            release: raw_kit.fx_comp_release.try_into()?,
            ratio: raw_kit.fx_comp_ratio.try_into()?,
            seq: raw_kit.fx_comp_seq.try_into()?,
            gain: raw_kit.fx_comp_gain,
            mix: raw_kit.fx_comp_mix,
            volume: raw_kit.fx_comp_volume,
        })
    }
}

impl FxCompressor {
    pub(crate) fn apply_to_raw_kit(self, raw_kit: &mut ar_kit_t) {
        raw_kit.fx_comp_threshold = self.threshold;
        raw_kit.fx_comp_attack = self.attack as u8;
        raw_kit.fx_comp_release = self.release as u8;
        raw_kit.fx_comp_ratio = self.ratio as u8;
        raw_kit.fx_comp_seq = self.seq as u8;
        raw_kit.fx_comp_gain = self.gain;
        raw_kit.fx_comp_mix = self.mix;
        raw_kit.fx_comp_volume = self.volume;
    }

    /// Sets the threshold of the compressor.
    ///
    /// Range: `0..=127`
    #[parameter_range(range = "threshold:0..=127")]
    pub fn set_threshold(&mut self, threshold: usize) -> Result<(), RytmError> {
        self.threshold = threshold as u8;
        Ok(())
    }

    /// Sets the attack of the compressor.
    pub fn set_attack(&mut self, attack: FxCompAttack) {
        self.attack = attack;
    }

    /// Sets the release of the compressor.
    pub fn set_release(&mut self, release: FxCompRelease) {
        self.release = release;
    }

    /// Sets the ratio of the compressor.
    pub fn set_ratio(&mut self, ratio: FxCompRatio) {
        self.ratio = ratio;
    }

    /// Sets the side chain eq of the compressor.
    pub fn set_side_chain_eq(&mut self, seq: FxCompSideChainEq) {
        self.seq = seq;
    }

    /// Sets the gain of the compressor.
    ///
    /// Range: `0..=127`
    #[parameter_range(range = "gain:0..=127")]
    pub fn set_gain(&mut self, gain: usize) -> Result<(), RytmError> {
        self.gain = gain as u8;
        Ok(())
    }

    /// Sets the mix of the compressor.
    ///
    /// Range: `0..=127`
    #[parameter_range(range = "mix:0..=127")]
    pub fn set_mix(&mut self, mix: usize) -> Result<(), RytmError> {
        self.mix = mix as u8;
        Ok(())
    }

    /// Sets the volume of the compressor.
    ///
    /// Range: `0..=127`
    #[parameter_range(range = "volume:0..=127")]
    pub fn set_volume(&mut self, volume: usize) -> Result<(), RytmError> {
        self.volume = volume as u8;
        Ok(())
    }

    /// Returns the threshold of the compressor.
    ///
    /// Range: `0..=127`
    pub const fn threshold(&self) -> usize {
        self.threshold as usize
    }

    /// Returns the attack of the compressor.
    pub const fn attack(&self) -> &FxCompAttack {
        &self.attack
    }

    /// Returns the release of the compressor.
    pub const fn release(&self) -> &FxCompRelease {
        &self.release
    }

    /// Returns the ratio of the compressor.
    pub const fn ratio(&self) -> &FxCompRatio {
        &self.ratio
    }

    /// Returns the side chain eq of the compressor.
    pub const fn side_chain_eq(&self) -> &FxCompSideChainEq {
        &self.seq
    }

    /// Returns the gain of the compressor.
    ///
    /// Range: `0..=127`
    pub const fn gain(&self) -> usize {
        self.gain as usize
    }

    /// Returns the mix of the compressor.
    ///
    /// Range: `0..=127`
    pub const fn mix(&self) -> usize {
        self.mix as usize
    }

    /// Returns the volume of the compressor.
    ///
    /// Range: `0..=127`
    pub const fn volume(&self) -> usize {
        self.volume as usize
    }
}