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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
use std::collections::HashMap;
use std::error::Error;

use ndarray::Array1;
use reqwest;
use serde_json::Value;
use tokio::fs;
use urlencoding;

use crate::cea2034 as score;
use crate::read::directory::{data_dir_for, measurement_filename};
use crate::read::interpolate::interpolate;
use crate::read::plot::{normalize_plotly_json_from_str, normalize_plotly_value_with_suggestions};
use crate::{Curve, DirectivityCurve, DirectivityData};

/// CEA2034 spin data with all standard curves
#[derive(Debug, Clone)]
pub struct Cea2034Data {
    /// On Axis response
    pub on_axis: Curve,
    /// Listening Window response
    pub listening_window: Curve,
    /// Early Reflections response
    pub early_reflections: Curve,
    /// Sound Power response
    pub sound_power: Curve,
    /// Estimated In-Room Response (PIR)
    pub estimated_in_room: Curve,
    /// Early Reflections Directivity Index (On Axis - ER)
    pub er_di: Curve,
    /// Sound Power Directivity Index (On Axis - SP)
    pub sp_di: Curve,
    /// All curves as HashMap (for backward compatibility)
    pub curves: HashMap<String, Curve>,
}

/// Fetch a frequency response curve from the spinorama API
///
/// # Arguments
/// * `speaker` - Speaker name
/// * `version` - Measurement version
/// * `measurement` - Measurement type (e.g., "CEA2034")
/// * `curve_name` - Name of the specific curve to extract
///
/// # Returns
/// * Result containing a Curve struct or an error
pub async fn read_spinorama(
    speaker: &str,
    version: &str,
    measurement: &str,
    curve_name: &str,
) -> Result<crate::Curve, Box<dyn Error>> {
    fetch_curve_from_api(speaker, version, measurement, curve_name).await
}

/// Fetch a frequency response curve from the spinorama API
///
/// # Arguments
/// * `speaker` - Speaker name
/// * `version` - Measurement version
/// * `measurement` - Measurement type (e.g., "CEA2034")
/// * `curve_name` - Name of the specific curve to extract
///
/// # Returns
/// * Result containing a Curve struct or an error
pub async fn fetch_curve_from_api(
    speaker: &str,
    version: &str,
    measurement: &str,
    curve_name: &str,
) -> Result<crate::Curve, Box<dyn Error>> {
    // Fetch the full measurement once, then extract the requested curve
    let plot_data = fetch_measurement_plot_data(speaker, version, measurement).await?;
    extract_curve_by_name(&plot_data, measurement, curve_name)
}

/// Fetch and parse the full Plotly JSON object for a given measurement (single HTTP GET)
///
/// # Arguments
/// * `speaker` - Speaker name
/// * `version` - Measurement version
/// * `measurement` - Measurement type (e.g., "CEA2034")
///
/// # Returns
/// * Result containing the Plotly JSON data or an error
pub async fn fetch_measurement_plot_data(
    speaker: &str,
    version: &str,
    measurement: &str,
) -> Result<Value, Box<dyn Error>> {
    // 1) Try local cache first: data_cached/speakers/org.spinorama/{speaker}/{measurement}.json
    // We keep filename identical to measurement name when possible (with path separators replaced).
    let cache_dir = data_dir_for(speaker);
    let cache_file = cache_dir.join(measurement_filename(measurement));

    if let Ok(content) = fs::read_to_string(&cache_file).await {
        if let Ok(plot_data) = normalize_plotly_json_from_str(&content) {
            return Ok(plot_data);
        } else {
            log::debug!(
                "⚠️  Cache file exists but could not be parsed as Plotly JSON: {:?}",
                &cache_file
            );
        }
    }

    // URL-encode the parameters
    let encoded_speaker = urlencoding::encode(speaker);
    let encoded_version = urlencoding::encode(version);
    let encoded_measurement = urlencoding::encode(measurement);

    let url = format!(
        "https://api.spinorama.org/v1/speaker/{}/version/{}/measurements/{}?measurement_format=json",
        encoded_speaker, encoded_version, encoded_measurement
    );

    // println!("* Fetching data from {}", url);

    let response = reqwest::get(&url).await?;
    if !response.status().is_success() {
        return Err(format!("API request failed with status: {}", response.status()).into());
    }
    let api_response: Value = response.json().await?;

    // Normalize from API response (array-of-string JSON) to Plotly JSON object
    let plot_data = normalize_plotly_value_with_suggestions(&api_response).await?;

    // 2) Save normalized Plotly JSON to cache for future use
    if let Err(e) = fs::create_dir_all(&cache_dir).await {
        log::debug!("⚠️  Failed to create cache dir {:?}: {}", &cache_dir, e);
    } else {
        match serde_json::to_string(&plot_data) {
            Ok(serialized) => {
                if let Err(e) = fs::write(&cache_file, serialized).await {
                    log::debug!("⚠️  Failed to write cache file {:?}: {}", &cache_file, e);
                }
            }
            Err(e) => log::debug!("⚠️  Failed to serialize plot data for cache: {}", e),
        }
    }

    Ok(plot_data)
}

