rust-tts-wrapper 1.4.0

Cross-platform TTS wrapper with C API — mirrors js-tts-wrapper / SwiftTTSWrapper
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
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
//! Sherpa-ONNX offline TTS engine with model registry.

use crate::boundaries::{EstimateFirer, EstimatePlan};
use crate::engine::TtsEngine;
use crate::types::{
    Gender, LanguageCode, SherpaLanguage, SherpaModelInfo, TtsError, TtsResult, Voice,
};
use std::collections::HashMap;
use std::fmt;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

/// Shared cancellation flag — set by `stop()`, read by the progress callback.
static CANCEL_REQUESTED: AtomicBool = AtomicBool::new(false);

// The sherpa-onnx generate callback must be 'static, but speak() holds the
// caller's on_audio/on_boundary borrows only for the method body. The C++
// runtime invokes the callback synchronously on the same thread inside the
// generate call, so we stash the callback pointers here for the duration
// (same technique as VISEME_CB in the cloud engine) and clear them after.
// Synthesis per engine instance is serialised by the tts_instance mutex.
type AudioCbPtr = *mut dyn FnMut(&[u8]);
type BoundaryCbPtr = *mut dyn FnMut(&str, f32, f32, i32, i32, bool);

thread_local! {
    static STREAM_AUDIO_CB: std::cell::RefCell<Option<AudioCbPtr>> =
        const { std::cell::RefCell::new(None) };
    static STREAM_BOUNDARY_CB: std::cell::RefCell<Option<BoundaryCbPtr>> =
        const { std::cell::RefCell::new(None) };
}

/// PCM delivery chunk size. Sentence batches arrive whole from the
/// generate progress callback and larger ones are sliced into 8 KB chunks
/// before pushing them through `on_audio` — matching the cloud engines'
/// streamed-chunk shape so callers see the same multi-callback delivery
/// instead of one giant buffer.
const STREAMING_CHUNK_SIZE: usize = 8 * 1024;

/// Maps a 2-letter ISO 639-1 code to its 3-letter ISO 639-3 equivalent for the
/// languages covered by the Sherpa-ONNX model registry. Falls back to the
/// input when no mapping is known.
fn iso639_3(lang_code: &str) -> String {
    let lower = lang_code.to_ascii_lowercase();
    let two = lower.split(['-', '_']).next().unwrap_or(&lower);
    let three = match two {
        "en" => "eng",
        "zh" => "zho",
        "de" => "deu",
        "fr" => "fra",
        "es" => "spa",
        "ru" => "rus",
        "ar" => "ara",
        "ko" => "kor",
        "ja" => "jpn",
        "it" => "ita",
        "pt" => "por",
        "pl" => "pol",
        "nl" => "nld",
        "tr" => "tur",
        "cs" => "ces",
        "uk" => "ukr",
        "vi" => "vie",
        "th" => "tha",
        "hi" => "hin",
        "bn" => "ben",
        "fa" => "fas",
        "hu" => "hun",
        "el" => "ell",
        "fi" => "fin",
        "sv" => "swe",
        "da" => "dan",
        "no" => "nor",
        "he" => "heb",
        "ms" => "msa",
        "id" => "ind",
        "ro" => "ron",
        "sk" => "slk",
        "bg" => "bul",
        "ca" => "cat",
        "hr" => "hrv",
        "lt" => "lit",
        "lv" => "lav",
        "sr" => "srp",
        "sl" => "slv",
        "et" => "est",
        "tl" => "tgl",
        _ => return lower,
    };
    three.to_string()
}

/// Offline TTS engine using [Sherpa-ONNX](https://github.com/k2-fsa/sherpa-onnx).
pub struct SherpaOnnxEngine {
    models: HashMap<String, SherpaModelInfo>,
    model_dir: PathBuf,
    loaded_model_id: String,
    num_threads: i32,
    /// Supertonic denoising step count (quality knob: 5 low → 12 high,
    /// default 8). Ignored by other model types.
    num_steps: i32,
    /// Optional user-supplied reference clip (path to a 16-bit PCM wav) for
    /// zero-shot voice-cloning models (zipvoice, pocket). When absent the
    /// first bundled `test_wavs/*.wav` is used.
    reference_audio: Option<PathBuf>,
    /// Transcript matching [`Self::reference_audio`] — required by zipvoice
    /// (its bundled test wavs have known transcripts). Ignored by pocket,
    /// which clones from audio alone.
    reference_text: Option<String>,
    provider: Option<String>,
    // Cached ONNX runtime instance. Recreating OfflineTts per speak() is
    // expensive (model loading + ONNX init). Cache it so the first speak()
    // pays the cost and subsequent calls reuse it.
    tts_instance: Mutex<Option<sherpa_onnx::OfflineTts>>,
}

impl fmt::Debug for SherpaOnnxEngine {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SherpaOnnxEngine")
            .field("loaded_model_id", &self.loaded_model_id)
            .field("num_threads", &self.num_threads)
            .field("num_steps", &self.num_steps)
            .field("provider", &self.provider)
            .field(
                "tts_cached",
                &self.tts_instance.lock().is_ok_and(|g| g.is_some()),
            )
            .finish_non_exhaustive()
    }
}

impl SherpaOnnxEngine {
    /// Create a new Sherpa-ONNX engine.
    ///
    /// Credentials JSON keys:
    /// - `modelPath`: directory containing downloaded models (defaults to
    ///   `~/.rust-tts-wrapper/sherpaonnx`)
    /// - `modelId`: id from the registry (e.g. `kokoro-en-v0_19`). Required —
    ///   if absent, no model is loaded and `speak` will return an error rather
    ///   than silently forcing a 305 MB download.
    /// - `numThreads`: ONNX runtime intra-op thread count (default 2).
    /// - `numSteps`: Supertonic denoising steps, 5–12 (default 8). Out-of-range
    ///   values fall back to 8. Ignored by non-Supertonic models.
    /// - `referenceAudio`: path to a 16-bit PCM wav used as the cloning
    ///   reference for zero-shot models (zipvoice, pocket). Defaults to the
    ///   model's bundled `test_wavs/` clip.
    /// - `referenceText`: transcript of `referenceAudio` (zipvoice requires
    ///   it to match the audio exactly; pocket ignores it).
    /// - `provider`: `cpu` (default), `coreml`, `cuda`, `directml`, etc.
    #[must_use]
    pub fn new(credentials_json: &str) -> Self {
        let mut model_dir = default_model_dir();
        let mut model_id = String::new();
        let mut num_threads = 2;
        let mut num_steps = 8;
        let mut provider: Option<String> = None;
        let mut reference_audio: Option<PathBuf> = None;
        let mut reference_text: Option<String> = None;

        if !credentials_json.is_empty() {
            // Numeric values are coerced to strings so callers can write
            // `{"numThreads": 2}` (JSON number) or `"2"` interchangeably —
            // a strict HashMap<String, String> parse would silently drop
            // the whole object on the first number and leave every option
            // at its default with no error.
            if let Ok(creds) =
                serde_json::from_str::<HashMap<String, serde_json::Value>>(credentials_json)
            {
                let creds: HashMap<String, String> = creds
                    .into_iter()
                    .map(|(k, v)| match v {
                        serde_json::Value::String(s) => (k, s),
                        other => (k, other.to_string()),
                    })
                    .collect();
                if let Some(dir) = creds.get("modelPath") {
                    model_dir = PathBuf::from(dir);
                }
                if let Some(id) = creds.get("modelId") {
                    model_id.clone_from(id);
                }
                if let Some(t) = creds.get("numThreads").and_then(|s| s.parse::<i32>().ok()) {
                    if t > 0 {
                        num_threads = t;
                    }
                }
                if let Some(s) = creds.get("numSteps").and_then(|s| s.parse::<i32>().ok()) {
                    // Clamp to Supertonic's supported quality range. Values
                    // outside 5–12 degrade output or waste compute, so fall
                    // back to the default rather than passing them through.
                    if (5..=12).contains(&s) {
                        num_steps = s;
                    }
                }
                if let Some(p) = creds.get("provider") {
                    if !p.is_empty() {
                        provider = Some(p.clone());
                    }
                }
                if let Some(r) = creds.get("referenceAudio") {
                    if !r.is_empty() {
                        reference_audio = Some(PathBuf::from(r));
                    }
                }
                if let Some(t) = creds.get("referenceText") {
                    if !t.is_empty() {
                        reference_text = Some(t.clone());
                    }
                }
            }
        }

        let models = load_models();

        SherpaOnnxEngine {
            models,
            model_dir,
            loaded_model_id: model_id,
            num_threads,
            num_steps,
            provider,
            reference_audio,
            reference_text,
            tts_instance: Mutex::new(None),
        }
    }

    /// Return the map of available models from the registry.
    pub fn available_models(&self) -> &HashMap<String, SherpaModelInfo> {
        &self.models
    }

    /// Resolve the reference clip for zero-shot cloning models: the
    /// user-supplied `referenceAudio` credentials path wins, else the first
    /// wav bundled under the model's `test_wavs/`.
    fn resolve_reference_audio(&self, model_dir: &std::path::Path) -> TtsResult<(Vec<f32>, i32)> {
        let wav = match &self.reference_audio {
            Some(p) => p.clone(),
            None => bundled_reference_wav(model_dir).ok_or_else(|| {
                TtsError(format!(
                    "no reference audio for zero-shot cloning: pass a 'referenceAudio' \
                     path in credentials, or bundle a wav under {}/test_wavs/",
                    model_dir.display()
                ))
            })?,
        };
        read_wav_mono_16bit(&wav)
    }

    /// Resolve the reference transcript (zipvoice only): the
    /// user-supplied `referenceText` credential wins, else the known
    /// transcript for a bundled sherpa-onnx test wav.
    fn resolve_reference_text(&self, model_dir: &std::path::Path) -> TtsResult<String> {
        if let Some(t) = &self.reference_text {
            return Ok(t.clone());
        }
        let wav = self
            .reference_audio
            .clone()
            .or_else(|| bundled_reference_wav(model_dir));
        if let Some(name) = wav
            .as_ref()
            .and_then(|p| p.file_name())
            .and_then(|n| n.to_str())
        {
            if let Some((_, text)) = ZIPVOICE_TEST_WAV_TRANSCRIPTS
                .iter()
                .find(|(known, _)| *known == name)
            {
                return Ok(text.to_string());
            }
        }
        Err(TtsError(
            "zipvoice requires the reference clip's exact transcript: pass \
             'referenceText' in credentials (it must match 'referenceAudio' \
             verbatim — a mismatch audibly degrades the clone)"
                .into(),
        ))
    }
}

/// Default directory for downloaded Sherpa-ONNX models.
fn default_model_dir() -> PathBuf {
    let home = std::env::var("HOME")
        .or_else(|_| std::env::var("USERPROFILE"))
        .unwrap_or_else(|_| ".".into());
    let mut dir = PathBuf::from(home);
    dir.push(".rust-tts-wrapper");
    dir.push("sherpaonnx");
    dir
}

/// Parse the embedded `models.json` into a hashmap.
fn load_models() -> HashMap<String, SherpaModelInfo> {
    // Convert the typed registry crate into the wrapper's public
    // SherpaModelInfo shape (API-compatible with the old embedded copy).
    sherpa_onnx_models::models()
        .iter()
        .map(|(id, m)| {
            (
                id.clone(),
                SherpaModelInfo {
                    id: m.id.clone(),
                    model_type: m.model_type.clone(),
                    engines: m.engines.clone(),
                    name: m.name.clone(),
                    language: m
                        .language
                        .iter()
                        .map(|l| SherpaLanguage {
                            lang_code: l.lang_code.clone(),
                            language_name: l.language_name.clone(),
                            country: l.country.clone(),
                        })
                        .collect(),
                    sample_rate: m.sample_rate,
                    num_speakers: m.num_speakers,
                    quality: m.quality.clone(),
                    url: m.url.clone(),
                    compression: m.compression,
                    filesize_mb: m.filesize_mb,
                    license: m.license.clone(),
                    license_url: m.license_url.clone(),
                },
            )
        })
        .collect()
}

