Skip to main content

secs_engine/
export_view.rs

1//! Narrow derived state used by response exports.
2
3use crate::{EngineConfig, EngineError, MeasurementSet, Spectra, measurement, setup::SubFilter};
4
5/// Long-window spectra and timing on the engine's internal axis.
6#[derive(Debug)]
7pub struct ExportView {
8    pub spectra: Spectra,
9    pub base_s: f64,
10    pub has_sub_filters: bool,
11}
12
13/// Rebuilds the 2 s spectra, reusing the design's preparation for matching inputs.
14/// New same-input callers use `design.replay.export_view(design)` directly.
15pub fn export_view(
16    design: &crate::Design,
17    measurements: &MeasurementSet,
18    config: &EngineConfig,
19) -> Result<ExportView, EngineError> {
20    if design.replay.matches(measurements, config) {
21        design.replay.export_view(design)
22    } else {
23        let prepared =
24            measurement::prepare(measurements, config, &crate::setup::Setup::new(config))?;
25        export_view_prepared(design, measurements, &prepared, &|| false)
26    }
27}
28
29/// [`export_view`] on measurements already prepared with the same config;
30/// `Err(Cancelled)` once `cancelled()` reads true while the spectra are built.
31pub(crate) fn export_view_prepared(
32    design: &crate::Design,
33    measurements: &MeasurementSet,
34    prepared: &measurement::Prepared,
35    cancelled: &dyn Fn() -> bool,
36) -> Result<ExportView, EngineError> {
37    let sub_filters: Vec<_> = design
38        .chain
39        .subs
40        .iter()
41        .filter(|sub| sub.hp_hz != 0.0 || sub.lp_hz != 0.0 || !sub.peq.is_empty())
42        .map(|sub| {
43            (
44                sub.role.clone(),
45                SubFilter {
46                    hp_hz: sub.hp_hz,
47                    hp_type: sub.hp_type,
48                    lp_hz: sub.lp_hz,
49                    lp_type: sub.lp_type,
50                    peq: sub.peq.clone(),
51                },
52            )
53        })
54        .collect();
55    let spectra =
56        measurement::long_spectra_until(measurements, prepared, &sub_filters, 2000.0, cancelled)?;
57    Ok(ExportView {
58        spectra,
59        base_s: prepared.base_s,
60        has_sub_filters: !sub_filters.is_empty(),
61    })
62}