/// Extract a single curve from a previously-fetched Plotly JSON object
///
/// # Arguments
/// * `plot_data` - The Plotly JSON data
/// * `measurement` - Measurement type (e.g., "CEA2034")
/// * `curve_name` - Name of the specific curve to extract
///
/// # Returns
/// * Result containing a Curve struct or an error
pub fn extract_curve_by_name(
    plot_data: &Value,
    measurement: &str,
    curve_name: &str,
) -> Result<crate::Curve, Box<dyn Error>> {
    // Extract frequency and SPL data from the Plotly JSON structure
    let mut freqs = Vec::new();
    let mut spls = Vec::new();
    let mut phases = Vec::new();

    // Look for the trace with the expected name and extract x and y data
    if let Some(data) = plot_data.get("data").and_then(|d| d.as_array()) {
        // First pass: find SPL (magnitude)
        for trace in data {
            // Check if this is the SPL trace (not DI or other traces)
            let is_spl_trace = trace
                .get("name")
                .and_then(|n| n.as_str())
                .map(|name| is_target_trace_name(measurement, curve_name, name))
                .unwrap_or(false);

            if is_spl_trace {
                // Try to extract x and y data - supports both typed arrays and plain arrays
                let x_val = trace.get("x");
                let y_val = trace.get("y");

                if let (Some(x_data), Some(y_data)) = (x_val, y_val) {
                    // Try typed array format first (object with dtype/bdata)
                    if let (Some(x_obj), Some(y_obj)) = (x_data.as_object(), y_data.as_object()) {
                        // Decode x values (frequency)
                        if let (Some(dtype), Some(bdata)) = (
                            x_obj.get("dtype").and_then(|d| d.as_str()),
                            x_obj.get("bdata").and_then(|b| b.as_str()),
                        ) {
                            freqs = decode_typed_array(bdata, dtype)?;
                        }

                        // Decode y values (SPL)
                        if let (Some(dtype), Some(bdata)) = (
                            y_obj.get("dtype").and_then(|d| d.as_str()),
                            y_obj.get("bdata").and_then(|b| b.as_str()),
                        ) {
                            spls = decode_typed_array(bdata, dtype)?;
                        }
                    }

                    // Fallback: try plain array format
                    if freqs.is_empty()
                        && let (Some(x_arr), Some(y_arr)) = (x_data.as_array(), y_data.as_array())
                    {
                        freqs = x_arr.iter().filter_map(|v| v.as_f64()).collect();
                        spls = y_arr.iter().filter_map(|v| v.as_f64()).collect();
                    }
                }
                if !freqs.is_empty() {
                    break;
                }
            }
        }

        // Second pass: find phase (if available)
        let phase_name = format!("{} Phase", curve_name);
        for trace in data {
            let is_phase_trace = trace
                .get("name")
                .and_then(|n| n.as_str())
                .map(|name| name == phase_name)
                .unwrap_or(false);

            if is_phase_trace {
                let y_val = trace.get("y");
                if let Some(y_data) = y_val {
                    if let Some(y_obj) = y_data.as_object() {
                        if let (Some(dtype), Some(bdata)) = (
                            y_obj.get("dtype").and_then(|d| d.as_str()),
                            y_obj.get("bdata").and_then(|b| b.as_str()),
                        ) {
                            phases = decode_typed_array(bdata, dtype)?;
                        }
                    } else if let Some(y_arr) = y_data.as_array() {
                        phases = y_arr.iter().filter_map(|v| v.as_f64()).collect();
                    }
                }
                if !phases.is_empty() {
                    break;
                }
            }
        }
    }

    if freqs.is_empty() {
        let available = collect_trace_names(plot_data);
        return Err(format!(
            "Failed to extract frequency and SPL data for curve '{}' in measurement '{}'. Available traces: {:?}",
            curve_name, measurement, available
        ).into());
    }

    Ok(crate::Curve {
        freq: Array1::from(freqs),
        spl: Array1::from(spls),
        phase: if !phases.is_empty() {
            Some(Array1::from(phases))
        } else {
            None
        },
    })
}