impl TtsEngine for SherpaOnnxEngine {
    #[allow(clippy::too_many_lines)]
    fn speak(
        &self,
        text: &str,
        voice: Option<&str>,
        rate: f32,
        pitch: f32,
        volume: f32,
        mut on_audio: Option<crate::engine::OnAudioCallback>,
        mut on_boundary: Option<crate::engine::OnBoundaryCallback>,
        _on_mark: Option<crate::engine::OnMarkCallback>,
    ) -> TtsResult<()> {
        if self.loaded_model_id.is_empty() {
            return Err(TtsError(
                "No SherpaOnnx modelId configured. Pass modelId in credentials JSON. \
                 See available_models() for the registry."
                    .into(),
            ));
        }

        let model_info = self.models.get(&self.loaded_model_id).ok_or_else(|| {
            TtsError(format!(
                "Model '{}' not found in registry ({} models available)",
                self.loaded_model_id,
                self.models.len()
            ))
        })?;

        let model_dir = self.model_dir.join(&self.loaded_model_id);
        if !model_dir.exists() {
            return Err(TtsError(format!(
                "Model directory not found: {}. Download from: {}",
                model_dir.display(),
                model_info.url
            )));
        }

        // Dispatch model config by model_type. The branches below mirror the
        // file-layout conventions used by js-tts-wrapper and dotnet-tts-wrapper:
        //
        //   kokoro  → model.onnx + voices.bin + tokens.txt + espeak-ng-data/
        //   matcha  → acoustic-model.onnx + vocoder.onnx + tokens.txt
        //             (vocoder may be hifigan_v2.onnx, vocos-22khz-univ.onnx,
        //              or live in a shared base dir)
        //   vits    → model.onnx + tokens.txt + (lexicon.txt | espeak-ng-data/)
        //             Piper / GitHub models prefer espeak-ng-data and ignore
        //             dict_dir; Chinese models want a dict/ directory for
        //             jieba segmentation.
        //   zipvoice→ encoder/decoder .onnx (int8 variants preferred) +
        //             tokens.txt + lexicon.txt + espeak-ng-data/ + a shared
        //             vocos_24khz.onnx vocoder (base dir). Zero-shot cloning:
        //             speak() needs a reference clip + its transcript.
        //   pocket  → lm_flow/lm_main/encoder/decoder/text_conditioner .onnx
        //             (int8 where shipped) + vocab.json + token_scores.json.
        //             Zero-shot cloning from a reference clip alone.
        //   mms /   → MMS models use the VITS config but typically have no
        //   unknown   espeak-ng-data; they ship just model.onnx + tokens.txt
        //             + lexicon.txt.
        //
        // The registry has ~1143 MMS entries that omit
        // `model_type`, so empty/unknown falls through to VITS handling.
        let id_lower = self.loaded_model_id.to_ascii_lowercase();
        let is_piper_or_github = is_piper_or_github_model(&id_lower);
        let is_chinese = id_lower.starts_with("vits-icefall-zh")
            || id_lower.contains("cantonese")
            || id_lower.starts_with("mms_zho")
            || id_lower.starts_with("mms_cmn");

        // Piper and GitHub archives often extract to a nested subdirectory
        // (e.g. vits-piper-en_US-amy-low/en_US-amy-low.onnx). If the model
        // dir has no top-level model files, descend into the single child
        // directory (mirrors VoiceGarden's ResolveModelScanDir).
        let model_dir = resolve_model_scan_dir(&model_dir);

        let model_config = match model_info.model_type.as_str() {
            "kokoro" => sherpa_onnx::OfflineTtsModelConfig {
                kokoro: build_kokoro_config(&model_dir),
                num_threads: self.num_threads,
                debug: false,
                provider: self.provider.clone(),
                ..Default::default()
            },
            "matcha" => sherpa_onnx::OfflineTtsModelConfig {
                matcha: build_matcha_config(&model_dir, &self.model_dir)?,
                num_threads: self.num_threads,
                debug: false,
                provider: self.provider.clone(),
                ..Default::default()
            },
            "kitten" => sherpa_onnx::OfflineTtsModelConfig {
                kitten: sherpa_onnx::OfflineTtsKittenModelConfig {
                    model: Some(model_dir.join("model.onnx").to_string_lossy().to_string()),
                    voices: Some(model_dir.join("voices.bin").to_string_lossy().to_string()),
                    tokens: Some(model_dir.join("tokens.txt").to_string_lossy().to_string()),
                    data_dir: existing_path(&model_dir, "espeak-ng-data"),
                    ..Default::default()
                },
                num_threads: self.num_threads,
                debug: false,
                provider: self.provider.clone(),
                ..Default::default()
            },
            "supertonic" => sherpa_onnx::OfflineTtsModelConfig {
                supertonic: build_supertonic_config(&model_dir),
                num_threads: self.num_threads,
                debug: false,
                provider: self.provider.clone(),
                ..Default::default()
            },
            "zipvoice" => sherpa_onnx::OfflineTtsModelConfig {
                zipvoice: build_zipvoice_config(&model_dir, &self.model_dir)?,
                num_threads: self.num_threads,
                debug: false,
                provider: self.provider.clone(),
                ..Default::default()
            },
            "pocket" => sherpa_onnx::OfflineTtsModelConfig {
                pocket: build_pocket_config(&model_dir),
                num_threads: self.num_threads,
                debug: false,
                provider: self.provider.clone(),
                ..Default::default()
            },
            // VITS, MMS (Facebook Massively Multilingual Speech), and unknown
            // model types all use the VITS config family.
            "vits" | "mms" | "unknown" | "" => sherpa_onnx::OfflineTtsModelConfig {
                vits: build_vits_config(&model_dir, is_piper_or_github, is_chinese),
                num_threads: self.num_threads,
                debug: false,
                provider: self.provider.clone(),
                ..Default::default()
            },
            other => {
                return Err(TtsError(format!(
                    "Unsupported SherpaOnnx model_type '{other}' for model '{}'",
                    self.loaded_model_id
                )));
            }
        };

        let config = sherpa_onnx::OfflineTtsConfig {
            model: model_config,
            // Single-sentence mode matches the reference implementations and
            // avoids extra allocations when the input is short.
            max_num_sentences: 1,
            ..Default::default()
        };

        // Use cached OfflineTts instance if available; create on first call.
        // The Mutex guards the Option; we hold it for the entire synthesis
        // since OfflineTts::generate_with_config needs &self.
        let mut tts_guard = self.tts_instance.lock().unwrap();
        if tts_guard.is_none() {
            let tts = sherpa_onnx::OfflineTts::create(&config)
                .ok_or_else(|| TtsError("Failed to create SherpaOnnx TTS engine".into()))?;
            *tts_guard = Some(tts);
        }
        let tts = tts_guard.as_ref().expect("tts was just initialised");

        let sid = voice.and_then(|v| v.parse::<i32>().ok()).unwrap_or(0);
        // Supertonic needs both a speaker id and a language; the language is
        // delivered through GenerationConfig.extra. We encode both in the
        // voice id as "sid:lang" (produced by get_voices), and fall back to a
        // bare integer + default "en" for backwards compatibility.
        //
        // Zipvoice / pocket are zero-shot cloning models: instead of a preset
        // speaker they need a reference clip (and, for zipvoice, its exact
        // transcript) delivered through GenerationConfig.
        let gen_config = if model_info.model_type == "supertonic" {
            let (sid, lang) = parse_supertonic_voice(voice);
            let mut extra = HashMap::new();
            extra.insert("lang".to_string(), serde_json::Value::String(lang));
            sherpa_onnx::GenerationConfig {
                sid,
                speed: rate.max(0.1),
                num_steps: self.num_steps,
                extra: Some(extra),
                ..Default::default()
            }
        } else if model_info.model_type == "zipvoice" || model_info.model_type == "pocket" {
            let (ref_samples, ref_rate) = self.resolve_reference_audio(&model_dir)?;
            let reference_text = if model_info.model_type == "zipvoice" {
                Some(self.resolve_reference_text(&model_dir)?)
            } else {
                None
            };
            // Flow-matching step counts from the sherpa-onnx docs: zipvoice
            // defaults to 4, pocket to 2 (higher = better quality, slower).
            let num_steps = if model_info.model_type == "zipvoice" {
                4
            } else {
                2
            };
            sherpa_onnx::GenerationConfig {
                speed: rate.max(0.1),
                num_steps,
                reference_audio: Some(ref_samples),
                reference_sample_rate: ref_rate,
                reference_text,
                ..Default::default()
            }
        } else {
            sherpa_onnx::GenerationConfig {
                sid,
                speed: rate.max(0.1),
                ..Default::default()
            }
        };

        let volume_factor = volume.clamp(0.0, 4.0);
        let pitch_factor = pitch.clamp(0.25, 4.0);
        let wants_callback = on_audio.is_some();

        // Reset cancellation flag before synthesis.
        CANCEL_REQUESTED.store(false, Ordering::SeqCst);

        // Streaming delivery: sherpa-onnx's generate callback fires after
        // each sentence batch (max_num_sentences=1 above) with the batch's
        // NEWLY generated samples — not cumulative — so audio is delivered
        // through `on_audio` while later sentences are still synthesising.
        // Volume/pitch are applied per batch (batches are sentence-aligned,
        // so per-batch resampling for pitch doesn't seam mid-speech).
        //
        // Word-boundary estimates (150-wpm baseline) fire progressively,
        // anchored to delivered samples and scaled by 1/speed so they track
        // the audio actually emitted; the reported times stay on the
        // rate-1.0 baseline (existing callers, e.g. VoiceGarden-SPD and the
        // SAPI adapter, compensate for rate themselves).
        //
        // The generate callback must be `'static`, so it cannot capture the
        // method-lifetime `on_audio`/`on_boundary` borrows directly. The C++
        // runtime invokes the callback synchronously on this same thread
        // while the borrows are live, so the pointers are stashed in
        // thread-locals for the duration of the call (the same technique as
        // VISEME_CB above) and cleared afterwards.
        #[allow(clippy::cast_precision_loss)]
        let time_scale = 1.0 / rate.max(0.1);
        let sample_rate_out = model_info.sample_rate.max(1);

        if wants_callback {
            let plan = on_boundary.is_some().then(|| EstimatePlan::build(text));

            // SAFETY (stash): the raw pointers are only dereferenced inside
            // the generate callback, which sherpa-onnx calls synchronously
            // on this thread between the stash and the clear below, while
            // `on_audio`/`on_boundary` are still borrowed. Synthesis on a
            // given engine instance is serialised by the `tts_instance`
            // mutex held for the whole call.
            // ptr→ptr transmute + explicit borrow-to-pointer are the
            // standard lifetime-erasure idioms for synchronous callback
            // stashing; the safety argument is documented above.
            #[allow(clippy::transmute_ptr_to_ptr)]
            let audio_ptr: Option<AudioCbPtr> = on_audio.as_mut().map(|cb| {
                // SAFETY: the fat pointer's layout is identical; only the
                // lifetime is erased. See the thread-local docs for why
                // use is confined to this call.
                unsafe {
                    std::mem::transmute::<*mut (dyn FnMut(&[u8]) + '_), AudioCbPtr>(
                        std::ptr::from_mut(&mut **cb),
                    )
                }
            });
            #[allow(clippy::transmute_ptr_to_ptr)]
            let boundary_ptr: Option<BoundaryCbPtr> = on_boundary.as_mut().map(|cb| {
                // SAFETY: as above.
                unsafe {
                    std::mem::transmute::<
                        *mut (dyn FnMut(&str, f32, f32, i32, i32, bool) + '_),
                        BoundaryCbPtr,
                    >(std::ptr::from_mut(&mut **cb))
                }
            });
            STREAM_AUDIO_CB.with(|c| *c.borrow_mut() = audio_ptr);
            STREAM_BOUNDARY_CB.with(|c| *c.borrow_mut() = boundary_ptr);

            // The firer owns the plan and is shared with the 'static
            // callback via Arc<Mutex<..>>, so the outer scope can flush
            // the remainder after generation ends.
            let firer = plan.map(|p| Arc::new(Mutex::new(EstimateFirer::new(p, time_scale))));
            let firer_for_cb = firer.clone();

            // A `None` result means the engine rejected the config outright;
            // cancellation mid-stream still returns Some(audio-so-far),
            // which we ignore — every batch was already streamed.
            let result = tts.generate_with_config(
                text,
                &gen_config,
                Some(move |batch: &[f32], _progress: f32| -> bool {
                    if CANCEL_REQUESTED.load(Ordering::SeqCst) {
                        return false;
                    }
                    let processed = apply_volume_and_pitch(batch, volume_factor, pitch_factor);
                    let pcm = samples_to_le_bytes(&processed);
                    STREAM_AUDIO_CB.with(|c| {
                        if let Some(ptr) = *c.borrow() {
                            // SAFETY (stash): see the thread-local docs.
                            // Chunk to keep the documented 8 KB multi-
                            // callback delivery shape.
                            for chunk in pcm.chunks(STREAMING_CHUNK_SIZE) {
                                unsafe { (*ptr)(chunk) };
                            }
                        }
                    });
                    if let Some(f) = firer_for_cb.as_ref() {
                        if let Ok(mut guard) = f.lock() {
                            guard.on_samples(
                                batch.len() as u64,
                                Some(sample_rate_out),
                                &mut |ev| {
                                    STREAM_BOUNDARY_CB.with(|c| {
                                        if let Some(ptr) = *c.borrow() {
                                            // SAFETY (stash): see above.
                                            unsafe {
                                                (*ptr)(
                                                    &ev.word,
                                                    ev.start_s,
                                                    ev.end_s,
                                                    ev.char_offset,
                                                    ev.char_len,
                                                    true, // wpm estimates
                                                );
                                            }
                                        }
                                    });
                                },
                            );
                        }
                    }
                    !CANCEL_REQUESTED.load(Ordering::SeqCst)
                }),
            );

            STREAM_AUDIO_CB.with(|c| *c.borrow_mut() = None);
            STREAM_BOUNDARY_CB.with(|c| *c.borrow_mut() = None);

            if result.is_none() {
                return Err(TtsError("SherpaOnnx synthesis returned no audio".into()));
            }

            // Fire any estimates whose audio never arrived (short audio,
            // cancellation): the boundary set is now closed.
            if let (Some(f), Some(cb)) = (firer.as_ref(), on_boundary.as_mut()) {
                if let Ok(mut f) = f.lock() {
                    f.flush(&mut |ev| {
                        cb(
                            &ev.word,
                            ev.start_s,
                            ev.end_s,
                            ev.char_offset,
                            ev.char_len,
                            true,
                        );
                    });
                }
            }
        } else {
            let audio = tts
                .generate_with_config(
                    text,
                    &gen_config,
                    Some(|_s: &[f32], _p: f32| -> bool {
                        !CANCEL_REQUESTED.load(Ordering::SeqCst)
                    }),
                )
                .ok_or_else(|| TtsError("SherpaOnnx synthesis returned no audio".into()))?;
            let processed = apply_volume_and_pitch(audio.samples(), volume_factor, pitch_factor);
            let filename = std::env::temp_dir().join("rust-tts-wrapper-sherpa.wav");
            if write_wav(&filename, &processed, audio.sample_rate()) {
                play_wav_file(&filename);
            }

            // No streaming took place; estimates fire as before.
            if let Some(cb) = on_boundary.as_mut() {
                let plan = EstimatePlan::build(text);
                for i in 0..plan.len() {
                    let ev = plan.event(i).expect("in range");
                    cb(
                        &ev.word,
                        ev.start_s,
                        ev.end_s,
                        ev.char_offset,
                        ev.char_len,
                        true,
                    );
                }
            }
        }

        Ok(())
    }

    fn speak_sync(
        &self,
        text: &str,
        voice: Option<&str>,
        rate: f32,
        pitch: f32,
        volume: f32,
        on_audio: Option<crate::engine::OnAudioCallback>,
        on_boundary: Option<crate::engine::OnBoundaryCallback>,
        on_mark: Option<crate::engine::OnMarkCallback>,
    ) -> TtsResult<()> {
        self.speak(
            text,
            voice,
            rate,
            pitch,
            volume,
            on_audio,
            on_boundary,
            on_mark,
        )
    }

    fn stop(&self) -> TtsResult<()> {
        // The progress callback reads this flag on every chunk and aborts
        // synthesis when set.
        CANCEL_REQUESTED.store(true, Ordering::SeqCst);
        Ok(())
    }

    fn get_voices(&self) -> TtsResult<Vec<Voice>> {
        let model_info = self.models.get(&self.loaded_model_id);

        // Supertonic is addressed by (speaker, language) rather than a bare
        // speaker id. Emit one voice per speaker × language pair, encoding
        // both in the id as "sid:lang" so speak() can route the language
        // through GenerationConfig.extra.
        if let Some(info) = model_info {
            if info.model_type == "supertonic" {
                return Ok(supertonic_voices(info));
            }
        }

        let num_speakers = model_info.map_or(1, |m| m.num_speakers);
        let lang = model_info
            .and_then(|m| m.language.first())
            .map(|l| l.language_name.clone())
            .unwrap_or_default();
        let lang_code = model_info
            .and_then(|m| m.language.first())
            .map(|l| l.lang_code.clone())
            .unwrap_or_default();
        let iso639 = iso639_3(&lang_code);
        let mut voices = Vec::new();
        for i in 0..num_speakers {
            voices.push(Voice {
                id: format!("{i}"),
                name: format!("Speaker {i}"),
                gender: Gender::Unknown,
                provider: "sherpaonnx".to_string(),
                language_codes: vec![LanguageCode {
                    bcp47: lang.clone(),
                    iso639_3: iso639.clone(),
                    display: crate::types::locale_display_name(&lang),
                }],
            });
        }
        Ok(voices)
    }

    fn engine_id(&self) -> &'static str {
        "sherpaonnx"
    }
}

