voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework 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
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
//! Elderly and Pathological Speech Assessment
//!
//! This module provides specialized evaluation metrics designed for elderly speakers
//! and pathological speech conditions. It includes age-related voice change analysis,
//! communication disorder assessment, and assistive technology evaluation.

use std::collections::HashMap;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use crate::traits::*;
use crate::{AudioBuffer, EvaluationError, LanguageCode};

/// Age categories for elderly speech assessment
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ElderlyAgeGroup {
    /// Young elderly (65-74 years)
    YoungElderly,
    /// Old elderly (75-84 years)
    OldElderly,
    /// Oldest elderly (85+ years)
    OldestElderly,
}

impl ElderlyAgeGroup {
    /// Get typical age range for the group
    pub fn age_range(&self) -> (u8, u8) {
        match self {
            ElderlyAgeGroup::YoungElderly => (65, 74),
            ElderlyAgeGroup::OldElderly => (75, 84),
            ElderlyAgeGroup::OldestElderly => (85, 120),
        }
    }

    /// Get expected speech changes for age group
    pub fn expected_changes(&self) -> AgeRelatedChanges {
        match self {
            ElderlyAgeGroup::YoungElderly => AgeRelatedChanges {
                fundamental_frequency_change: 0.1,
                voice_tremor_likelihood: 0.2,
                articulation_precision_decline: 0.1,
                speaking_rate_change: 0.05,
                volume_control_issues: 0.1,
                breath_support_decline: 0.15,
                cognitive_load_sensitivity: 0.1,
            },
            ElderlyAgeGroup::OldElderly => AgeRelatedChanges {
                fundamental_frequency_change: 0.2,
                voice_tremor_likelihood: 0.4,
                articulation_precision_decline: 0.2,
                speaking_rate_change: 0.15,
                volume_control_issues: 0.2,
                breath_support_decline: 0.3,
                cognitive_load_sensitivity: 0.25,
            },
            ElderlyAgeGroup::OldestElderly => AgeRelatedChanges {
                fundamental_frequency_change: 0.3,
                voice_tremor_likelihood: 0.6,
                articulation_precision_decline: 0.3,
                speaking_rate_change: 0.25,
                volume_control_issues: 0.35,
                breath_support_decline: 0.45,
                cognitive_load_sensitivity: 0.4,
            },
        }
    }
}

/// Age-related speech changes
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AgeRelatedChanges {
    /// Fundamental frequency change factor (0.0-1.0)
    pub fundamental_frequency_change: f32,
    /// Likelihood of voice tremor (0.0-1.0)
    pub voice_tremor_likelihood: f32,
    /// Articulation precision decline (0.0-1.0)
    pub articulation_precision_decline: f32,
    /// Speaking rate change factor (0.0-1.0)
    pub speaking_rate_change: f32,
    /// Volume control issues (0.0-1.0)
    pub volume_control_issues: f32,
    /// Breath support decline (0.0-1.0)
    pub breath_support_decline: f32,
    /// Cognitive load sensitivity (0.0-1.0)
    pub cognitive_load_sensitivity: f32,
}

/// Types of pathological speech conditions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PathologicalCondition {
    /// Parkinson's disease speech characteristics
    Parkinsons,
    /// Stroke-related speech impairments
    Stroke,
    /// Alzheimer's and dementia-related changes
    Dementia,
    /// Vocal cord paralysis or weakness
    VocalCordParalysis,
    /// Laryngeal pathologies
    LaryngealPathology,
    /// Respiratory conditions affecting speech
    RespiratoryConditions,
    /// Hearing loss affecting speech production
    HearingLoss,
    /// General age-related voice changes
    Presbyphonia,
    /// Motor speech disorders
    MotorSpeechDisorder,
    /// Cognitive-communication disorders
    CognitiveCommunicationDisorder,
}

impl PathologicalCondition {
    /// Get characteristic features of the condition
    pub fn characteristic_features(&self) -> PathologicalFeatures {
        match self {
            PathologicalCondition::Parkinsons => PathologicalFeatures {
                reduced_loudness: 0.8,
                monotone_speech: 0.7,
                rapid_speech_rate: 0.6,
                voice_tremor: 0.5,
                articulation_imprecision: 0.6,
                reduced_stress: 0.7,
                breathy_voice: 0.4,
                communication_impact: 0.6,
            },
            PathologicalCondition::Stroke => PathologicalFeatures {
                reduced_loudness: 0.5,
                monotone_speech: 0.6,
                rapid_speech_rate: 0.3,
                voice_tremor: 0.2,
                articulation_imprecision: 0.8,
                reduced_stress: 0.6,
                breathy_voice: 0.3,
                communication_impact: 0.7,
            },
            PathologicalCondition::Dementia => PathologicalFeatures {
                reduced_loudness: 0.4,
                monotone_speech: 0.5,
                rapid_speech_rate: 0.2,
                voice_tremor: 0.3,
                articulation_imprecision: 0.5,
                reduced_stress: 0.6,
                breathy_voice: 0.3,
                communication_impact: 0.8,
            },
            PathologicalCondition::VocalCordParalysis => PathologicalFeatures {
                reduced_loudness: 0.9,
                monotone_speech: 0.6,
                rapid_speech_rate: 0.1,
                voice_tremor: 0.1,
                articulation_imprecision: 0.3,
                reduced_stress: 0.5,
                breathy_voice: 0.9,
                communication_impact: 0.7,
            },
            PathologicalCondition::LaryngealPathology => PathologicalFeatures {
                reduced_loudness: 0.7,
                monotone_speech: 0.5,
                rapid_speech_rate: 0.2,
                voice_tremor: 0.4,
                articulation_imprecision: 0.3,
                reduced_stress: 0.4,
                breathy_voice: 0.8,
                communication_impact: 0.6,
            },
            PathologicalCondition::RespiratoryConditions => PathologicalFeatures {
                reduced_loudness: 0.6,
                monotone_speech: 0.4,
                rapid_speech_rate: 0.1,
                voice_tremor: 0.2,
                articulation_imprecision: 0.4,
                reduced_stress: 0.5,
                breathy_voice: 0.6,
                communication_impact: 0.5,
            },
            PathologicalCondition::HearingLoss => PathologicalFeatures {
                reduced_loudness: 0.3,
                monotone_speech: 0.6,
                rapid_speech_rate: 0.2,
                voice_tremor: 0.1,
                articulation_imprecision: 0.5,
                reduced_stress: 0.5,
                breathy_voice: 0.2,
                communication_impact: 0.6,
            },
            PathologicalCondition::Presbyphonia => PathologicalFeatures {
                reduced_loudness: 0.5,
                monotone_speech: 0.4,
                rapid_speech_rate: 0.2,
                voice_tremor: 0.6,
                articulation_imprecision: 0.3,
                reduced_stress: 0.4,
                breathy_voice: 0.7,
                communication_impact: 0.4,
            },
            PathologicalCondition::MotorSpeechDisorder => PathologicalFeatures {
                reduced_loudness: 0.6,
                monotone_speech: 0.7,
                rapid_speech_rate: 0.4,
                voice_tremor: 0.3,
                articulation_imprecision: 0.8,
                reduced_stress: 0.7,
                breathy_voice: 0.4,
                communication_impact: 0.8,
            },
            PathologicalCondition::CognitiveCommunicationDisorder => PathologicalFeatures {
                reduced_loudness: 0.3,
                monotone_speech: 0.5,
                rapid_speech_rate: 0.3,
                voice_tremor: 0.2,
                articulation_imprecision: 0.4,
                reduced_stress: 0.6,
                breathy_voice: 0.3,
                communication_impact: 0.9,
            },
        }
    }
}

