rytm_rs/object/kit/
comp.rsuse 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};
#[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;
}
#[parameter_range(range = "threshold:0..=127")]
pub fn set_threshold(&mut self, threshold: usize) -> Result<(), RytmError> {
self.threshold = threshold as u8;
Ok(())
}
pub fn set_attack(&mut self, attack: FxCompAttack) {
self.attack = attack;
}
pub fn set_release(&mut self, release: FxCompRelease) {
self.release = release;
}
pub fn set_ratio(&mut self, ratio: FxCompRatio) {
self.ratio = ratio;
}
pub fn set_side_chain_eq(&mut self, seq: FxCompSideChainEq) {
self.seq = seq;
}
#[parameter_range(range = "gain:0..=127")]
pub fn set_gain(&mut self, gain: usize) -> Result<(), RytmError> {
self.gain = gain as u8;
Ok(())
}
#[parameter_range(range = "mix:0..=127")]
pub fn set_mix(&mut self, mix: usize) -> Result<(), RytmError> {
self.mix = mix as u8;
Ok(())
}
#[parameter_range(range = "volume:0..=127")]
pub fn set_volume(&mut self, volume: usize) -> Result<(), RytmError> {
self.volume = volume as u8;
Ok(())
}
pub const fn threshold(&self) -> usize {
self.threshold as usize
}
pub const fn attack(&self) -> &FxCompAttack {
&self.attack
}
pub const fn release(&self) -> &FxCompRelease {
&self.release
}
pub const fn ratio(&self) -> &FxCompRatio {
&self.ratio
}
pub const fn side_chain_eq(&self) -> &FxCompSideChainEq {
&self.seq
}
pub const fn gain(&self) -> usize {
self.gain as usize
}
pub const fn mix(&self) -> usize {
self.mix as usize
}
pub const fn volume(&self) -> usize {
self.volume as usize
}
}