Repository Public
1//! ORIX Sonata2//! File: crates/sonata-dsp/src/effects/mod.rs3//! Description: Insert effects, their descriptors and factory.4//!5//! Author: Simon-Pierre Boucher6//! Contact: contact@spboucher.ai7#![allow(clippy::needless_range_loop)] // DSP loops index several parallel buffers by frame89pub mod color;10pub mod convolution;11pub mod dynamics;12pub mod eq;13pub mod limiter_core;14pub mod reverb;15pub mod spatial;16pub mod time;1718#[cfg(test)]19mod tests;2021use crate::effect::Effect;22use sonata_ir::{EffectDesc, LatencySpec, ParamDesc, PresetDesc, Unit};2324/// An automatable numeric parameter.25pub(crate) fn num(name: &str, unit: Unit, min: f64, max: f64, default: f64, doc: &str) -> ParamDesc {26 ParamDesc::num(name, unit, min, max, default, true, doc)27}2829/// A numeric parameter read when the program is compiled (not automatable).30pub(crate) fn fixed(name: &str, unit: Unit, min: f64, max: f64, default: f64, doc: &str) -> ParamDesc {31 ParamDesc::num(name, unit, min, max, default, false, doc)32}3334pub(crate) fn choice(name: &str, choices: &[&str], default: &str, doc: &str) -> ParamDesc {35 ParamDesc::choice(name, choices, default, doc)36}3738pub(crate) fn preset(name: &str, doc: &str, values: &[(&str, f64)]) -> PresetDesc {39 PresetDesc { name: name.into(), doc: doc.into(), values: values.iter().map(|(k, v)| (k.to_string(), *v)).collect(), choices: Vec::new() }40}4142#[allow(clippy::too_many_arguments)]43pub(crate) fn desc(44 kind: &str,45 title: &str,46 category: &str,47 doc: &str,48 params: Vec<ParamDesc>,49 presets: Vec<PresetDesc>,50 latency: LatencySpec,51 default_mix: f64,52) -> EffectDesc {53 EffectDesc { kind: kind.into(), title: title.into(), category: category.into(), doc: doc.into(), params, presets, latency, default_mix }54}5556fn construct(kind: &str) -> Option<Box<dyn Effect>> {57 Some(match kind {58 "eq" => Box::new(eq::Eq::new()),59 "filter" => Box::new(eq::Filter::new()),60 "compressor" => Box::new(dynamics::Compressor::new()),61 "limiter" => Box::new(dynamics::Limiter::new()),62 "gate" => Box::new(dynamics::Gate::new()),63 "expander" => Box::new(dynamics::Expander::new()),64 "delay" => Box::new(time::Delay::new()),65 "chorus" => Box::new(time::Chorus::new()),66 "flanger" => Box::new(time::Flanger::new()),67 "phaser" => Box::new(time::Phaser::new()),68 "reverb" => Box::new(reverb::Reverb::new()),69 "convolution" => Box::new(convolution::Convolution::new()),70 "distortion" => Box::new(color::Distortion::new()),71 "saturation" => Box::new(color::Saturation::new()),72 "bitcrusher" => Box::new(color::BitCrusher::new()),73 "stereo_width" => Box::new(spatial::StereoWidth::new()),74 "tremolo" => Box::new(spatial::Tremolo::new()),75 "autopan" => Box::new(spatial::AutoPan::new()),76 _ => return None,77 })78}7980/// Creates an effect by kind and gives it the default values published in its descriptor, so the81/// descriptor is the single source of truth for defaults. `prepare` must be called before use.82pub fn create(kind: &str) -> Option<Box<dyn Effect>> {83 let mut fx = construct(kind)?;84 let d = descriptors().into_iter().find(|d| d.kind == kind)?;85 for (i, p) in d.params.iter().enumerate() {86 match (&p.unit, &p.default_choice) {87 (Unit::Choice, Some(c)) => fx.set_choice(i, p.choices.iter().position(|x| x == c).unwrap_or(0)),88 _ => fx.set_param(i, p.default as f32),89 }90 }91 Some(fx)92}9394/// Descriptors of every effect, in documentation order.95pub fn descriptors() -> Vec<EffectDesc> {96 vec![97 eq::Eq::desc(),98 eq::Filter::desc(),99 dynamics::Compressor::desc(),100 dynamics::Limiter::desc(),101 dynamics::Gate::desc(),102 dynamics::Expander::desc(),103 time::Delay::desc(),104 time::Chorus::desc(),105 time::Flanger::desc(),106 time::Phaser::desc(),107 reverb::Reverb::desc(),108 convolution::Convolution::desc(),109 color::Distortion::desc(),110 color::Saturation::desc(),111 color::BitCrusher::desc(),112 spatial::StereoWidth::desc(),113 spatial::Tremolo::desc(),114 spatial::AutoPan::desc(),115 ]116}117