roomeq-model 0.5.5

RoomEQ configuration, pipeline, and DSP-chain contracts
Documentation
use crate::config::SpeakerConfig;
use crate::{MeasurementRef, MeasurementSource};

/// Frequency (Hz) above which `ProcessingMode::PhaseLinear` tends to need an
/// impractical number of FIR taps. Crossing this with default FIR settings
/// produces quietly-degraded high-frequency response.
pub(super) const PHASE_LINEAR_RECOMMENDED_MAX_FREQ_HZ: f64 = 2000.0;

/// Collect all `MeasurementSource`s referenced by a speaker, so the validator
/// can inspect counts, paths, and speaker-name metadata uniformly.
pub fn collect_sources(speaker: &SpeakerConfig) -> Vec<&MeasurementSource> {
    match speaker {
        SpeakerConfig::SupportingSource(s) => vec![&s.primary, &s.support],
        SpeakerConfig::Single(s) => vec![s],
        SpeakerConfig::Group(g) => g.measurements.iter().collect(),
        SpeakerConfig::Topology(t) => t.drivers.iter().map(|driver| &driver.measurement).collect(),
        SpeakerConfig::MultiSub(m) => m.subwoofers.iter().collect(),
        SpeakerConfig::Cardioid(c) => vec![&c.front, &c.rear],
        SpeakerConfig::Dba(d) => d.front.iter().chain(d.rear.iter()).collect(),
    }
}

/// Return true if a measurement path/metadata plausibly points at CEA2034
/// (spinorama) data. The check is heuristic on purpose — the validator's job
/// is to flag the common misuse "`cea2034_correction.enabled=true` applied to
/// plain in-room measurements", not to guarantee correctness.
pub(super) fn source_is_cea2034_shaped(source: &MeasurementSource) -> bool {
    // A named speaker is the strongest signal: spinorama fetches set it, and
    // the 3-pass pipeline uses that name as a cache key.
    if source.speaker_name().is_some() {
        return true;
    }
    let path_hints = |path: &std::path::Path| {
        let lower = path.to_string_lossy().to_lowercase();
        lower.contains("cea2034") || lower.contains("spinorama") || lower.contains("cea-2034")
    };
    let ref_hint = |r: &MeasurementRef| match r {
        MeasurementRef::Path(p) => path_hints(p),
        MeasurementRef::Named { path, name } => {
            path_hints(path)
                || name
                    .as_deref()
                    .map(|n| {
                        n.to_lowercase().contains("cea2034")
                            || n.to_lowercase().contains("spinorama")
                    })
                    .unwrap_or(false)
        }
        MeasurementRef::Inline(_) => false,
    };
    match source {
        MeasurementSource::Single(s) => ref_hint(&s.measurement),
        MeasurementSource::Multiple(m) => m.measurements.iter().any(ref_hint),
        MeasurementSource::InMemory(_) | MeasurementSource::InMemoryMultiple(_) => false,
    }
}

/// Check if a speaker name is valid (alphanumeric, spaces, hyphens)
pub(super) fn is_valid_speaker_name(name: &str) -> bool {
    if name.is_empty() {
        return false;
    }
    name.chars()
        .all(|c| c.is_alphanumeric() || c == ' ' || c == '-')
}