fn decode_typed_array(bdata: &str, dtype: &str) -> Result<Vec<f64>, Box<dyn Error>> {
    // Create lookup table for base64 decoding
    let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut lookup = [0u8; 256];
    for (i, c) in chars.chars().enumerate() {
        lookup[c as usize] = i as u8;
    }

    // Calculate buffer length
    let len = bdata.len();
    let mut buffer_length = len * 3 / 4;

    // Adjust for padding
    if len > 0 && bdata.chars().nth(len - 1) == Some('=') {
        buffer_length -= 1;
        if len > 1 && bdata.chars().nth(len - 2) == Some('=') {
            buffer_length -= 1;
        }
    }

    // Decode base64
    let mut bytes = vec![0u8; buffer_length];
    let mut p = 0;
    let bdata_bytes = bdata.as_bytes();

    for i in (0..len).step_by(4) {
        let encoded1 = lookup[bdata_bytes[i] as usize] as u32;
        let encoded2 = if i + 1 < len {
            lookup[bdata_bytes[i + 1] as usize] as u32
        } else {
            0
        };
        let encoded3 = if i + 2 < len {
            lookup[bdata_bytes[i + 2] as usize] as u32
        } else {
            0
        };
        let encoded4 = if i + 3 < len {
            lookup[bdata_bytes[i + 3] as usize] as u32
        } else {
            0
        };

        if p < buffer_length {
            bytes[p] = ((encoded1 << 2) | (encoded2 >> 4)) as u8;
            p += 1;
        }

        if p < buffer_length {
            bytes[p] = (((encoded2 & 15) << 4) | (encoded3 >> 2)) as u8;
            p += 1;
        }

        if p < buffer_length {
            bytes[p] = (((encoded3 & 3) << 6) | (encoded4 & 63)) as u8;
            p += 1;
        }
    }

    // Convert to appropriate typed array based on dtype
    let result = match dtype {
        "f8" => {
            // Float64Array - 8 bytes per element
            let mut values = Vec::new();
            for chunk in bytes.chunks_exact(8) {
                let bits = u64::from_le_bytes([
                    chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7],
                ]);
                values.push(f64::from_bits(bits));
            }
            values
        }
        "f4" => {
            // Float32Array - 4 bytes per element
            let mut values = Vec::new();
            for chunk in bytes.chunks_exact(4) {
                let bits = u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
                values.push(f32::from_bits(bits) as f64);
            }
            values
        }
        "i4" => {
            // Int32Array - 4 bytes per element
            let mut values = Vec::new();
            for chunk in bytes.chunks_exact(4) {
                let val = i32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
                values.push(val as f64);
            }
            values
        }
        "i2" => {
            // Int16Array - 2 bytes per element
            let mut values = Vec::new();
            for chunk in bytes.chunks_exact(2) {
                let val = i16::from_le_bytes([chunk[0], chunk[1]]);
                values.push(val as f64);
            }
            values
        }
        "i1" => {
            // Int8Array - 1 byte per element
            bytes.into_iter().map(|b| b as i8 as f64).collect()
        }
        "u4" => {
            // Uint32Array - 4 bytes per element
            let mut values = Vec::new();
            for chunk in bytes.chunks_exact(4) {
                let val = u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
                values.push(val as f64);
            }
            values
        }
        "u2" => {
            // Uint16Array - 2 bytes per element
            let mut values = Vec::new();
            for chunk in bytes.chunks_exact(2) {
                let val = u16::from_le_bytes([chunk[0], chunk[1]]);
                values.push(val as f64);
            }
            values
        }
        "u1" | "u1c" => {
            // Uint8Array or Uint8ClampedArray - 1 byte per element
            bytes.into_iter().map(|b| b as f64).collect()
        }
        _ => {
            // Default to treating as bytes
            bytes.into_iter().map(|b| b as f64).collect()
        }
    };

    Ok(result)
}

fn is_target_trace_name(measurement: &str, curve_name: &str, candidate: &str) -> bool {
    if measurement.eq_ignore_ascii_case("CEA2034") || measurement.eq("Estimated In-Room Response") {
        // For CEA2034 data, select the specific curve provided by the user
        // Prefer exact match; allow substring match as a fallback
        candidate == curve_name
    } else {
        // Fallback heuristic for other measurement types
        log::debug!(
            "⚠️  Warning: unable to determine if trace name {} is a target for curve {}, using heuristic",
            candidate,
            curve_name
        );
        candidate.contains("Listening Window")
            || candidate.contains("On Axis")
            || candidate.contains("Sound Power")
            || candidate.contains("Early Reflections")
            || candidate.contains("Estimated In-Room Response")
    }
}

fn collect_trace_names(plot_data: &Value) -> Vec<String> {
    plot_data
        .get("data")
        .and_then(|d| d.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|trace| {
                    trace
                        .get("name")
                        .and_then(|n| n.as_str())
                        .map(|s| s.to_string())
                })
                .collect::<Vec<String>>()
        })
        .unwrap_or_default()
}

