autoeq_plot/
plot_results.rs1use std::collections::HashMap;
2use std::error::Error;
3use std::fs::File;
4use std::io::Write;
5use std::path::Path;
6
7use build_html::*;
8use plotly::Plot;
9
10use crate::plot_filters::plot_filters;
11use crate::plot_spin::{plot_spin, plot_spin_details, plot_spin_tonal};
12use crate::x2peq::compute_peq_response_from_x;
13
14pub async fn plot_compute(
15 config: &crate::PlotConfig,
16 optimized_params: &[f64],
17 input_curve: &crate::Curve,
18 target_curve: &crate::Curve,
19 deviation_curve: &crate::Curve,
20 cea2034_curves: &Option<HashMap<String, crate::Curve>>,
21) -> (Plot, Option<Plot>, Option<Plot>, Option<Plot>) {
22 let freqs = input_curve.freq.clone();
23
24 let plot_filters = plot_filters(
26 config,
27 input_curve,
28 target_curve,
29 deviation_curve,
30 optimized_params,
31 );
32
33 let eq_response = compute_peq_response_from_x(
34 &freqs,
35 optimized_params,
36 config.sample_rate,
37 config.peq_model,
38 );
39 let plot_spin_details = if cea2034_curves.is_some() {
40 Some(plot_spin_details(
41 cea2034_curves.as_ref(),
42 Some(&eq_response),
43 ))
44 } else {
45 None
46 };
47
48 let plot_spin_tonal = if cea2034_curves.is_some() {
49 Some(plot_spin_tonal(cea2034_curves.as_ref(), Some(&eq_response)))
50 } else {
51 None
52 };
53
54 let plot_spin_opt = if cea2034_curves.is_some() {
55 Some(plot_spin(cea2034_curves.as_ref(), Some(&eq_response)))
56 } else {
57 None
58 };
59
60 (
61 plot_filters,
62 plot_spin_details,
63 plot_spin_tonal,
64 plot_spin_opt,
65 )
66}
67
68pub async fn plot_results(
83 config: &crate::PlotConfig,
84 optimized_params: &[f64],
85 input_curve: &crate::Curve,
86 target_curve: &crate::Curve,
87 deviation_curve: &crate::Curve,
88 cea2034_curves: &Option<HashMap<String, crate::Curve>>,
89 output_path: &Path,
90) -> Result<(), Box<dyn Error>> {
91 let speaker = config.speaker_name.as_deref();
92 let (plot_filters, plot_spin_details, plot_spin_tonal, plot_spin_opt) = plot_compute(
93 config,
94 optimized_params,
95 input_curve,
96 target_curve,
97 deviation_curve,
98 cea2034_curves,
99 )
100 .await;
101
102 let title_text = match speaker {
104 Some(s) if !s.is_empty() => format!("{} -- #{} peq(s)", s, config.num_filters),
105 _ => "IIR Filter Optimization Results".to_string(),
106 };
107
108 let html: String = {
109 let base = HtmlPage::new()
110 .with_title(title_text)
111 .with_script_link("https://cdn.plot.ly/plotly-3.2.0.min.js")
112 .with_raw(plot_filters.to_inline_html(Some("filters")));
113 let page = if let Some(ref plot_spin) = plot_spin_details {
114 base.with_raw(plot_spin.to_inline_html(Some("details")))
115 } else {
116 base
117 };
118 let page2 = if let Some(ref plot_spin) = plot_spin_tonal {
119 page.with_raw(plot_spin.to_inline_html(Some("tonal")))
120 } else {
121 page
122 };
123 let page3 = if let Some(ref plot_spin) = plot_spin_opt {
124 page2.with_raw(plot_spin.to_inline_html(Some("spinorama")))
125 } else {
126 page2
127 };
128 page3.to_html_string()
129 };
130
131 let html_output_path = output_path.with_extension("html");
133 if let Some(parent) = html_output_path.parent() {
134 std::fs::create_dir_all(parent)?;
135 }
136
137 let mut file = File::create(&html_output_path)?;
138 file.write_all(html.as_bytes())?;
139 file.flush()?;
140
141 #[cfg(feature = "plotly_static")]
144 let stem = output_path
145 .file_stem()
146 .and_then(|s| s.to_str())
147 .unwrap_or("output");
148
149 #[cfg(feature = "plotly_static")]
150 let mut plots: Vec<(Plot, &str, usize, usize)> =
151 vec![(plot_filters, "filters", 1280usize, 800usize)];
152 #[cfg(feature = "plotly_static")]
153 if let Some(plot_spin) = plot_spin_details {
154 plots.push((plot_spin, "details", 1280, 650));
155 }
156
157 #[cfg(feature = "plotly_static")]
158 if let Some(plot_spin) = plot_spin_tonal {
159 plots.push((plot_spin, "tonal", 1280, 650));
160 }
161
162 #[cfg(feature = "plotly_static")]
163 if let Some(plot_spin) = plot_spin_opt {
164 plots.push((plot_spin, "spinorama", 1280, 450));
165 }
166
167 #[cfg(feature = "plotly_static")]
169 {
170 for (plot, name, width, height) in plots {
171 let img_path = output_path.with_file_name(format!("{}-{}.png", stem, name));
172 if let Some(parent) = img_path.parent() {
173 std::fs::create_dir_all(parent)?;
174 }
175 crate::static_export::write_plot_png(&plot, &img_path, width, height)?;
176 }
177 }
178
179 #[cfg(not(feature = "plotly_static"))]
180 {
181 eprintln!(
182 "ℹ️ Info: PNG export disabled. Enable with --features plotly_static. HTML report was generated at {}",
183 html_output_path.display()
184 );
185 }
186
187 Ok(())
188}