voirs-spatial 0.1.0-rc.1

3D spatial audio and HRTF processing for VoiRS
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
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
//! Real-time Adaptive Acoustic Environment System
//!
//! This module provides real-time adaptation of acoustic environment parameters
//! based on environmental sensors, user feedback, content analysis, and machine learning.

use crate::room::{Room, RoomAcoustics, RoomSimulator};
use crate::types::Position3D;
use crate::{Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};

/// Real-time adaptive acoustic environment system
pub struct AdaptiveAcousticEnvironment {
    /// Current room environment
    current_room: Room,
    /// Environment sensors
    sensors: EnvironmentSensors,
    /// Adaptation controller
    adaptation_controller: AdaptationController,
    /// Machine learning model for adaptation
    ml_adapter: Option<AcousticAdaptationModel>,
    /// Configuration
    config: AdaptiveAcousticsConfig,
    /// Performance metrics
    metrics: AdaptationMetrics,
    /// Adaptation history
    adaptation_history: VecDeque<AdaptationAction>,
}

/// Environment sensors for real-time data collection
#[derive(Debug, Clone)]
pub struct EnvironmentSensors {
    /// Temperature sensor data
    temperature_sensor: TemperatureSensor,
    /// Humidity sensor data  
    humidity_sensor: HumiditySensor,
    /// Ambient noise sensor
    noise_sensor: NoiseSensor,
    /// Occupancy detection
    occupancy_sensor: OccupancySensor,
    /// Material detection (via computer vision/ML)
    material_detector: MaterialDetector,
    /// Acoustic probe measurements
    acoustic_probe: AcousticProbe,
}

/// Adaptation controller for real-time parameter adjustment
pub struct AdaptationController {
    /// Current adaptation state
    adaptation_state: AdaptationState,
    /// Adaptation strategies
    strategies: HashMap<AdaptationTrigger, AdaptationStrategy>,
    /// Learning rate for adaptation
    learning_rate: f32,
    /// Adaptation thresholds
    thresholds: AdaptationThresholds,
    /// Recent adaptations
    recent_adaptations: VecDeque<AdaptationAction>,
}

/// Machine learning model for acoustic adaptation
pub struct AcousticAdaptationModel {
    /// Environment classifier
    environment_classifier: EnvironmentClassifier,
    /// Parameter predictor
    parameter_predictor: ParameterPredictor,
    /// User preference model
    user_preference_model: UserPreferenceModel,
    /// Training data cache
    training_data: VecDeque<AdaptationTrainingExample>,
}

/// Configuration for adaptive acoustics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptiveAcousticsConfig {
    /// Enable real-time adaptation
    pub enable_adaptation: bool,
    /// Adaptation update frequency (Hz)
    pub update_frequency: f32,
    /// Minimum change threshold for adaptation
    pub min_change_threshold: f32,
    /// Maximum adaptation rate (per second)
    pub max_adaptation_rate: f32,
    /// Sensor configuration
    pub sensor_config: SensorConfig,
    /// ML model configuration
    pub ml_config: Option<MLModelConfig>,
    /// Adaptation strategies
    pub adaptation_strategies: Vec<AdaptationStrategyConfig>,
    /// User preference learning
    pub enable_preference_learning: bool,
}

/// Temperature sensor for environmental monitoring
#[derive(Debug, Clone)]
pub struct TemperatureSensor {
    /// Current temperature (Celsius)
    current_temperature: f32,
    /// Temperature history
    temperature_history: VecDeque<SensorReading<f32>>,
    /// Calibration offset
    calibration_offset: f32,
    /// Sensor accuracy
    accuracy: f32,
}

/// Humidity sensor for environmental monitoring
#[derive(Debug, Clone)]
pub struct HumiditySensor {
    /// Current relative humidity (0.0-1.0)
    current_humidity: f32,
    /// Humidity history
    humidity_history: VecDeque<SensorReading<f32>>,
    /// Calibration parameters
    calibration_params: HumidityCalibration,
}

/// Ambient noise sensor for noise floor detection
#[derive(Debug, Clone)]
pub struct NoiseSensor {
    /// Current noise level (dB SPL)
    current_level: f32,
    /// Noise spectrum analysis
    spectrum: NoiseSpectrum,
    /// Noise type classification
    noise_type: NoiseType,
    /// Noise history
    noise_history: VecDeque<NoiseReading>,
}

/// Occupancy sensor for people detection
#[derive(Debug, Clone)]
pub struct OccupancySensor {
    /// Number of people detected
    occupant_count: usize,
    /// Occupant positions (if available)
    occupant_positions: Vec<Position3D>,
    /// Activity level detection
    activity_level: ActivityLevel,
    /// Detection confidence
    confidence: f32,
}

/// Material detector using computer vision/ML
#[derive(Debug, Clone)]
pub struct MaterialDetector {
    /// Detected surface materials
    detected_materials: HashMap<String, MaterialProperties>,
    /// Material detection confidence
    detection_confidence: HashMap<String, f32>,
    /// Last update timestamp
    last_update: Instant,
    /// Detection method
    detection_method: MaterialDetectionMethod,
}