/// Extract all CEA2034 curves from plot data using the original frequency grid
///
/// # Arguments
/// * `plot_data` - The Plotly JSON data containing CEA2034 measurements
/// * `measurement` - Measurement type (e.g., "CEA2034")
///
/// # Returns
/// * HashMap of curve names to Curve structs with original frequency grid
///
/// # Details
/// Extracts standard CEA2034 curves (On Axis, Listening Window, Early Reflections,
/// Sound Power, etc.) using the original frequency grid from the measurement data.
/// This is preferred for score calculations to match the Python implementation.
pub fn extract_cea2034_curves_original(
    plot_data: &Value,
    measurement: &str,
) -> Result<HashMap<String, Curve>, Box<dyn Error>> {
    let mut curves = HashMap::new();

    // List of CEA2034 curves to extract
    let curve_names = [
        "On Axis",
        "Listening Window",
        "Early Reflections",
        "Sound Power",
        "Early Reflections DI",
        "Sound Power DI",
    ];

    // Extract each curve with its original frequency grid
    for name in &curve_names {
        match extract_curve_by_name(plot_data, measurement, name) {
            Ok(curve) => {
                curves.insert(name.to_string(), curve);
            }
            Err(e) => {
                let available = collect_trace_names(plot_data);
                return Err(format!(
                    "Could not extract curve '{}' for measurement '{}': {}. Available traces: {:?}",
                    name, measurement, e, available
                )
                .into());
            }
        }
    }

    // Ensure required curves exist for PIR computation
    let lw_curve = curves.get("Listening Window").ok_or_else(|| {
        std::io::Error::other("Missing 'Listening Window' curve after extraction")
    })?;
    let er_curve = curves.get("Early Reflections").ok_or_else(|| {
        std::io::Error::other("Missing 'Early Reflections' curve after extraction")
    })?;
    let sp_curve = curves
        .get("Sound Power")
        .ok_or_else(|| std::io::Error::other("Missing 'Sound Power' curve after extraction"))?;

    let freq = &lw_curve.freq;
    let lw = &lw_curve.spl;
    let er = &er_curve.spl;
    let sp = &sp_curve.spl;
    let pir = score::compute_pir_from_lw_er_sp(lw, er, sp);

    curves.insert(
        "Estimated In-Room Response".to_string(),
        Curve {
            freq: freq.clone(),
            spl: pir,
            phase: None,
        },
    );

    Ok(curves)
}

/// Extract all CEA2034 curves from plot data and interpolate to target frequency grid
///
/// # Arguments
/// * `plot_data` - The Plotly JSON data containing CEA2034 measurements
/// * `measurement` - Measurement type (e.g., "CEA2034")
/// * `freq` - Target frequency grid for interpolation
///
/// # Returns
/// * HashMap of curve names to interpolated Curve structs
///
/// # Details
/// Extracts standard CEA2034 curves (On Axis, Listening Window, Early Reflections,
/// Sound Power, etc.) and interpolates them to the specified frequency grid.
pub fn extract_cea2034_curves(
    plot_data: &Value,
    measurement: &str,
    freq: &Array1<f64>,
) -> Result<HashMap<String, Curve>, Box<dyn Error>> {
    let mut curves = HashMap::new();

    // List of CEA2034 curves to extract
    let curve_names = [
        "On Axis",
        "Listening Window",
        "Early Reflections",
        "Sound Power",
        "Early Reflections DI",
        "Sound Power DI",
    ];

    // Extract each curve
    for name in &curve_names {
        match extract_curve_by_name(plot_data, measurement, name) {
            Ok(curve) => {
                // Interpolate to the target frequency grid
                let interpolated = interpolate(freq, &curve);
                curves.insert(
                    name.to_string(),
                    Curve {
                        freq: freq.clone(),
                        spl: interpolated.spl,
                        phase: None,
                    },
                );
            }
            Err(e) => {
                let available = collect_trace_names(plot_data);
                return Err(format!(
                    "Could not extract curve '{}' for measurement '{}': {}. Available traces: {:?}",
                    name, measurement, e, available
                )
                .into());
            }
        }
    }

    // Ensure required curves exist for PIR computation
    let lw_curve = curves.get("Listening Window").ok_or_else(|| {
        std::io::Error::other("Missing 'Listening Window' curve after extraction")
    })?;
    let er_curve = curves.get("Early Reflections").ok_or_else(|| {
        std::io::Error::other("Missing 'Early Reflections' curve after extraction")
    })?;
    let sp_curve = curves
        .get("Sound Power")
        .ok_or_else(|| std::io::Error::other("Missing 'Sound Power' curve after extraction"))?;

    let lw = &lw_curve.spl;
    let er = &er_curve.spl;
    let sp = &sp_curve.spl;
    let pir = score::compute_pir_from_lw_er_sp(lw, er, sp);
    curves.insert(
        "Estimated In-Room Response".to_string(),
        Curve {
            freq: freq.clone(),
            spl: pir,
            phase: None,
        },
    );

    Ok(curves)
}

