autoeq 0.4.40

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
use super::*;

/// Post-generate FIR coefficients for a channel that only has IIR results.
///
/// For Hybrid mode, uses the IIR-corrected curve as FIR input;
/// for PhaseLinear (FIR-only) mode, uses the raw measurement.
pub(super) fn post_generate_fir(
    name: &str,
    initial_curve: &Curve,
    final_curve: &Curve,
    config: &crate::roomeq::types::OptimizerConfig,
    target_curve: Option<&crate::roomeq::types::TargetCurveConfig>,
    sample_rate: f64,
    output_dir: Option<&Path>,
) -> Option<Vec<f64>> {
    let fir_input = match config.processing_mode {
        ProcessingMode::Hybrid => final_curve,
        _ => initial_curve,
    };
    match fir::generate_fir_correction(fir_input, config, target_curve, sample_rate) {
        Ok(coeffs) => {
            if let Some(out_dir) = output_dir {
                let filename = format!("{}_fir.wav", name);
                let wav_path = out_dir.join(&filename);
                if let Err(e) = crate::fir::save_fir_to_wav(&coeffs, sample_rate as u32, &wav_path)
                {
                    warn!("Failed to save FIR WAV for {}: {}", name, e);
                } else {
                    info!("  Saved FIR filter to {}", wav_path.display());
                }
            }
            Some(coeffs)
        }
        Err(e) => {
            warn!("FIR generation failed for {}: {}", name, e);
            None
        }
    }
}

/// Post-generate a short excess-phase FIR for MixedPhase mode.
///
/// The workflow path only runs IIR optimisation.  For MixedPhase we still need
/// the short FIR that corrects residual excess phase.  This mirrors the logic
/// in `optimize_speaker_eq` MixedPhase branch but runs after the workflow.
pub(super) fn post_generate_mixed_phase_fir(
    name: &str,
    initial_curve: &Curve,
    config: &crate::roomeq::types::OptimizerConfig,
    sample_rate: f64,
    output_dir: Option<&Path>,
) -> Option<Vec<f64>> {
    let phase = initial_curve.phase.as_ref()?;
    if phase.is_empty() {
        return None;
    }

    let mp_config = match &config.mixed_phase {
        Some(sc) => crate::roomeq::mixed_phase::MixedPhaseConfig {
            max_fir_length_ms: sc.max_fir_length_ms,
            pre_ringing_threshold_db: sc.pre_ringing_threshold_db,
            min_spatial_depth: sc.min_spatial_depth,
            phase_smoothing_octaves: sc.phase_smoothing_octaves,
        },
        None => crate::roomeq::mixed_phase::MixedPhaseConfig::default(),
    };

    match crate::roomeq::mixed_phase::decompose_phase(initial_curve, &mp_config) {
        Ok((_min_phase, _excess, delay_ms, residual)) => {
            info!(
                "  Mixed-phase (post-workflow) '{}': delay={:.2} ms",
                name, delay_ms
            );
            let coeffs = crate::roomeq::mixed_phase::generate_excess_phase_fir(
                &initial_curve.freq,
                &residual,
                &mp_config,
                sample_rate,
            );

            if let Some(out_dir) = output_dir {
                let filename = format!("{}_excess_phase_fir.wav", name);
                let wav_path = out_dir.join(&filename);
                if let Err(e) = crate::fir::save_fir_to_wav(&coeffs, sample_rate as u32, &wav_path)
                {
                    warn!("Failed to save excess phase FIR for {}: {}", name, e);
                } else {
                    info!("  Saved excess phase FIR to {}", wav_path.display());
                }
            }

            Some(coeffs)
        }
        Err(e) => {
            warn!(
                "  Mixed-phase decomposition failed for '{}': {}. Using IIR only.",
                name, e
            );
            None
        }
    }
}