/// Pathological speech features
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PathologicalFeatures {
    /// Reduced loudness severity (0.0-1.0)
    pub reduced_loudness: f32,
    /// Monotone speech severity (0.0-1.0)
    pub monotone_speech: f32,
    /// Rapid speech rate issues (0.0-1.0)
    pub rapid_speech_rate: f32,
    /// Voice tremor presence (0.0-1.0)
    pub voice_tremor: f32,
    /// Articulation imprecision (0.0-1.0)
    pub articulation_imprecision: f32,
    /// Reduced stress patterns (0.0-1.0)
    pub reduced_stress: f32,
    /// Breathy voice quality (0.0-1.0)
    pub breathy_voice: f32,
    /// Overall communication impact (0.0-1.0)
    pub communication_impact: f32,
}

/// Severity levels for speech impairments
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SeverityLevel {
    /// Normal or minimal impairment
    Normal,
    /// Mild impairment
    Mild,
    /// Moderate impairment
    Moderate,
    /// Severe impairment
    Severe,
    /// Profound impairment
    Profound,
}

impl SeverityLevel {
    /// Convert severity score to level
    pub fn from_score(score: f32) -> Self {
        match score {
            s if s >= 0.9 => SeverityLevel::Normal,
            s if s >= 0.7 => SeverityLevel::Mild,
            s if s >= 0.5 => SeverityLevel::Moderate,
            s if s >= 0.3 => SeverityLevel::Severe,
            _ => SeverityLevel::Profound,
        }
    }

    /// Get score range for severity level
    pub fn score_range(&self) -> (f32, f32) {
        match self {
            SeverityLevel::Normal => (0.9, 1.0),
            SeverityLevel::Mild => (0.7, 0.9),
            SeverityLevel::Moderate => (0.5, 0.7),
            SeverityLevel::Severe => (0.3, 0.5),
            SeverityLevel::Profound => (0.0, 0.3),
        }
    }
}

/// Communication effectiveness assessment
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CommunicationEffectivenessResult {
    /// Overall communication effectiveness score
    pub overall_effectiveness: f32,
    /// Intelligibility in quiet conditions
    pub quiet_intelligibility: f32,
    /// Intelligibility in noise
    pub noise_intelligibility: f32,
    /// Listener burden (effort required to understand)
    pub listener_burden: f32,
    /// Communication efficiency
    pub communication_efficiency: f32,
    /// Functional communication level
    pub functional_level: SeverityLevel,
    /// Specific communication strengths
    pub strengths: Vec<String>,
    /// Areas needing support
    pub support_areas: Vec<String>,
}

/// Assistive technology evaluation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AssistiveTechnologyResult {
    /// Voice amplification effectiveness
    pub amplification_effectiveness: f32,
    /// Speech clarity enhancement
    pub clarity_enhancement: f32,
    /// Technology adaptation score
    pub technology_adaptation: f32,
    /// User acceptance likelihood
    pub user_acceptance: f32,
    /// Recommended assistive technologies
    pub recommended_technologies: Vec<String>,
    /// Technology configuration suggestions
    pub configuration_suggestions: Vec<String>,
}

/// Clinical assessment metrics
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ClinicalAssessmentResult {
    /// Dysarthria severity assessment
    pub dysarthria_severity: Option<SeverityLevel>,
    /// Voice quality assessment
    pub voice_quality: VoiceQualityMetrics,
    /// Respiratory support assessment
    pub respiratory_support: f32,
    /// Motor speech control
    pub motor_speech_control: f32,
    /// Cognitive-linguistic function
    pub cognitive_linguistic_function: f32,
    /// Clinical recommendations
    pub clinical_recommendations: Vec<String>,
    /// Therapy goals suggestions
    pub therapy_goals: Vec<String>,
}

/// Voice quality metrics for clinical assessment
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VoiceQualityMetrics {
    /// Fundamental frequency stability
    pub f0_stability: f32,
    /// Jitter (frequency perturbation)
    pub jitter: f32,
    /// Shimmer (amplitude perturbation)
    pub shimmer: f32,
    /// Harmonic-to-noise ratio
    pub harmonic_noise_ratio: f32,
    /// Voice breaks or interruptions
    pub voice_breaks: f32,
    /// Breathiness rating
    pub breathiness: f32,
    /// Roughness rating
    pub roughness: f32,
    /// Strain rating
    pub strain: f32,
}

/// Configuration for elderly and pathological speech evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElderlyPathologicalConfig {
    /// Target age group (if elderly)
    pub target_age_group: Option<ElderlyAgeGroup>,
    /// Known pathological conditions
    pub pathological_conditions: Vec<PathologicalCondition>,
    /// Language being evaluated
    pub language: LanguageCode,
    /// Whether to perform clinical assessment
    pub perform_clinical_assessment: bool,
    /// Whether to evaluate assistive technology needs
    pub evaluate_assistive_technology: bool,
    /// Whether to assess communication effectiveness
    pub assess_communication_effectiveness: bool,
    /// Expected severity level (if known)
    pub expected_severity: Option<SeverityLevel>,
    /// Listener familiarity level
    pub listener_familiarity: ListenerFamiliarity,
    /// Communication context (clinical, home, etc.)
    pub communication_context: CommunicationContext,
    /// Whether to apply age-adjusted scoring
    pub age_adjusted_scoring: bool,
}

/// Listener familiarity with speaker's condition
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ListenerFamiliarity {
    /// Very familiar (family, caregivers)
    VeryFamiliar,
    /// Familiar (healthcare workers, friends)
    Familiar,
    /// Somewhat familiar (occasional contact)
    SomewhatFamiliar,
    /// Unfamiliar (strangers, general public)
    Unfamiliar,
}

impl ListenerFamiliarity {
    /// Get intelligibility adjustment factor
    pub fn intelligibility_adjustment(&self) -> f32 {
        match self {
            ListenerFamiliarity::VeryFamiliar => 1.3,
            ListenerFamiliarity::Familiar => 1.15,
            ListenerFamiliarity::SomewhatFamiliar => 1.0,
            ListenerFamiliarity::Unfamiliar => 0.8,
        }
    }
}

/// Communication context
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CommunicationContext {
    /// Clinical or therapeutic setting
    Clinical,
    /// Home environment
    Home,
    /// Community or public setting
    Community,
    /// Workplace
    Workplace,
    /// Educational setting
    Educational,
    /// Telehealth or remote
    Telehealth,
}

impl CommunicationContext {
    /// Get context-specific expectations
    pub fn expectations(&self) -> ContextExpectations {
        match self {
            CommunicationContext::Clinical => ContextExpectations {
                intelligibility_requirement: 0.9,
                listener_patience: 0.9,
                time_pressure: 0.3,
                background_noise: 0.1,
                communication_importance: 0.9,
            },
            CommunicationContext::Home => ContextExpectations {
                intelligibility_requirement: 0.8,
                listener_patience: 0.8,
                time_pressure: 0.2,
                background_noise: 0.3,
                communication_importance: 0.8,
            },
            CommunicationContext::Community => ContextExpectations {
                intelligibility_requirement: 0.7,
                listener_patience: 0.4,
                time_pressure: 0.7,
                background_noise: 0.6,
                communication_importance: 0.7,
            },
            CommunicationContext::Workplace => ContextExpectations {
                intelligibility_requirement: 0.8,
                listener_patience: 0.5,
                time_pressure: 0.8,
                background_noise: 0.4,
                communication_importance: 0.9,
            },
            CommunicationContext::Educational => ContextExpectations {
                intelligibility_requirement: 0.85,
                listener_patience: 0.7,
                time_pressure: 0.5,
                background_noise: 0.5,
                communication_importance: 0.9,
            },
            CommunicationContext::Telehealth => ContextExpectations {
                intelligibility_requirement: 0.9,
                listener_patience: 0.8,
                time_pressure: 0.4,
                background_noise: 0.2,
                communication_importance: 0.95,
            },
        }
    }
}

