autoeq 0.4.24

Automatic equalization for speakers, headphones and rooms!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Measurement source handling (single file or averaging)

use crate::Curve;
use crate::read::{interpolate_log_space, read_curve_from_csv};
use ndarray::Array1;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::path::{Path, PathBuf};

/// Inline measurement data (frequencies, SPL, phase)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct InlineMeasurement {
    /// Frequency points in Hz
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub frequencies: Vec<f64>,
    /// Sound Pressure Level in dB
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub magnitude_db: Vec<f64>,
    /// Phase in degrees (optional)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub phase_deg: Option<Vec<f64>>,
    /// Optional display name
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Optional path to associated WAV file
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub wav_path: Option<String>,
    /// Optional path to associated CSV file
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub csv_path: Option<String>,
}

impl InlineMeasurement {
    /// Resolve relative paths in this measurement against a base directory.
    /// If csv_path or wav_path is relative, prepend the base directory.
    pub fn resolve_paths(&mut self, base_dir: &Path) {
        if let Some(ref csv_path) = self.csv_path {
            let path = PathBuf::from(csv_path);
            if path.is_relative() {
                self.csv_path = Some(base_dir.join(&path).to_string_lossy().to_string());
            }
        }
        if let Some(ref wav_path) = self.wav_path {
            let path = PathBuf::from(wav_path);
            if path.is_relative() {
                self.wav_path = Some(base_dir.join(&path).to_string_lossy().to_string());
            }
        }
    }
}

/// Reference to a measurement file
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum MeasurementRef {
    /// Inline measurement data (stored directly in JSON)
    Inline(InlineMeasurement),

    /// Named measurement with optional metadata
    Named {
        /// Path to the CSV measurement file.
        path: PathBuf,
        /// Optional display name for the measurement.
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
    },

    /// Path to CSV file (freq, spl, phase columns)
    Path(PathBuf),
}

impl MeasurementRef {
    /// Returns the path to the measurement file, if this is a file-based reference.
    /// Returns None for inline measurements.
    pub fn path(&self) -> Option<&PathBuf> {
        match self {
            MeasurementRef::Path(p) => Some(p),
            MeasurementRef::Named { path, .. } => Some(path),
            MeasurementRef::Inline(_) => None,
        }
    }

    /// Returns the optional display name, if provided.
    pub fn name(&self) -> Option<&str> {
        match self {
            MeasurementRef::Path(_) => None,
            MeasurementRef::Named { name, .. } => name.as_deref(),
            MeasurementRef::Inline(inline) => inline.name.as_deref(),
        }
    }

    /// Returns true if this is an inline measurement (data stored in JSON)
    pub fn is_inline(&self) -> bool {
        matches!(self, MeasurementRef::Inline(_))
    }

    /// Returns the inline measurement data, if this is an inline reference.
    pub fn inline_data(&self) -> Option<&InlineMeasurement> {
        match self {
            MeasurementRef::Inline(data) => Some(data),
            _ => None,
        }
    }

    /// Resolve relative paths in this measurement reference against a base directory.
    pub fn resolve_paths(&mut self, base_dir: &Path) {
        match self {
            MeasurementRef::Path(p) => {
                if p.is_relative() {
                    *p = base_dir.join(&*p);
                }
            }
            MeasurementRef::Named { path, .. } => {
                if path.is_relative() {
                    *path = base_dir.join(&*path);
                }
            }
            MeasurementRef::Inline(inline) => {
                inline.resolve_paths(base_dir);
            }
        }
    }
}

/// Source of measurements (single file, multiple files for averaging, or in-memory curve)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum MeasurementSource {
    /// A single measurement file with optional speaker name
    Single(MeasurementSingle),
    /// Multiple measurement files to be averaged with optional speaker name
    Multiple(MeasurementMultiple),
    /// In-memory curve data (not serializable to JSON config files).
    /// Use this when curves are already loaded in memory.
    #[serde(skip)]
    InMemory(Curve),
    /// Multiple in-memory curves (e.g., multi-mic recordings).
    /// Not serializable — use for GPUI in-memory data.
    #[serde(skip)]
    InMemoryMultiple(Vec<Curve>),
}

/// Single measurement with metadata
///
/// Custom implementation to support both string path and object with speaker_name
#[derive(Debug, Clone, JsonSchema)]
pub struct MeasurementSingle {
    pub measurement: MeasurementRef,
    pub speaker_name: Option<String>,
}