/// Acoustic probe for direct acoustic measurements
#[derive(Debug, Clone)]
pub struct AcousticProbe {
    /// Impulse response measurements
    impulse_responses: HashMap<Position3D, Vec<f32>>,
    /// Reverberation time measurements (RT60)
    rt60_measurements: HashMap<String, f32>,
    /// Frequency response measurements
    frequency_responses: HashMap<Position3D, Vec<f32>>,
    /// Last probe time
    last_probe_time: Instant,
}

/// Generic sensor reading with timestamp
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SensorReading<T> {
    /// Sensor value
    pub value: T,
    /// Reading timestamp (seconds since epoch)
    pub timestamp: f64,
    /// Reading confidence/quality
    pub confidence: f32,
}

/// Current adaptation state
#[derive(Debug, Clone, Default)]
pub struct AdaptationState {
    /// Current environment classification
    pub environment_type: EnvironmentType,
    /// Active adaptations
    pub active_adaptations: HashMap<String, f32>,
    /// Adaptation confidence
    pub confidence: f32,
    /// Last adaptation time
    pub last_adaptation_time: Option<Instant>,
    /// Stability score (0.0 = unstable, 1.0 = stable)
    pub stability_score: f32,
}

/// Types of environmental triggers for adaptation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AdaptationTrigger {
    /// Temperature change
    TemperatureChange,
    /// Humidity change
    HumidityChange,
    /// Noise level change
    NoiseChange,
    /// Occupancy change
    OccupancyChange,
    /// Material change (furniture moved, etc.)
    MaterialChange,
    /// Content type change (music vs speech)
    ContentChange,
    /// User preference feedback
    UserFeedback,
    /// Time-based adaptation (day/night cycles)
    TimeAdaptation,
}

/// Adaptation strategies for different triggers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationStrategy {
    /// Strategy name
    pub name: String,
    /// Applicable triggers
    pub triggers: Vec<AdaptationTrigger>,
    /// Parameter adjustments
    pub parameter_adjustments: HashMap<String, ParameterAdjustment>,
    /// Adaptation speed (0.0 = instant, 1.0 = very slow)
    pub adaptation_speed: f32,
    /// Strategy priority
    pub priority: f32,
}

/// Types of acoustic environments
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum EnvironmentType {
    /// Living room
    #[default]
    LivingRoom,
    /// Bedroom
    Bedroom,
    /// Kitchen
    Kitchen,
    /// Office
    Office,
    /// Bathroom
    Bathroom,
    /// Outdoor
    Outdoor,
    /// Vehicle interior
    Vehicle,
    /// Concert hall
    ConcertHall,
    /// Small room
    SmallRoom,
    /// Large hall
    LargeHall,
    /// Unknown/unclassified
    Unknown,
}

/// Types of ambient noise
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NoiseType {
    /// White noise
    White,
    /// Pink noise
    Pink,
    /// Traffic noise
    Traffic,
    /// Air conditioning/HVAC
    HVAC,
    /// Human conversation
    Conversation,
    /// Music
    Music,
    /// Mechanical noise
    Mechanical,
    /// Natural sounds (wind, rain)
    Natural,
    /// Electronic interference
    Electronic,
    /// Mixed/complex noise
    Mixed,
}

/// Occupancy activity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ActivityLevel {
    /// No activity
    None,
    /// Low activity (sitting, reading)
    Low,
    /// Medium activity (talking, light movement)
    Medium,
    /// High activity (dancing, exercise)
    High,
    /// Very high activity (party, event)
    VeryHigh,
}

/// Material detection methods
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MaterialDetectionMethod {
    /// Computer vision analysis
    ComputerVision,
    /// Acoustic analysis (clap test, etc.)
    AcousticAnalysis,
    /// LIDAR/depth sensing
    LIDAR,
    /// User input/manual
    Manual,
    /// Machine learning classification
    MLClassification,
}

/// Material properties for acoustic simulation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterialProperties {
    /// Material name
    pub name: String,
    /// Absorption coefficients per frequency band
    pub absorption: HashMap<String, f32>,
    /// Scattering coefficients
    pub scattering: HashMap<String, f32>,
    /// Transmission coefficients
    pub transmission: HashMap<String, f32>,
    /// Material density
    pub density: f32,
    /// Surface roughness
    pub roughness: f32,
}

/// Parameter adjustment specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParameterAdjustment {
    /// Parameter name
    pub parameter: String,
    /// Adjustment type
    pub adjustment_type: AdjustmentType,
    /// Adjustment value
    pub value: f32,
    /// Adjustment curve (linear, exponential, etc.)
    pub curve: AdjustmentCurve,
    /// Minimum/maximum bounds
    pub bounds: (f32, f32),
}

/// Types of parameter adjustments
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AdjustmentType {
    /// Absolute value
    Absolute,
    /// Relative change (multiply)
    Relative,
    /// Additive change
    Additive,
    /// Exponential scaling
    Exponential,
}

/// Adjustment curve types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AdjustmentCurve {
    /// Linear adjustment
    Linear,
    /// Exponential curve
    Exponential,
    /// Logarithmic curve
    Logarithmic,
    /// S-curve (sigmoid)
    Sigmoid,
    /// Custom curve (defined by points)
    Custom,
}

