rytm-rs 0.1.3

More than safe rust abstractions over rytm-sys, an unofficial SDK for Analog Rytm MKII running firmware 1.70
Documentation
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
    }
}