/// Parse an angle string from trace names (e.g., "-60°", "0° (ON)", "10°")
///
/// # Arguments
/// * `name` - The trace name containing an angle
///
/// # Returns
/// * Some(angle) if the angle could be parsed, None otherwise
fn parse_angle_from_trace_name(name: &str) -> Option<f64> {
    // Handle special case "0° (ON)"
    let name = name.replace("(ON)", "").trim().to_string();

    // Remove degree symbol and parse
    let angle_str = name.replace('°', "").trim().to_string();

    angle_str.parse::<f64>().ok()
}

/// Extract all directivity curves from a measurement plot
///
/// # Arguments
/// * `plot_data` - The Plotly JSON data containing directivity measurements
///
/// # Returns
/// * Vector of DirectivityCurve structs, sorted by angle
fn extract_directivity_curves(plot_data: &Value) -> Result<Vec<DirectivityCurve>, Box<dyn Error>> {
    let mut curves = Vec::new();

    if let Some(data) = plot_data.get("data").and_then(|d| d.as_array()) {
        for trace in data {
            // Get the trace name
            let name = match trace.get("name").and_then(|n| n.as_str()) {
                Some(n) => n,
                None => continue,
            };

            // Try to parse the angle from the name
            let angle = match parse_angle_from_trace_name(name) {
                Some(a) => a,
                None => continue, // Skip traces without valid angles
            };

            // Extract x and y data which are encoded as typed arrays
            if let (Some(x_data), Some(y_data)) = (
                trace.get("x").and_then(|x| x.as_object()),
                trace.get("y").and_then(|y| y.as_object()),
            ) {
                let mut freqs = Vec::new();
                let mut spls = Vec::new();

                // Decode x values (frequency)
                if let (Some(dtype), Some(bdata)) = (
                    x_data.get("dtype").and_then(|d| d.as_str()),
                    x_data.get("bdata").and_then(|b| b.as_str()),
                ) {
                    freqs = decode_typed_array(bdata, dtype)?;
                }

                // Decode y values (SPL)
                if let (Some(dtype), Some(bdata)) = (
                    y_data.get("dtype").and_then(|d| d.as_str()),
                    y_data.get("bdata").and_then(|b| b.as_str()),
                ) {
                    spls = decode_typed_array(bdata, dtype)?;
                }

                if !freqs.is_empty() && freqs.len() == spls.len() {
                    curves.push(DirectivityCurve {
                        angle,
                        freq: Array1::from(freqs),
                        spl: Array1::from(spls),
                    });
                }
            }
        }
    }

    // Sort by angle
    curves.sort_by(|a, b| {
        a.angle
            .partial_cmp(&b.angle)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    Ok(curves)
}

/// Fetch directivity data (SPL Horizontal and SPL Vertical) from the spinorama API
///
/// # Arguments
/// * `speaker` - Speaker name
/// * `version` - Measurement version
///
/// # Returns
/// * Result containing DirectivityData or an error
///
/// # Details
/// Fetches both "SPL Horizontal" and "SPL Vertical" measurements, extracting
/// all angle traces (typically -60° to +60° in 10° increments).
pub async fn fetch_directivity_data(
    speaker: &str,
    version: &str,
) -> Result<DirectivityData, Box<dyn Error>> {
    // Fetch horizontal data
    let horizontal_data = fetch_measurement_plot_data(speaker, version, "SPL Horizontal").await?;
    let horizontal = extract_directivity_curves(&horizontal_data)?;

    if horizontal.is_empty() {
        return Err("No horizontal directivity curves found".into());
    }

    // Fetch vertical data
    let vertical_data = fetch_measurement_plot_data(speaker, version, "SPL Vertical").await?;
    let vertical = extract_directivity_curves(&vertical_data)?;

    if vertical.is_empty() {
        return Err("No vertical directivity curves found".into());
    }

    Ok(DirectivityData {
        horizontal,
        vertical,
    })
}

/// Extract a single directivity curve at a specific angle
///
/// # Arguments
/// * `directivity` - The full directivity data
/// * `angle` - The desired angle in degrees
/// * `plane` - "horizontal" or "vertical"
///
/// # Returns
/// * Option containing the DirectivityCurve if found
pub fn get_directivity_at_angle<'a>(
    directivity: &'a DirectivityData,
    angle: f64,
    plane: &str,
) -> Option<&'a DirectivityCurve> {
    let curves = match plane.to_lowercase().as_str() {
        "horizontal" | "h" => &directivity.horizontal,
        "vertical" | "v" => &directivity.vertical,
        _ => return None,
    };

    curves.iter().find(|c| (c.angle - angle).abs() < 0.5)
}

/// Get the on-axis response from directivity data
///
/// # Arguments
/// * `directivity` - The full directivity data
/// * `plane` - "horizontal" or "vertical"
///
/// # Returns
/// * Option containing the on-axis DirectivityCurve
pub fn get_on_axis<'a>(
    directivity: &'a DirectivityData,
    plane: &str,
) -> Option<&'a DirectivityCurve> {
    get_directivity_at_angle(directivity, 0.0, plane)
}