/// Adaptation thresholds for triggering changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationThresholds {
    /// Temperature threshold (Celsius)
    pub temperature_threshold: f32,
    /// Humidity threshold (relative)
    pub humidity_threshold: f32,
    /// Noise level threshold (dB)
    pub noise_threshold: f32,
    /// Occupancy change threshold
    pub occupancy_threshold: usize,
    /// Material change threshold
    pub material_threshold: f32,
    /// Time threshold for stability
    pub stability_time_threshold: Duration,
}

/// Record of an adaptation event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationEvent {
    /// Event timestamp (seconds since epoch)
    pub timestamp: f64,
    /// Trigger that caused adaptation
    pub trigger: AdaptationTrigger,
    /// Environmental conditions at the time
    pub environment_snapshot: EnvironmentSnapshot,
    /// Parameters that were changed
    pub parameter_changes: HashMap<String, ParameterChange>,
    /// Adaptation success/failure
    pub result: AdaptationResult,
    /// User feedback (if any)
    pub user_feedback: Option<UserFeedback>,
}

/// Snapshot of environmental conditions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvironmentSnapshot {
    /// Temperature at the time
    pub temperature: f32,
    /// Humidity at the time
    pub humidity: f32,
    /// Noise level at the time
    pub noise_level: f32,
    /// Number of occupants
    pub occupant_count: usize,
    /// Detected materials
    pub materials: HashMap<String, f32>,
    /// Time of day
    pub time_of_day: f32,
}

/// Performance metrics for adaptation system
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AdaptationMetrics {
    /// Total adaptations performed
    pub total_adaptations: usize,
    /// Successful adaptations
    pub successful_adaptations: usize,
    /// Average adaptation time
    pub average_adaptation_time: Duration,
    /// User satisfaction score (0.0-1.0)
    pub user_satisfaction: f32,
    /// Environment classification accuracy
    pub classification_accuracy: f32,
    /// Parameter prediction accuracy
    pub prediction_accuracy: f32,
    /// Adaptation frequency (per hour)
    pub adaptation_frequency: f32,
}

/// Configuration for sensors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SensorConfig {
    /// Temperature sensor configuration
    pub temperature: TemperatureSensorConfig,
    /// Humidity sensor configuration
    pub humidity: HumiditySensorConfig,
    /// Noise sensor configuration
    pub noise: NoiseSensorConfig,
    /// Occupancy sensor configuration
    pub occupancy: OccupancySensorConfig,
    /// Material detector configuration
    pub material_detector: MaterialDetectorConfig,
    /// Acoustic probe configuration
    pub acoustic_probe: AcousticProbeConfig,
}

/// Individual sensor configurations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemperatureSensorConfig {
    /// Update frequency (Hz)
    pub update_frequency: f32,
    /// Calibration offset
    pub calibration_offset: f32,
    /// Sensor accuracy (±degrees)
    pub accuracy: f32,
}

/// Configuration for humidity sensors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HumiditySensorConfig {
    /// Update frequency (Hz)
    pub update_frequency: f32,
    /// Calibration parameters
    pub calibration: HumidityCalibration,
}

/// Configuration for noise sensors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NoiseSensorConfig {
    /// Update frequency (Hz)
    pub update_frequency: f32,
    /// Frequency analysis bands
    pub frequency_bands: Vec<(f32, f32)>,
    /// Noise classification enabled
    pub enable_classification: bool,
}

/// Configuration for occupancy sensors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OccupancySensorConfig {
    /// Detection method
    pub detection_method: OccupancyDetectionMethod,
    /// Update frequency (Hz)
    pub update_frequency: f32,
    /// Position tracking enabled
    pub enable_position_tracking: bool,
}

/// Configuration for material detection sensors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterialDetectorConfig {
    /// Detection method
    pub detection_method: MaterialDetectionMethod,
    /// Update frequency (Hz)
    pub update_frequency: f32,
    /// Confidence threshold
    pub confidence_threshold: f32,
}

/// Configuration for acoustic probes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AcousticProbeConfig {
    /// Probe frequency (Hz)
    pub probe_frequency: f32,
    /// Probe signal type
    pub probe_signal: ProbeSignalType,
    /// Analysis window size
    pub analysis_window: Duration,
}

/// Types of probe signals for acoustic analysis
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProbeSignalType {
    /// Sine sweep
    SineSweep,
    /// White noise burst
    WhiteNoise,
    /// Pink noise burst
    PinkNoise,
    /// Maximum length sequence
    MLS,
    /// Time-stretched pulse
    TSP,
}

/// Occupancy detection methods
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OccupancyDetectionMethod {
    /// Computer vision (camera)
    ComputerVision,
    /// Infrared sensors
    Infrared,
    /// Ultrasonic sensors
    Ultrasonic,
    /// WiFi presence detection
    WiFi,
    /// Bluetooth beacons
    Bluetooth,
    /// Audio analysis (voice activity)
    AudioAnalysis,
}

/// Humidity calibration parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HumidityCalibration {
    /// Linear calibration coefficient
    pub linear_coeff: f32,
    /// Offset calibration
    pub offset: f32,
    /// Temperature compensation
    pub temp_compensation: f32,
}

