roomeq-model 0.5.5

RoomEQ configuration, pipeline, and DSP-chain contracts
Documentation
use super::default::default_decomposed_min_q;
use super::default::default_decomposed_mode_weight;
use super::default::default_decomposed_prominence;
use super::default::default_decomposed_reflection_weight;
use super::default::default_decomposed_schroeder;
use super::default::default_decomposed_steady_weight;
use super::default::default_fdw_cycles;
use super::default::default_fdw_max_window_ms;
use super::default::default_fdw_min_window_ms;
use super::default::default_fdw_smoothing_octaves;
use super::default::default_true;
use super::room_dimensions::RoomDimensions;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Serializable decomposed correction configuration for JSON config files
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct DecomposedCorrectionSerdeConfig {
    /// Whether decomposed correction is enabled. Default: true
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Schroeder frequency (Hz). Below: modal, above: statistical.
    ///
    /// When `room_dimensions` is also provided AND an impulse response is
    /// available, this value is overridden at run time by a
    /// measurement-driven Schroeder frequency: the optimizer measures
    /// RT60 from the IR via Schroeder backward integration and plugs it
    /// into `f_S ≈ 2000 · √(RT60 / V)` with V from `room_dimensions`. In
    /// that case this field is used only as the fallback if the RT60 fit
    /// fails.
    #[serde(default = "default_decomposed_schroeder")]
    pub schroeder_freq: f64,
    /// Room dimensions (L × W × H in metres). When present together with
    /// a measured impulse response, enables a measurement-driven
    /// Schroeder frequency via `RoomDimensions::schroeder_frequency_with_rt60`
    /// using the RT60 measured from the IR. When absent, the optimizer
    /// falls back to the `schroeder_freq` field above.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub room_dimensions: Option<RoomDimensions>,
    /// Minimum Q to qualify as a room mode. Default: 3.0
    #[serde(default = "default_decomposed_min_q")]
    pub min_mode_q: f64,
    /// Minimum prominence (dB) for mode detection. Default: 3.0
    #[serde(default = "default_decomposed_prominence")]
    pub min_mode_prominence_db: f64,
    /// Correction weight for detected room modes (0.0-1.0). Default: 1.0
    #[serde(default = "default_decomposed_mode_weight")]
    pub mode_correction_weight: f64,
    /// Correction weight for early reflections (0.0-1.0). Default: 0.3
    #[serde(default = "default_decomposed_reflection_weight")]
    pub early_reflection_weight: f64,
    /// Correction weight for steady-state above Schroeder (0.0-1.0). Default: 0.4
    #[serde(default = "default_decomposed_steady_weight")]
    pub steady_state_weight: f64,
    /// Enable Frequency-Dependent Windowing when `ssir_wav_path` provides an IR.
    #[serde(default = "default_true")]
    pub fdw_enabled: bool,
    /// FDW window length in cycles before min/max clamping. Default: 8.0
    #[serde(default = "default_fdw_cycles")]
    pub fdw_cycles: f64,
    /// Minimum FDW window length in milliseconds. Default: 3.0
    #[serde(default = "default_fdw_min_window_ms")]
    pub fdw_min_window_ms: f64,
    /// Maximum FDW window length in milliseconds. Default: 500.0
    #[serde(default = "default_fdw_max_window_ms")]
    pub fdw_max_window_ms: f64,
    /// FDW smoothing width in octaves. Default: 1/24 octave
    #[serde(default = "default_fdw_smoothing_octaves")]
    pub fdw_smoothing_octaves: f64,
}

impl Default for DecomposedCorrectionSerdeConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            schroeder_freq: default_decomposed_schroeder(),
            room_dimensions: None,
            min_mode_q: default_decomposed_min_q(),
            min_mode_prominence_db: default_decomposed_prominence(),
            mode_correction_weight: default_decomposed_mode_weight(),
            early_reflection_weight: default_decomposed_reflection_weight(),
            steady_state_weight: default_decomposed_steady_weight(),
            fdw_enabled: true,
            fdw_cycles: default_fdw_cycles(),
            fdw_min_window_ms: default_fdw_min_window_ms(),
            fdw_max_window_ms: default_fdw_max_window_ms(),
            fdw_smoothing_octaves: default_fdw_smoothing_octaves(),
        }
    }
}

pub(super) fn decomposed_correction_is_default(config: &DecomposedCorrectionSerdeConfig) -> bool {
    config.enabled
        && (config.schroeder_freq - default_decomposed_schroeder()).abs() < 1e-9
        && config.room_dimensions.is_none()
        && (config.min_mode_q - default_decomposed_min_q()).abs() < 1e-9
        && (config.min_mode_prominence_db - default_decomposed_prominence()).abs() < 1e-9
        && (config.mode_correction_weight - default_decomposed_mode_weight()).abs() < 1e-9
        && (config.early_reflection_weight - default_decomposed_reflection_weight()).abs() < 1e-9
        && (config.steady_state_weight - default_decomposed_steady_weight()).abs() < 1e-9
        && config.fdw_enabled
        && (config.fdw_cycles - default_fdw_cycles()).abs() < 1e-9
        && (config.fdw_min_window_ms - default_fdw_min_window_ms()).abs() < 1e-9
        && (config.fdw_max_window_ms - default_fdw_max_window_ms()).abs() < 1e-9
        && (config.fdw_smoothing_octaves - default_fdw_smoothing_octaves()).abs() < 1e-12
}