/// Apply volume scaling and pitch shifting to a buffer of f32 samples.
///
/// Volume is a straightforward linear scale. Pitch shifting uses simple
/// linear-interpolation resampling — it does change duration slightly, but it
/// is the cheapest DSP approach that doesn't pull in an FFT dependency. The
/// shift is a no-op when both factors are 1.0.
fn apply_volume_and_pitch(samples: &[f32], volume: f32, pitch: f32) -> Vec<f32> {
    if samples.is_empty() {
        return Vec::new();
    }
    // First resample for pitch (changes length).
    let resampled: Vec<f32> = if (pitch - 1.0).abs() > f32::EPSILON {
        #[allow(clippy::cast_precision_loss)]
        let out_len = ((samples.len() as f32) / pitch).round().max(1.0) as usize;
        let mut out = Vec::with_capacity(out_len);
        #[allow(clippy::cast_precision_loss)]
        let step = (samples.len() as f32) / out_len as f32;
        let mut idx = 0.0f32;
        while (idx as usize) < samples.len() {
            let i = idx as usize;
            #[allow(clippy::cast_precision_loss)]
            let frac = idx - i as f32;
            let next = samples.get(i + 1).copied().unwrap_or(samples[i]);
            let v = samples[i] * (1.0 - frac) + next * frac;
            out.push(v);
            idx += step;
        }
        out
    } else {
        samples.to_vec()
    };
    // Then scale amplitude for volume.
    if (volume - 1.0).abs() > f32::EPSILON {
        resampled.iter().map(|&s| s * volume).collect()
    } else {
        resampled
    }
}

/// Convert f32 samples to little-endian PCM16 bytes (one allocation).
#[allow(clippy::cast_possible_truncation)]
fn samples_to_le_bytes(samples: &[f32]) -> Vec<u8> {
    let mut pcm = Vec::with_capacity(samples.len() * 2);
    for &s in samples {
        let s16 = (s.clamp(-1.0, 1.0) * 32767.0) as i16;
        pcm.extend_from_slice(&s16.to_le_bytes());
    }
    pcm
}

/// Write a 16-bit PCM mono WAV file. Returns `false` on I/O error.
fn write_wav(path: &std::path::Path, samples: &[f32], sample_rate: i32) -> bool {
    use std::io::Write;
    let Ok(mut f) = std::fs::File::create(path) else {
        return false;
    };
    let mut pcm = Vec::with_capacity(samples.len() * 2);
    for &s in samples {
        let s16 = (s.clamp(-1.0, 1.0) * 32767.0) as i16;
        pcm.extend_from_slice(&s16.to_ne_bytes());
    }
    let data_len = pcm.len() as u32;
    let sample_rate = sample_rate as u32;
    let byte_rate = sample_rate * 2; // 16-bit mono
                                     // WAV PCM header: block_align and bits_per_sample are u16, NOT u32 —
                                     // emitting them as 4 bytes shifts every subsequent field by 2 bytes
                                     // and produces a technically malformed header (most players tolerate
                                     // it but it's wrong per the RIFF spec).
    let block_align: u16 = 2;
    let bits_per_sample: u16 = 16;
    let riff_len = 36 + data_len;
    let header = [
        b"RIFF".as_slice(),
        &riff_len.to_le_bytes(),
        b"WAVE",
        b"fmt ",
        &16u32.to_le_bytes(), // PCM chunk size
        &1u16.to_le_bytes(),  // PCM format
        &1u16.to_le_bytes(),  // mono
        &sample_rate.to_le_bytes(),
        &byte_rate.to_le_bytes(),
        &block_align.to_le_bytes(),
        &bits_per_sample.to_le_bytes(),
        b"data",
        &data_len.to_le_bytes(),
    ]
    .concat();
    if f.write_all(&header).is_err() || f.write_all(&pcm).is_err() {
        return false;
    }
    true
}

/// Play a WAV file using a platform-appropriate command.
///
/// - Linux: `aplay`
/// - macOS: `afplay`
/// - Windows: PowerShell `(New-Object Media.SoundPlayer).PlaySync()`
///
/// Failures are swallowed because playback is best-effort (audio has already
/// been rendered to a file the caller can locate).
fn play_wav_file(path: &std::path::Path) {
    let result = if cfg!(target_os = "linux") {
        std::process::Command::new("aplay")
            .arg("-q")
            .arg(path)
            .spawn()
            .map(|mut c| c.wait())
    } else if cfg!(target_os = "macos") {
        std::process::Command::new("afplay")
            .arg(path)
            .spawn()
            .map(|mut c| c.wait())
    } else if cfg!(target_os = "windows") {
        let p = path.to_string_lossy().replace('\'', "''");
        std::process::Command::new("powershell")
            .args([
                "-NoProfile",
                "-Command",
                &format!("(New-Object Media.SoundPlayer '{p}').PlaySync()"),
            ])
            .spawn()
            .map(|mut c| c.wait())
    } else {
        return;
    };
    let _ = result;
}

// ===== Model-config builders =====
//
// These helpers encapsulate the file-layout differences between Kokoro,
// Matcha, and the various VITS flavours (Piper, MMS, Coqui, Chinese, ...).
// They mirror the per-model logic in js-tts-wrapper / dotnet-tts-wrapper.

/// If `dir` has no top-level model files but has exactly one subdirectory,
/// return that subdirectory. Mirrors VoiceGarden's `ResolveModelScanDir`.
fn resolve_model_scan_dir(dir: &std::path::Path) -> std::path::PathBuf {
    let has_top = dir.join("tokens.txt").exists()
        || dir.join("model.onnx").exists()
        || dir.join("voices.bin").exists()
        || dir.join("espeak-ng-data").exists()
        || std::fs::read_dir(dir).is_ok_and(|entries| {
            entries
                .filter_map(Result::ok)
                .any(|e| e.path().extension().is_some_and(|ext| ext == "onnx"))
        });
    if has_top {
        return dir.to_path_buf();
    }
    // No top-level files — look for a single subdirectory.
    if let Ok(entries) = std::fs::read_dir(dir) {
        let subdirs: Vec<_> = entries
            .filter_map(Result::ok)
            .filter(|e| e.path().is_dir())
            .collect();
        if subdirs.len() == 1 {
            return subdirs[0].path();
        }
    }
    dir.to_path_buf()
}

/// Find the primary model .onnx in a directory. Prefers `model.onnx`,
/// then falls back to the first .onnx that isn't an acoustic model or
/// vocoder. Mirrors VoiceGarden's `FindPrimaryModelOnnx`.
fn find_primary_model_onnx(dir: &std::path::Path) -> Option<std::path::PathBuf> {
    let model_onnx = dir.join("model.onnx");
    if model_onnx.exists() {
        return Some(model_onnx);
    }
    std::fs::read_dir(dir)
        .ok()?
        .filter_map(Result::ok)
        .find_map(|entry| {
            let path = entry.path();
            if path
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("onnx"))
            {
                let name = path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .map(str::to_ascii_lowercase)
                    .unwrap_or_default();
                // Skip acoustic models and vocoders.
                if !name.starts_with("model-steps")
                    && !name.starts_with("vocos")
                    && !name.starts_with("vocoder")
                    && !name.starts_with("hifigan")
                {
                    return Some(path);
                }
            }
            None
        })
}