/// Noise spectrum analysis data
#[derive(Debug, Clone)]
pub struct NoiseSpectrum {
    /// Frequency bands (Hz)
    pub frequency_bands: Vec<f32>,
    /// Power levels per band (dB)
    pub power_levels: Vec<f32>,
    /// Spectral centroid
    pub centroid: f32,
    /// Spectral rolloff
    pub rolloff: f32,
}

/// Detailed noise reading with spectrum
#[derive(Debug, Clone)]
pub struct NoiseReading {
    /// Overall level (dB SPL)
    pub level: f32,
    /// Noise spectrum
    pub spectrum: NoiseSpectrum,
    /// Noise type classification
    pub noise_type: NoiseType,
    /// Reading timestamp (seconds since epoch)
    pub timestamp: f64,
    /// Classification confidence
    pub confidence: f32,
}

/// Adaptation action record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationAction {
    /// Action timestamp (seconds since epoch)
    pub timestamp: f64,
    /// Parameter that was changed
    pub parameter: String,
    /// Old value
    pub old_value: f32,
    /// New value
    pub new_value: f32,
    /// Action reason/trigger
    pub trigger: AdaptationTrigger,
}

/// Parameter change record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParameterChange {
    /// Parameter name
    pub parameter: String,
    /// Change amount
    pub change: f32,
    /// Change type
    pub change_type: AdjustmentType,
    /// Success of the change
    pub success: bool,
}

/// Result of an adaptation attempt
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AdaptationResult {
    /// Adaptation successful
    Success,
    /// Adaptation failed
    Failed,
    /// Adaptation partially successful
    Partial,
    /// Adaptation cancelled by user
    Cancelled,
    /// Adaptation skipped (no change needed)
    Skipped,
}

/// User feedback on adaptations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserFeedback {
    /// Feedback rating (0.0-1.0)
    pub rating: f32,
    /// Textual feedback
    pub comment: Option<String>,
    /// Specific parameter preferences
    pub parameter_preferences: HashMap<String, f32>,
    /// Feedback timestamp (seconds since epoch)
    pub timestamp: f64,
}

/// Training example for ML adaptation model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationTrainingExample {
    /// Input environment features
    pub environment_features: Vec<f32>,
    /// Target parameter values
    pub target_parameters: HashMap<String, f32>,
    /// User satisfaction score
    pub satisfaction_score: f32,
    /// Context information
    pub context: EnvironmentSnapshot,
}

/// ML model configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MLModelConfig {
    /// Enable environment classification
    pub enable_classification: bool,
    /// Enable parameter prediction
    pub enable_prediction: bool,
    /// Enable user preference learning
    pub enable_preference_learning: bool,
    /// Model update frequency
    pub update_frequency: Duration,
    /// Training batch size
    pub batch_size: usize,
    /// Learning rate
    pub learning_rate: f64,
}

/// Strategy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationStrategyConfig {
    /// Strategy name
    pub name: String,
    /// Enabled triggers
    pub triggers: Vec<AdaptationTrigger>,
    /// Parameter mappings
    pub parameters: HashMap<String, ParameterAdjustment>,
    /// Strategy weight/priority
    pub weight: f32,
}

// Placeholder implementations for ML components
/// Machine learning environment classifier
#[derive(Debug, Clone)]
pub struct EnvironmentClassifier {
    /// Model weights (placeholder)
    weights: Vec<f32>,
}

/// Machine learning parameter predictor
#[derive(Debug, Clone)]
pub struct ParameterPredictor {
    /// Model weights (placeholder)
    weights: Vec<f32>,
}

/// Model for user preferences in adaptive acoustics
#[derive(Debug, Clone)]
pub struct UserPreferenceModel {
    /// User preference weights
    preferences: HashMap<String, f32>,
}

impl Default for AdaptiveAcousticsConfig {
    fn default() -> Self {
        Self {
            enable_adaptation: true,
            update_frequency: 1.0, // 1 Hz
            min_change_threshold: 0.05,
            max_adaptation_rate: 10.0, // per second
            sensor_config: SensorConfig::default(),
            ml_config: None,
            adaptation_strategies: vec![
                AdaptationStrategyConfig {
                    name: "Temperature Compensation".to_string(),
                    triggers: vec![AdaptationTrigger::TemperatureChange],
                    parameters: HashMap::new(),
                    weight: 1.0,
                },
                AdaptationStrategyConfig {
                    name: "Occupancy Adjustment".to_string(),
                    triggers: vec![AdaptationTrigger::OccupancyChange],
                    parameters: HashMap::new(),
                    weight: 1.0,
                },
            ],
            enable_preference_learning: true,
        }
    }
}