/// Context-specific expectations
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ContextExpectations {
    /// Required intelligibility level (0.0-1.0)
    pub intelligibility_requirement: f32,
    /// Listener patience level (0.0-1.0)
    pub listener_patience: f32,
    /// Time pressure level (0.0-1.0)
    pub time_pressure: f32,
    /// Background noise level (0.0-1.0)
    pub background_noise: f32,
    /// Communication importance (0.0-1.0)
    pub communication_importance: f32,
}

impl Default for ElderlyPathologicalConfig {
    fn default() -> Self {
        Self {
            target_age_group: Some(ElderlyAgeGroup::YoungElderly),
            pathological_conditions: vec![],
            language: LanguageCode::EnUs,
            perform_clinical_assessment: true,
            evaluate_assistive_technology: true,
            assess_communication_effectiveness: true,
            expected_severity: None,
            listener_familiarity: ListenerFamiliarity::Familiar,
            communication_context: CommunicationContext::Clinical,
            age_adjusted_scoring: true,
        }
    }
}

/// Comprehensive elderly and pathological speech evaluation result
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ElderlyPathologicalResult {
    /// Overall evaluation score
    pub overall_score: f32,
    /// Communication effectiveness assessment
    pub communication_effectiveness: CommunicationEffectivenessResult,
    /// Clinical assessment results
    pub clinical_assessment: Option<ClinicalAssessmentResult>,
    /// Assistive technology evaluation
    pub assistive_technology: Option<AssistiveTechnologyResult>,
    /// Detected pathological features
    pub pathological_features: HashMap<PathologicalCondition, f32>,
    /// Age-related changes assessment
    pub age_related_changes: Option<AgeRelatedChanges>,
    /// Overall severity level
    pub severity_level: SeverityLevel,
    /// Context-adjusted scores
    pub context_adjusted_scores: HashMap<String, f32>,
    /// Recommendations for support
    pub support_recommendations: Vec<String>,
    /// Quality of life impact assessment
    pub quality_of_life_impact: f32,
    /// Confidence in the evaluation
    pub confidence: f32,
}

/// Elderly and pathological speech evaluator implementation
pub struct ElderlyPathologicalEvaluator {
    config: ElderlyPathologicalConfig,
}

impl ElderlyPathologicalEvaluator {
    /// Create a new elderly/pathological speech evaluator
    pub async fn new() -> Result<Self, EvaluationError> {
        Ok(Self {
            config: ElderlyPathologicalConfig::default(),
        })
    }

    /// Create evaluator with custom configuration
    pub async fn with_config(config: ElderlyPathologicalConfig) -> Result<Self, EvaluationError> {
        Ok(Self { config })
    }

    /// Update evaluator configuration
    pub fn set_config(&mut self, config: ElderlyPathologicalConfig) {
        self.config = config;
    }

    /// Evaluate elderly/pathological speech synthesis quality
    pub async fn evaluate_elderly_pathological_speech(
        &self,
        generated_audio: &AudioBuffer,
        reference_audio: Option<&AudioBuffer>,
        target_text: Option<&str>,
    ) -> Result<ElderlyPathologicalResult, EvaluationError> {
        // Assess communication effectiveness
        let communication_effectiveness = self
            .assess_communication_effectiveness(generated_audio, reference_audio, target_text)
            .await?;

        // Perform clinical assessment if enabled
        let clinical_assessment = if self.config.perform_clinical_assessment {
            Some(
                self.perform_clinical_assessment(generated_audio, reference_audio)
                    .await?,
            )
        } else {
            None
        };

        // Evaluate assistive technology needs if enabled
        let assistive_technology = if self.config.evaluate_assistive_technology {
            Some(
                self.evaluate_assistive_technology_needs(generated_audio)
                    .await?,
            )
        } else {
            None
        };

        // Detect pathological features
        let pathological_features = self.detect_pathological_features(generated_audio).await?;

        // Assess age-related changes if applicable
        let age_related_changes = if let Some(age_group) = self.config.target_age_group {
            Some(
                self.assess_age_related_changes(generated_audio, age_group)
                    .await?,
            )
        } else {
            None
        };

        // Determine overall severity level
        let severity_level =
            self.determine_severity_level(&communication_effectiveness, &pathological_features);

        // Calculate context-adjusted scores
        let context_adjusted_scores = self.calculate_context_adjusted_scores(
            &communication_effectiveness,
            &pathological_features,
        );

        // Generate support recommendations
        let support_recommendations = self.generate_support_recommendations(
            &communication_effectiveness,
            &pathological_features,
            clinical_assessment.as_ref(),
        );

        // Assess quality of life impact
        let quality_of_life_impact = self.assess_quality_of_life_impact(
            &communication_effectiveness,
            &pathological_features,
            severity_level,
        );

        // Calculate overall score with appropriate adjustments
        let overall_score = self
            .calculate_overall_score(
                &communication_effectiveness,
                &pathological_features,
                severity_level,
                &context_adjusted_scores,
            )
            .await?;

        // Calculate evaluation confidence
        let confidence =
            self.calculate_evaluation_confidence(generated_audio, reference_audio, target_text);

        Ok(ElderlyPathologicalResult {
            overall_score,
            communication_effectiveness,
            clinical_assessment,
            assistive_technology,
            pathological_features,
            age_related_changes,
            severity_level,
            context_adjusted_scores,
            support_recommendations,
            quality_of_life_impact,
            confidence,
        })
    }

    /// Assess communication effectiveness
    async fn assess_communication_effectiveness(
        &self,
        generated_audio: &AudioBuffer,
        reference_audio: Option<&AudioBuffer>,
        target_text: Option<&str>,
    ) -> Result<CommunicationEffectivenessResult, EvaluationError> {
        // Basic intelligibility assessment
        let quiet_intelligibility = self
            .assess_intelligibility_quiet(generated_audio, reference_audio)
            .await?;

        // Simulate noise conditions
        let noise_intelligibility = self
            .assess_intelligibility_noise(generated_audio, reference_audio)
            .await?;

        // Calculate listener burden
        let listener_burden = self
            .calculate_listener_burden(generated_audio, target_text)
            .await?;

        // Assess communication efficiency
        let communication_efficiency = self
            .assess_communication_efficiency(generated_audio, target_text)
            .await?;

        // Apply familiarity adjustment
        let familiarity_adjustment = self
            .config
            .listener_familiarity
            .intelligibility_adjustment();
        let adjusted_quiet = (quiet_intelligibility * familiarity_adjustment).min(1.0);
        let adjusted_noise = (noise_intelligibility * familiarity_adjustment).min(1.0);

        // Calculate overall effectiveness
        let overall_effectiveness = (adjusted_quiet * 0.4
            + adjusted_noise * 0.3
            + (1.0 - listener_burden) * 0.2
            + communication_efficiency * 0.1)
            .min(1.0);

        // Determine functional level
        let functional_level = SeverityLevel::from_score(overall_effectiveness);

        // Identify strengths and support areas
        let strengths = self.identify_communication_strengths(
            adjusted_quiet,
            adjusted_noise,
            communication_efficiency,
        );
        let support_areas = self.identify_support_areas(
            adjusted_quiet,
            adjusted_noise,
            listener_burden,
            communication_efficiency,
        );

        Ok(CommunicationEffectivenessResult {
            overall_effectiveness,
            quiet_intelligibility: adjusted_quiet,
            noise_intelligibility: adjusted_noise,
            listener_burden,
            communication_efficiency,
            functional_level,
            strengths,
            support_areas,
        })
    }

