Repository Public
1//! ORIX Sonata2//! File: crates/sonata-semantics/src/testing.rs3//! Description: A small self-contained registry for tests of the frontend that must not depend4//! on the DSP crates. The real registry comes from `sonata_compiler::registry()`.5//!6//! Author: Simon-Pierre Boucher7//! Contact: contact@spboucher.ai89use sonata_ir::{EffectDesc, EngineDesc, LatencySpec, ParamDesc, PatchNodeDesc, PortDesc, PresetDesc, Rate, Registry, Unit};1011fn port(name: &str, unit: Unit, default: f64, min: f64, max: f64, constant_only: bool) -> PortDesc {12 PortDesc { name: name.into(), unit, default, min, max, constant_only, doc: format!("{name}.") }13}1415fn node(kind: &str, category: &str, ports: Vec<PortDesc>, rate: Rate) -> PatchNodeDesc {16 PatchNodeDesc { kind: kind.into(), title: kind.into(), category: category.into(), doc: format!("{kind} node."), ports, output_rate: rate }17}1819fn engine(model: &str, family: &str, range: (u8, u8), params: Vec<ParamDesc>, presets: Vec<PresetDesc>) -> EngineDesc {20 let mut all = vec![21 ParamDesc::num("envelope.attack", Unit::Seconds, 0.0, 10.0, 0.005, false, "Attack time."),22 ParamDesc::num("envelope.decay", Unit::Seconds, 0.0, 10.0, 0.3, false, "Decay time."),23 ParamDesc::num("envelope.sustain", Unit::Ratio, 0.0, 1.0, 0.7, false, "Sustain level."),24 ParamDesc::num("envelope.release", Unit::Seconds, 0.0, 20.0, 0.3, false, "Release time."),25 ParamDesc::choice("envelope", &["adsr"], "adsr", "Amplitude envelope shape."),26 ];27 all.extend(params);28 EngineDesc {29 model: model.into(),30 title: model.into(),31 family: family.into(),32 method: "fixture".into(),33 doc: format!("{model} fixture engine."),34 range,35 params: all,36 presets,37 articulations: vec!["normal".into(), "legato".into(), "staccato".into()],38 pitched: !matches!(model, "kick" | "snare" | "hihat"),39 }40}4142/// A registry with the engine models and effect kinds required by the mandate, with a43/// plausible subset of parameters. Stable: frontend golden tests depend on it.44pub fn fixture_registry() -> Registry {45 let brightness = || ParamDesc::num("brightness", Unit::Ratio, 0.0, 1.0, 0.5, true, "Spectral brightness.");46 let engines = vec![47 engine(48 "piano",49 "keyboard",50 (21, 108),51 vec![brightness(), ParamDesc::num("hammer_hardness", Unit::Ratio, 0.0, 1.0, 0.5, false, "Hammer hardness.")],52 vec![53 PresetDesc { name: "grand".into(), doc: "Concert grand.".into(), values: vec![("brightness".into(), 0.55)], choices: vec![] },54 PresetDesc { name: "upright".into(), doc: "Upright.".into(), values: vec![("brightness".into(), 0.4)], choices: vec![] },55 ],56 ),57 engine(58 "plucked_string",59 "plucked",60 (28, 96),61 vec![62 ParamDesc::num("damping", Unit::Ratio, 0.0, 1.0, 0.02, true, "String damping."),63 ParamDesc::choice("excitation", &["pluck", "strike"], "pluck", "Excitation type."),64 ParamDesc::num("excitation.position", Unit::Ratio, 0.01, 0.99, 0.2, false, "Excitation position along the string."),65 ParamDesc::choice("resonance", &["none", "modal"], "none", "Body resonance."),66 ParamDesc::list("resonance.frequencies", Unit::HertzList, 20.0, 20000.0, "Modal frequencies of the body."),67 ],68 vec![PresetDesc { name: "nylon".into(), doc: "Nylon guitar.".into(), values: vec![("damping".into(), 0.03)], choices: vec![] }],69 ),70 engine(71 "bowed_string",72 "bowed",73 (28, 100),74 vec![75 ParamDesc::num("bow_pressure", Unit::Ratio, 0.0, 1.0, 0.5, true, "Bow pressure."),76 ParamDesc::num("vibrato_depth", Unit::Semitones, 0.0, 1.0, 0.15, true, "Vibrato depth."),77 ],78 vec![PresetDesc { name: "ensemble".into(), doc: "String ensemble.".into(), values: vec![("bow_pressure".into(), 0.45)], choices: vec![] }],79 ),80 engine("flute", "woodwind", (60, 96), vec![ParamDesc::num("breath", Unit::Ratio, 0.0, 1.0, 0.3, true, "Breath noise.")], vec![]),81 engine("clarinet", "woodwind", (50, 94), vec![ParamDesc::num("reed_stiffness", Unit::Ratio, 0.0, 1.0, 0.5, true, "Reed stiffness.")], vec![]),82 engine("brass", "brass", (40, 84), vec![ParamDesc::num("lip_tension", Unit::Ratio, 0.0, 1.0, 0.5, true, "Lip tension.")], vec![]),83 engine("organ", "organ", (24, 108), vec![ParamDesc::list("drawbars", Unit::ScalarList, 0.0, 8.0, "Drawbar levels.")], vec![]),84 engine("bass", "bass", (24, 67), vec![brightness()], vec![]),85 engine(86 "synth",87 "synth",88 (0, 127),89 vec![90 ParamDesc::choice("waveform", &["sine", "triangle", "saw", "square", "pulse"], "saw", "Oscillator waveform."),91 ParamDesc::num("cutoff", Unit::Hertz, 20.0, 20000.0, 2000.0, true, "Filter cutoff."),92 ParamDesc::num("resonance", Unit::Ratio, 0.0, 1.0, 0.2, true, "Filter resonance."),93 ParamDesc::num("detune", Unit::Semitones, -1.0, 1.0, 0.07, true, "Detune of the second oscillator."),94 ParamDesc::num("unison", Unit::Count, 1.0, 8.0, 1.0, false, "Unison voices."),95 ],96 vec![PresetDesc {97 name: "pad".into(),98 doc: "Soft pad.".into(),99 values: vec![("cutoff".into(), 1200.0), ("envelope.attack".into(), 0.4)],100 choices: vec![("waveform".into(), "saw".into())],101 }],102 ),103 engine("kick", "percussion", (0, 127), vec![ParamDesc::num("tune", Unit::Hertz, 30.0, 120.0, 50.0, true, "Fundamental.")], vec![]),104 engine("snare", "percussion", (0, 127), vec![ParamDesc::num("snappy", Unit::Ratio, 0.0, 1.0, 0.5, true, "Noise amount.")], vec![]),105 engine("hihat", "percussion", (0, 127), vec![ParamDesc::num("decay", Unit::Seconds, 0.01, 2.0, 0.08, true, "Decay time.")], vec![]),106 engine("modal_percussion", "percussion", (36, 108), vec![ParamDesc::num("hardness", Unit::Ratio, 0.0, 1.0, 0.5, false, "Mallet hardness.")], vec![]),107 engine("patch", "synth", (0, 127), vec![], vec![]),108 ];109 let fx = |kind: &str, category: &str, params: Vec<ParamDesc>, presets: Vec<PresetDesc>, latency: LatencySpec, mix: f64| EffectDesc {110 kind: kind.into(),111 title: kind.into(),112 category: category.into(),113 doc: format!("{kind} fixture effect."),114 params,115 presets,116 latency,117 default_mix: mix,118 };119 let effects = vec![120 fx(121 "reverb",122 "space",123 vec![124 ParamDesc::num("size", Unit::Ratio, 0.0, 1.0, 0.5, true, "Room size."),125 ParamDesc::num("decay", Unit::Seconds, 0.1, 30.0, 2.0, true, "Decay time."),126 ParamDesc::num("damping", Unit::Ratio, 0.0, 1.0, 0.5, true, "High damping."),127 ],128 vec![129 PresetDesc { name: "hall".into(), doc: "Concert hall.".into(), values: vec![("size".into(), 0.8), ("decay".into(), 2.8)], choices: vec![] },130 PresetDesc { name: "room".into(), doc: "Small room.".into(), values: vec![("size".into(), 0.3), ("decay".into(), 0.6)], choices: vec![] },131 ],132 LatencySpec::default(),133 0.25,134 ),135 fx(136 "delay",137 "time",138 vec![139 ParamDesc::num("time", Unit::Seconds, 0.001, 4.0, 0.25, true, "Delay time."),140 ParamDesc::num("feedback", Unit::Ratio, 0.0, 0.95, 0.3, true, "Feedback."),141 ],142 vec![],143 LatencySpec::default(),144 0.3,145 ),146 fx(147 "eq",148 "tone",149 vec![150 ParamDesc::num("low_gain", Unit::Decibel, -24.0, 24.0, 0.0, true, "Low shelf gain."),151 ParamDesc::num("high_gain", Unit::Decibel, -24.0, 24.0, 0.0, true, "High shelf gain."),152 ParamDesc::num("low_freq", Unit::Hertz, 20.0, 2000.0, 120.0, true, "Low shelf frequency."),153 ],154 vec![],155 LatencySpec::default(),156 1.0,157 ),158 fx(159 "compressor",160 "dynamics",161 vec![162 ParamDesc::num("threshold", Unit::Decibel, -60.0, 0.0, -18.0, true, "Threshold."),163 ParamDesc::num("ratio", Unit::Scalar, 1.0, 20.0, 4.0, true, "Ratio."),164 ParamDesc::num("attack", Unit::Seconds, 0.0001, 0.5, 0.01, true, "Attack."),165 ],166 vec![],167 LatencySpec::default(),168 1.0,169 ),170 fx(171 "limiter",172 "dynamics",173 vec![ParamDesc::num("ceiling", Unit::Decibel, -24.0, 0.0, -1.0, true, "Ceiling.")],174 vec![],175 LatencySpec { seconds: 0.005, samples: 0 },176 1.0,177 ),178 fx(179 "chorus",180 "modulation",181 vec![ParamDesc::num("rate", Unit::Hertz, 0.01, 10.0, 0.8, true, "LFO rate."), ParamDesc::num("depth", Unit::Ratio, 0.0, 1.0, 0.5, true, "Depth.")],182 vec![],183 LatencySpec::default(),184 0.5,185 ),186 fx(187 "distortion",188 "drive",189 vec![190 ParamDesc::num("drive", Unit::Decibel, 0.0, 48.0, 12.0, true, "Drive."),191 ParamDesc::choice("mode", &["soft", "hard", "fold"], "soft", "Curve."),192 ],193 vec![],194 LatencySpec { seconds: 0.0, samples: 16 },195 1.0,196 ),197 ];198 let hz = |name: &str, d: f64| port(name, Unit::Hertz, d, 0.0, 24000.0, false);199 let sc = |name: &str, d: f64| port(name, Unit::Scalar, d, -1.0e6, 1.0e6, false);200 let osc =201 |kind: &str| node(kind, "source", vec![hz("freq", 440.0), port("detune", Unit::Semitones, 0.0, -48.0, 48.0, false), sc("phase", 0.0)], Rate::Audio);202 let filt = |kind: &str| node(kind, "filter", vec![sc("in", 0.0), hz("cutoff", 1000.0), port("q", Unit::Scalar, 0.707, 0.1, 40.0, false)], Rate::Audio);203 let patch_nodes = vec![204 osc("sine"),205 osc("saw"),206 osc("square"),207 osc("triangle"),208 node("pulse", "source", vec![hz("freq", 440.0), port("width", Unit::Ratio, 0.5, 0.01, 0.99, false)], Rate::Audio),209 node("noise", "source", vec![], Rate::Audio),210 node(211 "adsr",212 "envelope",213 vec![214 port("attack", Unit::Seconds, 0.005, 0.0, 10.0, true),215 port("decay", Unit::Seconds, 0.2, 0.0, 10.0, true),216 port("sustain", Unit::Ratio, 0.7, 0.0, 1.0, true),217 port("release", Unit::Seconds, 0.3, 0.0, 20.0, true),218 ],219 Rate::Control,220 ),221 node("lfo", "modulator", vec![hz("freq", 5.0), port("shape", Unit::Count, 0.0, 0.0, 3.0, true)], Rate::Control),222 filt("lowpass"),223 filt("highpass"),224 filt("bandpass"),225 node("gain", "math", vec![sc("in", 0.0), sc("amount", 1.0)], Rate::Audio),226 node("add", "math", vec![sc("a", 0.0), sc("b", 0.0)], Rate::Audio),227 node("mul", "math", vec![sc("a", 0.0), sc("b", 1.0)], Rate::Audio),228 node(229 "delay",230 "delay",231 vec![sc("in", 0.0), port("time", Unit::Seconds, 0.01, 0.0, 2.0, false), port("max_time", Unit::Seconds, 2.0, 0.001, 10.0, true)],232 Rate::Audio,233 ),234 node("saturate", "shaper", vec![sc("in", 0.0), port("drive", Unit::Decibel, 0.0, 0.0, 48.0, false)], Rate::Audio),235 ];236 let r = Registry { engines, effects, patch_nodes };237 debug_assert!(r.validate().is_ok());238 r239}240