impl Default for SensorConfig {
    fn default() -> Self {
        Self {
            temperature: TemperatureSensorConfig {
                update_frequency: 0.1, // 0.1 Hz (every 10 seconds)
                calibration_offset: 0.0,
                accuracy: 0.5, // ±0.5°C
            },
            humidity: HumiditySensorConfig {
                update_frequency: 0.1,
                calibration: HumidityCalibration {
                    linear_coeff: 1.0,
                    offset: 0.0,
                    temp_compensation: 0.01,
                },
            },
            noise: NoiseSensorConfig {
                update_frequency: 10.0, // 10 Hz
                frequency_bands: vec![
                    (20.0, 200.0),     // Low
                    (200.0, 2000.0),   // Mid
                    (2000.0, 20000.0), // High
                ],
                enable_classification: true,
            },
            occupancy: OccupancySensorConfig {
                detection_method: OccupancyDetectionMethod::AudioAnalysis,
                update_frequency: 1.0, // 1 Hz
                enable_position_tracking: false,
            },
            material_detector: MaterialDetectorConfig {
                detection_method: MaterialDetectionMethod::AcousticAnalysis,
                update_frequency: 0.01, // Every 100 seconds
                confidence_threshold: 0.7,
            },
            acoustic_probe: AcousticProbeConfig {
                probe_frequency: 0.01, // Every 100 seconds
                probe_signal: ProbeSignalType::SineSweep,
                analysis_window: Duration::from_secs(5),
            },
        }
    }
}

impl Default for AdaptationThresholds {
    fn default() -> Self {
        Self {
            temperature_threshold: 2.0, // 2°C
            humidity_threshold: 0.1,    // 10%
            noise_threshold: 5.0,       // 5 dB
            occupancy_threshold: 1,     // 1 person
            material_threshold: 0.2,    // 20% change
            stability_time_threshold: Duration::from_secs(30),
        }
    }
}

impl AdaptiveAcousticEnvironment {
    /// Get current timestamp as seconds since epoch
    fn get_current_timestamp() -> f64 {
        use std::time::{SystemTime, UNIX_EPOCH};
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("SystemTime should be after UNIX_EPOCH")
            .as_secs_f64()
    }

    /// Create new adaptive acoustic environment
    pub fn new(initial_room: Room, config: AdaptiveAcousticsConfig) -> Result<Self> {
        let sensors = EnvironmentSensors::new(&config.sensor_config)?;
        let adaptation_controller = AdaptationController::new(&config)?;
        let ml_adapter = if config.ml_config.is_some() {
            Some(AcousticAdaptationModel::new()?)
        } else {
            None
        };

        Ok(Self {
            current_room: initial_room,
            sensors,
            adaptation_controller,
            ml_adapter,
            config,
            metrics: AdaptationMetrics::default(),
            adaptation_history: VecDeque::new(),
        })
    }

    /// Update environment with sensor data and perform adaptations
    pub fn update(&mut self) -> Result<Vec<AdaptationAction>> {
        if !self.config.enable_adaptation {
            return Ok(Vec::new());
        }

        // Update sensors
        self.sensors.update()?;

        // Detect environmental changes
        let triggers = self.detect_environmental_changes()?;

        // Perform adaptations based on triggers
        let mut actions = Vec::new();
        for trigger in triggers {
            if let Some(new_actions) = self.perform_adaptation(trigger)? {
                actions.extend(new_actions);
            }
        }

        // Update metrics
        self.update_metrics(&actions);

        // Record adaptations in history
        for action in &actions {
            self.adaptation_history.push_back(action.clone());
        }

        // Limit history size
        if self.adaptation_history.len() > 1000 {
            self.adaptation_history.pop_front();
        }

        Ok(actions)
    }

    /// Manually trigger adaptation for specific parameter
    pub fn manual_adaptation(&mut self, parameter: &str, value: f32) -> Result<AdaptationAction> {
        let old_value = self.get_parameter_value(parameter)?;
        self.set_parameter_value(parameter, value)?;

        let action = AdaptationAction {
            timestamp: Self::get_current_timestamp(),
            parameter: parameter.to_string(),
            old_value,
            new_value: value,
            trigger: AdaptationTrigger::UserFeedback,
        };

        self.adaptation_history.push_back(action.clone());
        self.metrics.total_adaptations += 1;

        Ok(action)
    }

    /// Provide user feedback on current acoustic settings
    pub fn provide_user_feedback(&mut self, feedback: UserFeedback) -> Result<()> {
        // Update user satisfaction metrics
        let total_feedback = self.metrics.total_adaptations as f32;
        self.metrics.user_satisfaction = (self.metrics.user_satisfaction * (total_feedback - 1.0)
            + feedback.rating)
            / total_feedback;

        // Learn from user preferences
        if self.config.enable_preference_learning {
            let snapshot = self.get_current_environment_snapshot();
            if let Some(ref mut ml_adapter) = self.ml_adapter {
                ml_adapter.learn_from_feedback(&feedback, &snapshot)?;
            }
        }

        // Trigger adaptation based on preferences
        for (param, preferred_value) in &feedback.parameter_preferences {
            let current_value = self.get_parameter_value(param)?;
            if (current_value - preferred_value).abs() > self.config.min_change_threshold {
                self.manual_adaptation(param, *preferred_value)?;
            }
        }

        Ok(())
    }

    /// Get current adaptation metrics
    pub fn metrics(&self) -> &AdaptationMetrics {
        &self.metrics
    }

    /// Get adaptation history
    pub fn adaptation_history(&self) -> &VecDeque<AdaptationAction> {
        &self.adaptation_history
    }