/// Return `Some(path)` only when `dir/name` exists on disk; otherwise `None`.
fn existing_path(dir: &std::path::Path, name: &str) -> Option<String> {
    let p = dir.join(name);
    if p.exists() {
        Some(p.to_string_lossy().to_string())
    } else {
        None
    }
}

/// Walk `dir` and return the path of the first child matching `name`, if any.
fn find_file(dir: &std::path::Path, name: &str) -> Option<std::path::PathBuf> {
    std::fs::read_dir(dir)
        .ok()?
        .filter_map(Result::ok)
        .find_map(|entry| {
            let path = entry.path();
            if path.file_name().is_some_and(|n| n == name) {
                Some(path)
            } else {
                None
            }
        })
}

/// Return the first existing file under `dir` matching one of `names`.
fn first_existing(dir: &std::path::Path, names: &[&str]) -> Option<std::path::PathBuf> {
    names.iter().map(|n| dir.join(n)).find(|p| p.exists())
}

/// Heuristic: is this a Piper voice or another "GitHub-style" archive model
/// (Coqui / icefall / mimic3 / melo / vctk / ljs / cantonese / zh / kokoro)?
/// These layouts ship `espeak-ng-data/` rather than a lexicon and shouldn't
/// be configured with `dict_dir` (jieba would otherwise warn on every call).
fn is_piper_or_github_model(model_id: &str) -> bool {
    const PREFIXES: &[&str] = &[
        "piper-",
        "coqui-",
        "icefall-",
        "mimic3-",
        "melo-",
        "vctk-",
        "zh-",
        "ljs-",
        "cantonese-",
        "kokoro-",
    ];
    PREFIXES.iter().any(|p| model_id.starts_with(p))
}

/// Collect lexicon paths for a Kokoro model. Multilingual releases (e.g.
/// `kokoro-zh_en-int8`) ship several `lexicon-*.txt` files which
/// sherpa-onnx accepts as a comma-separated list; English-only Kokoro has
/// none and relies on `espeak-ng-data/` instead. Returns `None` when no
/// lexicon files are present so the field is left unset.
fn collect_lexicons(dir: &std::path::Path) -> Option<String> {
    let mut paths: Vec<std::path::PathBuf> = std::fs::read_dir(dir)
        .ok()?
        .filter_map(Result::ok)
        .map(|e| e.path())
        .filter(|p| {
            p.is_file()
                && p.extension()
                    .is_some_and(|ext| ext.eq_ignore_ascii_case("txt"))
                && p.file_name()
                    .and_then(|n| n.to_str())
                    .is_some_and(|n| n == "lexicon.txt" || n.starts_with("lexicon-"))
        })
        .collect();
    if paths.is_empty() {
        return None;
    }
    // Sort for deterministic ordering across platforms / readdir order.
    paths.sort();
    Some(
        paths
            .iter()
            .map(|p| p.to_string_lossy().to_string())
            .collect::<Vec<_>>()
            .join(","),
    )
}

/// Locate the dictionary directory for Chinese text normalisation. Kokoro zh
/// models keep `*.fst` files (date-zh.fst, number-zh.fst, phone-zh.fst) at the
/// model root; some VITS models nest a `dict/` folder. Returns `None` when no
/// FST/dict layout is present.
fn find_dict_dir(dir: &std::path::Path) -> Option<String> {
    let dict_sub = dir.join("dict");
    if dict_sub.is_dir() {
        return Some(dict_sub.to_string_lossy().to_string());
    }
    let has_fst = std::fs::read_dir(dir)
        .ok()?
        .filter_map(Result::ok)
        .any(|e| {
            e.path()
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("fst"))
        });
    if has_fst {
        Some(dir.to_string_lossy().to_string())
    } else {
        None
    }
}

/// Kokoro config: model.onnx + voices.bin + tokens.txt + (espeak-ng-data/ |
/// lexicon-*.txt + dict_dir). Quantised releases ship `model.int8.onnx`;
/// `find_primary_model_onnx` resolves both layouts.
fn build_kokoro_config(model_dir: &std::path::Path) -> sherpa_onnx::OfflineTtsKokoroModelConfig {
    // Prefer the canonical model.onnx; fall back to a directory scan that
    // skips vocoders and matcha acoustic models (handles model.int8.onnx).
    let model = find_primary_model_onnx(model_dir).unwrap_or_else(|| model_dir.join("model.onnx"));
    sherpa_onnx::OfflineTtsKokoroModelConfig {
        model: Some(model.to_string_lossy().to_string()),
        voices: existing_path(model_dir, "voices.bin"),
        tokens: Some(model_dir.join("tokens.txt").to_string_lossy().to_string()),
        data_dir: existing_path(model_dir, "espeak-ng-data"),
        // Multilingual Kokoro (e.g. zh_en) ships lexicon-*.txt and Chinese FST
        // normalisation; English-only Kokoro uses espeak-ng-data instead, in
        // which case these resolve to None.
        lexicon: collect_lexicons(model_dir),
        dict_dir: find_dict_dir(model_dir),
        // length_scale left at default — rate is applied via GenerationConfig.speed.
        ..Default::default()
    }
}

/// Supertonic config: four ONNX sessions (`duration_predictor`,
/// `text_encoder`, `vector_estimator`, `vocoder`) plus `tts.json`,
/// `unicode_indexer.bin`, and `voice.bin`. The sherpa-onnx release ships
/// int8-quantised onnx files (`<name>.int8.onnx`); fall back to the
/// unquantised `<name>.onnx` if the int8 variant is absent. Missing files
/// surface as `None` and sherpa-onnx will fail at `OfflineTts::create` with a
/// clear message — matching how the Kokoro builder handles an absent
/// `voices.bin`.
fn build_supertonic_config(
    model_dir: &std::path::Path,
) -> sherpa_onnx::OfflineTtsSupertonicModelConfig {
    sherpa_onnx::OfflineTtsSupertonicModelConfig {
        duration_predictor: first_existing(
            model_dir,
            &["duration_predictor.int8.onnx", "duration_predictor.onnx"],
        )
        .map(|p| p.to_string_lossy().to_string()),
        text_encoder: first_existing(model_dir, &["text_encoder.int8.onnx", "text_encoder.onnx"])
            .map(|p| p.to_string_lossy().to_string()),
        vector_estimator: first_existing(
            model_dir,
            &["vector_estimator.int8.onnx", "vector_estimator.onnx"],
        )
        .map(|p| p.to_string_lossy().to_string()),
        vocoder: first_existing(model_dir, &["vocoder.int8.onnx", "vocoder.onnx"])
            .map(|p| p.to_string_lossy().to_string()),
        tts_json: existing_path(model_dir, "tts.json"),
        unicode_indexer: existing_path(model_dir, "unicode_indexer.bin"),
        voice_style: existing_path(model_dir, "voice.bin"),
    }
}

/// Zipvoice config: encoder + decoder (int8 variants preferred) + tokens +
/// lexicon + espeak-ng-data, plus the shared `vocos_24khz.onnx` vocoder.
///
/// The vocoder is *not* bundled in the zipvoice archive — it lives in the
/// separate `vocoder-models` release, so it's resolved from the model dir
/// first (custom layouts) then the user's base model dir (the same
/// shared-vocoder convention as Matcha). A missing vocoder is a hard error
/// with the download URL rather than a sherpa-onnx create() panic.
fn build_zipvoice_config(
    model_dir: &std::path::Path,
    base_dir: &std::path::Path,
) -> TtsResult<sherpa_onnx::OfflineTtsZipvoiceModelConfig> {
    let quant_onnx = |stem: &str| {
        first_existing(
            model_dir,
            &[&format!("{stem}.int8.onnx"), &format!("{stem}.onnx")],
        )
        .map(|p| p.to_string_lossy().to_string())
    };

    let vocoder = first_existing(model_dir, &["vocos_24khz.onnx", "vocoder.onnx"])
        .or_else(|| first_existing(base_dir, &["vocos_24khz.onnx", "vocoder.onnx"]));
    let vocoder = vocoder.ok_or_else(|| {
        TtsError(
            "Zipvoice requires the vocos_24khz.onnx vocoder, which is not bundled \
             with the model. Download it into {} from:\n  \
             https://github.com/k2-fsa/sherpa-onnx/releases/download/vocoder-models/vocos_24khz.onnx"
                .replace("{}", &base_dir.display().to_string()),
        )
    })?;

    Ok(sherpa_onnx::OfflineTtsZipvoiceModelConfig {
        tokens: existing_path(model_dir, "tokens.txt"),
        encoder: quant_onnx("encoder"),
        decoder: quant_onnx("decoder"),
        vocoder: Some(vocoder.to_string_lossy().to_string()),
        data_dir: existing_path(model_dir, "espeak-ng-data"),
        lexicon: existing_path(model_dir, "lexicon.txt"),
        // feat_scale / t_shift / target_rms / guidance_scale: 0.0 makes the
        // C API fall back to the model's trained defaults (same behaviour as
        // the sherpa-onnx CLI, which leaves them unset).
        ..Default::default()
    })
}

/// Pocket config: five ONNX components + two JSON sidecars. Only `decoder`,
/// `lm_flow`, and `lm_main` ship int8 variants in the quantised build —
/// `encoder` and `text_conditioner` are always plain `.onnx`.
fn build_pocket_config(model_dir: &std::path::Path) -> sherpa_onnx::OfflineTtsPocketModelConfig {
    let component = |stems: &[&str]| {
        stems
            .iter()
            .find_map(|s| first_existing(model_dir, &[s]))
            .map(|p| p.to_string_lossy().to_string())
    };
    sherpa_onnx::OfflineTtsPocketModelConfig {
        lm_flow: component(&["lm_flow.int8.onnx", "lm_flow.onnx"]),
        lm_main: component(&["lm_main.int8.onnx", "lm_main.onnx"]),
        encoder: component(&["encoder.int8.onnx", "encoder.onnx"]),
        decoder: component(&["decoder.int8.onnx", "decoder.onnx"]),
        text_conditioner: component(&["text_conditioner.int8.onnx", "text_conditioner.onnx"]),
        vocab_json: existing_path(model_dir, "vocab.json"),
        token_scores_json: existing_path(model_dir, "token_scores.json"),
        ..Default::default()
    }
}

/// Exact transcripts for the reference wavs bundled in the sherpa-onnx
/// zero-shot archives (from the sherpa-onnx docs — zipvoice requires the
/// transcript to match the audio, a mismatch audibly degrades the clone).
const ZIPVOICE_TEST_WAV_TRANSCRIPTS: &[(&str, &str)] = &[(
    "leijun-1.wav",
    "那还是三十六年前, 一九八七年. 我呢考上了武汉大学的计算机系.",
)];

fn bundled_reference_wav(model_dir: &std::path::Path) -> Option<std::path::PathBuf> {
    for sub in ["test_wavs", "."] {
        let dir = model_dir.join(sub);
        if dir.is_dir() {
            let mut wavs: Vec<_> = std::fs::read_dir(&dir)
                .into_iter()
                .flatten()
                .filter_map(Result::ok)
                .map(|e| e.path())
                .filter(|p| p.extension().is_some_and(|e| e.eq_ignore_ascii_case("wav")))
                .collect();
            wavs.sort();
            if let Some(first) = wavs.first() {
                return Some(first.clone());
            }
        }
    }
    None
}

