pikopiko 0.1.0

MML parser, compiler and chiptune player for games, with diagnostics designed for tools and AI.
Documentation
//! サチュレーション(`%sat`、0〜100)。音源全体を `tanh` でやわらかく歪ませ、音を太く・前に出す。
//!
//! `tanh(x·k) / tanh(k)`(k = 1 + sat/20)で、小さい音はほぼそのまま、大きい音ほど丸める。
//! 0 なら何もしない。状態は持たない。

use crate::synth::va::engine::param;
use crate::synth::va::fx::{Effect, Params};
use crate::synth::va::m;

/// チェーンに入れるサチュレーション。
pub(crate) struct SaturateFx {
    /// 歪ませる強さ(`tanh` に入れる前の倍率)。
    k: f32,
}

impl SaturateFx {
    pub(crate) const fn new() -> Self {
        SaturateFx { k: 1.0 }
    }
}

impl Effect for SaturateFx {
    fn update(&mut self, p: &Params<'_>, _sample_rate: f32) -> bool {
        let sat = p.get(param::SAT);
        if sat > 0.0 {
            self.k = 1.0 + sat / 20.0;
        }
        sat > 0.0
    }

    fn process(&mut self, x: [f32; 2], _sample_rate: f32) -> [f32; 2] {
        let k = self.k;
        [
            m::tanh(x[0] * k) / m::tanh(k),
            m::tanh(x[1] * k) / m::tanh(k),
        ]
    }

    fn clear(&mut self) {}
}