    /// Get current environment snapshot
    pub fn get_current_environment_snapshot(&self) -> EnvironmentSnapshot {
        EnvironmentSnapshot {
            temperature: self.sensors.temperature_sensor.current_temperature,
            humidity: self.sensors.humidity_sensor.current_humidity,
            noise_level: self.sensors.noise_sensor.current_level,
            occupant_count: self.sensors.occupancy_sensor.occupant_count,
            materials: self
                .sensors
                .material_detector
                .detected_materials
                .iter()
                .map(|(name, props)| (name.clone(), props.density))
                .collect(),
            time_of_day: {
                use std::time::SystemTime;
                let now = SystemTime::now()
                    .duration_since(SystemTime::UNIX_EPOCH)
                    .expect("SystemTime should be after UNIX_EPOCH")
                    .as_secs()
                    % 86400; // Seconds since midnight
                now as f32 / 86400.0 // Normalized to 0.0-1.0
            },
        }
    }

    // Private helper methods

    fn detect_environmental_changes(&self) -> Result<Vec<AdaptationTrigger>> {
        let mut triggers = Vec::new();
        let thresholds = &self.adaptation_controller.thresholds;

        // Temperature change detection
        if let Some(recent_temp) = self.sensors.temperature_sensor.temperature_history.back() {
            let temp_change =
                (recent_temp.value - self.sensors.temperature_sensor.current_temperature).abs();
            if temp_change > thresholds.temperature_threshold {
                triggers.push(AdaptationTrigger::TemperatureChange);
            }
        }

        // Humidity change detection
        if let Some(recent_humidity) = self.sensors.humidity_sensor.humidity_history.back() {
            let humidity_change =
                (recent_humidity.value - self.sensors.humidity_sensor.current_humidity).abs();
            if humidity_change > thresholds.humidity_threshold {
                triggers.push(AdaptationTrigger::HumidityChange);
            }
        }

        // Noise level change detection
        if let Some(recent_noise) = self.sensors.noise_sensor.noise_history.back() {
            let noise_change = (recent_noise.level - self.sensors.noise_sensor.current_level).abs();
            if noise_change > thresholds.noise_threshold {
                triggers.push(AdaptationTrigger::NoiseChange);
            }
        }

        // Add other trigger detections as needed...

        Ok(triggers)
    }

    fn perform_adaptation(
        &mut self,
        trigger: AdaptationTrigger,
    ) -> Result<Option<Vec<AdaptationAction>>> {
        if let Some(strategy) = self.adaptation_controller.strategies.get(&trigger).cloned() {
            let mut actions = Vec::new();

            for (param_name, adjustment) in &strategy.parameter_adjustments {
                let current_value = self.get_parameter_value(param_name)?;
                let new_value = self.apply_parameter_adjustment(current_value, adjustment)?;

                // Check bounds
                let bounded_value = new_value.clamp(adjustment.bounds.0, adjustment.bounds.1);

                if (bounded_value - current_value).abs() > self.config.min_change_threshold {
                    self.set_parameter_value(param_name, bounded_value)?;

                    actions.push(AdaptationAction {
                        timestamp: Self::get_current_timestamp(),
                        parameter: param_name.clone(),
                        old_value: current_value,
                        new_value: bounded_value,
                        trigger,
                    });
                }
            }

            Ok(Some(actions))
        } else {
            Ok(None)
        }
    }

    fn apply_parameter_adjustment(
        &self,
        current_value: f32,
        adjustment: &ParameterAdjustment,
    ) -> Result<f32> {
        let adjusted = match adjustment.adjustment_type {
            AdjustmentType::Absolute => adjustment.value,
            AdjustmentType::Relative => current_value * adjustment.value,
            AdjustmentType::Additive => current_value + adjustment.value,
            AdjustmentType::Exponential => current_value * adjustment.value.exp(),
        };

        // Apply adjustment curve
        let final_value = match adjustment.curve {
            AdjustmentCurve::Linear => adjusted,
            AdjustmentCurve::Exponential => adjusted.exp(),
            AdjustmentCurve::Logarithmic => adjusted.ln(),
            AdjustmentCurve::Sigmoid => 1.0 / (1.0 + (-adjusted).exp()),
            AdjustmentCurve::Custom => adjusted, // Would implement custom curve logic
        };

        Ok(final_value)
    }

    fn get_parameter_value(&self, parameter: &str) -> Result<f32> {
        // This would access the actual room parameters
        // For now, return placeholder values
        match parameter {
            "reverb_time" => Ok(1.5),              // RT60 in seconds
            "early_reflection_level" => Ok(-12.0), // dB
            "diffusion" => Ok(0.7),
            "absorption" => Ok(0.3),
            _ => Err(Error::processing(&format!(
                "Unknown parameter: {parameter}"
            ))),
        }
    }

    fn set_parameter_value(&mut self, parameter: &str, value: f32) -> Result<()> {
        // This would actually update the room parameters
        // For now, just validate the parameter name
        match parameter {
            "reverb_time" | "early_reflection_level" | "diffusion" | "absorption" => {
                // Parameter update would happen here
                Ok(())
            }
            _ => Err(Error::processing(&format!(
                "Cannot set unknown parameter: {parameter}"
            ))),
        }
    }