    /// Perform clinical assessment
    async fn perform_clinical_assessment(
        &self,
        generated_audio: &AudioBuffer,
        _reference_audio: Option<&AudioBuffer>,
    ) -> Result<ClinicalAssessmentResult, EvaluationError> {
        // Assess voice quality metrics
        let voice_quality = self.assess_voice_quality_metrics(generated_audio).await?;

        // Assess dysarthria severity
        let dysarthria_severity = self
            .assess_dysarthria_severity(generated_audio, &voice_quality)
            .await?;

        // Assess respiratory support
        let respiratory_support = self.assess_respiratory_support(generated_audio).await?;

        // Assess motor speech control
        let motor_speech_control = self.assess_motor_speech_control(generated_audio).await?;

        // Assess cognitive-linguistic function
        let cognitive_linguistic_function = self
            .assess_cognitive_linguistic_function(generated_audio)
            .await?;

        // Generate clinical recommendations
        let clinical_recommendations = self.generate_clinical_recommendations(
            &voice_quality,
            dysarthria_severity,
            respiratory_support,
            motor_speech_control,
        );

        // Generate therapy goals
        let therapy_goals =
            self.generate_therapy_goals(&voice_quality, dysarthria_severity, respiratory_support);

        Ok(ClinicalAssessmentResult {
            dysarthria_severity,
            voice_quality,
            respiratory_support,
            motor_speech_control,
            cognitive_linguistic_function,
            clinical_recommendations,
            therapy_goals,
        })
    }

    /// Evaluate assistive technology needs
    async fn evaluate_assistive_technology_needs(
        &self,
        generated_audio: &AudioBuffer,
    ) -> Result<AssistiveTechnologyResult, EvaluationError> {
        // Assess need for voice amplification
        let amplification_effectiveness = self.assess_amplification_needs(generated_audio).await?;

        // Assess need for clarity enhancement
        let clarity_enhancement = self
            .assess_clarity_enhancement_needs(generated_audio)
            .await?;

        // Assess technology adaptation capability
        let technology_adaptation = self.assess_technology_adaptation(generated_audio).await?;

        // Estimate user acceptance likelihood
        let user_acceptance = self.estimate_user_acceptance(
            amplification_effectiveness,
            clarity_enhancement,
            technology_adaptation,
        );

        // Recommend specific technologies
        let recommended_technologies = self.recommend_assistive_technologies(
            amplification_effectiveness,
            clarity_enhancement,
            technology_adaptation,
        );

        // Suggest technology configurations
        let configuration_suggestions = self.suggest_technology_configurations(
            &recommended_technologies,
            amplification_effectiveness,
            clarity_enhancement,
        );

        Ok(AssistiveTechnologyResult {
            amplification_effectiveness,
            clarity_enhancement,
            technology_adaptation,
            user_acceptance,
            recommended_technologies,
            configuration_suggestions,
        })
    }

    /// Assess voice quality metrics
    async fn assess_voice_quality_metrics(
        &self,
        generated_audio: &AudioBuffer,
    ) -> Result<VoiceQualityMetrics, EvaluationError> {
        let samples = generated_audio.samples();
        let sample_rate = generated_audio.sample_rate() as f32;

        // Assess F0 stability
        let f0_stability = self.calculate_f0_stability(samples, sample_rate)?;

        // Calculate jitter (simplified)
        let jitter = self.calculate_jitter(samples, sample_rate)?;

        // Calculate shimmer (simplified)
        let shimmer = self.calculate_shimmer(samples)?;

        // Estimate harmonic-to-noise ratio
        let harmonic_noise_ratio = self.calculate_harmonic_noise_ratio(samples)?;

        // Detect voice breaks
        let voice_breaks = self.detect_voice_breaks(samples)?;

        // Assess breathiness
        let breathiness = self.assess_breathiness(samples)?;

        // Assess roughness
        let roughness = self.assess_roughness(samples, sample_rate)?;

        // Assess strain
        let strain = self.assess_strain(samples)?;

        Ok(VoiceQualityMetrics {
            f0_stability,
            jitter,
            shimmer,
            harmonic_noise_ratio,
            voice_breaks,
            breathiness,
            roughness,
            strain,
        })
    }

    /// Detect pathological features
    async fn detect_pathological_features(
        &self,
        generated_audio: &AudioBuffer,
    ) -> Result<HashMap<PathologicalCondition, f32>, EvaluationError> {
        let mut features = HashMap::new();
        let samples = generated_audio.samples();

        // Basic acoustic analysis
        let energy = samples.iter().map(|&x| x * x).sum::<f32>() / samples.len() as f32;
        let rms = energy.sqrt();

        // Assess features for each known condition
        for &condition in &self.config.pathological_conditions {
            let severity = self.assess_condition_severity(samples, condition).await?;
            features.insert(condition, severity);
        }

        // If no specific conditions specified, perform general screening
        if self.config.pathological_conditions.is_empty() {
            // Screen for common pathological features
            features.insert(
                PathologicalCondition::Presbyphonia,
                self.screen_presbyphonia(samples).await?,
            );
            features.insert(
                PathologicalCondition::VocalCordParalysis,
                self.screen_vocal_cord_issues(samples).await?,
            );
            features.insert(
                PathologicalCondition::Parkinsons,
                self.screen_parkinsons_features(samples).await?,
            );
        }

        Ok(features)
    }

    /// Assessment helper methods
    async fn assess_intelligibility_quiet(
        &self,
        generated_audio: &AudioBuffer,
        _reference_audio: Option<&AudioBuffer>,
    ) -> Result<f32, EvaluationError> {
        let samples = generated_audio.samples();
        let energy = samples.iter().map(|&x| x * x).sum::<f32>() / samples.len() as f32;
        let rms = energy.sqrt();

        // Basic intelligibility estimate based on signal clarity
        let signal_clarity = (rms * 2.0).min(1.0);
        let noise_level = self.estimate_noise_level(samples);
        let snr = (rms / noise_level.max(0.001)).log10() / 2.0; // Normalize to 0-1 range

        Ok((signal_clarity * 0.6 + snr.min(1.0) * 0.4).min(1.0))
    }

    async fn assess_intelligibility_noise(
        &self,
        generated_audio: &AudioBuffer,
        reference_audio: Option<&AudioBuffer>,
    ) -> Result<f32, EvaluationError> {
        // Simulate noise degradation
        let quiet_score = self
            .assess_intelligibility_quiet(generated_audio, reference_audio)
            .await?;
        let noise_degradation = 0.3; // Typical degradation in noise

        Ok((quiet_score * (1.0 - noise_degradation)).max(0.0))
    }

    async fn calculate_listener_burden(
        &self,
        generated_audio: &AudioBuffer,
        _target_text: Option<&str>,
    ) -> Result<f32, EvaluationError> {
        let samples = generated_audio.samples();

        // Assess signal variability (higher variability = higher burden)
        let mean_energy = samples.iter().map(|&x| x * x).sum::<f32>() / samples.len() as f32;
        let variance = samples
            .iter()
            .map(|&x| (x * x - mean_energy).powi(2))
            .sum::<f32>()
            / samples.len() as f32;

        let variability = variance.sqrt() / mean_energy.sqrt().max(0.001);

        // Higher variability suggests more effort needed
        Ok(variability.min(1.0))
    }

    async fn assess_communication_efficiency(
        &self,
        generated_audio: &AudioBuffer,
        target_text: Option<&str>,
    ) -> Result<f32, EvaluationError> {
        let duration = generated_audio.duration();

        if let Some(text) = target_text {
            let word_count = text.split_whitespace().count();
            let words_per_second = word_count as f32 / duration;

            // Optimal speaking rate for elderly/pathological: 140-180 words/minute (2.3-3.0 words/second)
            let efficiency = if words_per_second >= 2.3 && words_per_second <= 3.0 {
                1.0
            } else if words_per_second < 2.3 {
                words_per_second / 2.3
            } else {
                3.0 / words_per_second
            };

            Ok(efficiency.min(1.0))
        } else {
            // Estimate based on temporal characteristics
            Ok(0.7) // Default moderate efficiency
        }
    }

