|
| 1 | +use log::warn; |
| 2 | +use nam_rs::WaveNet; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +use crate::amp::stages::Stage; |
| 6 | +use crate::amp::stages::common::db_to_lin; |
| 7 | +use crate::nam::registry; |
| 8 | + |
| 9 | +/// Valid range for the input/output gain knobs, matching the UI and plugin params. |
| 10 | +const GAIN_DB_MIN: f32 = -24.0; |
| 11 | +const GAIN_DB_MAX: f32 = 24.0; |
| 12 | + |
| 13 | +/// A Neural Amp Modeler stage running a WaveNet `.nam` model. |
| 14 | +/// |
| 15 | +/// With no model loaded the stage is a passthrough. Input/output gain are applied |
| 16 | +/// around the model and the wet output is blended with the dry signal via `mix`. |
| 17 | +pub struct NamStage { |
| 18 | + wavenet: Option<WaveNet>, |
| 19 | + input_gain: f32, |
| 20 | + output_gain: f32, |
| 21 | + mix: f32, |
| 22 | + /// Native sample rate of the loaded model (0.0 if none), for UI display. |
| 23 | + native_sample_rate: f32, |
| 24 | + /// True if the model's native rate differs from the engine rate. |
| 25 | + sample_rate_mismatch: bool, |
| 26 | +} |
| 27 | + |
| 28 | +impl NamStage { |
| 29 | + const fn passthrough(input_gain: f32, output_gain: f32, mix: f32) -> Self { |
| 30 | + Self { |
| 31 | + wavenet: None, |
| 32 | + input_gain, |
| 33 | + output_gain, |
| 34 | + mix, |
| 35 | + native_sample_rate: 0.0, |
| 36 | + sample_rate_mismatch: false, |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl Stage for NamStage { |
| 42 | + fn process(&mut self, input: f32) -> f32 { |
| 43 | + let Some(wavenet) = self.wavenet.as_mut() else { |
| 44 | + return input; |
| 45 | + }; |
| 46 | + let wet = wavenet.process_sample(input * self.input_gain) * self.output_gain; |
| 47 | + self.mix.mul_add(wet - input, input) |
| 48 | + } |
| 49 | + |
| 50 | + fn set_parameter(&mut self, name: &str, value: f32) -> Result<(), &'static str> { |
| 51 | + match name { |
| 52 | + "input_gain_db" => { |
| 53 | + if (GAIN_DB_MIN..=GAIN_DB_MAX).contains(&value) { |
| 54 | + self.input_gain = db_to_lin(value); |
| 55 | + Ok(()) |
| 56 | + } else { |
| 57 | + Err("Input gain must be between -24 and 24 dB") |
| 58 | + } |
| 59 | + } |
| 60 | + "output_gain_db" => { |
| 61 | + if (GAIN_DB_MIN..=GAIN_DB_MAX).contains(&value) { |
| 62 | + self.output_gain = db_to_lin(value); |
| 63 | + Ok(()) |
| 64 | + } else { |
| 65 | + Err("Output gain must be between -24 and 24 dB") |
| 66 | + } |
| 67 | + } |
| 68 | + "mix" => { |
| 69 | + if (0.0..=1.0).contains(&value) { |
| 70 | + self.mix = value; |
| 71 | + Ok(()) |
| 72 | + } else { |
| 73 | + Err("Mix must be between 0.0 and 1.0") |
| 74 | + } |
| 75 | + } |
| 76 | + "native_sample_rate" | "sample_rate_mismatch" => Err("Parameter is read-only"), |
| 77 | + _ => Err("Unknown parameter"), |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + fn get_parameter(&self, name: &str) -> Result<f32, &'static str> { |
| 82 | + match name { |
| 83 | + "input_gain_db" => Ok(20.0 * self.input_gain.log10()), |
| 84 | + "output_gain_db" => Ok(20.0 * self.output_gain.log10()), |
| 85 | + "mix" => Ok(self.mix), |
| 86 | + "native_sample_rate" => Ok(self.native_sample_rate), |
| 87 | + "sample_rate_mismatch" => Ok(f32::from(u8::from(self.sample_rate_mismatch))), |
| 88 | + _ => Err("Unknown parameter name"), |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// --- Config --- |
| 94 | + |
| 95 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 96 | +pub struct NamConfig { |
| 97 | + /// Display name of the selected model, or `None` for passthrough. |
| 98 | + #[serde(default)] |
| 99 | + pub model_name: Option<String>, |
| 100 | + pub input_gain_db: f32, |
| 101 | + pub output_gain_db: f32, |
| 102 | + pub mix: f32, |
| 103 | + #[serde(default)] |
| 104 | + pub bypassed: bool, |
| 105 | +} |
| 106 | + |
| 107 | +impl Default for NamConfig { |
| 108 | + fn default() -> Self { |
| 109 | + Self { |
| 110 | + model_name: None, |
| 111 | + input_gain_db: 0.0, |
| 112 | + output_gain_db: 0.0, |
| 113 | + mix: 1.0, |
| 114 | + bypassed: false, |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl NamConfig { |
| 120 | + /// Build a runnable stage. Resolves the model from the global registry and |
| 121 | + /// allocates the `WaveNet` here (off the real-time thread). On any failure the |
| 122 | + /// stage falls back to passthrough with a warning. |
| 123 | + pub fn to_stage(&self, sample_rate: f32) -> NamStage { |
| 124 | + let input_gain = db_to_lin(self.input_gain_db.clamp(GAIN_DB_MIN, GAIN_DB_MAX)); |
| 125 | + let output_gain = db_to_lin(self.output_gain_db.clamp(GAIN_DB_MIN, GAIN_DB_MAX)); |
| 126 | + let mix = self.mix.clamp(0.0, 1.0); |
| 127 | + |
| 128 | + let Some(name) = self.model_name.as_deref() else { |
| 129 | + return NamStage::passthrough(input_gain, output_gain, mix); |
| 130 | + }; |
| 131 | + |
| 132 | + let Some(model) = registry::get(name) else { |
| 133 | + warn!("NAM model '{name}' not found in registry; using passthrough"); |
| 134 | + return NamStage::passthrough(input_gain, output_gain, mix); |
| 135 | + }; |
| 136 | + |
| 137 | + let native_sample_rate = model.sample_rate() as f32; |
| 138 | + let sample_rate_mismatch = (native_sample_rate - sample_rate).abs() > 1.0; |
| 139 | + if sample_rate_mismatch { |
| 140 | + warn!( |
| 141 | + "NAM model '{name}' native rate {native_sample_rate} Hz differs from engine \ |
| 142 | + rate {sample_rate} Hz; tone may be affected" |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + match WaveNet::new(&model) { |
| 147 | + Ok(wavenet) => NamStage { |
| 148 | + wavenet: Some(wavenet), |
| 149 | + input_gain, |
| 150 | + output_gain, |
| 151 | + mix, |
| 152 | + native_sample_rate, |
| 153 | + sample_rate_mismatch, |
| 154 | + }, |
| 155 | + Err(e) => { |
| 156 | + warn!("Failed to build NAM model '{name}': {e}; using passthrough"); |
| 157 | + NamStage::passthrough(input_gain, output_gain, mix) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +#[cfg(test)] |
| 164 | +mod tests { |
| 165 | + use super::*; |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn passthrough_when_no_model() { |
| 169 | + let stage = NamConfig::default().to_stage(48_000.0); |
| 170 | + let mut stage = stage; |
| 171 | + for x in [-1.0, 0.0, 0.25, 0.9] { |
| 172 | + assert_eq!(stage.process(x), x); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn gain_and_mix_round_trip() { |
| 178 | + let mut stage = NamConfig::default().to_stage(48_000.0); |
| 179 | + stage.set_parameter("mix", 0.5).unwrap(); |
| 180 | + assert!((stage.get_parameter("mix").unwrap() - 0.5).abs() < 1e-6); |
| 181 | + |
| 182 | + stage.set_parameter("input_gain_db", 6.0).unwrap(); |
| 183 | + assert!((stage.get_parameter("input_gain_db").unwrap() - 6.0).abs() < 1e-3); |
| 184 | + |
| 185 | + assert!(stage.set_parameter("mix", 2.0).is_err()); |
| 186 | + assert!(stage.set_parameter("native_sample_rate", 1.0).is_err()); |
| 187 | + |
| 188 | + // Gains outside ±24 dB (and NaN) are rejected. |
| 189 | + assert!(stage.set_parameter("input_gain_db", 30.0).is_err()); |
| 190 | + assert!(stage.set_parameter("output_gain_db", -30.0).is_err()); |
| 191 | + assert!(stage.set_parameter("input_gain_db", f32::NAN).is_err()); |
| 192 | + } |
| 193 | +} |
0 commit comments