    fn update_metrics(&mut self, actions: &[AdaptationAction]) {
        self.metrics.total_adaptations += actions.len();
        // Update other metrics based on actions...
    }
}

// Implement placeholder methods for components
impl EnvironmentSensors {
    fn new(_config: &SensorConfig) -> Result<Self> {
        Ok(Self {
            temperature_sensor: TemperatureSensor::new(),
            humidity_sensor: HumiditySensor::new(),
            noise_sensor: NoiseSensor::new(),
            occupancy_sensor: OccupancySensor::new(),
            material_detector: MaterialDetector::new(),
            acoustic_probe: AcousticProbe::new(),
        })
    }

    fn update(&mut self) -> Result<()> {
        // Update all sensors
        self.temperature_sensor.update()?;
        self.humidity_sensor.update()?;
        self.noise_sensor.update()?;
        self.occupancy_sensor.update()?;
        self.material_detector.update()?;
        self.acoustic_probe.update()?;
        Ok(())
    }
}

impl AdaptationController {
    fn new(config: &AdaptiveAcousticsConfig) -> Result<Self> {
        let mut strategies = HashMap::new();

        // Create strategies from config
        for strategy_config in &config.adaptation_strategies {
            let strategy = AdaptationStrategy {
                name: strategy_config.name.clone(),
                triggers: strategy_config.triggers.clone(),
                parameter_adjustments: strategy_config.parameters.clone(),
                adaptation_speed: 0.5,
                priority: strategy_config.weight,
            };

            for trigger in &strategy.triggers {
                strategies.insert(*trigger, strategy.clone());
            }
        }

        Ok(Self {
            adaptation_state: AdaptationState::default(),
            strategies,
            learning_rate: 0.01,
            thresholds: AdaptationThresholds::default(),
            recent_adaptations: VecDeque::new(),
        })
    }
}

impl AcousticAdaptationModel {
    fn new() -> Result<Self> {
        Ok(Self {
            environment_classifier: EnvironmentClassifier {
                weights: vec![0.0; 10],
            },
            parameter_predictor: ParameterPredictor {
                weights: vec![0.0; 20],
            },
            user_preference_model: UserPreferenceModel {
                preferences: HashMap::new(),
            },
            training_data: VecDeque::new(),
        })
    }

    fn learn_from_feedback(
        &mut self,
        _feedback: &UserFeedback,
        _snapshot: &EnvironmentSnapshot,
    ) -> Result<()> {
        // Placeholder for ML learning
        Ok(())
    }
}

// Implement sensor classes with placeholder methods
impl TemperatureSensor {
    fn new() -> Self {
        Self {
            current_temperature: 22.0, // Room temperature
            temperature_history: VecDeque::new(),
            calibration_offset: 0.0,
            accuracy: 0.5,
        }
    }

    fn update(&mut self) -> Result<()> {
        // Simulate temperature reading
        let reading = SensorReading {
            value: self.current_temperature + fastrand::f32() * 0.1 - 0.05, // Small random variation
            timestamp: AdaptiveAcousticEnvironment::get_current_timestamp(),
            confidence: 0.95,
        };

        self.temperature_history.push_back(reading);
        if self.temperature_history.len() > 100 {
            self.temperature_history.pop_front();
        }

        Ok(())
    }
}

impl HumiditySensor {
    fn new() -> Self {
        Self {
            current_humidity: 0.45, // 45% RH
            humidity_history: VecDeque::new(),
            calibration_params: HumidityCalibration {
                linear_coeff: 1.0,
                offset: 0.0,
                temp_compensation: 0.01,
            },
        }
    }

    fn update(&mut self) -> Result<()> {
        let reading = SensorReading {
            value: self.current_humidity + fastrand::f32() * 0.02 - 0.01,
            timestamp: AdaptiveAcousticEnvironment::get_current_timestamp(),
            confidence: 0.9,
        };

        self.humidity_history.push_back(reading);
        if self.humidity_history.len() > 100 {
            self.humidity_history.pop_front();
        }

        Ok(())
    }
}

impl NoiseSensor {
    fn new() -> Self {
        Self {
            current_level: 35.0, // 35 dB SPL
            spectrum: NoiseSpectrum {
                frequency_bands: vec![125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0],
                power_levels: vec![30.0, 32.0, 34.0, 35.0, 33.0, 31.0, 29.0],
                centroid: 800.0,
                rolloff: 2000.0,
            },
            noise_type: NoiseType::HVAC,
            noise_history: VecDeque::new(),
        }
    }

    fn update(&mut self) -> Result<()> {
        let reading = NoiseReading {
            level: self.current_level + fastrand::f32() * 2.0 - 1.0,
            spectrum: self.spectrum.clone(),
            noise_type: self.noise_type,
            timestamp: AdaptiveAcousticEnvironment::get_current_timestamp(),
            confidence: 0.8,
        };

        self.noise_history.push_back(reading);
        if self.noise_history.len() > 1000 {
            self.noise_history.pop_front();
        }

        Ok(())
    }
}