/// Minimal RIFF/PCM wav reader for reference clips: 16-bit PCM mono only
/// (the sherpa-onnx test wavs and typical cloning references are 16-bit
/// mono; anything else errors clearly rather than being silently mangled).
#[allow(clippy::cast_precision_loss)] // stereo downmix averages samples
fn read_wav_mono_16bit(path: &std::path::Path) -> TtsResult<(Vec<f32>, i32)> {
    let bytes = std::fs::read(path)
        .map_err(|e| TtsError(format!("cannot read reference wav {}: {e}", path.display())))?;
    let rd = |off: usize| -> Option<u32> {
        bytes
            .get(off..off + 4)
            .map(|b| u32::from_le_bytes(b.try_into().unwrap()))
    };
    if bytes.len() < 44 || &bytes[0..4] != b"RIFF" || &bytes[8..12] != b"WAVE" {
        return Err(TtsError(format!(
            "{} is not a RIFF/WAVE file",
            path.display()
        )));
    }
    // Walk chunks for fmt (format + rate) and data (samples).
    let (mut format, mut channels, mut rate, mut bits) = (0u16, 0u16, 0i32, 0u16);
    let mut samples: Vec<f32> = Vec::new();
    let mut off = 12;
    while off + 8 <= bytes.len() {
        let id = &bytes[off..off + 4];
        let len = rd(off + 4).unwrap_or(0) as usize;
        let body = off + 8;
        match id {
            b"fmt " if len >= 16 => {
                format = u16::from_le_bytes(bytes[body..body + 2].try_into().unwrap());
                channels = u16::from_le_bytes(bytes[body + 2..body + 4].try_into().unwrap());
                rate = rd(body + 4).unwrap_or(0) as i32;
                bits = u16::from_le_bytes(bytes[body + 14..body + 16].try_into().unwrap());
            }
            b"data" => {
                let end = (body + len).min(bytes.len());
                samples = bytes[body..end]
                    .as_chunks::<2>()
                    .0
                    .iter()
                    .map(|c| f32::from(i16::from_le_bytes(*c)) / 32768.0)
                    .collect();
            }
            _ => {}
        }
        off = body + len + (len & 1); // chunks are word-aligned
    }
    if format != 1 || bits != 16 {
        return Err(TtsError(format!(
            "{}: only 16-bit PCM reference wavs are supported (got format {format}, {bits}-bit). \
             Convert with e.g. `ffmpeg -i in.mp3 -ac 1 -ar 24000 -c:a pcm_s16le out.wav`.",
            path.display()
        )));
    }
    if channels > 1 {
        // Downmix to mono by averaging channels (cloning references are
        // effectively mono anyway; sherpa-onnx expects a single channel).
        let frame_len = channels as usize;
        samples = samples
            .chunks_exact(frame_len)
            .map(|fr| fr.iter().sum::<f32>() / frame_len as f32)
            .collect();
    }
    if samples.is_empty() || rate == 0 {
        return Err(TtsError(format!(
            "{}: wav has no usable samples (rate {rate}, {} samples)",
            path.display(),
            samples.len()
        )));
    }
    Ok((samples, rate))
}

/// Parse a Supertonic voice id of the form `"sid:lang"` (e.g. `"6:ja"`)
/// into a `(speaker_id, language_code)` pair. A bare integer (`"6"`) is
/// accepted for backwards compatibility and defaults the language to `"en"`;
/// `None`/empty yields speaker 0 + `"en"`.
fn parse_supertonic_voice(voice: Option<&str>) -> (i32, String) {
    const DEFAULT_LANG: &str = "en";
    match voice {
        None | Some("") => (0, DEFAULT_LANG.to_string()),
        Some(v) => match v.split_once(':') {
            Some((sid_str, lang)) => {
                let sid = sid_str.parse::<i32>().unwrap_or(0);
                let lang = if lang.is_empty() { DEFAULT_LANG } else { lang };
                (sid, lang.to_string())
            }
            None => (v.parse::<i32>().unwrap_or(0), DEFAULT_LANG.to_string()),
        },
    }
}

/// Build one [`Voice`] per `(speaker, language)` pair for a Supertonic model.
/// Each voice id is `"sid:lang"` so [`SherpaOnnxEngine::speak`] can recover
/// both. With 10 preset speakers × 31 languages this yields 310 voices; the
/// list is deliberately exhaustive so callers can discover every combination.
fn supertonic_voices(info: &SherpaModelInfo) -> Vec<Voice> {
    let mut voices = Vec::new();
    for lang in &info.language {
        let iso639 = iso639_3(&lang.lang_code);
        let display = crate::types::locale_display_name(&lang.lang_code);
        for sid in 0..info.num_speakers {
            voices.push(Voice {
                id: format!("{sid}:{}", lang.lang_code),
                name: format!("Speaker {sid} ({})", lang.language_name),
                gender: Gender::Unknown,
                provider: "sherpaonnx".to_string(),
                language_codes: vec![LanguageCode {
                    bcp47: lang.lang_code.clone(),
                    iso639_3: iso639.clone(),
                    display: display.clone(),
                }],
            });
        }
    }
    voices
}

/// Matcha config: acoustic-model.onnx + vocoder.onnx + tokens.txt.
///
/// Matcha tarballs ship the acoustic model only — the vocoder is a separate
/// download (commonly `hifigan_v2.onnx`, or `vocos-22khz-univ.onnx` for newer
/// models). We look for the vocoder in the model directory first, then fall
/// back to the user's base model dir so a single shared vocoder can be reused
/// across Matcha models.
fn build_matcha_config(
    model_dir: &std::path::Path,
    base_dir: &std::path::Path,
) -> TtsResult<sherpa_onnx::OfflineTtsMatchaModelConfig> {
    // Acoustic model: try the canonical names in order of prevalence.
    let acoustic = first_existing(
        model_dir,
        &[
            "acoustic-model.onnx",
            "model-steps-3.onnx",
            "model-steps-1000.onnx",
            "model.onnx",
        ],
    )
    .ok_or_else(|| TtsError("Matcha acoustic model not found".into()))?;

    // Vocoder: prefer co-located; fall back to shared in base_dir.
    let vocoder = first_existing(
        model_dir,
        &[
            "hifigan_v2.onnx",
            "hifigan_v2_en_zh.onnx",
            "hifigan_vitimator_v2.onnx",
            "vocos-22khz-univ.onnx",
            "vocoder.onnx",
        ],
    )
    .or_else(|| {
        first_existing(
            base_dir,
            &["vocos-22khz-univ.onnx", "hifigan_v2.onnx", "vocoder.onnx"],
        )
    });

    Ok(sherpa_onnx::OfflineTtsMatchaModelConfig {
        acoustic_model: Some(acoustic.to_string_lossy().to_string()),
        vocoder: vocoder.as_ref().map(|p| p.to_string_lossy().to_string()),
        lexicon: existing_path(model_dir, "lexicon.txt"),
        tokens: Some(model_dir.join("tokens.txt").to_string_lossy().to_string()),
        data_dir: existing_path(model_dir, "espeak-ng-data"),
        dict_dir: existing_path(model_dir, "dict"),
        ..Default::default()
    })
}

