rytm_rs/object/kit/
comp.rs

1use super::types::{FxCompAttack, FxCompRatio, FxCompRelease, FxCompSideChainEq};
2use crate::error::{ConversionError, ParameterError, RytmError};
3use rytm_rs_macro::parameter_range;
4use rytm_sys::ar_kit_t;
5use serde::{Deserialize, Serialize};
6
7/// Compressor parameters for the kit.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
9pub struct FxCompressor {
10    threshold: u8,
11    attack: FxCompAttack,
12    release: FxCompRelease,
13    ratio: FxCompRatio,
14    seq: FxCompSideChainEq,
15    gain: u8,
16    mix: u8,
17    volume: u8,
18}
19
20impl Default for FxCompressor {
21    fn default() -> Self {
22        Self {
23            threshold: 96,
24            attack: FxCompAttack::default(),
25            release: FxCompRelease::default(),
26            ratio: FxCompRatio::default(),
27            seq: FxCompSideChainEq::default(),
28            gain: 0,
29            mix: 0,
30            volume: 64,
31        }
32    }
33}
34
35impl TryFrom<&ar_kit_t> for FxCompressor {
36    type Error = ConversionError;
37    fn try_from(raw_kit: &ar_kit_t) -> Result<Self, Self::Error> {
38        Ok(Self {
39            threshold: raw_kit.fx_comp_threshold,
40            attack: raw_kit.fx_comp_attack.try_into()?,
41            release: raw_kit.fx_comp_release.try_into()?,
42            ratio: raw_kit.fx_comp_ratio.try_into()?,
43            seq: raw_kit.fx_comp_seq.try_into()?,
44            gain: raw_kit.fx_comp_gain,
45            mix: raw_kit.fx_comp_mix,
46            volume: raw_kit.fx_comp_volume,
47        })
48    }
49}
50
51impl FxCompressor {
52    pub(crate) fn apply_to_raw_kit(self, raw_kit: &mut ar_kit_t) {
53        raw_kit.fx_comp_threshold = self.threshold;
54        raw_kit.fx_comp_attack = self.attack as u8;
55        raw_kit.fx_comp_release = self.release as u8;
56        raw_kit.fx_comp_ratio = self.ratio as u8;
57        raw_kit.fx_comp_seq = self.seq as u8;
58        raw_kit.fx_comp_gain = self.gain;
59        raw_kit.fx_comp_mix = self.mix;
60        raw_kit.fx_comp_volume = self.volume;
61    }
62
63    /// Sets the threshold of the compressor.
64    ///
65    /// Range: `0..=127`
66    #[parameter_range(range = "threshold:0..=127")]
67    pub fn set_threshold(&mut self, threshold: usize) -> Result<(), RytmError> {
68        self.threshold = threshold as u8;
69        Ok(())
70    }
71
72    /// Sets the attack of the compressor.
73    pub fn set_attack(&mut self, attack: FxCompAttack) {
74        self.attack = attack;
75    }
76
77    /// Sets the release of the compressor.
78    pub fn set_release(&mut self, release: FxCompRelease) {
79        self.release = release;
80    }
81
82    /// Sets the ratio of the compressor.
83    pub fn set_ratio(&mut self, ratio: FxCompRatio) {
84        self.ratio = ratio;
85    }
86
87    /// Sets the side chain eq of the compressor.
88    pub fn set_side_chain_eq(&mut self, seq: FxCompSideChainEq) {
89        self.seq = seq;
90    }
91
92    /// Sets the gain of the compressor.
93    ///
94    /// Range: `0..=127`
95    #[parameter_range(range = "gain:0..=127")]
96    pub fn set_gain(&mut self, gain: usize) -> Result<(), RytmError> {
97        self.gain = gain as u8;
98        Ok(())
99    }
100
101    /// Sets the mix of the compressor.
102    ///
103    /// Range: `0..=127`
104    #[parameter_range(range = "mix:0..=127")]
105    pub fn set_mix(&mut self, mix: usize) -> Result<(), RytmError> {
106        self.mix = mix as u8;
107        Ok(())
108    }
109
110    /// Sets the volume of the compressor.
111    ///
112    /// Range: `0..=127`
113    #[parameter_range(range = "volume:0..=127")]
114    pub fn set_volume(&mut self, volume: usize) -> Result<(), RytmError> {
115        self.volume = volume as u8;
116        Ok(())
117    }
118
119    /// Returns the threshold of the compressor.
120    ///
121    /// Range: `0..=127`
122    pub const fn threshold(&self) -> usize {
123        self.threshold as usize
124    }
125
126    /// Returns the attack of the compressor.
127    pub const fn attack(&self) -> &FxCompAttack {
128        &self.attack
129    }
130
131    /// Returns the release of the compressor.
132    pub const fn release(&self) -> &FxCompRelease {
133        &self.release
134    }
135
136    /// Returns the ratio of the compressor.
137    pub const fn ratio(&self) -> &FxCompRatio {
138        &self.ratio
139    }
140
141    /// Returns the side chain eq of the compressor.
142    pub const fn side_chain_eq(&self) -> &FxCompSideChainEq {
143        &self.seq
144    }
145
146    /// Returns the gain of the compressor.
147    ///
148    /// Range: `0..=127`
149    pub const fn gain(&self) -> usize {
150        self.gain as usize
151    }
152
153    /// Returns the mix of the compressor.
154    ///
155    /// Range: `0..=127`
156    pub const fn mix(&self) -> usize {
157        self.mix as usize
158    }
159
160    /// Returns the volume of the compressor.
161    ///
162    /// Range: `0..=127`
163    pub const fn volume(&self) -> usize {
164        self.volume as usize
165    }
166}