impl OccupancySensor {
    fn new() -> Self {
        Self {
            occupant_count: 1,
            occupant_positions: vec![Position3D::new(0.0, 0.0, 1.7)], // Standing height
            activity_level: ActivityLevel::Low,
            confidence: 0.85,
        }
    }

    fn update(&mut self) -> Result<()> {
        // Simulate occupancy detection
        Ok(())
    }
}

impl MaterialDetector {
    fn new() -> Self {
        let mut detected_materials = HashMap::new();
        detected_materials.insert(
            "carpet".to_string(),
            MaterialProperties {
                name: "Carpet".to_string(),
                absorption: [
                    ("125Hz", 0.02),
                    ("250Hz", 0.04),
                    ("500Hz", 0.08),
                    ("1000Hz", 0.12),
                    ("2000Hz", 0.22),
                    ("4000Hz", 0.35),
                ]
                .iter()
                .map(|(k, v)| (k.to_string(), *v))
                .collect(),
                scattering: HashMap::new(),
                transmission: HashMap::new(),
                density: 400.0,
                roughness: 0.3,
            },
        );

        Self {
            detected_materials,
            detection_confidence: HashMap::new(),
            last_update: Instant::now(),
            detection_method: MaterialDetectionMethod::AcousticAnalysis,
        }
    }

    fn update(&mut self) -> Result<()> {
        self.last_update = Instant::now();
        Ok(())
    }
}

impl AcousticProbe {
    fn new() -> Self {
        Self {
            impulse_responses: HashMap::new(),
            rt60_measurements: [
                ("125Hz", 1.2),
                ("250Hz", 1.1),
                ("500Hz", 0.9),
                ("1000Hz", 0.8),
                ("2000Hz", 0.7),
                ("4000Hz", 0.6),
            ]
            .iter()
            .map(|(k, v)| (k.to_string(), *v))
            .collect(),
            frequency_responses: HashMap::new(),
            last_probe_time: Instant::now(),
        }
    }

    fn update(&mut self) -> Result<()> {
        // Perform acoustic measurements periodically
        if self.last_probe_time.elapsed() > Duration::from_secs(60) {
            self.last_probe_time = Instant::now();
            // Trigger acoustic measurement
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::room::Room;

    #[test]
    fn test_adaptive_environment_creation() {
        let room = Room::new(
            "test_room".to_string(),
            (5.0, 4.0, 3.0),
            1.2,
            Position3D::new(0.0, 0.0, 0.0),
        )
        .unwrap();
        let config = AdaptiveAcousticsConfig::default();
        let environment = AdaptiveAcousticEnvironment::new(room, config);
        assert!(environment.is_ok());
    }

    #[test]
    fn test_sensor_updates() {
        let mut temp_sensor = TemperatureSensor::new();
        assert!(temp_sensor.update().is_ok());
        assert_eq!(temp_sensor.temperature_history.len(), 1);
    }

    #[test]
    fn test_adaptation_triggers() {
        let trigger = AdaptationTrigger::TemperatureChange;
        assert_eq!(trigger, AdaptationTrigger::TemperatureChange);
    }

    #[test]
    fn test_parameter_adjustment() {
        let adjustment = ParameterAdjustment {
            parameter: "reverb_time".to_string(),
            adjustment_type: AdjustmentType::Relative,
            value: 1.2,
            curve: AdjustmentCurve::Linear,
            bounds: (0.1, 3.0),
        };

        assert_eq!(adjustment.parameter, "reverb_time");
        assert_eq!(adjustment.value, 1.2);
    }

    #[test]
    fn test_environment_snapshot() {
        let snapshot = EnvironmentSnapshot {
            temperature: 22.5,
            humidity: 0.45,
            noise_level: 35.0,
            occupant_count: 2,
            materials: HashMap::new(),
            time_of_day: 0.5,
        };

        assert_eq!(snapshot.temperature, 22.5);
        assert_eq!(snapshot.occupant_count, 2);
    }

    #[test]
    fn test_user_feedback() {
        let mut preferences = HashMap::new();
        preferences.insert("reverb_time".to_string(), 1.2);

        let feedback = UserFeedback {
            rating: 0.8,
            comment: Some("Sounds good".to_string()),
            parameter_preferences: preferences,
            timestamp: AdaptiveAcousticEnvironment::get_current_timestamp(),
        };

        assert_eq!(feedback.rating, 0.8);
        assert!(feedback.comment.is_some());
    }

    #[test]
    fn test_noise_spectrum() {
        let spectrum = NoiseSpectrum {
            frequency_bands: vec![125.0, 250.0, 500.0],
            power_levels: vec![30.0, 32.0, 28.0],
            centroid: 400.0,
            rolloff: 800.0,
        };

        assert_eq!(spectrum.frequency_bands.len(), 3);
        assert_eq!(spectrum.power_levels.len(), 3);
    }

    #[test]
    fn test_adaptation_metrics() {
        let mut metrics = AdaptationMetrics::default();
        metrics.total_adaptations = 50;
        metrics.successful_adaptations = 45;

        let success_rate = metrics.successful_adaptations as f32 / metrics.total_adaptations as f32;
        assert_eq!(success_rate, 0.9);
    }
}