/// Apply standalone phase correction to a channel (rePhase-style).
///
/// Generates a phase-only FIR from the measurement's excess phase and appends it
/// to the channel's DSP chain. If the channel already has a magnitude FIR, the
/// two are convolved together so `fir_coeffs` remains a single filter for IR
/// computation.
pub(super) fn apply_phase_correction(
    name: &str,
    ch: &mut ChannelOptimizationResult,
    chain: &mut crate::roomeq::types::ChannelDspChain,
    config: &crate::roomeq::types::MixedPhaseSerdeConfig,
    sample_rate: f64,
    output_dir: Option<&Path>,
) {
    let phase = match ch.initial_curve.phase.as_ref() {
        Some(p) if !p.is_empty() => p,
        _ => return,
    };
    let _ = phase; // used via initial_curve below

    let mp_config = crate::roomeq::mixed_phase::MixedPhaseConfig {
        max_fir_length_ms: config.max_fir_length_ms,
        pre_ringing_threshold_db: config.pre_ringing_threshold_db,
        min_spatial_depth: config.min_spatial_depth,
        phase_smoothing_octaves: config.phase_smoothing_octaves,
    };

    let phase_fir = match crate::roomeq::mixed_phase::decompose_phase(&ch.initial_curve, &mp_config)
    {
        Ok((_min, _excess, delay_ms, residual)) => {
            info!(
                "  Phase correction '{}': delay={:.2} ms, generating phase-only FIR",
                name, delay_ms
            );
            crate::roomeq::mixed_phase::generate_excess_phase_fir(
                &ch.initial_curve.freq,
                &residual,
                &mp_config,
                sample_rate,
            )
        }
        Err(e) => {
            warn!("  Phase correction failed for '{}': {}", name, e);
            return;
        }
    };

    // Save phase FIR WAV and add convolution plugin
    let filename = format!("{}_phase_correction.wav", name);
    if let Some(out_dir) = output_dir {
        let wav_path = out_dir.join(&filename);
        if let Err(e) = crate::fir::save_fir_to_wav(&phase_fir, sample_rate as u32, &wav_path) {
            warn!("Failed to save phase correction FIR for {}: {}", name, e);
        } else {
            info!("  Saved phase correction FIR to {}", wav_path.display());
        }
    }
    chain
        .plugins
        .push(crate::roomeq::output::create_convolution_plugin(&filename));

    let phase_response = crate::response::compute_fir_complex_response(
        &phase_fir,
        &ch.final_curve.freq,
        sample_rate,
    );
    ch.final_curve = crate::response::apply_complex_response(&ch.final_curve, &phase_response);
    chain.final_curve = Some((&ch.final_curve).into());

    // Combine with existing FIR for IR computation (convolve the two)
    if let Some(ref existing) = ch.fir_coeffs {
        ch.fir_coeffs = Some(convolve(existing, &phase_fir));
    } else {
        ch.fir_coeffs = Some(phase_fir);
    }
}

/// Linear convolution of two FIR filters.
pub(super) fn convolve(a: &[f64], b: &[f64]) -> Vec<f64> {
    let len = a.len() + b.len() - 1;
    let mut out = vec![0.0; len];
    for (i, &av) in a.iter().enumerate() {
        for (j, &bv) in b.iter().enumerate() {
            out[i + j] += av * bv;
        }
    }
    out
}

pub(super) fn apply_phase_only_adjustment_to_reported_curve(
    curve: &mut Curve,
    delay_ms: f64,
    invert_polarity: bool,
) {
    if curve.freq.is_empty() {
        return;
    }

    let base_phase = curve
        .phase
        .clone()
        .unwrap_or_else(|| ndarray::Array1::zeros(curve.freq.len()));
    let inversion_phase = if invert_polarity { 180.0 } else { 0.0 };
    let delay_s = delay_ms / 1000.0;

    let phase =
        ndarray::Array1::from_iter(curve.freq.iter().zip(base_phase.iter()).map(
            |(&freq_hz, &phase_deg)| phase_deg + inversion_phase - (360.0 * freq_hz * delay_s),
        ));
    curve.phase = Some(phase);
}