/// Get available angles in the directivity data
///
/// # Arguments
/// * `directivity` - The full directivity data
/// * `plane` - "horizontal" or "vertical"
///
/// # Returns
/// * Vector of available angles
pub fn get_available_angles(directivity: &DirectivityData, plane: &str) -> Vec<f64> {
    let curves = match plane.to_lowercase().as_str() {
        "horizontal" | "h" => &directivity.horizontal,
        "vertical" | "v" => &directivity.vertical,
        _ => return Vec::new(),
    };

    curves.iter().map(|c| c.angle).collect()
}

/// Contour plot data with 2D heatmap structure
///
/// Used for "SPL Horizontal Contour" and "SPL Vertical Contour" measurements
/// which provide full angle range data (-180° to +180°).
#[derive(Debug, Clone)]
pub struct ContourPlotData {
    /// Frequency values (X axis)
    pub freq: Vec<f64>,
    /// Angle values in degrees (Y axis)
    pub angles: Vec<f64>,
    /// SPL values as a 2D grid (row-major: angles × freq)
    pub spl: Vec<f64>,
    /// Number of frequency points
    pub freq_count: usize,
    /// Number of angle points
    pub angle_count: usize,
}

/// Fetch contour plot data (SPL Horizontal Contour or SPL Vertical Contour) from the spinorama API
///
/// # Arguments
/// * `speaker` - Speaker name
/// * `version` - Measurement version
/// * `plane` - Either "horizontal" or "vertical"
///
/// # Returns
/// * Result containing ContourPlotData or an error
///
/// # Details
/// Fetches "SPL Horizontal Contour" or "SPL Vertical Contour" measurements, which are
/// pre-computed heatmaps with full angle range (typically -180° to +180°).
pub async fn fetch_contour_data(
    speaker: &str,
    version: &str,
    plane: &str,
) -> Result<ContourPlotData, Box<dyn Error>> {
    let measurement = match plane.to_lowercase().as_str() {
        "horizontal" | "h" => "SPL Horizontal Contour",
        "vertical" | "v" => "SPL Vertical Contour",
        _ => {
            return Err(format!("Invalid plane: {}. Use 'horizontal' or 'vertical'", plane).into());
        }
    };

    let plot_data = fetch_measurement_plot_data(speaker, version, measurement).await?;
    extract_contour_data(&plot_data)
}

/// Load spinorama measurement with full CEA2034 spin data
///
/// This fetches the requested curve and also extracts all CEA2034 curves
/// when the measurement type is CEA2034.
///
/// # Arguments
/// * `speaker` - Speaker name (e.g., "KEF R3")
/// * `version` - Version (e.g., "asr")
/// * `measurement` - Measurement type (e.g., "CEA2034")
/// * `curve_name` - Specific curve to use as primary (e.g., "Listening Window")
///
/// # Returns
/// Tuple of (primary curve, optional spin data)
pub async fn load_spinorama_with_spin(
    speaker: &str,
    version: &str,
    measurement: &str,
    curve_name: &str,
) -> Result<(Curve, Option<Cea2034Data>), Box<dyn Error>> {
    // Handle Estimated In-Room Response specially
    if measurement == "Estimated In-Room Response"
        || (measurement == "CEA2034" && curve_name == "Estimated In-Room Response")
    {
        let plot_data = fetch_measurement_plot_data(speaker, version, "CEA2034").await?;
        let curves = extract_cea2034_curves_original(&plot_data, "CEA2034")?;

        let pir_curve = curves
            .get("Estimated In-Room Response")
            .ok_or_else(|| {
                Box::<dyn Error>::from("Estimated In-Room Response curve not found in CEA2034 data")
            })?
            .clone();

        let spin_data = build_cea2034_data(curves)?;
        return Ok((pir_curve, Some(spin_data)));
    }

    // Standard curve fetch
    let curve = read_spinorama(speaker, version, measurement, curve_name).await?;

    // Extract spin data if CEA2034
    let spin_data = if measurement == "CEA2034" {
        let plot_data = fetch_measurement_plot_data(speaker, version, measurement).await?;
        let curves = extract_cea2034_curves_original(&plot_data, "CEA2034")?;
        Some(build_cea2034_data(curves)?)
    } else {
        None
    };

    Ok((curve, spin_data))
}