impl Serialize for MeasurementSingle {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if self.speaker_name.is_none() {
            // No metadata, serialize as the inner ref (might be string or object)
            self.measurement.serialize(serializer)
        } else {
            // Has metadata, must serialize as object
            use serde::ser::SerializeMap;
            let mut map = serializer.serialize_map(None)?;
            match &self.measurement {
                MeasurementRef::Path(p) => {
                    map.serialize_entry("path", p)?;
                }
                MeasurementRef::Named { path, name } => {
                    map.serialize_entry("path", path)?;
                    if let Some(n) = name {
                        map.serialize_entry("name", n)?;
                    }
                }
                MeasurementRef::Inline(inline) => {
                    // Inline is already an object, but we can't easily merge without duplicating fields
                    // or using a temporary value.
                    map.serialize_entry("inline", inline)?;
                }
            }
            map.serialize_entry("speaker_name", &self.speaker_name)?;
            map.end()
        }
    }
}

impl<'de> Deserialize<'de> for MeasurementSingle {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Helper {
            path: Option<PathBuf>,
            name: Option<String>,
            inline: Option<InlineMeasurement>,
            speaker_name: Option<String>,
        }

        let value = serde_json::Value::deserialize(deserializer)?;

        if value.is_string() {
            // Case: "path/to/file.csv"
            let path = value.as_str().unwrap().into();
            return Ok(MeasurementSingle {
                measurement: MeasurementRef::Path(path),
                speaker_name: None,
            });
        }

        if let Ok(helper) = serde_json::from_value::<Helper>(value.clone()) {
            let speaker_name = helper.speaker_name;

            if let Some(inline) = helper.inline {
                return Ok(MeasurementSingle {
                    measurement: MeasurementRef::Inline(inline),
                    speaker_name,
                });
            }

            if let Some(path) = helper.path {
                if let Some(name) = helper.name {
                    return Ok(MeasurementSingle {
                        measurement: MeasurementRef::Named {
                            path,
                            name: Some(name),
                        },
                        speaker_name,
                    });
                } else {
                    return Ok(MeasurementSingle {
                        measurement: MeasurementRef::Path(path),
                        speaker_name,
                    });
                }
            }
        }

        // Fallback to trying to parse as MeasurementRef if it doesn't match our helper
        let measurement = serde_json::from_value(value).map_err(serde::de::Error::custom)?;
        Ok(MeasurementSingle {
            measurement,
            speaker_name: None,
        })
    }
}

/// Multiple measurements with metadata
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct MeasurementMultiple {
    pub measurements: Vec<MeasurementRef>,
    /// Optional speaker name (e.g., "Genelec 8361A")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub speaker_name: Option<String>,
}

impl MeasurementSource {
    /// Returns the optional speaker name, if provided.
    pub fn speaker_name(&self) -> Option<&str> {
        match self {
            MeasurementSource::Single(s) => s.speaker_name.as_deref(),
            MeasurementSource::Multiple(m) => m.speaker_name.as_deref(),
            MeasurementSource::InMemory(_) | MeasurementSource::InMemoryMultiple(_) => None,
        }
    }

    /// Resolve relative paths in this measurement source against a base directory.
    pub fn resolve_paths(&mut self, base_dir: &Path) {
        match self {
            MeasurementSource::Single(s) => s.measurement.resolve_paths(base_dir),
            MeasurementSource::Multiple(m) => {
                for r in &mut m.measurements {
                    r.resolve_paths(base_dir);
                }
            }
            MeasurementSource::InMemory(_) | MeasurementSource::InMemoryMultiple(_) => {} // No paths to resolve
        }
    }
}

/// Load a single measurement from a file or inline data
pub fn load_measurement(measurement: &MeasurementRef) -> Result<Curve, Box<dyn Error>> {
    match measurement {
        MeasurementRef::Path(path) => read_curve_from_csv(path),
        MeasurementRef::Named { path, .. } => read_curve_from_csv(path),
        MeasurementRef::Inline(inline) => {
            // If inline data is empty but csv_path is provided, load from CSV
            if inline.frequencies.is_empty() || inline.magnitude_db.is_empty() {
                if let Some(ref csv_path) = inline.csv_path {
                    return read_curve_from_csv(&PathBuf::from(csv_path));
                }
                return Err(format!(
                    "Inline measurement has empty data and no csv_path to fall back to (name: {:?})",
                    inline.name
                )
                .into());
            }

            if inline.frequencies.len() != inline.magnitude_db.len() {
                return Err(format!(
                    "Inline measurement has mismatched lengths: {} frequencies, {} magnitude values",
                    inline.frequencies.len(),
                    inline.magnitude_db.len()
                )
                .into());
            }

            let phase = inline.phase_deg.as_ref().and_then(|p| {
                if p.len() != inline.frequencies.len() {
                    log::debug!(
                        "Warning: phase array length ({}) doesn't match frequencies ({}), ignoring phase",
                        p.len(),
                        inline.frequencies.len()
                    );
                    None
                } else {
                    Some(Array1::from(p.clone()))
                }
            });

            Ok(Curve {
                freq: Array1::from(inline.frequencies.clone()),
                spl: Array1::from(inline.magnitude_db.clone()),
                phase,
            })
        }
    }
}