    /// Voice quality assessment methods
    fn calculate_f0_stability(
        &self,
        samples: &[f32],
        sample_rate: f32,
    ) -> Result<f32, EvaluationError> {
        // Simplified F0 stability calculation
        let frame_size = (sample_rate * 0.025) as usize; // 25ms frames
        let hop_size = (sample_rate * 0.010) as usize; // 10ms hop

        let mut f0_values = Vec::new();

        for i in (0..samples.len()).step_by(hop_size) {
            if i + frame_size > samples.len() {
                break;
            }

            let frame = &samples[i..i + frame_size];
            let f0 = self.estimate_frame_f0(frame, sample_rate)?;
            if f0 > 0.0 {
                f0_values.push(f0);
            }
        }

        if f0_values.is_empty() {
            return Ok(0.0);
        }

        // Calculate coefficient of variation
        let mean_f0 = f0_values.iter().sum::<f32>() / f0_values.len() as f32;
        let variance = f0_values
            .iter()
            .map(|&f0| (f0 - mean_f0).powi(2))
            .sum::<f32>()
            / f0_values.len() as f32;
        let std_dev = variance.sqrt();

        let cv = std_dev / mean_f0;
        let stability = (1.0 - cv.min(1.0)).max(0.0);

        Ok(stability)
    }

    fn estimate_frame_f0(&self, frame: &[f32], sample_rate: f32) -> Result<f32, EvaluationError> {
        // Simple autocorrelation-based F0 estimation
        let min_period = (sample_rate / 500.0) as usize; // Max F0
        let max_period = (sample_rate / 50.0) as usize; // Min F0

        if max_period >= frame.len() {
            return Ok(0.0);
        }

        let mut max_correlation = 0.0;
        let mut best_period = min_period;

        for period in min_period..=max_period.min(frame.len() - 1) {
            let mut correlation = 0.0;
            for i in 0..(frame.len() - period) {
                correlation += frame[i] * frame[i + period];
            }

            if correlation > max_correlation {
                max_correlation = correlation;
                best_period = period;
            }
        }

        if max_correlation > 0.3 {
            Ok(sample_rate / best_period as f32)
        } else {
            Ok(0.0)
        }
    }

    fn calculate_jitter(&self, samples: &[f32], sample_rate: f32) -> Result<f32, EvaluationError> {
        // Simplified jitter calculation as F0 period variation
        let frame_size = (sample_rate * 0.025) as usize;
        let hop_size = (sample_rate * 0.010) as usize;

        let mut periods = Vec::new();

        for i in (0..samples.len()).step_by(hop_size) {
            if i + frame_size > samples.len() {
                break;
            }

            let frame = &samples[i..i + frame_size];
            let f0 = self.estimate_frame_f0(frame, sample_rate)?;
            if f0 > 0.0 {
                periods.push(sample_rate / f0);
            }
        }

        if periods.len() < 2 {
            return Ok(0.0);
        }

        // Calculate period-to-period variation
        let mut jitter_sum = 0.0;
        for window in periods.windows(2) {
            jitter_sum += (window[1] - window[0]).abs();
        }

        let mean_period = periods.iter().sum::<f32>() / periods.len() as f32;
        let jitter = (jitter_sum / (periods.len() - 1) as f32) / mean_period;

        Ok(jitter.min(1.0))
    }

    fn calculate_shimmer(&self, samples: &[f32]) -> Result<f32, EvaluationError> {
        // Simplified shimmer calculation as amplitude variation
        let frame_size = 1024;
        let hop_size = 512;

        let mut amplitudes = Vec::new();

        for i in (0..samples.len()).step_by(hop_size) {
            if i + frame_size > samples.len() {
                break;
            }

            let frame = &samples[i..i + frame_size];
            let rms = (frame.iter().map(|&x| x * x).sum::<f32>() / frame.len() as f32).sqrt();
            amplitudes.push(rms);
        }

        if amplitudes.len() < 2 {
            return Ok(0.0);
        }

        // Calculate amplitude-to-amplitude variation
        let mut shimmer_sum = 0.0;
        for window in amplitudes.windows(2) {
            if window[0] > 0.0 && window[1] > 0.0 {
                shimmer_sum += ((window[1] - window[0]).abs() / window[0]).min(2.0);
            }
        }

        let shimmer = shimmer_sum / (amplitudes.len() - 1) as f32;
        Ok(shimmer.min(1.0))
    }

    fn calculate_harmonic_noise_ratio(&self, samples: &[f32]) -> Result<f32, EvaluationError> {
        // Simplified HNR calculation
        let energy = samples.iter().map(|&x| x * x).sum::<f32>() / samples.len() as f32;
        let rms = energy.sqrt();

        // Estimate noise level
        let noise_level = self.estimate_noise_level(samples);

        // Calculate HNR in dB, then normalize to 0-1
        let hnr_db = 20.0 * (rms / noise_level.max(0.001)).log10();
        let normalized_hnr = (hnr_db / 30.0).min(1.0).max(0.0); // Normalize to 0-30 dB range

        Ok(normalized_hnr)
    }

    fn detect_voice_breaks(&self, samples: &[f32]) -> Result<f32, EvaluationError> {
        // Detect sudden amplitude drops that might indicate voice breaks
        let threshold = 0.1; // Amplitude threshold
        let min_break_duration = 0.05; // 50ms minimum break
        let sample_rate = 16000.0; // Assume 16kHz
        let min_break_samples = (min_break_duration * sample_rate) as usize;

        let mut in_break = false;
        let mut break_count = 0;
        let mut break_samples = 0;

        for &sample in samples {
            if sample.abs() < threshold {
                if !in_break {
                    in_break = true;
                    break_samples = 1;
                } else {
                    break_samples += 1;
                }
            } else {
                if in_break && break_samples >= min_break_samples {
                    break_count += 1;
                }
                in_break = false;
            }
        }

        // Normalize by audio duration
        let total_duration = samples.len() as f32 / sample_rate;
        let breaks_per_second = break_count as f32 / total_duration;

        Ok((breaks_per_second / 2.0).min(1.0)) // Normalize to 0-1 (2 breaks/sec = 1.0)
    }

    fn assess_breathiness(&self, samples: &[f32]) -> Result<f32, EvaluationError> {
        // Estimate breathiness based on spectral characteristics
        // Higher frequency content relative to low frequencies suggests breathiness
        let window_size = 1024;
        let mut total_breathiness = 0.0;
        let mut window_count = 0;

        for chunk in samples.chunks(window_size) {
            if chunk.len() == window_size {
                let high_freq_energy = chunk
                    .iter()
                    .skip(window_size / 2)
                    .map(|&x| x * x)
                    .sum::<f32>();
                let low_freq_energy = chunk
                    .iter()
                    .take(window_size / 2)
                    .map(|&x| x * x)
                    .sum::<f32>();

                if low_freq_energy > 0.0 {
                    let ratio = high_freq_energy / low_freq_energy;
                    total_breathiness += ratio.min(2.0); // Cap at 2.0
                    window_count += 1;
                }
            }
        }

        if window_count > 0 {
            Ok((total_breathiness / window_count as f32 / 2.0).min(1.0))
        } else {
            Ok(0.0)
        }
    }