pub(super) fn sync_reported_phase_adjustment(
    channel_name: &str,
    channel_results: &mut HashMap<String, ChannelOptimizationResult>,
    channel_chains: &mut HashMap<String, ChannelDspChain>,
    delay_ms: f64,
    invert_polarity: bool,
) {
    if let Some(ch_result) = channel_results.get_mut(channel_name) {
        apply_phase_only_adjustment_to_reported_curve(
            &mut ch_result.final_curve,
            delay_ms,
            invert_polarity,
        );

        if let Some(chain) = channel_chains.get_mut(channel_name) {
            chain.final_curve = Some((&ch_result.final_curve).into());
        }
    } else if let Some(chain) = channel_chains.get_mut(channel_name)
        && let Some(final_curve) = chain.final_curve.clone()
    {
        let mut curve: Curve = final_curve.into();
        apply_phase_only_adjustment_to_reported_curve(&mut curve, delay_ms, invert_polarity);
        chain.final_curve = Some((&curve).into());
    }
}

pub(super) fn sync_reported_gain_adjustment(
    channel_name: &str,
    channel_results: &mut HashMap<String, ChannelOptimizationResult>,
    channel_chains: &mut HashMap<String, ChannelDspChain>,
    gain_db: f64,
    invert_polarity: bool,
) {
    if let Some(ch_result) = channel_results.get_mut(channel_name) {
        ch_result.final_curve.spl = &ch_result.final_curve.spl + gain_db;
        apply_phase_only_adjustment_to_reported_curve(
            &mut ch_result.final_curve,
            0.0,
            invert_polarity,
        );

        if let Some(chain) = channel_chains.get_mut(channel_name) {
            chain.final_curve = Some((&ch_result.final_curve).into());
        }
    } else if let Some(chain) = channel_chains.get_mut(channel_name)
        && let Some(final_curve) = chain.final_curve.clone()
    {
        let mut curve: Curve = final_curve.into();
        curve.spl = &curve.spl + gain_db;
        apply_phase_only_adjustment_to_reported_curve(&mut curve, 0.0, invert_polarity);
        chain.final_curve = Some((&curve).into());
    }
}

pub(super) fn sync_reported_biquad_adjustment(
    channel_name: &str,
    channel_results: &mut HashMap<String, ChannelOptimizationResult>,
    channel_chains: &mut HashMap<String, ChannelDspChain>,
    filters: &[Biquad],
    sample_rate: f64,
) {
    if filters.is_empty() {
        return;
    }

    if let Some(ch_result) = channel_results.get_mut(channel_name) {
        let response = crate::response::compute_peq_complex_response(
            filters,
            &ch_result.final_curve.freq,
            sample_rate,
        );
        ch_result.final_curve =
            crate::response::apply_complex_response(&ch_result.final_curve, &response);

        if let Some(chain) = channel_chains.get_mut(channel_name) {
            chain.final_curve = Some((&ch_result.final_curve).into());
        }
    } else if let Some(chain) = channel_chains.get_mut(channel_name)
        && let Some(final_curve) = chain.final_curve.clone()
    {
        let curve: Curve = final_curve.into();
        let response =
            crate::response::compute_peq_complex_response(filters, &curve.freq, sample_rate);
        let corrected = crate::response::apply_complex_response(&curve, &response);
        chain.final_curve = Some((&corrected).into());
    }
}

pub(super) fn sync_reported_fir_adjustment(
    channel_name: &str,
    channel_results: &mut HashMap<String, ChannelOptimizationResult>,
    channel_chains: &mut HashMap<String, ChannelDspChain>,
    coeffs: &[f64],
    sample_rate: f64,
) {
    if coeffs.is_empty() {
        return;
    }

    if let Some(ch_result) = channel_results.get_mut(channel_name) {
        let response = crate::response::compute_fir_complex_response(
            coeffs,
            &ch_result.final_curve.freq,
            sample_rate,
        );
        ch_result.final_curve =
            crate::response::apply_complex_response(&ch_result.final_curve, &response);

        if let Some(chain) = channel_chains.get_mut(channel_name) {
            chain.final_curve = Some((&ch_result.final_curve).into());
        }
    } else if let Some(chain) = channel_chains.get_mut(channel_name)
        && let Some(final_curve) = chain.final_curve.clone()
    {
        let curve: Curve = final_curve.into();
        let response =
            crate::response::compute_fir_complex_response(coeffs, &curve.freq, sample_rate);
        let corrected = crate::response::apply_complex_response(&curve, &response);
        chain.final_curve = Some((&corrected).into());
    }
}

