pikopiko 0.1.0

MML parser, compiler and chiptune player for games, with diagnostics designed for tools and AI.
Documentation
//! 重低音の持ち上げ(`%bass_boost`、dB)。音源全体の 90Hz あたりから下を足して太くする。
//!
//! ローパス(90Hz、左右)で取り出した低音を、元の音に `10^(dB/20) - 1` 倍だけ足す(シェルフ EQ と
//! 同じ効き方)。ローパスの状態は `bass_boost` が 0 の間も進める(あとで持ち上げ始めたときに、
//! 低音が途中から急に出てこないように)。

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

/// 低音を取り出すローパスの周波数(Hz)と、その Q の値(`Svf` の k)。
const SHELF_HZ: f32 = 90.0;
const SHELF_K: f32 = 1.4;

/// チェーンに入れる重低音の持ち上げ。
pub(crate) struct BassBoostFx {
    shelf: [Svf; 2],
    /// 足す低音の倍率(`10^(dB/20) - 1`)。
    boost: f32,
}

impl BassBoostFx {
    pub(crate) fn new(sample_rate: u32) -> Self {
        let mut shelf = [Svf::default(); 2];
        for f in &mut shelf {
            f.set(SHELF_HZ, SHELF_K, sample_rate as f32);
        }
        BassBoostFx { shelf, boost: 0.0 }
    }
}

impl Effect for BassBoostFx {
    fn update(&mut self, p: &Params<'_>, _sample_rate: f32) -> bool {
        self.boost = m::powf(10.0, p.get(param::BASS_BOOST) / 20.0) - 1.0;
        // ローパスの状態を止めないため、いつも通す(0dB なら足す量が 0)
        true
    }

    fn process(&mut self, x: [f32; 2], _sample_rate: f32) -> [f32; 2] {
        let mut out = x;
        for (o, f) in out.iter_mut().zip(&mut self.shelf) {
            let low = f.process(*o).lp;
            *o += low * self.boost;
        }
        out
    }

    fn clear(&mut self) {
        // 係数(90Hz)はそのまま、状態だけ戻す
        for f in &mut self.shelf {
            f.clear();
        }
    }
}