    fn assess_roughness(&self, samples: &[f32], _sample_rate: f32) -> Result<f32, EvaluationError> {
        // Estimate roughness based on signal irregularity
        let mut roughness_sum = 0.0;
        let window_size = 256;

        for chunk in samples.chunks(window_size) {
            if chunk.len() == window_size {
                // Calculate signal variability within window
                let mean = chunk.iter().sum::<f32>() / chunk.len() as f32;
                let variance =
                    chunk.iter().map(|&x| (x - mean).powi(2)).sum::<f32>() / chunk.len() as f32;

                roughness_sum += variance.sqrt();
            }
        }

        let num_windows = samples.len() / window_size;
        if num_windows > 0 {
            Ok((roughness_sum / num_windows as f32).min(1.0))
        } else {
            Ok(0.0)
        }
    }

    fn assess_strain(&self, samples: &[f32]) -> Result<f32, EvaluationError> {
        // Estimate strain based on signal compression and energy concentration
        let energy = samples.iter().map(|&x| x * x).sum::<f32>() / samples.len() as f32;
        let peak_energy = samples.iter().map(|&x| x * x).fold(0.0, f32::max);

        // High peak-to-average ratio might indicate strain
        if energy > 0.0 {
            let crest_factor = peak_energy.sqrt() / energy.sqrt();
            Ok((crest_factor / 10.0).min(1.0)) // Normalize assuming max crest factor of 10
        } else {
            Ok(0.0)
        }
    }

    fn estimate_noise_level(&self, samples: &[f32]) -> f32 {
        // Estimate noise floor as minimum energy in quiet segments
        let window_size = 512;
        let mut min_energy = f32::INFINITY;

        for chunk in samples.chunks(window_size) {
            let energy = chunk.iter().map(|&x| x * x).sum::<f32>() / chunk.len() as f32;
            if energy < min_energy {
                min_energy = energy;
            }
        }

        min_energy.sqrt()
    }

    /// Additional assessment methods (simplified implementations)
    async fn assess_condition_severity(
        &self,
        _samples: &[f32],
        condition: PathologicalCondition,
    ) -> Result<f32, EvaluationError> {
        // Simplified condition-specific assessment
        let features = condition.characteristic_features();
        Ok(features.communication_impact * 0.8) // Simplified scoring
    }

    async fn screen_presbyphonia(&self, samples: &[f32]) -> Result<f32, EvaluationError> {
        // Screen for age-related voice changes
        let voice_quality = self
            .assess_voice_quality_metrics(&AudioBuffer::new(samples.to_vec(), 16000, 1))
            .await?;
        Ok((voice_quality.breathiness + voice_quality.voice_breaks) / 2.0)
    }

    async fn screen_vocal_cord_issues(&self, samples: &[f32]) -> Result<f32, EvaluationError> {
        // Screen for vocal cord paralysis/weakness
        let voice_quality = self
            .assess_voice_quality_metrics(&AudioBuffer::new(samples.to_vec(), 16000, 1))
            .await?;
        Ok((voice_quality.breathiness + (1.0 - voice_quality.f0_stability)) / 2.0)
    }

    async fn screen_parkinsons_features(&self, samples: &[f32]) -> Result<f32, EvaluationError> {
        // Screen for Parkinson's-related features
        let voice_quality = self
            .assess_voice_quality_metrics(&AudioBuffer::new(samples.to_vec(), 16000, 1))
            .await?;
        Ok((voice_quality.voice_breaks + voice_quality.jitter + voice_quality.shimmer) / 3.0)
    }

    async fn assess_age_related_changes(
        &self,
        _generated_audio: &AudioBuffer,
        age_group: ElderlyAgeGroup,
    ) -> Result<AgeRelatedChanges, EvaluationError> {
        // Return expected changes for age group (could be enhanced with actual analysis)
        Ok(age_group.expected_changes())
    }

    async fn assess_dysarthria_severity(
        &self,
        _generated_audio: &AudioBuffer,
        voice_quality: &VoiceQualityMetrics,
    ) -> Result<Option<SeverityLevel>, EvaluationError> {
        // Assess dysarthria severity based on voice quality metrics
        let severity_score = (voice_quality.f0_stability
            + (1.0 - voice_quality.jitter)
            + (1.0 - voice_quality.shimmer)
            + voice_quality.harmonic_noise_ratio / 30.0)
            / 4.0;

        Ok(Some(SeverityLevel::from_score(severity_score)))
    }

    async fn assess_respiratory_support(
        &self,
        generated_audio: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        // Assess respiratory support based on volume consistency and breath patterns
        let samples = generated_audio.samples();
        let consistency = self.assess_signal_consistency(samples);
        Ok(consistency)
    }

    async fn assess_motor_speech_control(
        &self,
        generated_audio: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        // Assess motor control based on speech precision and coordination
        let voice_quality = self.assess_voice_quality_metrics(generated_audio).await?;
        let control_score = (voice_quality.f0_stability + (1.0 - voice_quality.jitter)) / 2.0;
        Ok(control_score)
    }

    async fn assess_cognitive_linguistic_function(
        &self,
        _generated_audio: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        // Simplified cognitive-linguistic assessment
        Ok(0.8) // Would require more complex analysis in practice
    }

    async fn assess_amplification_needs(
        &self,
        generated_audio: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        let samples = generated_audio.samples();
        let rms = (samples.iter().map(|&x| x * x).sum::<f32>() / samples.len() as f32).sqrt();

        // Lower RMS suggests higher need for amplification
        let amplification_need = 1.0 - (rms * 5.0).min(1.0);
        Ok(amplification_need)
    }

    async fn assess_clarity_enhancement_needs(
        &self,
        generated_audio: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        let voice_quality = self.assess_voice_quality_metrics(generated_audio).await?;

        // Higher jitter/shimmer suggests higher need for clarity enhancement
        let clarity_need = (voice_quality.jitter + voice_quality.shimmer) / 2.0;
        Ok(clarity_need)
    }

    async fn assess_technology_adaptation(
        &self,
        _generated_audio: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        // Simplified technology adaptation assessment
        // Would involve cognitive assessment in practice
        if let Some(age_group) = self.config.target_age_group {
            let changes = age_group.expected_changes();
            Ok(1.0 - changes.cognitive_load_sensitivity)
        } else {
            Ok(0.7) // Default moderate adaptation
        }
    }

    fn assess_signal_consistency(&self, samples: &[f32]) -> f32 {
        let window_size = 512;
        let mut energies = Vec::new();

        for chunk in samples.chunks(window_size) {
            let energy = chunk.iter().map(|&x| x * x).sum::<f32>() / chunk.len() as f32;
            energies.push(energy.sqrt());
        }

        if energies.is_empty() {
            return 0.0;
        }

        let mean_energy = energies.iter().sum::<f32>() / energies.len() as f32;
        let variance = energies
            .iter()
            .map(|&e| (e - mean_energy).powi(2))
            .sum::<f32>()
            / energies.len() as f32;

        (1.0 - variance.sqrt().min(1.0)).max(0.0)
    }

    /// Helper methods for generating recommendations and assessments
    fn determine_severity_level(
        &self,
        communication_effectiveness: &CommunicationEffectivenessResult,
        _pathological_features: &HashMap<PathologicalCondition, f32>,
    ) -> SeverityLevel {
        SeverityLevel::from_score(communication_effectiveness.overall_effectiveness)
    }

    fn calculate_context_adjusted_scores(
        &self,
        communication_effectiveness: &CommunicationEffectivenessResult,
        pathological_features: &HashMap<PathologicalCondition, f32>,
    ) -> HashMap<String, f32> {
        let mut scores = HashMap::new();
        let context_expectations = self.config.communication_context.expectations();

        // Adjust intelligibility for context
        let context_adjusted_intelligibility = communication_effectiveness.quiet_intelligibility
            * context_expectations.intelligibility_requirement;
        scores.insert(
            "context_adjusted_intelligibility".to_string(),
            context_adjusted_intelligibility,
        );

        // Adjust for time pressure
        let time_pressure_adjustment = 1.0 - context_expectations.time_pressure * 0.2;
        scores.insert(
            "time_pressure_adjusted".to_string(),
            communication_effectiveness.communication_efficiency * time_pressure_adjustment,
        );

        // Adjust for background noise
        let noise_adjusted = communication_effectiveness.noise_intelligibility
            * (1.0 - context_expectations.background_noise * 0.3);
        scores.insert("noise_adjusted".to_string(), noise_adjusted);

        scores
    }