pub(super) fn collect_current_final_curves(
    channel_results: &HashMap<String, ChannelOptimizationResult>,
) -> HashMap<String, Curve> {
    channel_results
        .iter()
        .map(|(name, result)| (name.clone(), result.final_curve.clone()))
        .collect()
}

pub(super) fn total_chain_delay_ms(chain: &ChannelDspChain) -> f64 {
    chain
        .plugins
        .iter()
        .filter(|plugin| plugin.plugin_type == "delay")
        .filter_map(|plugin| {
            plugin
                .parameters
                .get("delay_ms")
                .and_then(|value| value.as_f64())
        })
        .sum()
}

pub(super) fn compute_phase_alignment_delay_schedule(
    phase_alignment_results: &HashMap<String, (f64, bool, String)>,
) -> HashMap<String, f64> {
    let mut graph: HashMap<String, Vec<(String, f64)>> = HashMap::new();

    for (main_name, (relative_delay_ms, _invert, sub_name)) in phase_alignment_results {
        // The optimizer's delay means: delay(main) - delay(sub) = relative_delay_ms.
        graph
            .entry(sub_name.clone())
            .or_default()
            .push((main_name.clone(), *relative_delay_ms));
        graph
            .entry(main_name.clone())
            .or_default()
            .push((sub_name.clone(), -*relative_delay_ms));
    }

    let mut raw_offsets: HashMap<String, f64> = HashMap::new();
    let mut schedule: HashMap<String, f64> = HashMap::new();

    for start in graph.keys() {
        if raw_offsets.contains_key(start) {
            continue;
        }

        raw_offsets.insert(start.clone(), 0.0);
        let mut stack = vec![start.clone()];
        let mut component = Vec::new();

        while let Some(channel) = stack.pop() {
            component.push(channel.clone());
            let channel_offset = raw_offsets[&channel];

            if let Some(neighbors) = graph.get(&channel) {
                for (neighbor, delta_ms) in neighbors {
                    let neighbor_offset = channel_offset + *delta_ms;
                    if let Some(existing) = raw_offsets.get(neighbor) {
                        if (existing - neighbor_offset).abs() > 0.05 {
                            warn!(
                                "Conflicting phase-alignment delay constraints for '{}': {:.3} ms vs {:.3} ms; keeping first schedule",
                                neighbor, existing, neighbor_offset
                            );
                        }
                    } else {
                        raw_offsets.insert(neighbor.clone(), neighbor_offset);
                        stack.push(neighbor.clone());
                    }
                }
            }
        }

        let min_offset = component
            .iter()
            .filter_map(|name| raw_offsets.get(name))
            .copied()
            .fold(f64::INFINITY, f64::min);

        for name in component {
            if let Some(offset) = raw_offsets.get(&name) {
                let delay_ms = offset - min_offset;
                if delay_ms > 0.01 {
                    schedule.insert(name, delay_ms);
                }
            }
        }
    }

    schedule
}

pub(super) fn apply_phase_alignment_delay_schedule(
    phase_alignment_results: &HashMap<String, (f64, bool, String)>,
    channel_results: &mut HashMap<String, ChannelOptimizationResult>,
    channel_chains: &mut HashMap<String, ChannelDspChain>,
) -> HashMap<String, f64> {
    let schedule = compute_phase_alignment_delay_schedule(phase_alignment_results);

    for (channel_name, delay_ms) in &schedule {
        let applied = if let Some(chain) = channel_chains.get_mut(channel_name.as_str()) {
            output::add_delay_plugin(chain, *delay_ms);
            true
        } else {
            false
        };

        if applied {
            sync_reported_phase_adjustment(
                channel_name,
                channel_results,
                channel_chains,
                *delay_ms,
                false,
            );
            info!(
                "  Applied {:.3} ms phase alignment delay to '{}'",
                delay_ms, channel_name
            );
        }
    }

    schedule
}