/// Load individual measurement curves from a source without averaging.
///
/// - `Single` → returns `vec![curve]`
/// - `Multiple` → loads all curves, interpolates to first curve's frequency grid
/// - `InMemory` → returns `vec![curve]`
pub fn load_source_individual(source: &MeasurementSource) -> Result<Vec<Curve>, Box<dyn Error>> {
    match source {
        MeasurementSource::Single(s) => {
            let curve = load_measurement(&s.measurement)?;
            Ok(vec![curve])
        }
        MeasurementSource::InMemory(curve) => Ok(vec![curve.clone()]),
        MeasurementSource::InMemoryMultiple(curves) => Ok(curves.clone()),
        MeasurementSource::Multiple(m) => {
            if m.measurements.is_empty() {
                return Err("Measurement list is empty".into());
            }

            let mut curves = Vec::new();
            for r in &m.measurements {
                match load_measurement(r) {
                    Ok(c) => curves.push(c),
                    Err(e) => {
                        let name = r
                            .path()
                            .map(|p| p.display().to_string())
                            .or_else(|| r.name().map(String::from))
                            .unwrap_or_else(|| "inline".to_string());
                        log::debug!("Warning: failed to load measurement {}: {}", name, e)
                    }
                }
            }

            if curves.is_empty() {
                return Err("No valid measurements loaded".into());
            }

            // Interpolate all curves to the first curve's frequency grid
            let ref_freqs = curves[0].freq.clone();
            let mut result = vec![curves[0].clone()];
            for curve in &curves[1..] {
                result.push(interpolate_log_space(&ref_freqs, curve));
            }
            Ok(result)
        }
    }
}

/// Load measurement(s) from a source and average if necessary
pub fn load_source(source: &MeasurementSource) -> Result<Curve, Box<dyn Error>> {
    match source {
        MeasurementSource::Single(s) => load_measurement(&s.measurement),
        MeasurementSource::InMemory(curve) => Ok(curve.clone()),
        MeasurementSource::InMemoryMultiple(curves) => {
            if curves.is_empty() {
                return Err("InMemoryMultiple has no curves".into());
            }
            if curves.len() == 1 {
                return Ok(curves[0].clone());
            }
            // Average: same logic as Multiple below
            let ref_curve = &curves[0];
            let freqs = ref_curve.freq.clone();
            let mut power_sum = Array1::<f64>::zeros(freqs.len());
            for curve in curves {
                let interpolated = interpolate_log_space(&freqs, curve);
                let p = interpolated.spl.mapv(|spl| 10.0_f64.powf(spl / 10.0));
                power_sum = power_sum + p;
            }
            let avg_power = power_sum / (curves.len() as f64);
            let avg_spl = avg_power.mapv(|p| 10.0 * p.log10());
            let phase = ref_curve.phase.clone();
            Ok(Curve {
                freq: freqs,
                spl: avg_spl,
                phase,
            })
        }
        MeasurementSource::Multiple(m) => {
            if m.measurements.is_empty() {
                return Err("Measurement list is empty".into());
            }

            // Load all curves
            let mut curves = Vec::new();
            for r in &m.measurements {
                match load_measurement(r) {
                    Ok(c) => curves.push(c),
                    Err(e) => {
                        let name = r
                            .path()
                            .map(|p| p.display().to_string())
                            .or_else(|| r.name().map(String::from))
                            .unwrap_or_else(|| "inline".to_string());
                        log::debug!("Warning: failed to load measurement {}: {}", name, e)
                    }
                }
            }

            if curves.is_empty() {
                return Err("No valid measurements loaded".into());
            }

            // Use first curve as reference grid
            let ref_curve = &curves[0];
            let freqs = ref_curve.freq.clone();

            // Interpolate all to reference grid and sum power
            let mut power_sum = Array1::<f64>::zeros(freqs.len());

            for curve in &curves {
                let interpolated = interpolate_log_space(&freqs, curve);
                // Convert SPL to power (proportional to pressure squared)
                // Power = 10^(SPL/10)
                let p = interpolated.spl.mapv(|spl| 10.0_f64.powf(spl / 10.0));
                power_sum = power_sum + p;
            }

            // Average power
            let avg_power = power_sum / (curves.len() as f64);

            // Convert back to SPL
            let avg_spl = avg_power.mapv(|p| 10.0 * p.log10());

            // Use phase from first measurement (primary position)
            let phase = ref_curve.phase.clone();

            Ok(Curve {
                freq: freqs,
                spl: avg_spl,
                phase,
            })
        }
    }
}