    fn generate_support_recommendations(
        &self,
        communication_effectiveness: &CommunicationEffectivenessResult,
        pathological_features: &HashMap<PathologicalCondition, f32>,
        clinical_assessment: Option<&ClinicalAssessmentResult>,
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        // Based on communication effectiveness
        if communication_effectiveness.overall_effectiveness < 0.7 {
            recommendations.push(
                "Consider communication partner training for improved understanding".to_string(),
            );
        }

        if communication_effectiveness.listener_burden > 0.6 {
            recommendations
                .push("Implement communication strategies to reduce listener effort".to_string());
        }

        // Based on pathological features
        for (condition, &severity) in pathological_features {
            if severity > 0.5 {
                match condition {
                    PathologicalCondition::Parkinsons => {
                        recommendations
                            .push("LSVT LOUD therapy may improve voice loudness".to_string());
                    }
                    PathologicalCondition::VocalCordParalysis => {
                        recommendations.push(
                            "Voice therapy focusing on breath support and coordination".to_string(),
                        );
                    }
                    PathologicalCondition::Presbyphonia => {
                        recommendations.push(
                            "Voice exercises to maintain vocal strength and flexibility"
                                .to_string(),
                        );
                    }
                    _ => {
                        recommendations.push("Condition-specific therapy recommended".to_string());
                    }
                }
            }
        }

        // Based on clinical assessment
        if let Some(clinical) = clinical_assessment {
            if clinical.respiratory_support < 0.6 {
                recommendations
                    .push("Respiratory muscle training to improve breath support".to_string());
            }
        }

        recommendations
    }

    fn assess_quality_of_life_impact(
        &self,
        communication_effectiveness: &CommunicationEffectivenessResult,
        pathological_features: &HashMap<PathologicalCondition, f32>,
        severity_level: SeverityLevel,
    ) -> f32 {
        let communication_impact = 1.0 - communication_effectiveness.overall_effectiveness;

        let pathological_impact = pathological_features
            .values()
            .map(|&severity| severity * 0.1)
            .sum::<f32>()
            .min(0.5);

        let severity_impact = match severity_level {
            SeverityLevel::Normal => 0.0,
            SeverityLevel::Mild => 0.1,
            SeverityLevel::Moderate => 0.3,
            SeverityLevel::Severe => 0.6,
            SeverityLevel::Profound => 0.9,
        };

        (communication_impact + pathological_impact + severity_impact).min(1.0)
    }

    fn identify_communication_strengths(
        &self,
        quiet_intelligibility: f32,
        noise_intelligibility: f32,
        communication_efficiency: f32,
    ) -> Vec<String> {
        let mut strengths = Vec::new();

        if quiet_intelligibility > 0.8 {
            strengths.push("Good intelligibility in quiet conditions".to_string());
        }

        if noise_intelligibility > 0.7 {
            strengths.push("Maintains intelligibility in background noise".to_string());
        }

        if communication_efficiency > 0.8 {
            strengths.push("Efficient communication rate".to_string());
        }

        if strengths.is_empty() {
            strengths.push("Maintains functional communication".to_string());
        }

        strengths
    }

    fn identify_support_areas(
        &self,
        quiet_intelligibility: f32,
        noise_intelligibility: f32,
        listener_burden: f32,
        communication_efficiency: f32,
    ) -> Vec<String> {
        let mut areas = Vec::new();

        if quiet_intelligibility < 0.7 {
            areas.push("Speech clarity in quiet conditions".to_string());
        }

        if noise_intelligibility < 0.6 {
            areas.push("Communication in noisy environments".to_string());
        }

        if listener_burden > 0.6 {
            areas.push("Reducing listener effort and concentration".to_string());
        }

        if communication_efficiency < 0.6 {
            areas.push("Improving communication rate and efficiency".to_string());
        }

        areas
    }

    fn estimate_user_acceptance(&self, amplification: f32, clarity: f32, adaptation: f32) -> f32 {
        // Higher need for technology but lower adaptation ability = lower acceptance
        let technology_need = (amplification + clarity) / 2.0;
        let acceptance = (technology_need * 0.6 + adaptation * 0.4).min(1.0);
        acceptance
    }

    fn recommend_assistive_technologies(
        &self,
        amplification: f32,
        clarity: f32,
        adaptation: f32,
    ) -> Vec<String> {
        let mut technologies = Vec::new();

        if amplification > 0.6 {
            technologies.push("Personal voice amplifier".to_string());
            technologies.push("Portable PA system".to_string());
        }

        if clarity > 0.6 {
            technologies.push("Speech enhancement software".to_string());
            technologies.push("Real-time speech processing app".to_string());
        }

        if adaptation > 0.7 {
            technologies.push("Voice banking software".to_string());
            technologies.push("Communication app with pre-recorded messages".to_string());
        }

        if technologies.is_empty() {
            technologies.push("Communication strategies training".to_string());
        }

        technologies
    }

    fn suggest_technology_configurations(
        &self,
        technologies: &[String],
        amplification: f32,
        clarity: f32,
    ) -> Vec<String> {
        let mut suggestions = Vec::new();

        if technologies.iter().any(|t| t.contains("amplifier")) {
            if amplification > 0.8 {
                suggestions.push("Set amplification to maximum comfortable level".to_string());
            } else {
                suggestions
                    .push("Start with moderate amplification and adjust gradually".to_string());
            }
        }

        if technologies.iter().any(|t| t.contains("enhancement")) {
            if clarity > 0.8 {
                suggestions.push("Enable maximum clarity enhancement features".to_string());
            } else {
                suggestions.push("Use mild to moderate enhancement settings".to_string());
            }
        }

        suggestions.push("Provide comprehensive user training and support".to_string());
        suggestions.push("Regular follow-up to adjust settings as needed".to_string());

        suggestions
    }

    fn generate_clinical_recommendations(
        &self,
        voice_quality: &VoiceQualityMetrics,
        dysarthria_severity: Option<SeverityLevel>,
        respiratory_support: f32,
        motor_speech_control: f32,
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        if voice_quality.breathiness > 0.6 {
            recommendations.push(
                "Voice therapy focusing on breath support and vocal fold adduction".to_string(),
            );
        }

        if voice_quality.roughness > 0.6 {
            recommendations
                .push("Medical evaluation for possible vocal fold pathology".to_string());
        }

        if let Some(severity) = dysarthria_severity {
            match severity {
                SeverityLevel::Moderate | SeverityLevel::Severe | SeverityLevel::Profound => {
                    recommendations
                        .push("Comprehensive speech therapy evaluation and treatment".to_string());
                }
                _ => {}
            }
        }

        if respiratory_support < 0.6 {
            recommendations
                .push("Pulmonary function evaluation and respiratory therapy".to_string());
        }

        if motor_speech_control < 0.6 {
            recommendations
                .push("Motor speech therapy with focus on articulation precision".to_string());
        }

        recommendations
    }