/// Build Cea2034Data from curves HashMap
pub fn build_cea2034_data(curves: HashMap<String, Curve>) -> Result<Cea2034Data, Box<dyn Error>> {
    let get_curve = |name: &str| -> Result<Curve, Box<dyn Error>> {
        curves
            .get(name)
            .cloned()
            .ok_or_else(|| Box::<dyn Error>::from(format!("Missing CEA2034 curve: {}", name)))
    };

    let on_axis = get_curve("On Axis")?;
    let listening_window = get_curve("Listening Window")?;
    let early_reflections = get_curve("Early Reflections")?;
    let sound_power = get_curve("Sound Power")?;
    let estimated_in_room = get_curve("Estimated In-Room Response")?;

    // Compute directivity indices
    let er_di = Curve {
        freq: on_axis.freq.clone(),
        spl: &on_axis.spl - &early_reflections.spl,
        phase: None,
    };
    let sp_di = Curve {
        freq: on_axis.freq.clone(),
        spl: &on_axis.spl - &sound_power.spl,
        phase: None,
    };

    Ok(Cea2034Data {
        on_axis,
        listening_window,
        early_reflections,
        sound_power,
        estimated_in_room,
        er_di,
        sp_di,
        curves,
    })
}

/// Extract contour data from Plotly JSON (heatmap format)
fn extract_contour_data(plot_data: &Value) -> Result<ContourPlotData, Box<dyn Error>> {
    // Look for the first trace with a 'z' array (heatmap data)
    if let Some(data) = plot_data.get("data").and_then(|d| d.as_array()) {
        for trace in data {
            // Look for z data (2D grid), x (frequencies), and y (angles)
            if let (Some(x_data), Some(y_data), Some(z_data)) =
                (trace.get("x"), trace.get("y"), trace.get("z"))
            {
                let mut freq = Vec::new();
                let mut angles = Vec::new();
                let mut spl = Vec::new();

                // Decode x values (frequency) - could be typed array or regular array
                if let Some(x_obj) = x_data.as_object() {
                    if let (Some(dtype), Some(bdata)) = (
                        x_obj.get("dtype").and_then(|d| d.as_str()),
                        x_obj.get("bdata").and_then(|b| b.as_str()),
                    ) {
                        freq = decode_typed_array(bdata, dtype)?;
                    }
                } else if let Some(x_arr) = x_data.as_array() {
                    freq = x_arr.iter().filter_map(|v| v.as_f64()).collect();
                }

                // Decode y values (angles) - could be typed array or regular array
                if let Some(y_obj) = y_data.as_object() {
                    if let (Some(dtype), Some(bdata)) = (
                        y_obj.get("dtype").and_then(|d| d.as_str()),
                        y_obj.get("bdata").and_then(|b| b.as_str()),
                    ) {
                        angles = decode_typed_array(bdata, dtype)?;
                    }
                } else if let Some(y_arr) = y_data.as_array() {
                    angles = y_arr.iter().filter_map(|v| v.as_f64()).collect();
                }

                // Decode z values (SPL grid) - could be typed 2D array or nested regular arrays
                if let Some(z_obj) = z_data.as_object() {
                    // Typed array format with bdata
                    if let (Some(dtype), Some(bdata)) = (
                        z_obj.get("dtype").and_then(|d| d.as_str()),
                        z_obj.get("bdata").and_then(|b| b.as_str()),
                    ) {
                        spl = decode_typed_array(bdata, dtype)?;
                    }
                } else if let Some(z_arr) = z_data.as_array() {
                    // Nested array format [[row0], [row1], ...]
                    for row in z_arr {
                        if let Some(row_obj) = row.as_object() {
                            // Typed array row
                            if let (Some(dtype), Some(bdata)) = (
                                row_obj.get("dtype").and_then(|d| d.as_str()),
                                row_obj.get("bdata").and_then(|b| b.as_str()),
                            ) {
                                let row_data = decode_typed_array(bdata, dtype)?;
                                spl.extend(row_data);
                            }
                        } else if let Some(row_arr) = row.as_array() {
                            let row_data: Vec<f64> =
                                row_arr.iter().filter_map(|v| v.as_f64()).collect();
                            spl.extend(row_data);
                        }
                    }
                }

                if !freq.is_empty() && !angles.is_empty() && !spl.is_empty() {
                    let freq_count = freq.len();
                    let angle_count = angles.len();

                    // Verify data dimensions
                    let expected_size = freq_count * angle_count;
                    if spl.len() != expected_size {
                        log::debug!(
                            "Warning: SPL grid size {} doesn't match expected {} ({}×{})",
                            spl.len(),
                            expected_size,
                            angle_count,
                            freq_count
                        );
                    }

                    return Ok(ContourPlotData {
                        freq,
                        angles,
                        spl,
                        freq_count,
                        angle_count,
                    });
                }
            }
        }
    }

    Err("Failed to extract contour data from plot data".into())
}

// ==========================================================================
// Headphone API
// ==========================================================================

