roomeq-model 0.5.5

RoomEQ configuration, pipeline, and DSP-chain contracts
Documentation
use super::supporting_source_config::SupportingSourceConfig;
use crate::MeasurementSource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// A speaker group representing a primary loudspeaker plus a delayed,
/// decorrelated supporting loudspeaker for room compensation.
///
/// This variant is distinguished from other `SpeakerConfig` variants by the
/// presence of the `primary` and `support` fields.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SupportingSourceGroup {
    /// Human-readable name for this primary/support pair.
    pub name: String,
    /// Optional speaker model string.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub speaker_name: Option<String>,
    /// Measurement source for the primary loudspeaker.
    pub primary: MeasurementSource,
    /// Measurement source for the supporting loudspeaker.
    pub support: MeasurementSource,
    /// Supporting-source processing configuration.
    #[serde(default)]
    pub supporting_source: SupportingSourceConfig,
}

impl SupportingSourceGroup {
    /// Resolve relative paths in both measurement sources against a base directory.
    pub fn resolve_paths(&mut self, base_dir: &std::path::Path) {
        self.primary.resolve_paths(base_dir);
        self.support.resolve_paths(base_dir);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::MeasurementSingle;

    fn dummy_source(path: &str) -> MeasurementSource {
        MeasurementSource::Single(MeasurementSingle {
            measurement: crate::MeasurementRef::Path(std::path::PathBuf::from(path)),
            speaker_name: None,
        })
    }

    #[test]
    fn json_roundtrip() {
        let group = SupportingSourceGroup {
            name: "Left Main + Support".to_string(),
            speaker_name: Some("KEF R3 + Genelec 8030".to_string()),
            primary: dummy_source("measurements/left_primary.csv"),
            support: dummy_source("measurements/left_support.csv"),
            supporting_source: SupportingSourceConfig::default(),
        };
        let json = serde_json::to_string(&group).unwrap();
        let back: SupportingSourceGroup = serde_json::from_str(&json).unwrap();
        assert_eq!(back.name, group.name);
        assert_eq!(back.speaker_name, group.speaker_name);
    }

    #[test]
    fn resolve_paths_updates_relative_paths() {
        let mut group = SupportingSourceGroup {
            name: "test".to_string(),
            speaker_name: None,
            primary: dummy_source("primary.csv"),
            support: dummy_source("support.csv"),
            supporting_source: SupportingSourceConfig::default(),
        };
        let base = std::path::PathBuf::from("/tmp/base");
        group.resolve_paths(&base);
        match &group.primary {
            MeasurementSource::Single(s) => {
                assert_eq!(s.measurement.path().unwrap(), &base.join("primary.csv"));
            }
            _ => panic!("expected Single source"),
        }
        match &group.support {
            MeasurementSource::Single(s) => {
                assert_eq!(s.measurement.path().unwrap(), &base.join("support.csv"));
            }
            _ => panic!("expected Single source"),
        }
    }
}