/// VITS-family config. The right combination of lexicon / data_dir / dict_dir
/// depends on where the model came from:
///
/// - Piper / GitHub models → prefer `espeak-ng-data/`, never `dict_dir`.
/// - Chinese/Cantonese models → use `dict/` for jieba segmentation.
/// - MMS and other VITS → `lexicon.txt` if present, else nothing.
fn build_vits_config(
    model_dir: &std::path::Path,
    is_piper_or_github: bool,
    is_chinese: bool,
) -> sherpa_onnx::OfflineTtsVitsModelConfig {
    // Try the canonical name first, then scan for any non-acoustic .onnx
    // (handles Piper's en_US-amy-low.onnx naming convention).
    let model = find_primary_model_onnx(model_dir)
        .or_else(|| first_existing(model_dir, &["vits-model.onnx", "generator.onnx"]))
        .unwrap_or_else(|| model_dir.join("model.onnx"));

    // Pick the right phonetic back-end.
    let (data_dir, dict_dir) = if is_piper_or_github {
        // Piper & friends ship espeak-ng-data; jieba would just complain.
        (existing_path(model_dir, "espeak-ng-data"), None)
    } else if is_chinese {
        // Chinese voices need jieba — point dict_dir at the bundled `dict/`.
        let dict = existing_path(model_dir, "dict").or_else(|| {
            // Some archives nest the dict directory under a child folder.
            std::fs::read_dir(model_dir).ok().and_then(|entries| {
                entries.filter_map(Result::ok).find_map(|e| {
                    let p = e.path();
                    if p.is_dir() && p.join("dict.txt").exists() {
                        Some(p.to_string_lossy().to_string())
                    } else {
                        None
                    }
                })
            })
        });
        (existing_path(model_dir, "espeak-ng-data"), dict)
    } else {
        // MMS / vanilla VITS: use espeak-ng-data if present, fall back to
        // a sibling dict/ directory only when lexicon.txt is absent.
        let has_lexicon = model_dir.join("lexicon.txt").exists();
        let dict = if has_lexicon {
            None
        } else {
            existing_path(model_dir, "dict")
        };
        (existing_path(model_dir, "espeak-ng-data"), dict)
    };

    sherpa_onnx::OfflineTtsVitsModelConfig {
        model: Some(model.to_string_lossy().to_string()),
        tokens: Some(model_dir.join("tokens.txt").to_string_lossy().to_string()),
        lexicon: existing_path(model_dir, "lexicon.txt"),
        data_dir,
        dict_dir,
        ..Default::default()
    }
}

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

    #[test]
    fn test_apply_volume_and_pitch_identity() {
        let samples = [0.0_f32, 0.5, -0.5, 1.0, -1.0];
        let out = apply_volume_and_pitch(&samples, 1.0, 1.0);
        assert_eq!(out.len(), samples.len());
        for (a, b) in samples.iter().zip(out.iter()) {
            assert!((a - b).abs() < f32::EPSILON);
        }
    }

    #[test]
    fn test_apply_volume_scales_amplitude() {
        let samples = [0.5_f32, -0.5];
        let out = apply_volume_and_pitch(&samples, 2.0, 1.0);
        assert!((out[0] - 1.0).abs() < f32::EPSILON);
        assert!((out[1] - (-1.0)).abs() < f32::EPSILON);
    }

    #[test]
    fn test_apply_volume_zero_silences() {
        let samples = [0.5_f32, -0.25, 0.8];
        let out = apply_volume_and_pitch(&samples, 0.0, 1.0);
        for s in &out {
            assert!(s.abs() < f32::EPSILON);
        }
    }

    #[test]
    fn test_apply_pitch_changes_length() {
        // Pitch > 1.0 shortens the buffer (fewer samples); pitch < 1.0 lengthens.
        let samples = vec![0.5_f32; 100];
        let shorter = apply_volume_and_pitch(&samples, 1.0, 2.0);
        let longer = apply_volume_and_pitch(&samples, 1.0, 0.5);
        assert!(shorter.len() < samples.len());
        assert!(longer.len() > samples.len());
    }

    #[test]
    fn test_apply_volume_and_pitch_empty_input() {
        assert_eq!(apply_volume_and_pitch(&[], 1.0, 1.0).first(), None);
        assert_eq!(apply_volume_and_pitch(&[], 2.0, 0.5).first(), None);
    }

    #[test]
    fn test_is_piper_or_github_model_known_piper_prefix() {
        assert!(is_piper_or_github_model("piper-en-amy-medium"));
        assert!(is_piper_or_github_model("coqui-en-ljspeech"));
        assert!(is_piper_or_github_model("icefall-tts"));
        assert!(is_piper_or_github_model("mimic3-en"));
        assert!(is_piper_or_github_model("melo-en"));
        assert!(is_piper_or_github_model("vctk-en"));
        assert!(is_piper_or_github_model("zh-cantonese"));
        assert!(is_piper_or_github_model("ljs-en"));
        assert!(is_piper_or_github_model("cantonese-yue-xiaomaiiwn"));
        assert!(is_piper_or_github_model("kokoro-en-v0_19"));
    }

    #[test]
    fn test_is_piper_or_github_model_other_returns_false() {
        assert!(!is_piper_or_github_model("mms-en"));
        assert!(!is_piper_or_github_model("vits-en"));
        assert!(!is_piper_or_github_model("matcha-en"));
    }

    #[test]
    fn test_iso639_3_known_codes() {
        assert_eq!(iso639_3("en-US"), "eng");
        assert_eq!(iso639_3("es-ES"), "spa");
        assert_eq!(iso639_3("fr"), "fra");
        assert_eq!(iso639_3("de-DE"), "deu");
        assert_eq!(iso639_3("zh-CN"), "zho");
    }

    #[test]
    fn test_iso639_3_unknown_returns_input_lowercased() {
        // Unknown codes are returned lowercased (not "unknown") so callers
        // can still distinguish them in voice listings.
        assert_eq!(iso639_3("xx-XX"), "xx-xx");
        assert_eq!(iso639_3("Unknown-Lang"), "unknown-lang");
    }

    #[test]
    fn test_iso639_3_handles_underscore_separator() {
        // Some providers use BCP-47 with underscores; treat both.
        assert_eq!(iso639_3("en_US"), "eng");
        assert_eq!(iso639_3("pt_BR"), "por");
    }

    #[test]
    fn test_write_wav_round_trip_header() {
        // Write a known buffer, read the header back, validate.
        let dir = std::env::temp_dir();
        let path = dir.join("rtw_test_write_wav.wav");
        let samples = vec![0.0_f32, 0.5, -0.5, 1.0, -1.0];
        assert!(write_wav(&path, &samples, 22050));

        let bytes = std::fs::read(&path).expect("wav written");
        assert!(bytes.len() > 44, "WAV must include 44-byte header");
        assert_eq!(&bytes[0..4], b"RIFF");
        assert_eq!(&bytes[8..12], b"WAVE");
        assert_eq!(&bytes[12..16], b"fmt ");
        // PCM format tag = 1, mono channels = 1.
        assert_eq!(u16::from_le_bytes([bytes[20], bytes[21]]), 1);
        assert_eq!(u16::from_le_bytes([bytes[22], bytes[23]]), 1);
        // Sample rate little-endian.
        assert_eq!(
            u32::from_le_bytes([bytes[24], bytes[25], bytes[26], bytes[27]]),
            22050
        );
        // 16-bit per sample.
        assert_eq!(u16::from_le_bytes([bytes[34], bytes[35]]), 16);

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_write_wav_clamps_samples() {
        // Samples outside [-1.0, 1.0] must clamp rather than wrap. The
        // writer scales by 32767 (not 32768) so the clamped min is -32767.
        let dir = std::env::temp_dir();
        let path = dir.join("rtw_test_write_wav_clamp.wav");
        let samples = vec![5.0_f32, -5.0]; // way out of range
        assert!(write_wav(&path, &samples, 16000));

        let bytes = std::fs::read(&path).expect("wav written");
        // PCM data starts at byte 44.
        let first_sample = i16::from_le_bytes([bytes[44], bytes[45]]);
        let second_sample = i16::from_le_bytes([bytes[46], bytes[47]]);
        assert_eq!(first_sample, 32767);
        assert_eq!(second_sample, -32767);

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_first_existing_returns_first_match() {
        let dir = std::env::temp_dir();
        let a = dir.join("rtw_first_a.txt");
        let b = dir.join("rtw_first_b.txt");
        std::fs::write(&a, b"x").unwrap();
        std::fs::write(&b, b"y").unwrap();

        let r = first_existing(&dir, &["rtw_first_a.txt", "rtw_first_b.txt"]).unwrap();
        assert_eq!(r.file_name().unwrap().to_str().unwrap(), "rtw_first_a.txt");

        // Order matters — first one wins.
        let r = first_existing(&dir, &["missing.txt", "rtw_first_b.txt"]).unwrap();
        assert_eq!(r.file_name().unwrap().to_str().unwrap(), "rtw_first_b.txt");

        // No matches.
        assert!(first_existing(&dir, &["nope.txt", "alsonope.txt"]).is_none());

        let _ = std::fs::remove_file(&a);
        let _ = std::fs::remove_file(&b);
    }

    #[test]
    fn test_existing_path_only_when_present() {
        let dir = std::env::temp_dir();
        let p = dir.join("rtw_existing.txt");
        std::fs::write(&p, b"x").unwrap();
        assert_eq!(
            existing_path(&dir, "rtw_existing.txt").as_deref(),
            Some(p.to_str().unwrap())
        );
        assert!(existing_path(&dir, "rtw_missing.txt").is_none());
        let _ = std::fs::remove_file(&p);
    }

    // ===== Model-config builders (one test per model family) =====
    //
    // These exercise the per-type dispatch in speak() without needing a real
    // sherpa-onnx runtime. Each test fakes the on-disk file layout for a
    // model family and verifies the resulting OfflineTts*ModelConfig points
    // at the expected paths.

    use std::fs;

    /// Build a temp directory resembling an extracted Kokoro archive.
    fn fake_kokoro_dir() -> tempfile::TempDir {
        let d = tempfile::tempdir().expect("tmp");
        fs::write(d.path().join("model.onnx"), b"x").unwrap();
        fs::write(d.path().join("voices.bin"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        fs::create_dir(d.path().join("espeak-ng-data")).unwrap();
        d
    }

    #[test]
    fn test_build_kokoro_config_points_at_canonical_files() {
        let d = fake_kokoro_dir();
        let cfg = build_kokoro_config(d.path());
        assert_eq!(
            cfg.model.as_deref(),
            Some(d.path().join("model.onnx").to_str().unwrap())
        );
        assert_eq!(
            cfg.voices.as_deref(),
            Some(d.path().join("voices.bin").to_str().unwrap())
        );
        assert_eq!(
            cfg.tokens.as_deref(),
            Some(d.path().join("tokens.txt").to_str().unwrap())
        );
        assert_eq!(
            cfg.data_dir.as_deref(),
            Some(d.path().join("espeak-ng-data").to_str().unwrap())
        );
    }

    #[test]
    fn test_build_kokoro_config_missing_files_are_none() {
        // Voices/data_dir are optional — their absence must surface as None
        // rather than a path to a nonexistent file.
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("model.onnx"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        // Intentionally no voices.bin or espeak-ng-data/.
        let cfg = build_kokoro_config(d.path());
        assert!(cfg.model.is_some());
        assert!(cfg.tokens.is_some());
        assert!(cfg.voices.is_none());
        assert!(cfg.data_dir.is_none());
    }

    fn fake_matcha_dir() -> tempfile::TempDir {
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("acoustic-model.onnx"), b"x").unwrap();
        fs::write(d.path().join("hifigan_v2.onnx"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        fs::write(d.path().join("lexicon.txt"), b"x").unwrap();
        d
    }

    #[test]
    fn test_build_matcha_config_finds_acoustic_and_vocoder() {
        let d = fake_matcha_dir();
        let base = tempfile::tempdir().unwrap();
        let cfg = build_matcha_config(d.path(), base.path()).expect("matcha config");
        assert!(cfg
            .acoustic_model
            .as_deref()
            .unwrap()
            .ends_with("acoustic-model.onnx"));
        assert!(cfg.vocoder.as_deref().unwrap().ends_with("hifigan_v2.onnx"));
        assert!(cfg.lexicon.is_some());
        assert!(cfg.tokens.is_some());
    }

    #[test]
    fn test_build_matcha_config_accepts_legacy_acoustic_names() {
        // Matcha archives have shipped several acoustic-model names; the
        // builder must accept any of them in priority order.
        for name in ["model-steps-3.onnx", "model-steps-1000.onnx", "model.onnx"] {
            let d = tempfile::tempdir().unwrap();
            fs::write(d.path().join(name), b"x").unwrap();
            fs::write(d.path().join("hifigan_v2.onnx"), b"x").unwrap();
            fs::write(d.path().join("tokens.txt"), b"x").unwrap();
            let base = tempfile::tempdir().unwrap();
            let cfg = build_matcha_config(d.path(), base.path()).expect("matcha config");
            assert!(cfg.acoustic_model.is_some(), "failed for acoustic {name}");
        }
    }

    #[test]
    fn test_build_matcha_config_vocoder_fallback_to_base_dir() {
        // Co-located vocoder is missing — fall back to a shared one in base.
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("acoustic-model.onnx"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();

        let base = tempfile::tempdir().unwrap();
        fs::write(base.path().join("vocos-22khz-univ.onnx"), b"x").unwrap();

        let cfg = build_matcha_config(d.path(), base.path()).expect("matcha config");
        assert!(cfg
            .vocoder
            .as_deref()
            .unwrap()
            .ends_with("vocos-22khz-univ.onnx"));
    }

    #[test]
    fn test_build_matcha_config_errors_without_acoustic() {
        let d = tempfile::tempdir().unwrap();
        // Only vocoder + tokens, no acoustic.
        fs::write(d.path().join("hifigan_v2.onnx"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        let base = tempfile::tempdir().unwrap();
        assert!(build_matcha_config(d.path(), base.path()).is_err());
    }

    // ===== Supertonic builder / voice parsing tests =====

    /// Build a temp directory resembling the extracted sherpa-onnx Supertonic
    /// int8 bundle: four `<name>.int8.onnx` files + tts.json +
    /// unicode_indexer.bin + voice.bin.
    fn fake_supertonic_int8_dir() -> tempfile::TempDir {
        let d = tempfile::tempdir().expect("tmp");
        for name in [
            "duration_predictor.int8.onnx",
            "text_encoder.int8.onnx",
            "vector_estimator.int8.onnx",
            "vocoder.int8.onnx",
        ] {
            fs::write(d.path().join(name), b"x").unwrap();
        }
        fs::write(d.path().join("tts.json"), b"{}").unwrap();
        fs::write(d.path().join("unicode_indexer.bin"), b"x").unwrap();
        fs::write(d.path().join("voice.bin"), b"x").unwrap();
        d
    }

    #[test]
    fn test_build_supertonic_config_resolves_all_int8_files() {
        let d = fake_supertonic_int8_dir();
        let cfg = build_supertonic_config(d.path());
        // All seven fields must resolve — sherpa-onnx rejects the config if
        // any required path is None.
        for (field, val) in [
            ("duration_predictor", cfg.duration_predictor.as_deref()),
            ("text_encoder", cfg.text_encoder.as_deref()),
            ("vector_estimator", cfg.vector_estimator.as_deref()),
            ("vocoder", cfg.vocoder.as_deref()),
        ] {
            assert!(
                val.is_some_and(|p| p.ends_with(&format!("{field}.int8.onnx"))),
                "{field} should resolve to its int8 onnx, got {val:?}"
            );
        }
        assert!(cfg.tts_json.is_some_and(|p| p.ends_with("tts.json")));
        assert!(cfg
            .unicode_indexer
            .is_some_and(|p| p.ends_with("unicode_indexer.bin")));
        assert!(cfg.voice_style.is_some_and(|p| p.ends_with("voice.bin")));
    }

    #[test]
    fn test_build_supertonic_config_falls_back_to_plain_onnx() {
        // When the int8 variants are absent, the builder must pick the plain
        // `<name>.onnx` files (some custom Supertonic layouts ship unquantised).
        let d = tempfile::tempdir().unwrap();
        for name in [
            "duration_predictor.onnx",
            "text_encoder.onnx",
            "vector_estimator.onnx",
            "vocoder.onnx",
        ] {
            fs::write(d.path().join(name), b"x").unwrap();
        }
        let cfg = build_supertonic_config(d.path());
        assert!(cfg
            .duration_predictor
            .as_deref()
            .unwrap()
            .ends_with("duration_predictor.onnx"));
        assert!(cfg.vocoder.as_deref().unwrap().ends_with("vocoder.onnx"));
    }

    #[test]
    fn test_build_supertonic_config_missing_files_are_none() {
        // An empty dir must produce None for every field rather than a path
        // to a nonexistent file (sherpa-onnx's create() handles the error).
        let d = tempfile::tempdir().unwrap();
        let cfg = build_supertonic_config(d.path());
        assert!(cfg.duration_predictor.is_none());
        assert!(cfg.text_encoder.is_none());
        assert!(cfg.vector_estimator.is_none());
        assert!(cfg.vocoder.is_none());
        assert!(cfg.tts_json.is_none());
        assert!(cfg.unicode_indexer.is_none());
        assert!(cfg.voice_style.is_none());
    }

    // ===== Zipvoice / pocket config builders =====

    /// Temp dir resembling the extracted zipvoice distill-int8 archive,
    /// plus a base dir holding the shared vocos vocoder.
    fn fake_zipvoice_dirs() -> (tempfile::TempDir, tempfile::TempDir) {
        let model = tempfile::tempdir().unwrap();
        let base = tempfile::tempdir().unwrap();
        for name in [
            "encoder.int8.onnx",
            "decoder.int8.onnx",
            "encoder.onnx",
            "decoder.onnx",
            "tokens.txt",
            "lexicon.txt",
        ] {
            std::fs::write(model.path().join(name), b"x").unwrap();
        }
        std::fs::create_dir(model.path().join("espeak-ng-data")).unwrap();
        std::fs::write(base.path().join("vocos_24khz.onnx"), b"x").unwrap();
        (model, base)
    }

    #[test]
    fn test_build_zipvoice_config_prefers_int8_and_shared_vocoder() {
        let (model, base) = fake_zipvoice_dirs();
        let cfg = build_zipvoice_config(model.path(), base.path()).expect("config");
        assert!(cfg
            .encoder
            .as_deref()
            .is_some_and(|p| p.ends_with("encoder.int8.onnx")));
        assert!(cfg
            .decoder
            .as_deref()
            .is_some_and(|p| p.ends_with("decoder.int8.onnx")));
        assert!(cfg
            .vocoder
            .as_deref()
            .is_some_and(|p| p.starts_with(base.path().to_str().unwrap())));
        assert!(cfg
            .tokens
            .as_deref()
            .is_some_and(|p| p.ends_with("tokens.txt")));
        assert!(cfg
            .lexicon
            .as_deref()
            .is_some_and(|p| p.ends_with("lexicon.txt")));
        assert!(cfg
            .data_dir
            .as_deref()
            .is_some_and(|p| p.ends_with("espeak-ng-data")));
    }

    #[test]
    fn test_build_zipvoice_config_falls_back_to_plain_onnx() {
        // Unquantised archive (fp32 build): plain encoder/decoder must win.
        let (model, base) = fake_zipvoice_dirs();
        std::fs::remove_file(model.path().join("encoder.int8.onnx")).unwrap();
        std::fs::remove_file(model.path().join("decoder.int8.onnx")).unwrap();
        let cfg = build_zipvoice_config(model.path(), base.path()).expect("config");
        assert!(cfg
            .encoder
            .as_deref()
            .is_some_and(|p| p.ends_with("encoder.onnx")));
        assert!(cfg
            .decoder
            .as_deref()
            .is_some_and(|p| p.ends_with("decoder.onnx")));
    }

    #[test]
    fn test_build_zipvoice_config_missing_vocoder_errors_with_url() {
        // The vocos vocoder is never bundled — a clear error beats a
        // sherpa-onnx create() failure deep in the C++ runtime.
        let (model, base) = fake_zipvoice_dirs();
        std::fs::remove_file(base.path().join("vocos_24khz.onnx")).unwrap();
        let err = build_zipvoice_config(model.path(), base.path()).unwrap_err();
        assert!(err.0.contains("vocos_24khz.onnx"));
        assert!(err.0.contains("vocoder-models"));
    }

    /// Temp dir resembling the pocket-tts int8 archive: only decoder,
    /// lm_flow, and lm_main are quantised; encoder + text_conditioner are
    /// plain .onnx even in the int8 build.
    fn fake_pocket_int8_dir() -> tempfile::TempDir {
        let d = tempfile::tempdir().unwrap();
        for name in [
            "lm_flow.int8.onnx",
            "lm_main.int8.onnx",
            "encoder.onnx",
            "decoder.int8.onnx",
            "text_conditioner.onnx",
            "vocab.json",
            "token_scores.json",
        ] {
            std::fs::write(d.path().join(name), b"x").unwrap();
        }
        d
    }

    #[test]
    fn test_build_pocket_config_int8_layout() {
        let d = fake_pocket_int8_dir();
        let cfg = build_pocket_config(d.path());
        assert!(cfg
            .lm_flow
            .as_deref()
            .is_some_and(|p| p.ends_with("lm_flow.int8.onnx")));
        assert!(cfg
            .lm_main
            .as_deref()
            .is_some_and(|p| p.ends_with("lm_main.int8.onnx")));
        assert!(cfg
            .decoder
            .as_deref()
            .is_some_and(|p| p.ends_with("decoder.int8.onnx")));
        // Never quantised upstream — the plain files must be picked.
        assert!(cfg
            .encoder
            .as_deref()
            .is_some_and(|p| p.ends_with("encoder.onnx") && !p.contains("int8")));
        assert!(cfg
            .text_conditioner
            .as_deref()
            .is_some_and(|p| p.ends_with("text_conditioner.onnx")));
        assert!(cfg
            .vocab_json
            .as_deref()
            .is_some_and(|p| p.ends_with("vocab.json")));
        assert!(cfg
            .token_scores_json
            .as_deref()
            .is_some_and(|p| p.ends_with("token_scores.json")));
    }

    #[test]
    fn test_build_pocket_config_fp32_layout() {
        let d = tempfile::tempdir().unwrap();
        for name in [
            "lm_flow.onnx",
            "lm_main.onnx",
            "encoder.onnx",
            "decoder.onnx",
            "text_conditioner.onnx",
        ] {
            std::fs::write(d.path().join(name), b"x").unwrap();
        }
        let cfg = build_pocket_config(d.path());
        assert!(cfg
            .lm_flow
            .as_deref()
            .is_some_and(|p| p.ends_with("lm_flow.onnx")));
        assert!(cfg
            .decoder
            .as_deref()
            .is_some_and(|p| p.ends_with("decoder.onnx")));
    }

    // ===== Reference-audio / wav reader =====

    #[test]
    fn test_read_wav_mono_16bit_round_trip() {
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("ref.wav");
        let samples = vec![0.0_f32, 0.25, -0.25, 0.5, -0.5];
        write_wav(&p, &samples, 24000);
        let (read, rate) = read_wav_mono_16bit(&p).expect("reads back");
        assert_eq!(rate, 24000);
        assert_eq!(read.len(), samples.len());
        for (a, b) in read.iter().zip(samples.iter()) {
            assert!((a - b).abs() < 0.001, "sample drifted: {a} vs {b}");
        }
    }

    #[test]
    fn test_read_wav_rejects_non_pcm16() {
        // Hand-roll a fmt chunk claiming 32-bit float (format 3).
        let mut bytes = Vec::new();
        bytes.extend_from_slice(b"RIFF");
        bytes.extend_from_slice(&36u32.to_le_bytes());
        bytes.extend_from_slice(b"WAVE");
        bytes.extend_from_slice(b"fmt ");
        bytes.extend_from_slice(&16u32.to_le_bytes());
        bytes.extend_from_slice(&3u16.to_le_bytes()); // IEEE float
        bytes.extend_from_slice(&1u16.to_le_bytes()); // mono
        bytes.extend_from_slice(&24000u32.to_le_bytes());
        bytes.extend_from_slice(&48000u32.to_le_bytes());
        bytes.extend_from_slice(&2u16.to_le_bytes());
        bytes.extend_from_slice(&32u16.to_le_bytes()); // 32-bit
        bytes.extend_from_slice(b"data");
        bytes.extend_from_slice(&4u32.to_le_bytes());
        bytes.extend_from_slice(&[0, 0, 0, 0]);
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("f32.wav");
        std::fs::write(&p, bytes).unwrap();
        let err = read_wav_mono_16bit(&p).unwrap_err();
        assert!(err.0.contains("16-bit PCM"));
    }

    #[test]
    fn test_bundled_reference_wav_prefers_test_wavs_sorted() {
        let d = tempfile::tempdir().unwrap();
        assert!(bundled_reference_wav(d.path()).is_none());
        std::fs::create_dir(d.path().join("test_wavs")).unwrap();
        std::fs::write(d.path().join("test_wavs/b.wav"), b"x").unwrap();
        std::fs::write(d.path().join("test_wavs/a.wav"), b"x").unwrap();
        std::fs::write(d.path().join("loose.wav"), b"x").unwrap();
        let wav = bundled_reference_wav(d.path()).expect("found");
        // Sorted order: a.wav before b.wav, and test_wavs/ wins over loose.
        assert!(wav.ends_with("test_wavs/a.wav"));
    }

    #[test]
    fn test_resolve_reference_text_bundled_transcript_and_overrides() {
        let d = tempfile::tempdir().unwrap();
        std::fs::create_dir(d.path().join("test_wavs")).unwrap();
        std::fs::write(d.path().join("test_wavs/leijun-1.wav"), b"x").unwrap();

        // Known bundled wav → known transcript, no credentials needed.
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"zipvoice-zh_en-emilia-distill-int8"}"#);
        let text = engine.resolve_reference_text(d.path()).expect("transcript");
        assert!(text.contains("武汉大学"));

        // User transcript wins.
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"x","referenceText":"custom words"}"#);
        assert_eq!(
            engine.resolve_reference_text(d.path()).expect("override"),
            "custom words"
        );

        // Unknown wav + no override → actionable error.
        std::fs::write(d.path().join("test_wavs/mystery.wav"), b"x").unwrap();
        std::fs::remove_file(d.path().join("test_wavs/leijun-1.wav")).unwrap();
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"x"}"#);
        let err = engine.resolve_reference_text(d.path()).unwrap_err();
        assert!(err.0.contains("referenceText"));
    }

    #[test]
    fn test_parse_supertonic_voice_sid_lang_form() {
        assert_eq!(parse_supertonic_voice(Some("6:ja")), (6, "ja".to_string()));
        assert_eq!(parse_supertonic_voice(Some("0:en")), (0, "en".to_string()));
        assert_eq!(parse_supertonic_voice(Some("9:ko")), (9, "ko".to_string()));
    }

    #[test]
    fn test_parse_supertonic_voice_bare_integer_defaults_en() {
        // Backwards compatibility: a plain speaker id picks the default lang.
        assert_eq!(parse_supertonic_voice(Some("6")), (6, "en".to_string()));
        assert_eq!(parse_supertonic_voice(Some("0")), (0, "en".to_string()));
    }

    #[test]
    fn test_parse_supertonic_voice_none_and_empty() {
        assert_eq!(parse_supertonic_voice(None), (0, "en".to_string()));
        assert_eq!(parse_supertonic_voice(Some("")), (0, "en".to_string()));
    }

    #[test]
    fn test_parse_supertonic_voice_empty_lang_after_colon() {
        // "6:" → sid 6, empty lang defaults to "en" rather than forwarding an
        // empty string that sherpa-onnx would reject.
        assert_eq!(parse_supertonic_voice(Some("6:")), (6, "en".to_string()));
    }

    #[test]
    fn test_parse_supertonic_voice_non_numeric_sid_falls_back_to_zero() {
        // A malformed sid must not panic — fall back to 0.
        assert_eq!(
            parse_supertonic_voice(Some("abc:ja")),
            (0, "ja".to_string())
        );
    }

    #[test]
    fn test_supertonic_voices_one_per_speaker_language_pair() {
        // 2 speakers × 3 languages = 6 voices, ids encoded as "sid:lang".
        let info = SherpaModelInfo {
            id: "test".into(),
            model_type: "supertonic".into(),
            engines: "sherpa-onnx".into(),
            name: "test".into(),
            language: vec![
                SherpaLanguage {
                    lang_code: "en".into(),
                    language_name: "English".into(),
                    country: String::new(),
                },
                SherpaLanguage {
                    lang_code: "ja".into(),
                    language_name: "Japanese".into(),
                    country: String::new(),
                },
                SherpaLanguage {
                    lang_code: "ko".into(),
                    language_name: "Korean".into(),
                    country: String::new(),
                },
            ],
            sample_rate: 24000,
            num_speakers: 2,
            quality: String::new(),
            url: String::new(),
            compression: false,
            filesize_mb: 0.0,
            license: String::new(),
            license_url: String::new(),
        };
        let voices = supertonic_voices(&info);
        assert_eq!(voices.len(), 6);
        // Each combination is present, and ids round-trip back to (sid, lang).
        let ids: Vec<&str> = voices.iter().map(|v| v.id.as_str()).collect();
        assert!(ids.contains(&"0:en"));
        assert!(ids.contains(&"1:ko"));
        assert!(ids.contains(&"0:ja"));
        // Voice carries the matching language code.
        let ja_voice = voices
            .iter()
            .find(|v| v.id == "1:ja")
            .expect("1:ja voice exists");
        assert_eq!(ja_voice.language_codes[0].bcp47, "ja");
        assert_eq!(ja_voice.provider, "sherpaonnx");
    }

    #[test]
    fn test_engine_get_voices_supertonic_enumerates_speakers_times_languages() {
        // Integration: the real registry entry must produce 10 speakers ×
        // 31 languages = 310 voices. This also verifies the JSON parses and
        // model_type "supertonic" routes through the expansion branch.
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"supertonic-3-multilingual"}"#);
        if !engine.models.contains_key("supertonic-3-multilingual") {
            eprintln!(
                "skipping: 'supertonic-3-multilingual' missing from registry; \
                 check src/models.json"
            );
            return;
        }
        let voices = engine.get_voices().expect("voices");
        assert_eq!(voices.len(), 10 * 31);
        // Spot-check a known pair and the id encoding.
        assert!(voices.iter().any(|v| v.id == "6:ja"));
        // Every voice carries the sherpaonnx provider tag.
        assert!(voices.iter().all(|v| v.provider == "sherpaonnx"));
    }

    #[test]
    fn test_engine_num_steps_default_and_valid_override() {
        // No numSteps → default 8.
        assert_eq!(SherpaOnnxEngine::new("{}").num_steps, 8);
        // Valid in-range value passes through.
        assert_eq!(SherpaOnnxEngine::new(r#"{"numSteps":"10"}"#).num_steps, 10);
        assert_eq!(SherpaOnnxEngine::new(r#"{"numSteps":"5"}"#).num_steps, 5);
        assert_eq!(SherpaOnnxEngine::new(r#"{"numSteps":"12"}"#).num_steps, 12);
    }

    #[test]
    fn test_engine_num_steps_out_of_range_clamps_to_default() {
        // Below 5 and above 12 are outside Supertonic's supported range.
        assert_eq!(SherpaOnnxEngine::new(r#"{"numSteps":"3"}"#).num_steps, 8);
        assert_eq!(SherpaOnnxEngine::new(r#"{"numSteps":"20"}"#).num_steps, 8);
    }

    #[test]
    fn test_engine_num_steps_non_numeric_keeps_default() {
        assert_eq!(SherpaOnnxEngine::new(r#"{"numSteps":"fast"}"#).num_steps, 8);
    }

    fn fake_piper_dir() -> tempfile::TempDir {
        let d = tempfile::tempdir().unwrap();
        // Piper archives use a domain-specific .onnx name rather than model.onnx.
        fs::write(d.path().join("en_US-amy-low.onnx"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        fs::create_dir(d.path().join("espeak-ng-data")).unwrap();
        d
    }

    #[test]
    fn test_build_vits_config_piper_uses_espeak_data_no_dict() {
        let d = fake_piper_dir();
        let cfg = build_vits_config(d.path(), true, false);
        // The model is found by scanning for the first non-vocoder .onnx.
        assert!(cfg
            .model
            .as_deref()
            .unwrap()
            .ends_with("en_US-amy-low.onnx"));
        assert!(cfg.data_dir.is_some(), "Piper needs espeak-ng-data");
        assert!(
            cfg.dict_dir.is_none(),
            "Piper must NOT set dict_dir (jieba would warn)"
        );
    }

    #[test]
    fn test_build_vits_config_chinese_uses_dict_dir() {
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("model.onnx"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        // Chinese models ship a dict/ directory for jieba segmentation.
        let dict_dir = d.path().join("dict");
        fs::create_dir(&dict_dir).unwrap();

        let cfg = build_vits_config(d.path(), false, true);
        assert_eq!(
            cfg.dict_dir.as_deref(),
            Some(dict_dir.to_str().unwrap()),
            "Chinese models must point dict_dir at bundled dict/"
        );
    }

    #[test]
    fn test_build_vits_config_mms_with_lexicon_no_dict() {
        // MMS-style: lexicon.txt present, no dict/, no espeak-ng-data.
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("model.onnx"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        fs::write(d.path().join("lexicon.txt"), b"x").unwrap();

        let cfg = build_vits_config(d.path(), false, false);
        assert!(cfg.lexicon.is_some());
        assert!(
            cfg.dict_dir.is_none(),
            "dict_dir must not be set when lexicon.txt is present"
        );
    }

    #[test]
    fn test_build_vits_config_mms_without_lexicon_uses_dict_fallback() {
        // MMS without lexicon.txt → fall back to dict/ if present.
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("model.onnx"), b"x").unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        fs::create_dir(d.path().join("dict")).unwrap();

        let cfg = build_vits_config(d.path(), false, false);
        assert!(cfg.dict_dir.is_some());
    }

    #[test]
    fn test_find_primary_model_onnx_prefers_canonical_name() {
        let d = tempfile::tempdir().unwrap();
        // Both model.onnx and a stray .onnx present — canonical wins.
        fs::write(d.path().join("model.onnx"), b"x").unwrap();
        fs::write(d.path().join("vits-en-foo.onnx"), b"x").unwrap();
        let r = find_primary_model_onnx(d.path()).expect("found");
        assert!(r.to_str().unwrap().ends_with("model.onnx"));
    }

    #[test]
    fn test_find_primary_model_onnx_skips_vocoders_and_acoustic_steps() {
        let d = tempfile::tempdir().unwrap();
        // Only vocoder/acoustic-steps files — none should match.
        fs::write(d.path().join("model-steps-3.onnx"), b"x").unwrap();
        fs::write(d.path().join("vocos-22khz-univ.onnx"), b"x").unwrap();
        fs::write(d.path().join("hifigan_v2.onnx"), b"x").unwrap();
        fs::write(d.path().join("vocoder.onnx"), b"x").unwrap();
        assert!(find_primary_model_onnx(d.path()).is_none());
    }

    #[test]
    fn test_find_primary_model_onnx_picks_first_unmatched() {
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("en_US-amy-low.onnx"), b"x").unwrap();
        let r = find_primary_model_onnx(d.path()).expect("found");
        assert!(r.to_str().unwrap().ends_with("en_US-amy-low.onnx"));
    }

    #[test]
    fn test_resolve_model_scan_dir_uses_top_when_files_present() {
        // If the top dir has tokens.txt or any .onnx, return it as-is.
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("tokens.txt"), b"x").unwrap();
        let r = resolve_model_scan_dir(d.path());
        assert_eq!(r, d.path());
    }

    #[test]
    fn test_resolve_model_scan_dir_descends_into_single_subdir() {
        // GitHub archives often extract to <name>/<name>/. If the outer dir
        // is empty except for a single subdir with the actual model, descend.
        let d = tempfile::tempdir().unwrap();
        let inner = d.path().join("vits-piper-en_US-amy-low");
        fs::create_dir(&inner).unwrap();
        fs::write(inner.join("model.onnx"), b"x").unwrap();

        let r = resolve_model_scan_dir(d.path());
        assert!(r.ends_with("vits-piper-en_US-amy-low"));
    }

    #[test]
    fn test_resolve_model_scan_dir_no_descent_when_multiple_subdirs() {
        // Ambiguous layout — don't guess, return the original.
        let d = tempfile::tempdir().unwrap();
        fs::create_dir(d.path().join("a")).unwrap();
        fs::create_dir(d.path().join("b")).unwrap();
        let r = resolve_model_scan_dir(d.path());
        assert_eq!(r, d.path());
    }

    #[test]
    fn test_find_file_locates_named_child() {
        let d = tempfile::tempdir().unwrap();
        fs::write(d.path().join("foo.txt"), b"x").unwrap();
        let r = find_file(d.path(), "foo.txt").expect("found");
        assert!(r.to_str().unwrap().ends_with("foo.txt"));
        assert!(find_file(d.path(), "missing.txt").is_none());
    }

    // ===== SherpaOnnxEngine public-API tests (no model download) =====
    //
    // Construct engines with various modelId values and verify registry
    // lookup, voice enumeration, and graceful failure paths. None of these
    // need an actual model on disk because they exit before generate().

    #[test]
    fn test_engine_construction_with_model_id_does_not_load_yet() {
        // Setting a modelId is lazy — actual load happens on speak(). So
        // construction must succeed even when the model isn't downloaded.
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"piper-en_US-amy-low"}"#);
        assert_eq!(engine.loaded_model_id, "piper-en_US-amy-low");
    }

    #[test]
    fn test_engine_construction_accepts_numeric_credential_values() {
        // JSON numbers must be coerced to strings, not silently dropped
        // (a strict HashMap<String, String> parse rejects the whole object).
        let engine = SherpaOnnxEngine::new(
            r#"{"modelId":"piper-en_US-amy-low","numThreads":4,"numSteps":10}"#,
        );
        assert_eq!(engine.loaded_model_id, "piper-en_US-amy-low");
        assert_eq!(engine.num_threads, 4);
        assert_eq!(engine.num_steps, 10);
    }

    #[test]
    fn test_engine_speak_without_model_id_errors_clearly() {
        let engine = SherpaOnnxEngine::new("");
        let err = engine
            .speak("hi", None, 1.0, 1.0, 1.0, None, None, None)
            .unwrap_err();
        assert!(
            err.to_string().contains("modelId"),
            "missing-model error should mention modelId: {err}"
        );
    }

    #[test]
    fn test_registry_crate_supplies_the_models() {
        // The registry now ships as the sherpa-onnx-models crate; the old
        // parse_model JSON path is gone. What used to be per-field parse
        // tests is now: registry is large, typed, and defaults sane.
        let models = load_models();
        assert!(models.len() > 1700, "registry shrank: {}", models.len());
        // engines matches the family rule: upstream graphs are
        // sherpa-onnx-drivable; AACTools artifacts may list floravox /
        // rust-tts-wrapper (or a combination) instead
        for m in models.values() {
            assert!(!m.engines.is_empty(), "{} has no engines value", m.id);
            assert!(
                m.engines.split(',').any(|e| e.trim() == "sherpa-onnx")
                    || m.engines.contains("floravox")
                    || m.engines.contains("rust-tts-wrapper"),
                "{} has an unknown engines value: {}",
                m.id,
                m.engines
            );
        }
        // a known MMS entry (no explicit model_type in raw data) parses
        // with the vits-family default
        let mms = models
            .values()
            .find(|m| m.id.starts_with("mms_"))
            .expect("MMS entries present");
        assert_eq!(mms.engines, "sherpa-onnx");
    }

    #[test]
    fn test_engine_speak_with_unknown_model_id_errors_with_count() {
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"not-a-real-model"}"#);
        let err = engine
            .speak("hi", None, 1.0, 1.0, 1.0, None, None, None)
            .unwrap_err();
        // Error message should hint at how many models ARE available so the
        // caller can pick a valid one.
        assert!(err.to_string().contains("not found in registry"));
        assert!(err.to_string().contains("models available"));
    }

    #[test]
    fn test_engine_get_voices_multi_speaker_enumeration() {
        // Pick a known multi-speaker model from the registry and verify
        // get_voices() enumerates `num_speakers` voice ids without needing
        // the actual model files (it reads from the registry only).
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"coqui-en-vctk"}"#);
        // If this particular id isn't in the registry, skip loudly so the
        // test output shows the skip rather than passing vacuously. A
        // silent `return` here previously masked the test becoming a no-op
        // when the model id was renamed.
        let known = engine.models.contains_key("coqui-en-vctk");
        if !known {
            eprintln!(
                "skipping: 'coqui-en-vctk' is no longer in the registry; \
                 update the model id in this test"
            );
            return;
        }
        let voices = engine.get_voices().expect("voices");
        assert!(
            !voices.is_empty(),
            "expected at least 1 voice for multi-speaker model"
        );
        // All voices must carry the sherpaonnx provider tag.
        assert!(voices.iter().all(|v| v.provider == "sherpaonnx"));
    }

    #[test]
    fn test_engine_get_voices_single_speaker_returns_one() {
        // kokoro-en-v0_19 is actually an 11-speaker model (the registry now
        // carries the real count), so use a genuinely single-speaker voice.
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"piper-en_US-amy-low"}"#);
        let voices = engine.get_voices().expect("voices");
        assert_eq!(voices.len(), 1);
        assert_eq!(voices[0].id, "0");
        assert_eq!(voices[0].name, "Speaker 0");
    }

    #[test]
    fn test_engine_get_voices_for_unloaded_model_id_returns_one_default() {
        // Unknown modelId: get_voices() still returns one voice (the
        // default speaker 0) rather than panicking.
        let engine = SherpaOnnxEngine::new(r#"{"modelId":"doesnt-matter"}"#);
        let voices = engine.get_voices().expect("voices");
        assert_eq!(voices.len(), 1);
    }

    #[test]
    fn test_engine_num_threads_parsed_from_credentials() {
        let engine = SherpaOnnxEngine::new(r#"{"numThreads":"4","provider":"cpu"}"#);
        assert_eq!(engine.num_threads, 4);
        assert_eq!(engine.provider.as_deref(), Some("cpu"));
    }

    #[test]
    fn test_engine_num_threads_invalid_falls_back_to_default() {
        let engine = SherpaOnnxEngine::new(r#"{"numThreads":"not-a-number"}"#);
        assert_eq!(engine.num_threads, 2); // default
    }

    #[test]
    fn test_engine_num_threads_zero_falls_back_to_default() {
        // 0 would cause sherpa-onnx to use no threads — clamp to default.
        let engine = SherpaOnnxEngine::new(r#"{"numThreads":"0"}"#);
        assert_eq!(engine.num_threads, 2);
    }

    #[test]
    fn test_engine_model_path_override() {
        let engine =
            SherpaOnnxEngine::new(r#"{"modelPath":"/tmp/custom-model-dir","modelId":"foo"}"#);
        assert_eq!(
            engine.model_dir,
            std::path::PathBuf::from("/tmp/custom-model-dir")
        );
        assert_eq!(engine.loaded_model_id, "foo");
    }
}