    fn generate_therapy_goals(
        &self,
        voice_quality: &VoiceQualityMetrics,
        dysarthria_severity: Option<SeverityLevel>,
        respiratory_support: f32,
    ) -> Vec<String> {
        let mut goals = Vec::new();

        if voice_quality.f0_stability < 0.7 {
            goals.push("Improve vocal stability and reduce tremor".to_string());
        }

        if voice_quality.harmonic_noise_ratio < 15.0 {
            goals.push("Enhance voice quality and reduce breathiness".to_string());
        }

        if respiratory_support < 0.7 {
            goals.push("Strengthen respiratory support for speech".to_string());
        }

        if let Some(severity) = dysarthria_severity {
            if matches!(severity, SeverityLevel::Moderate | SeverityLevel::Severe) {
                goals.push(
                    "Improve speech intelligibility for functional communication".to_string(),
                );
            }
        }

        goals.push("Maintain current level of communication function".to_string());

        goals
    }

    async fn calculate_overall_score(
        &self,
        communication_effectiveness: &CommunicationEffectivenessResult,
        pathological_features: &HashMap<PathologicalCondition, f32>,
        severity_level: SeverityLevel,
        context_adjusted_scores: &HashMap<String, f32>,
    ) -> Result<f32, EvaluationError> {
        // Weight communication effectiveness heavily
        let mut score = communication_effectiveness.overall_effectiveness * 0.6;

        // Adjust for pathological features
        let pathological_impact = pathological_features
            .values()
            .map(|&s| s * 0.1)
            .sum::<f32>()
            .min(0.3);
        score -= pathological_impact;

        // Adjust for severity
        let severity_adjustment = match severity_level {
            SeverityLevel::Normal => 0.0,
            SeverityLevel::Mild => -0.05,
            SeverityLevel::Moderate => -0.15,
            SeverityLevel::Severe => -0.3,
            SeverityLevel::Profound => -0.5,
        };
        score += severity_adjustment;

        // Apply context adjustments
        if let Some(&context_score) =
            context_adjusted_scores.get("context_adjusted_intelligibility")
        {
            score = (score + context_score * 0.2) / 1.2; // Weight context adjustment
        }

        Ok(score.max(0.0).min(1.0))
    }

    fn calculate_evaluation_confidence(
        &self,
        _generated_audio: &AudioBuffer,
        reference_audio: Option<&AudioBuffer>,
        target_text: Option<&str>,
    ) -> f32 {
        let mut confidence = 0.6_f32; // Base confidence

        // Reference audio increases confidence
        if reference_audio.is_some() {
            confidence += 0.15;
        }

        // Target text increases confidence
        if target_text.is_some() {
            confidence += 0.1;
        }

        // Known conditions increase confidence
        if !self.config.pathological_conditions.is_empty() {
            confidence += 0.1;
        }

        // Age group specified increases confidence
        if self.config.target_age_group.is_some() {
            confidence += 0.05;
        }

        confidence.min(1.0)
    }
}

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

    #[tokio::test]
    async fn test_elderly_pathological_evaluator_creation() {
        let evaluator = ElderlyPathologicalEvaluator::new().await.unwrap();
        assert_eq!(
            evaluator.config.target_age_group,
            Some(ElderlyAgeGroup::YoungElderly)
        );
    }

    #[tokio::test]
    async fn test_elderly_pathological_evaluation() {
        let evaluator = ElderlyPathologicalEvaluator::new().await.unwrap();

        // Create test audio
        let samples: Vec<f32> = (0..16000)
            .map(|i| (2.0 * std::f32::consts::PI * 150.0 * i as f32 / 16000.0).sin() * 0.3)
            .collect();
        let audio = AudioBuffer::new(samples, 16000, 1);

        let result = evaluator
            .evaluate_elderly_pathological_speech(&audio, None, Some("Hello world"))
            .await
            .unwrap();

        assert!(result.overall_score >= 0.0);
        assert!(result.overall_score <= 1.0);
        assert!(result.communication_effectiveness.overall_effectiveness >= 0.0);
        assert!(result.confidence >= 0.0);
    }

    #[test]
    fn test_age_group_characteristics() {
        let young_elderly = ElderlyAgeGroup::YoungElderly;
        let changes = young_elderly.expected_changes();

        assert_eq!(young_elderly.age_range(), (65, 74));
        assert!(changes.fundamental_frequency_change < 0.2);
        assert!(changes.voice_tremor_likelihood < 0.3);
    }

    #[test]
    fn test_pathological_condition_features() {
        let parkinsons = PathologicalCondition::Parkinsons;
        let features = parkinsons.characteristic_features();

        assert!(features.reduced_loudness > 0.5);
        assert!(features.monotone_speech > 0.5);
    }

    #[test]
    fn test_severity_level_conversion() {
        assert_eq!(SeverityLevel::from_score(0.95), SeverityLevel::Normal);
        assert_eq!(SeverityLevel::from_score(0.75), SeverityLevel::Mild);
        assert_eq!(SeverityLevel::from_score(0.55), SeverityLevel::Moderate);
        assert_eq!(SeverityLevel::from_score(0.35), SeverityLevel::Severe);
        assert_eq!(SeverityLevel::from_score(0.15), SeverityLevel::Profound);
    }

    #[test]
    fn test_listener_familiarity() {
        assert!(ListenerFamiliarity::VeryFamiliar.intelligibility_adjustment() > 1.0);
        assert!(ListenerFamiliarity::Unfamiliar.intelligibility_adjustment() < 1.0);
    }

    #[test]
    fn test_communication_context() {
        let clinical = CommunicationContext::Clinical;
        let expectations = clinical.expectations();

        assert!(expectations.intelligibility_requirement > 0.8);
        assert!(expectations.listener_patience > 0.8);
        assert!(expectations.background_noise < 0.2);
    }

    #[test]
    fn test_config_default() {
        let config = ElderlyPathologicalConfig::default();
        assert_eq!(config.target_age_group, Some(ElderlyAgeGroup::YoungElderly));
        assert!(config.perform_clinical_assessment);
        assert!(config.evaluate_assistive_technology);
    }

    #[tokio::test]
    async fn test_voice_quality_metrics() {
        let evaluator = ElderlyPathologicalEvaluator::new().await.unwrap();

        // Create test audio with some characteristics
        let samples: Vec<f32> = (0..16000)
            .map(|i| {
                let t = i as f32 / 16000.0;
                (2.0 * std::f32::consts::PI * 150.0 * t).sin() * 0.3
                    + (2.0 * std::f32::consts::PI * 75.0 * t).sin() * 0.1 // Add some noise
            })
            .collect();
        let audio = AudioBuffer::new(samples, 16000, 1);

        let voice_quality = evaluator
            .assess_voice_quality_metrics(&audio)
            .await
            .unwrap();

        assert!(voice_quality.f0_stability >= 0.0);
        assert!(voice_quality.f0_stability <= 1.0);
        assert!(voice_quality.jitter >= 0.0);
        assert!(voice_quality.shimmer >= 0.0);
        assert!(voice_quality.harmonic_noise_ratio >= 0.0);
    }

    #[tokio::test]
    async fn test_pathological_condition_evaluation() {
        let config = ElderlyPathologicalConfig {
            pathological_conditions: vec![PathologicalCondition::Parkinsons],
            ..Default::default()
        };
        let evaluator = ElderlyPathologicalEvaluator::with_config(config)
            .await
            .unwrap();

        let samples: Vec<f32> = (0..16000)
            .map(|i| (2.0 * std::f32::consts::PI * 120.0 * i as f32 / 16000.0).sin() * 0.2) // Quiet voice
            .collect();
        let audio = AudioBuffer::new(samples, 16000, 1);

        let result = evaluator
            .evaluate_elderly_pathological_speech(&audio, None, None)
            .await
            .unwrap();

        assert!(result
            .pathological_features
            .contains_key(&PathologicalCondition::Parkinsons));
        assert!(result.pathological_features[&PathologicalCondition::Parkinsons] >= 0.0);
    }
}