SPB Git

Repository Public

spatial.rs 8.1 KB 249 lines rust

1//! ORIX Sonata2//! File: crates/sonata-dsp/src/effects/spatial.rs3//! Description: Stereo and amplitude modulation effects: stereo width, tremolo, auto-pan.4//!5//! Author: Simon-Pierre Boucher6//! Contact: contact@spboucher.ai78use super::{choice, desc, num, preset};9use crate::core::OnePoleSmoother;10use crate::effect::Effect;11use crate::env::{Lfo, LfoShape};12use crate::filter::OnePole;13use sonata_ir::{EffectDesc, LatencySpec, Unit};1415/// Mid/side width control with optional mono bass.16pub struct StereoWidth {17    sr: f32,18    width: OnePoleSmoother,19    mono_below: f32,20    hp: OnePole,21}2223impl StereoWidth {24    pub fn new() -> StereoWidth {25        StereoWidth { sr: 48_000.0, width: OnePoleSmoother::new(1.0, 0.01, 48_000.0), mono_below: 0.0, hp: OnePole::new(20.0, 48_000.0) }26    }27    pub fn desc() -> EffectDesc {28        desc(29            "stereo_width",30            "Stereo Width",31            "stereo",32            "Scales the side (L - R) signal: 0 is mono, 1 leaves the image unchanged, 2 doubles the side level. `mono_below` removes side content under a frequency.",33            vec![num("width", Unit::Scalar, 0.0, 2.0, 1.0, "Gain of the side signal."), num("mono_below", Unit::Hertz, 0.0, 500.0, 0.0, "Side signal is high-passed at this frequency (0 = off).")],34            vec![preset("mono", "Collapse to mono.", &[("width", 0.0)]), preset("wide", "Wider, bass kept centred.", &[("width", 1.5), ("mono_below", 120.0)])],35            LatencySpec::default(),36            1.0,37        )38    }39}4041impl Default for StereoWidth {42    fn default() -> Self {43        Self::new()44    }45}4647impl Effect for StereoWidth {48    fn prepare(&mut self, sample_rate: f32, _: usize) {49        self.sr = sample_rate;50        let w = self.width.target();51        self.width = OnePoleSmoother::new(w, 0.01, sample_rate);52        self.hp = OnePole::new(self.mono_below.max(1.0), sample_rate);53    }54    fn reset(&mut self) {55        self.hp.reset();56        let w = self.width.target();57        self.width.snap(w);58    }59    fn set_param(&mut self, index: usize, value: f32) {60        match index {61            0 => self.width.set_target(value),62            1 => {63                self.mono_below = value;64                self.hp.set(value.max(1.0), self.sr);65            }66            _ => {}67        }68    }69    fn process(&mut self, left: &mut [f32], right: &mut [f32]) {70        let bass_mono = self.mono_below >= 1.0;71        for i in 0..left.len() {72            let w = self.width.tick();73            let mid = 0.5 * (left[i] + right[i]);74            let mut side = 0.5 * (left[i] - right[i]);75            if bass_mono {76                side = self.hp.highpass(side);77            }78            side *= w;79            left[i] = mid + side;80            right[i] = mid - side;81        }82    }83}8485fn lfo_shape(choice: usize) -> LfoShape {86    match choice {87        1 => LfoShape::Triangle,88        2 => LfoShape::Square,89        _ => LfoShape::Sine,90    }91}9293/// Amplitude modulation: gain swings between 1 and `1 - depth`.94pub struct Tremolo {95    lfos: [Lfo; 2],96    smooth: [OnePoleSmoother; 2],97    p: [f32; 3],98    shape: usize,99}100101impl Tremolo {102    pub fn new() -> Tremolo {103        Tremolo { lfos: [Lfo::new(48_000.0, 1); 2], smooth: [OnePoleSmoother::new(1.0, 0.002, 48_000.0); 2], p: [5.0, 0.5, 0.0], shape: 0 }104    }105    pub fn desc() -> EffectDesc {106        desc(107            "tremolo",108            "Tremolo",109            "modulation",110            "Periodic change of level. The modulator is smoothed over 2 ms so the square shape does not click.",111            vec![112                num("rate", Unit::Hertz, 0.05, 30.0, 5.0, "Modulation rate."),113                num("depth", Unit::Ratio, 0.0, 1.0, 0.5, "Depth: at 1 the level falls to silence once per cycle."),114                num("stereo_phase", Unit::Degrees, 0.0, 180.0, 0.0, "Phase offset of the right channel (180 = alternating sides)."),115                choice("shape", &["sine", "triangle", "square"], "sine", "Waveform of the modulator."),116            ],117            vec![preset("vintage", "Amp tremolo.", &[("rate", 5.5), ("depth", 0.6)]), preset("helicopter", "Hard chop.", &[("rate", 9.0), ("depth", 1.0)])],118            LatencySpec::default(),119            1.0,120        )121    }122    fn configure(&mut self) {123        for (c, l) in self.lfos.iter_mut().enumerate() {124            l.shape = lfo_shape(self.shape);125            l.set_rate(self.p[0]);126            let _ = c;127        }128    }129}130131impl Default for Tremolo {132    fn default() -> Self {133        Self::new()134    }135}136137impl Effect for Tremolo {138    fn prepare(&mut self, sample_rate: f32, _: usize) {139        self.lfos = [Lfo::new(sample_rate, 1), Lfo::new(sample_rate, 2)];140        self.smooth = [OnePoleSmoother::new(1.0, 0.002, sample_rate); 2];141        self.reset();142    }143    fn reset(&mut self) {144        self.configure();145        for (c, l) in self.lfos.iter_mut().enumerate() {146            // Start at the crest (gain 1) so a note is not attenuated at its onset.147            l.set_phase(0.25 + c as f32 * self.p[2] / 360.0);148        }149        for s in &mut self.smooth {150            s.snap(1.0);151        }152    }153    fn set_param(&mut self, index: usize, value: f32) {154        if let Some(p) = self.p.get_mut(index) {155            *p = value;156        }157        if index == 0 {158            self.configure();159        }160    }161    fn set_choice(&mut self, index: usize, choice: usize) {162        if index == 3 {163            self.shape = choice;164            self.configure();165        }166    }167    fn process(&mut self, left: &mut [f32], right: &mut [f32]) {168        let depth = self.p[1].clamp(0.0, 1.0);169        for i in 0..left.len() {170            let gl = 1.0 - depth * (0.5 - 0.5 * self.lfos[0].tick());171            let gr = 1.0 - depth * (0.5 - 0.5 * self.lfos[1].tick());172            self.smooth[0].set_target(gl);173            self.smooth[1].set_target(gr);174            left[i] *= self.smooth[0].tick();175            right[i] *= self.smooth[1].tick();176        }177    }178}179180/// Moves the stereo image from side to side with a constant-power balance law181/// (unity at centre: `gL = sqrt(2) cos θ`, `gR = sqrt(2) sin θ`).182pub struct AutoPan {183    lfo: Lfo,184    p: [f32; 2],185    shape: usize,186}187188impl AutoPan {189    pub fn new() -> AutoPan {190        AutoPan { lfo: Lfo::new(48_000.0, 1), p: [0.5, 1.0], shape: 0 }191    }192    pub fn desc() -> EffectDesc {193        desc(194            "autopan",195            "Auto Pan",196            "modulation",197            "Periodic left-right movement with a constant-power balance law. At depth 0 the signal is unchanged.",198            vec![199                num("rate", Unit::Hertz, 0.02, 20.0, 0.5, "Rate of the movement."),200                num("depth", Unit::Ratio, 0.0, 1.0, 1.0, "How far the image travels (1 = hard left to hard right)."),201                choice("shape", &["sine", "triangle"], "sine", "Trajectory of the movement."),202            ],203            vec![preset("slow_drift", "Subtle motion.", &[("rate", 0.1), ("depth", 0.4)])],204            LatencySpec::default(),205            1.0,206        )207    }208}209210impl Default for AutoPan {211    fn default() -> Self {212        Self::new()213    }214}215216impl Effect for AutoPan {217    fn prepare(&mut self, sample_rate: f32, _: usize) {218        self.lfo = Lfo::new(sample_rate, 1);219        self.reset();220    }221    fn reset(&mut self) {222        self.lfo.shape = lfo_shape(self.shape);223        self.lfo.set_rate(self.p[0]);224        self.lfo.set_phase(0.0);225    }226    fn set_param(&mut self, index: usize, value: f32) {227        if let Some(p) = self.p.get_mut(index) {228            *p = value;229        }230        if index == 0 {231            self.lfo.set_rate(value);232        }233    }234    fn set_choice(&mut self, index: usize, choice: usize) {235        if index == 2 {236            self.shape = choice;237            self.lfo.shape = lfo_shape(choice);238        }239    }240    fn process(&mut self, left: &mut [f32], right: &mut [f32]) {241        let depth = self.p[1].clamp(0.0, 1.0);242        for i in 0..left.len() {243            let theta = (depth * self.lfo.tick() + 1.0) * std::f32::consts::FRAC_PI_4;244            left[i] *= std::f32::consts::SQRT_2 * theta.cos();245            right[i] *= std::f32::consts::SQRT_2 * theta.sin();246        }247    }248}249
Files main
Menu

Theme