/// Return the cache directory for a headphone under `data_cached/headphones/org.spinorama/`
fn headphone_cache_dir(headphone: &str) -> std::path::PathBuf {
    let mut p = std::path::PathBuf::from("data_cached");
    p.push("headphones");
    p.push("org.spinorama");
    p.push(crate::read::directory::sanitize_dir_name(headphone));
    p
}

/// Fetch a headphone's frequency response from the spinorama API.
///
/// Uses `GET /v1/headphone/{name}/frequency_response`.
/// The API returns CSV data (4-column: L_freq, L_spl, R_freq, R_spl with header rows).
/// Caches the raw CSV and also writes a simplified 2-column CSV for the optimization pipeline.
pub async fn fetch_headphone_frequency_response(
    headphone: &str,
) -> Result<(String, crate::Curve), Box<dyn Error>> {
    let cache_dir = headphone_cache_dir(headphone);
    let raw_cache = cache_dir.join("frequency_response_raw.csv");
    let csv_path = cache_dir.join("measurement.csv");

    // Try local cache first
    let csv_text = if let Ok(content) = fs::read_to_string(&raw_cache).await {
        content
    } else {
        fetch_headphone_csv_from_api(headphone, &cache_dir, &raw_cache).await?
    };

    // Parse the CSV (handles 2-col and 4-col with multiple header rows)
    let curve = parse_headphone_csv(&csv_text)?;

    // Write simplified 2-column CSV for the optimization pipeline
    fs::create_dir_all(&cache_dir).await?;
    let mut out = String::from("frequency,spl\n");
    for (f, s) in curve.freq.iter().zip(curve.spl.iter()) {
        out.push_str(&format!("{},{}\n", f, s));
    }
    fs::write(&csv_path, &out).await?;

    Ok((csv_path.to_string_lossy().to_string(), curve))
}

async fn fetch_headphone_csv_from_api(
    headphone: &str,
    cache_dir: &std::path::Path,
    raw_cache: &std::path::Path,
) -> Result<String, Box<dyn Error>> {
    let encoded = urlencoding::encode(headphone);
    let url = format!(
        "https://api.spinorama.org/v1/headphone/{}/frequency_response",
        encoded
    );

    let response = reqwest::get(&url).await?;
    if !response.status().is_success() {
        return Err(format!("API request failed with status: {}", response.status()).into());
    }

    let body = response.text().await?;

    // Check for JSON error response (the API returns JSON errors even though
    // successful responses are CSV)
    if body.trim_start().starts_with('{') {
        if let Ok(json) = serde_json::from_str::<Value>(&body) {
            if let Some(error_msg) = json.get("error").and_then(|e| e.as_str()) {
                return Err(error_msg.to_string().into());
            }
        }
    }

    // Cache the raw CSV
    if let Err(e) = fs::create_dir_all(cache_dir).await {
        log::debug!("Failed to create headphone cache dir: {}", e);
    } else if let Err(e) = fs::write(raw_cache, &body).await {
        log::debug!("Failed to write headphone cache: {}", e);
    }

    Ok(body)
}

/// Parse headphone frequency response CSV from the spinorama API.
///
/// Format: 4-column CSV with multiple header rows:
/// ```text
/// "Frequency Response Reference 425 Hz @ 94 dBSPL",,,
/// "Left (94 dB)",,Right,
/// X,Y,X,Y
/// Hz,dBSPL,Hz,dBSPL
/// 19.23,104.00,19.23,102.33
/// ...
/// ```
/// Non-numeric rows are automatically skipped. L/R channels are averaged.
fn parse_headphone_csv(csv_text: &str) -> Result<crate::Curve, Box<dyn Error>> {
    let mut frequencies = Vec::new();
    let mut spl_values = Vec::new();

    for line in csv_text.lines() {
        let line = line.trim().trim_matches('"');
        if line.is_empty() {
            continue;
        }

        let parts: Vec<&str> = line.split(',').map(|s| s.trim().trim_matches('"')).collect();

        if parts.len() >= 4 {
            // 4-column: freq_L, spl_L, freq_R, spl_R → average L/R
            if let (Ok(freq_l), Ok(spl_l), Ok(_freq_r), Ok(spl_r)) = (
                parts[0].parse::<f64>(),
                parts[1].parse::<f64>(),
                parts[2].parse::<f64>(),
                parts[3].parse::<f64>(),
            ) {
                frequencies.push(freq_l);
                spl_values.push((spl_l + spl_r) / 2.0);
            }
        } else if parts.len() >= 2 {
            // 2-column: freq, spl
            if let (Ok(freq), Ok(spl)) = (parts[0].parse::<f64>(), parts[1].parse::<f64>()) {
                frequencies.push(freq);
                spl_values.push(spl);
            }
        }
    }

    if frequencies.is_empty() {
        return Err("No valid frequency response data found in CSV".into());
    }

    Ok(crate::Curve {
        freq: Array1::from(frequencies),
        spl: Array1::from(spl_values),
        phase: None,
    })
}