perovskite_client 0.3.2

Multiplayer voxel game written in Rust - Game client
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
use std::collections::hash_map::Entry;
use std::fmt::{Debug, Formatter};
use std::io::Cursor;
use std::num::NonZeroU64;
use std::ops::{Add, ControlFlow, DerefMut, Range};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::{bail, Context, Result};
use arc_swap::ArcSwap;
use atomicbox::AtomicOptionBox;
use cgmath::{InnerSpace, Vector3, Zero};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{Host, OutputCallbackInfo};
use crossbeam_channel::TrySendError;
use hound::WavReader;
use parking_lot::Mutex;
use perovskite_core::coordinates::{BlockCoordinate, ChunkCoordinate};
use perovskite_core::protocol::audio::{SampledSound, SoundSource};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use rubato::{FftFixedInOut, Resampler};
use rustc_hash::FxHashMap;
use seqlock::SeqLock;
use smallvec::SmallVec;
use tokio_util::sync::{CancellationToken, DropGuard};

use crate::client_state::entities::GameEntity;
use crate::client_state::settings::GameSettings;
use crate::client_state::timekeeper::Timekeeper;
use crate::client_state::ClientState;
use crate::media::CacheManager;

// Public for testing
pub struct EngineHandle {
    control: Arc<SharedControl>,
    _drop_guard: DropGuard,
    allocator_state: Mutex<AllocatorState>,
    simple_sound_lengths: FxHashMap<u32, u64>,
    freeing_receiver: crossbeam_channel::Receiver<Box<EntityData>>,
}

/// An opaque token used to modify/remove a sound in a slot.
/// Resilient to evictions, since it keeps a sequence number.
///
/// Note: PartialOrd/Ord derived implementation is only for the benefit of
/// ord-based datastructures (e.g. BTreeMap). No semantic meaning should
/// be ascribed to the result of these comparisons.
#[derive(PartialEq, Eq, Hash, Debug, PartialOrd, Ord, Clone, Copy)]
pub struct SimpleSoundToken(NonZeroU64);
#[derive(PartialEq, Eq, Hash, Debug, PartialOrd, Ord, Clone, Copy)]
pub struct ProceduralEntityToken(NonZeroU64);

struct AllocatorState {
    simple_sound_tokens: [u64; NUM_SIMPLE_SOUND_SLOTS],
    simple_sounds_next_sequence: u64,
    entity_slot_tokens: [u64; NUM_PROCEDURAL_ENTITY_SLOTS],
    // Used for computing scores
    entity_slot_data_local_copy: [Option<Box<EntityData>>; NUM_PROCEDURAL_ENTITY_SLOTS],
    entity_slots_next_sequence: u64,
}

impl AllocatorState {
    fn new() -> AllocatorState {
        AllocatorState {
            simple_sound_tokens: [0; NUM_SIMPLE_SOUND_SLOTS],
            // This must start at a nonzero value, since 0 is a sentinel
            simple_sounds_next_sequence: NUM_SIMPLE_SOUND_SLOTS as u64,
            entity_slot_tokens: [0; NUM_PROCEDURAL_ENTITY_SLOTS],
            // Likewise
            entity_slots_next_sequence: NUM_PROCEDURAL_ENTITY_SLOTS as u64,
            entity_slot_data_local_copy: [const { None }; NUM_PROCEDURAL_ENTITY_SLOTS],
        }
    }
}

impl EngineHandle {
    pub(crate) fn is_token_evicted(&self, token: SimpleSoundToken) -> bool {
        let index = token.0.get() % (NUM_SIMPLE_SOUND_SLOTS as u64);
        let alloc_lock = self.allocator_state.lock();
        alloc_lock.simple_sound_tokens[index as usize] != token.0.get()
    }

    pub(crate) fn insert_or_update_simple_sound(
        &self,
        tick_now: u64,
        player_position: Vector3<f64>,
        sound: SimpleSoundControlBlock,
        previous_token: Option<SimpleSoundToken>,
    ) -> Option<SimpleSoundToken> {
        assert_ne!(sound.flags & SOUND_PRESENT, 0);
        let mut alloc_lock = self.allocator_state.lock();

        let seqnum = alloc_lock.simple_sounds_next_sequence;

        if let Some(token) = previous_token {
            let index = token.0.get() % (NUM_SIMPLE_SOUND_SLOTS as u64);
            // If the sound hasn't been evicted, put it back in the same slot
            if alloc_lock.simple_sound_tokens[index as usize] == token.0.get() {
                let mut lock = self.control.simple_sounds[index as usize].lock_write();
                *lock = sound;
                return Some(token);
            }
        }
        alloc_lock.simple_sounds_next_sequence += NUM_SIMPLE_SOUND_SLOTS as u64;

        if let Some(index) = alloc_lock.simple_sound_tokens.iter().position(|x| *x == 0) {
            let token = seqnum + (index as u64);
            alloc_lock.simple_sound_tokens[index] = token;
            {
                let mut lock = self.control.simple_sounds[index].lock_write();
                *lock = sound;
            }
            Some(SimpleSoundToken(NonZeroU64::new(token).unwrap()))
        } else {
            let candidate_score = sound.compute_score(tick_now, player_position);
            let mut scores = [0; NUM_SIMPLE_SOUND_SLOTS];
            for i in 0..NUM_SIMPLE_SOUND_SLOTS {
                let control_block = self.control.simple_sounds[i].read();
                scores[i] = control_block.compute_score(tick_now, player_position);
            }
            let (min_index, min_score) = scores
                .iter()
                .copied()
                .enumerate()
                .min_by_key(|&(_, score)| score)
                .unwrap();
            if min_score < candidate_score {
                let token = seqnum + (min_index as u64);
                alloc_lock.simple_sound_tokens[min_index] = token;
                {
                    let mut lock = self.control.simple_sounds[min_index].lock_write();
                    *lock = sound;
                }
                return Some(SimpleSoundToken(NonZeroU64::new(token).unwrap()));
            }
            None
        }
    }

    pub(crate) fn remove_simple_sound(&self, token: SimpleSoundToken) -> bool {
        let mut alloc_lock = self.allocator_state.lock();

        let index = token.0.get() % (NUM_SIMPLE_SOUND_SLOTS as u64);
        if alloc_lock.simple_sound_tokens[index as usize] == token.0.get() {
            alloc_lock.simple_sound_tokens[index as usize] = 0;
            let mut lock = self.control.simple_sounds[index as usize].lock_write();
            *lock = SimpleSoundControlBlock::const_default();
            true
        } else {
            false
        }
    }

    pub(crate) fn remove_entity_state(&self, token: ProceduralEntityToken) -> bool {
        let mut alloc_lock = self.allocator_state.lock();

        let index = token.0.get() % (NUM_PROCEDURAL_ENTITY_SLOTS as u64);
        if alloc_lock.entity_slot_tokens[index as usize] == token.0.get() {
            alloc_lock.entity_slot_tokens[index as usize] = 0;
            self.control.entity_data_slots[index as usize].store(
                Some(Box::new(EntityData {
                    control: ProceduralEntitySoundControlBlock::const_default(),
                    entity: None,
                })),
                Ordering::AcqRel,
            );
            true
        } else {
            false
        }
    }

    fn process_pending_frees(&self) {
        if self.freeing_receiver.is_full() {
            log::warn!("Memory reclaim receiver is full; some memory may have leaked");
        }
        // Approximate because inherently racy
        let len_estimate = self.freeing_receiver.len();
        if len_estimate > 1 {
            log::debug!(
                "Dropping approximately {} entity audio control blocks",
                self.freeing_receiver.len()
            );
        }
        while let Ok(x) = self.freeing_receiver.try_recv() {
            drop(x);
        }
    }

    pub(crate) fn update_entity_state(
        &self,
        tick_now: u64,
        player_position: Vector3<f64>,
        data: EntityData,
        previous_token: Option<ProceduralEntityToken>,
    ) -> Option<ProceduralEntityToken> {
        self.process_pending_frees();

        let control = data.control;
        assert_ne!(control.flags & SOUND_PRESENT, 0);
        let mut alloc_lock = self.allocator_state.lock();

        if let Some(token) = previous_token {
            let index = token.0.get() % (NUM_PROCEDURAL_ENTITY_SLOTS as u64);
            // If the entity hasn't been evicted, put it back in the same slot
            if alloc_lock.entity_slot_tokens[index as usize] == token.0.get() {
                alloc_lock.entity_slot_data_local_copy[index as usize] =
                    Some(Box::new(data.clone()));
                self.control.entity_data_slots[index as usize]
                    .store(Some(Box::new(data)), Ordering::AcqRel);
                return Some(token);
            }
        }

        let seqnum = alloc_lock.entity_slots_next_sequence;
        alloc_lock.entity_slots_next_sequence += NUM_PROCEDURAL_ENTITY_SLOTS as u64;

        if let Some(index) = alloc_lock.entity_slot_tokens.iter().position(|x| *x == 0) {
            let token = seqnum + (index as u64);
            alloc_lock.entity_slot_tokens[index] = token;

            alloc_lock.entity_slot_data_local_copy[index] = Some(Box::new(data.clone()));
            self.control.entity_data_slots[index].store(Some(Box::new(data)), Ordering::AcqRel);

            Some(ProceduralEntityToken(NonZeroU64::new(token).unwrap()))
        } else {
            let candidate_score = data.compute_score(tick_now, player_position);
            let mut scores = [0; NUM_PROCEDURAL_ENTITY_SLOTS];
            for i in 0..NUM_PROCEDURAL_ENTITY_SLOTS {
                scores[i] = alloc_lock.entity_slot_data_local_copy[i]
                    .as_ref()
                    .map(|x| x.compute_score(tick_now, player_position))
                    .unwrap_or(0);
            }
            let (min_index, min_score) = scores
                .iter()
                .copied()
                .enumerate()
                .min_by_key(|&(_, score)| score)
                .unwrap();
            if min_score < candidate_score {
                let token = seqnum + (min_index as u64);
                alloc_lock.entity_slot_tokens[min_index] = token;
                {
                    alloc_lock.entity_slot_data_local_copy[min_index] =
                        Some(Box::new(data.clone()));
                    self.control.entity_data_slots[min_index]
                        .store(Some(Box::new(data)), Ordering::AcqRel);
                }
                return Some(ProceduralEntityToken(NonZeroU64::new(token).unwrap()));
            }
            None
        }
    }

    pub(crate) fn update_position(
        &self,
        tick: u64,
        pos: Vector3<f64>,
        velocity: Vector3<f32>,
        azimuth: f64,
        entity_attachment: u64,
    ) {
        {
            let mut lock = self.control.player_state.lock_write();
            lock.position = pos;
            lock.velocity = velocity;
            lock.azimuth_radian = azimuth;
            lock.position_timebase_tick = tick;
            lock.entity_filter_state = if entity_attachment != 0 {
                EntityFilterState {
                    cutoff_hz: 250,
                    degree: 2,
                }
            } else {
                EntityFilterState {
                    cutoff_hz: 0,
                    degree: 0,
                }
            };
            lock.entity_filter_extra_gain = if entity_attachment != 0 { 3.0 } else { 1.0 };
            lock.attached_entity = entity_attachment;
        }
        self.control.enabled.store(true, Ordering::Release);
    }

    pub(crate) fn sampled_sound_length(&self, id: u32) -> Option<u64> {
        self.simple_sound_lengths.get(&id).copied()
    }
}

/// Starts an engine and returns the handle that controls it
pub(crate) async fn start_engine(
    settings: Arc<ArcSwap<GameSettings>>,
    timekeeper: Arc<Timekeeper>,
    sampled_sounds: &[SampledSound],
    cache_manager: &mut CacheManager,
) -> Result<EngineHandle> {
    let mut wavs = FxHashMap::default();
    let mut wav_lengths = FxHashMap::default();
    for sound in sampled_sounds {
        let bytes = cache_manager
            .load_media_by_name(&sound.media_filename)
            .await?;
        let wav = WavReader::new(Cursor::new(bytes))?;
        let length_nanos = wav_len_nanos(&wav);

        if wavs.insert(sound.sound_id, wav).is_some() {
            log::warn!(
                "Duplicate sound with ID {}, name {}",
                sound.sound_id,
                sound.media_filename
            );
        };
        wav_lengths.insert(sound.sound_id, length_nanos);
    }

    let control = Arc::new(SharedControl::new(settings.clone(), Vector3::zero()));
    let cancellation_token = tokio_util::sync::CancellationToken::new();
    let (freeing_sender, freeing_receiver) =
        crossbeam_channel::bounded(NUM_PROCEDURAL_ENTITY_SLOTS * 2);
    if settings.load().audio.enable_audio {
        let control_clone = control.clone();
        let (tx, rx) = tokio::sync::oneshot::channel::<Result<()>>();
        let token_clone = cancellation_token.clone();
        tokio::task::spawn(async move {
            let host = cpal::default_host();
            // TODO use a selected device from the settings
            let output_device =
                select_output_device(&host, &settings.load().audio.preferred_output_device)?;
            let mut device_configs: Vec<_> = output_device.supported_output_configs()?.collect();
            device_configs.sort_by(cpal::SupportedStreamConfigRange::cmp_default_heuristics);
            log::info!("Available device configs: {:?}", device_configs);
            // TODO use a selected config from the settings
            let selected_config_range = device_configs.last().context("no supported config")?;
            let selected_config = selected_config_range.with_max_sample_rate();
            log::info!("Using config: {:?}", selected_config);

            let handle = tokio::runtime::Handle::current();
            let mut engine_state = EngineState::new(
                control_clone,
                selected_config.clone(),
                timekeeper,
                wavs,
                freeing_sender,
            )?;

            // OS threads rather than async tasks because cpal stream is not Send
            let _ = std::thread::spawn(move || {
                let startup_work = move || {
                    let mut first_callback = true;
                    let stream = output_device
                        .build_output_stream(
                            &selected_config.clone().into(),
                            move |data: &mut [f32], info: &OutputCallbackInfo| {
                                if first_callback {
                                    log::info!(
                                        "audio render thread is live, {} samples in first callback",
                                        data.len() / selected_config.channels() as usize
                                    );
                                    if let Err(e) =
                                        audio_thread_priority::promote_current_thread_to_real_time(
                                            (data.len() / selected_config.channels() as usize)
                                                as u32,
                                            selected_config.sample_rate().0,
                                        )
                                    {
                                        // Logging (and string formatting, and heap allocation, eww)
                                        // on the audio thread, but only doing it for one buffer,
                                        // at startup, in an exceptional case
                                        log::error!(
                                            "Failed to promote audio thread to real time: {:?}",
                                            e
                                        );
                                    }
                                    first_callback = false;
                                }
                                engine_state.callback(data, info);
                            },
                            move |err| {
                                log::error!("Output stream error: {}", err);
                            },
                            None,
                        )
                        .context("error creating output stream")?;

                    stream.play().context("error playing stream")?;
                    Ok(stream)
                };
                // We need to not drop the stream until after the cancellation token is cancelled
                let stream = match startup_work() {
                    Ok(stream) => {
                        tx.send(Ok(()))
                            .expect("Audio startup notification: Listener dropped");
                        stream
                    }
                    Err(e) => {
                        log::error!("Failed to start audio: {}", e);
                        tx.send(Err(e)).unwrap();
                        return;
                    }
                };
                handle.block_on(token_clone.cancelled());
                if let Err(e) = stream.pause() {
                    log::warn!("Failure pausing audio stream: {:?}", e)
                }
                drop(stream);
            });
            Ok::<(), anyhow::Error>(())
        })
        .await
        .context("audio spawn task panicked")?
        .context("audio spawn task returned error")?;
        rx.await
            .context("Waiting for audio engine startup failed")?
            .context("Audio engine startup")?;
        log::info!("Audio engine started successfully");
    } else {
        log::info!("Returning an empty audio engine because audio was disabled");
    }

    Ok(EngineHandle {
        control,
        _drop_guard: cancellation_token.drop_guard(),
        allocator_state: Mutex::new(AllocatorState::new()),
        simple_sound_lengths: wav_lengths,
        freeing_receiver,
    })
}

fn wav_len_nanos<R: std::io::Read>(wav: &WavReader<R>) -> u64 {
    let samples = wav.duration() as f64;
    let rate = wav.spec().sample_rate as f64;
    let length_nanos = (1_000_000_000.0 * samples / rate) as u64;
    length_nanos
}

fn select_output_device(host: &Host, prefix: &str) -> Result<cpal::Device> {
    if prefix.is_empty() {
        log::info!("Using default output device");
        return host
            .default_output_device()
            .context("no default output device");
    }
    let devices = host.output_devices()?;
    for device in devices {
        if device.name().unwrap().starts_with(prefix) {
            log::info!("Using output device: {}", device.name().unwrap());
            return Ok(device);
        }
    }
    log::warn!("No output device with name starting with {} found", prefix);
    host.default_output_device()
        .context("no default output device")
}

struct EngineState {
    control: Arc<SharedControl>,
    private_control: PrivateControl,
    buffer_counter: u64,
    stream_config: cpal::SupportedStreamConfig,
    initial_callback_timing: Option<(Instant, cpal::StreamInstant)>,
    initial_playback_timing: Option<(Instant, cpal::StreamInstant)>,
    game_timekeeper: Arc<Timekeeper>,

    nanos_per_sample: f64,
    total_samples_seen: u64,

    rng: rand::rngs::SmallRng,

    entity_filter_state: EntityFilterState,
    entity_filter_left: IirLpfCascade<8>,
    entity_filter_right: IirLpfCascade<8>,
    entity_filter_input_buffer: Vec<f32>,

    // HashMap: While the server has the IDs as consecutive, this isn't a protocol guarantee
    samplers: FxHashMap<u32, PrerecordedSampler>,
    freeing_sender: crossbeam_channel::Sender<Box<EntityData>>,
}

// Const rather than using log! because I don't know what locks we might have to deal when the
// logger checks whether it's enabled
const ENABLE_DEBUG_PRINT: bool = false;

impl EngineState {
    pub(crate) fn callback(&mut self, data: &mut [f32], info: &OutputCallbackInfo) {
        data.fill(0.0);
        let now = Instant::now();

        let jitter = if self.initial_callback_timing.is_none() {
            self.initial_callback_timing = Some((now, info.timestamp().callback));
            self.initial_playback_timing = Some((now, info.timestamp().playback));
            0
        } else {
            let time_delta = now - self.initial_callback_timing.unwrap().0;
            let estimated_callback = self
                .initial_callback_timing
                .unwrap()
                .1
                .add(time_delta)
                .unwrap();

            if info.timestamp().callback < estimated_callback {
                -1 * (estimated_callback
                    .duration_since(&info.timestamp().callback)
                    .unwrap()
                    .as_nanos() as i64)
            } else {
                info.timestamp()
                    .callback
                    .duration_since(&estimated_callback)
                    .unwrap()
                    .as_nanos() as i64
            }
        };

        let margin = info
            .timestamp()
            .playback
            .duration_since(&info.timestamp().callback)
            .unwrap_or(Duration::ZERO);

        let elapsed = info
            .timestamp()
            .playback
            .duration_since(&self.initial_playback_timing.unwrap().1)
            .unwrap_or(Duration::ZERO);

        let buffer_start_tick = self
            .game_timekeeper
            .time_to_tick_estimate(self.initial_playback_timing.unwrap().0.add(elapsed));

        if data.len() % self.stream_config.channels() as usize != 0 {
            panic!(
                "Output callback length {} not divisible by number of channels {}",
                data.len(),
                self.stream_config.channels()
            );
        }
        let samples = data.len() / self.stream_config.channels() as usize;

        let buffer_end_tick =
            buffer_start_tick + (data.len() as f64 * self.nanos_per_sample) as u64;

        if ENABLE_DEBUG_PRINT && self.buffer_counter % 30 == 0 {
            log::info!(
                "Output callback: {:?}, diff {:?}. Buffer length: {} samples ({} seconds). Tick {:?},  Jitter {:?}",
                info,
                margin,
                data.len(),
                data.len() as f64 / self.stream_config.sample_rate().0 as f64,
                buffer_start_tick,
                jitter
            );
            log::info!(
                "samples seen: {}, elapsed: {:?}. Effective sample rate {}",
                self.total_samples_seen,
                elapsed,
                self.total_samples_seen as f64 / elapsed.as_secs_f64()
            );
        }
        self.buffer_counter += 1;

        self.total_samples_seen += samples as u64;
        if !self.control.enabled.load(Ordering::Acquire) {
            return;
        }

        let player_state = self.control.player_state.read();

        if player_state.entity_filter_state != self.entity_filter_state {
            self.entity_filter_state = player_state.entity_filter_state;
            let degree = self.entity_filter_state.degree;
            // Recreate the entity filter
            self.entity_filter_left = if degree == 0 {
                IirLpfCascade::default()
            } else {
                IirLpfCascade::new_for_degree(
                    self.entity_filter_state.degree as usize,
                    self.entity_filter_state.cutoff_hz as f32,
                    (1_000_000_000.0 / self.nanos_per_sample) as f32,
                )
            };
            self.entity_filter_right = self.entity_filter_left;
        }
        self.entity_filter_input_buffer.clear();
        self.entity_filter_input_buffer.resize(data.len(), 0.0);

        let volumes = self.control.settings.load().audio.volumes;

        // Optimization note: on a 12900h, 256 slots in use spends about 10-15 seconds out
        // of 60 seconds wall time; while it could certainly be optimized further, it's not
        // currently critical. A lot of the time is spent in the sampler.sample call, but none
        // of the instructions inside it seem like hotspots.
        //
        // This experiment was performed with test audio blocks, which have spatial audio
        // enabled.
        'slots: for i in 0..NUM_SIMPLE_SOUND_SLOTS {
            let control_block = self.control.simple_sounds[i].read();
            let private_state = &mut self.private_control.simple_sounds[i];

            if control_block.flags & SOUND_PRESENT == 0 {
                continue;
            }
            if control_block.start_tick >= buffer_end_tick {
                continue;
            }

            // Compute the start and end samples, and the corresponding ticks.
            let (eval_start_tick, sample_range) =
                if control_block.start_tick != private_state.start_tick {
                    if ENABLE_DEBUG_PRINT {
                        log::info!("Sound {} start @{}", i, control_block.start_tick,);
                    }

                    private_state.start_tick = control_block.start_tick;
                    private_state.elapsed_samples = 0;
                    private_state.last_distance.clear();
                    private_state.last_balance.clear();
                    // A new sound is starting at some point in ths current buffer.
                    // This is how many samples into the buffer.
                    let start_offset_samples =
                        // saturating sub - we might underflow if either the sound was delivered
                        // after its scheduled start time, or if we missed it due to a tiny floating or
                        // rounding error
                        ((control_block.start_tick.saturating_sub(buffer_start_tick) as f64)
                            / self.nanos_per_sample) as usize;

                    // let remaining_samples = (samples.saturating_sub(start_offset_samples)) as u64;
                    // TODO: If a sound is truly short (fitting entirely into this buffer), we don't
                    // handle it quite right in the current prototype.

                    // The sound starts at the control block's start tick (game tick time since we
                    // haven't had a chance to accumulate any skew yet)
                    //
                    // The buffer will be filled from start_offset_samples to the end

                    (control_block.start_tick, start_offset_samples..samples)
                } else {
                    // The starting tick, in the reference frame of the audio source, but with skew
                    // correction (i.e. based on elapsed samples)
                    let start_tick = private_state.start_tick
                        + (private_state.elapsed_samples as f64 * self.nanos_per_sample) as u64;
                    (start_tick, 0..samples)
                };

            private_state.elapsed_samples += sample_range.len() as u64;
            if ENABLE_DEBUG_PRINT && self.buffer_counter % 30 == 0 {
                log::info!(
                    "ID {:?}, sample range: {:?}, start tick: {:?}, elapsed samples: {:?}",
                    control_block.id,
                    sample_range,
                    eval_start_tick,
                    private_state.elapsed_samples
                );
            }

            let sampler = match self.samplers.get(&control_block.id) {
                Some(x) => x,
                None => {
                    continue 'slots;
                }
            };
            let buf = if control_block.flags & SOUND_BYPASS_ATTACHED_ENTITY_FILTER == 0 {
                self.entity_filter_input_buffer.deref_mut()
            } else {
                &mut *data
            };

            Self::sample_simple_sound(
                self.stream_config.channels() as usize,
                self.nanos_per_sample,
                buf,
                sample_range,
                &player_state,
                &control_block,
                private_state,
                eval_start_tick,
                sampler,
                volumes.volume_for(control_block.source),
            );
        }
        self.update_entity_data();
        for i in 0..NUM_PROCEDURAL_ENTITY_SLOTS {
            let entity = match self.private_control.entity_data[i].as_ref() {
                Some(x) => x,
                None => {
                    continue;
                }
            };

            let control_block = entity.control;
            if control_block.flags & SOUND_PRESENT == 0 {
                continue;
            }

            let private_state = &mut self.private_control.procedural_entity_sounds[i];

            let buf = if control_block.flags & SOUND_BYPASS_ATTACHED_ENTITY_FILTER == 0 {
                self.entity_filter_input_buffer.deref_mut()
            } else {
                &mut *data
            };

            Self::sample_entity_turbulence(
                self.stream_config.channels() as usize,
                self.nanos_per_sample,
                buf,
                &entity,
                &player_state,
                private_state,
                &mut self.rng,
                buffer_start_tick,
                samples,
                volumes.volume_for(control_block.sound_source),
            )
        }
        match self.stream_config.channels() {
            1 => self.downmix_mono(data, player_state.entity_filter_extra_gain),
            2 => self.downmix_stereo(data, player_state.entity_filter_extra_gain),
            // TODO handle the messed-up sample rate here
            _ => self.downmix_mono(data, player_state.entity_filter_extra_gain),
        }
    }

    fn new(
        control: Arc<SharedControl>,
        stream_config: cpal::SupportedStreamConfig,
        timekeeper: Arc<Timekeeper>,
        sampled_sounds: FxHashMap<u32, WavReader<Cursor<Vec<u8>>>>,
        freeing_sender: crossbeam_channel::Sender<Box<EntityData>>,
    ) -> Result<EngineState> {
        let mut samplers = FxHashMap::default();
        let mut resampler_cache = FxHashMap::default();
        for (k, v) in sampled_sounds {
            log::info!("Resampling sound ID {k}");
            samplers.insert(
                k,
                PrerecordedSampler::from_wav(
                    v,
                    stream_config.sample_rate().0,
                    &mut resampler_cache,
                )?,
            );
        }

        Ok(EngineState {
            buffer_counter: 0,
            control,
            stream_config: stream_config.clone(),
            private_control: PrivateControl::default(),
            initial_callback_timing: None,
            initial_playback_timing: None,
            game_timekeeper: timekeeper,
            // what the heck? Extra factor of 2?
            nanos_per_sample: 1_000_000_000.0 / stream_config.sample_rate().0 as f64,
            total_samples_seen: 0,

            rng: rand::rngs::SmallRng::from_rng(&mut rand::thread_rng())?,
            entity_filter_state: EntityFilterState {
                cutoff_hz: 0,
                degree: 0,
            },
            entity_filter_left: Default::default(),
            entity_filter_right: Default::default(),
            // Generously allocate memory for one second
            entity_filter_input_buffer: Vec::with_capacity(
                stream_config.sample_rate().0 as usize * 2,
            ),
            freeing_sender,
            samplers,
        })
    }
    fn sample_simple_sound(
        channels: usize,
        nanos_per_sample: f64,
        buffer: &mut [f32],
        sample_range: Range<usize>,
        player_state: &PlayerState,
        control: &SimpleSoundControlBlock,
        state: &mut SimpleSoundState,
        start_tick: u64,
        sampler: &PrerecordedSampler,
        volume_multiplier: f32,
    ) {
        let left_ear_azimuth = player_state.azimuth_radian + std::f64::consts::FRAC_PI_2;
        let left_ear_z = left_ear_azimuth.cos();
        let left_ear_x = left_ear_azimuth.sin();
        let left_ear_vec = Vector3::new(left_ear_x, 0.0, left_ear_z);

        for sample in sample_range {
            let tick = start_tick + (sample as f64 * nanos_per_sample) as u64;
            let tick_delta = tick - control.start_tick;

            let position = player_state.position(tick);
            let distance = state
                .last_distance
                .update((position - control.position).magnitude(), 0.001);
            let travel_time = if control.flags & SOUND_MOVESPEED_ENABLED != 0 {
                ((distance / SPEED_OF_SOUND_METER_PER_SECOND) * 1_000_000_000.0) as i64
            } else {
                0
            };
            // Design note: We're pretty lax with possible i64 overflows. This is fine, since this
            // happens at around 292 years of uptime. If we wanted to get the full 584 years of u64,
            // we could do i128 math, at the slight expense of performance for all other reasonable
            // uses
            let effective_time_delta = tick_delta as i64 - travel_time;
            if effective_time_delta < 0 {
                // Sound not yet started
                continue;
            }
            if effective_time_delta as u64 > control.end_tick - control.start_tick {
                // Sound finished
                break;
            }
            let effective_time_delta = effective_time_delta as u64 % sampler.len_nanos;

            let effective_time_delta = effective_time_delta as f64 / 1_000_000_000.0;

            let amplitude = if control.flags & SOUND_SQUARELAW_ENABLED != 0 {
                let clamped_distance = distance.max(MIN_DISTANCE) as f32;
                // this affects amplitude, not power, so it should be non-squared
                // However, using something like 1.5th power sounds better in-game (although is
                // nonphysical)
                // Worth seeing if sqrt and float mul is indeed faster than powf
                volume_multiplier * control.volume / (clamped_distance * clamped_distance.sqrt())
            } else {
                volume_multiplier * control.volume
            };

            let balance_left = left_ear_vec.dot((position - control.position).normalize());
            let balance_left = state.last_balance.update(balance_left, 0.001);
            let l_amplitude = amplitude * (1.0 + 0.8 * balance_left as f32);

            let r_amplitude = amplitude * (1.0 - 0.8 * balance_left as f32);

            let (l, r) = sampler.sample(effective_time_delta / nanos_per_sample * 1_000_000_000.0);
            let (l, r) = if channels == 1 {
                (l + r / 2.0, 0.0)
            } else {
                (l, r)
            };

            for chan in 0..channels {
                let sample_value = if chan == 0 { l } else { r };
                let amplitude = if channels == 2 && control.flags & SOUND_DIRECTIONAL != 0 {
                    if chan == 0 {
                        l_amplitude
                    } else {
                        r_amplitude
                    }
                } else {
                    amplitude
                };
                let index = sample * channels + chan;
                buffer[index] += amplitude * sample_value / 4000.0;
            }
        }
    }
    fn sample_entity_turbulence(
        channels: usize,
        nanos_per_sample: f64,
        data: &mut [f32],
        entity: &EntityData,
        player_state: &PlayerState,
        scratchpad: &mut EntityScratchpad,
        rng: &mut SmallRng,
        buffer_start_tick: u64,
        samples: usize,
        volume_multiplier: f32,
    ) {
        let control_block = entity.control;
        let entity = match &entity.entity {
            Some(x) => x,
            None => {
                eprintln!("Entity absent");
                return;
            }
        };

        let left_ear_azimuth = player_state.azimuth_radian + std::f64::consts::FRAC_PI_2;
        let left_ear_z = left_ear_azimuth.cos();
        let left_ear_x = left_ear_azimuth.sin();
        let left_ear_vec = Vector3::new(left_ear_x, 0.0, left_ear_z);

        if control_block.entity_id != scratchpad.entity_id {
            *scratchpad = EntityScratchpad {
                entity_id: control_block.entity_id,
                turbulence_iir_state: IirLpfCascade::new_equal_cascade(
                    control_block.turbulence.lpf_cutoff_hz,
                    (1_000_000_000.0 / nanos_per_sample) as f32,
                ),
                ..EntityScratchpad::default()
            }
        }
        let current_move = match entity.current_move() {
            Some(x) => x,
            None => {
                eprintln!("Entity absent");
                return;
            }
        };

        // We make a bold assumption: The audio buffer is small enough that we can assume that the
        // state of entity passing (approaching, passing by, moving away) is small enough, _and_
        // (once added), edge effects can be triggered once per buffer.

        let is_spatial = control_block.flags & SOUND_ENTITY_SPATIAL != 0;
        let is_attached_here = player_state.attached_entity == control_block.entity_id;
        let (distance_falloff_multiplier, edge_multiplier, balance_left) = if is_spatial
            && !is_attached_here
        {
            let mut min_distance = f64::MAX;
            let mut min_distance_pos = Vector3::zero();
            let mut prev_trailing: Option<Vector3<f64>> = None;

            entity.visit_sub_entities_including_trailing(
                buffer_start_tick,
                |_idx, pos, _move_dist, _m, _class| {
                    // Potential later optimization: if we're far enough compared to the
                    // trailing length, bail out of the loop early

                    // If this is a trailing entity, look at the segment between prev and
                    // this one, pretending that the entity is linked up along it
                    if control_block.flags & SOUND_ENTITY_LINK_TRAILING_ENTITIES != 0 {
                        if let Some(p_pos) = prev_trailing {
                            let prev_to_cur = pos - p_pos;
                            let prev_to_player = player_state.position - p_pos;
                            let cur_to_player = player_state.position - pos;
                            // We are passing if prev_to_cur points in the same direction as
                            // prev_to_player, and cur_to_player points opposite prev_to_cur
                            if Vector3::dot(prev_to_player, prev_to_cur) > 0.0
                                && Vector3::dot(cur_to_player, prev_to_cur) < 0.0
                            {
                                let nearest_pos: Vector3<f64> =
                                    prev_to_player.project_on(prev_to_cur) + p_pos;
                                let dist = Vector3::magnitude(nearest_pos - player_state.position);
                                if dist < min_distance {
                                    min_distance = dist;
                                    min_distance_pos = nearest_pos;
                                }
                            }
                        }
                    }
                    // Now look at the trailing entity itself
                    let dist = Vector3::magnitude(pos - player_state.position);
                    if dist < min_distance {
                        min_distance = dist;
                        min_distance_pos = pos;
                    }

                    prev_trailing = Some(pos);
                    ControlFlow::Continue(())
                },
            );

            let displacement = min_distance_pos - player_state.position;
            let effective_dir = displacement.normalize();
            let mut balance_left = left_ear_vec.dot(effective_dir);
            if balance_left.is_nan() {
                balance_left = 0.0;
            }

            let clamped_distance = min_distance.max(MIN_ENTITY_DISTANCE) as f32;
            (1.0 / clamped_distance, 1.0, balance_left)
        } else {
            (1.0, 1.0, 0.0)
        };

        for sample in 0..samples {
            let tick = buffer_start_tick + (sample as f64 * nanos_per_sample) as u64;
            let move_time = current_move.elapsed(tick);
            let current_velocity = current_move.instantaneous_velocity(move_time);

            // Theoretically should be N^5 or N^6, but this is subjectively better
            let speed_multiplier = (current_velocity.magnitude() / 70.0).powi(4);

            let control_volume = if is_attached_here {
                control_block.turbulence.volume_attached
            } else {
                control_block.turbulence.volume
            };
            let turbulence_amplitude = control_volume
                * distance_falloff_multiplier
                * edge_multiplier
                * speed_multiplier
                * volume_multiplier;

            let balance_left = scratchpad.last_balance.update(balance_left, 0.001);
            let l_amplitude = turbulence_amplitude * (1.0 - 0.8 * balance_left as f32);

            let r_amplitude = turbulence_amplitude * (1.0 + 0.8 * balance_left as f32);
            let (l, r) = sample_turbulence(scratchpad, rng);
            let (l, r) = if channels == 1 {
                (l + r / 2.0, 0.0)
            } else {
                (l, r)
            };
            for chan in 0..channels {
                let sample_value = if chan == 0 { l } else { r };
                let amplitude = if channels == 2 {
                    if chan == 0 {
                        l_amplitude
                    } else {
                        r_amplitude
                    }
                } else {
                    turbulence_amplitude
                };
                let index = sample * channels + chan;
                data[index] += amplitude * sample_value / 800.0;
            }
        }
    }
    fn downmix_mono(&mut self, dst: &mut [f32], gain: f32) {
        assert_eq!(self.entity_filter_input_buffer.len(), dst.len());
        for (in_sample, out_sample) in self.entity_filter_input_buffer.iter().zip(dst.iter_mut()) {
            *out_sample = gain * self.entity_filter_left.sample(*in_sample);
        }
    }
    fn downmix_stereo(&mut self, dst: &mut [f32], gain: f32) {
        assert_eq!(self.entity_filter_input_buffer.len(), dst.len());
        assert_eq!(self.entity_filter_input_buffer.len() % 2, 0);
        for (idx, (in_sample, out_sample)) in self
            .entity_filter_input_buffer
            .iter()
            .zip(dst.iter_mut())
            .enumerate()
        {
            if idx % 2 == 0 {
                *out_sample = gain * self.entity_filter_left.sample(*in_sample);
            }
        }
        for (idx, (in_sample, out_sample)) in self
            .entity_filter_input_buffer
            .iter()
            .zip(dst.iter_mut())
            .enumerate()
        {
            if idx % 2 == 1 {
                *out_sample = gain * self.entity_filter_right.sample(*in_sample);
            }
        }
    }

    fn update_entity_data(&mut self) {
        for i in 0..NUM_PROCEDURAL_ENTITY_SLOTS {
            // The store portion doesn't matter
            // The load portion also really doesn't matter; we aren't synchronizing with anything
            if let Some(data) = self.control.entity_data_slots[i].swap(None, Ordering::AcqRel) {
                let old = self.private_control.entity_data[i].take();
                if data.control.flags & SOUND_PRESENT != 0 {
                    self.private_control.entity_data[i] = Some(data);
                }
                if let Some(old) = old {
                    self.free_entity_data_off_thread(old)
                }
            }
        }
    }

    fn free_entity_data_off_thread(&mut self, data: Box<EntityData>) {
        // If the channel is full, we can't really do anything other than sadly give up
        match self.freeing_sender.try_send(data) {
            Ok(()) => {}
            // Two error cases, same code, different rationale
            Err(TrySendError::Disconnected(data)) => {
                // Shutdown ordering error; if we've lost the freeing receiver, we've also lost
                // any paths for inbound data, some leakage is a reasonable tradeoff here
                Box::leak(data);
            }
            Err(TrySendError::Full(data)) => {
                // This shouldn't happen as long as the buffer is large enough, if it does happen
                // we'll just leak the data to avoid blocking on this thread
                Box::leak(data);
            }
        }
    }
}

/// The player's movement state
/// This must be small and copiable since it's accessed via a seqlock
#[derive(Clone, Copy, Debug)]
#[repr(align(64))]
pub(crate) struct PlayerState {
    position: Vector3<f64>,
    velocity: Vector3<f32>,
    acceleration: Vector3<f32>,
    azimuth_radian: f64,
    position_timebase_tick: u64,
    entity_filter_state: EntityFilterState,
    entity_filter_extra_gain: f32,
    attached_entity: u64,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct EntityFilterState {
    cutoff_hz: u32,
    degree: u32,
}

impl PlayerState {
    #[inline]
    pub(crate) fn position(&self, tick: u64) -> Vector3<f64> {
        let dt = tick.saturating_sub(self.position_timebase_tick) as f64 / 1_000_000_000.0;
        let x = self.position.x
            + (self.velocity.x as f64 * dt)
            + 0.5 * (self.acceleration.x as f64 * dt * dt);
        let y = self.position.y
            + (self.velocity.y as f64 * dt)
            + 0.5 * (self.acceleration.y as f64 * dt * dt);
        let z = self.position.z
            + (self.velocity.z as f64 * dt)
            + 0.5 * (self.acceleration.z as f64 * dt * dt);
        Vector3::new(x, y, z)
    }
}

pub const NUM_SIMPLE_SOUND_SLOTS: usize = 256;
pub const NUM_PROCEDURAL_ENTITY_SLOTS: usize = 64;

/// The private control state, used internally by the audio engine to track the state of
/// the player and sounds
struct PrivateControl {
    simple_sounds: Box<[SimpleSoundState; NUM_SIMPLE_SOUND_SLOTS]>,
    procedural_entity_sounds: Box<[EntityScratchpad; NUM_PROCEDURAL_ENTITY_SLOTS]>,
    entity_data: [Option<Box<EntityData>>; NUM_PROCEDURAL_ENTITY_SLOTS],
}
impl Default for PrivateControl {
    fn default() -> PrivateControl {
        PrivateControl {
            simple_sounds: Box::new([SimpleSoundState::default(); NUM_SIMPLE_SOUND_SLOTS]),
            procedural_entity_sounds: Box::new(
                [EntityScratchpad::default(); NUM_PROCEDURAL_ENTITY_SLOTS],
            ),
            entity_data: [const { None }; NUM_PROCEDURAL_ENTITY_SLOTS],
        }
    }
}

#[derive(Clone)]
pub(crate) struct EntityData {
    pub(crate) control: ProceduralEntitySoundControlBlock,
    // This contains allocations; we promise to never mutate it on the audio thread,
    // nor to drop it on the audio thread. The only thing we can do with an EntityData on the
    // audio thread is to either send it to a lock-free channel, or leak it if the channel is full.
    //
    // TODO: Refactor this so we only copy the truly necessary fields, if benchmarks show issues
    pub(crate) entity: Option<GameEntity>,
}
impl Debug for EntityData {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EntityData")
            .field("control", &self.control)
            .field(
                "entity w/ current move",
                &self.entity.as_ref().map(|x| x.current_move()),
            )
            .finish()
    }
}
impl EntityData {
    fn compute_score(&self, tick_now: u64, player_position: Vector3<f64>) -> u64 {
        if self.control.flags & SOUND_PRESENT == 0 {
            return 0;
        }

        let entity_data = match self.entity.as_ref() {
            Some(x) => x,
            None => return 0,
        };
        // TODO: What does sticky mean for an entity that exhausts its moves?
        if self.control.flags & SOUND_STICKY != 0 {
            return u64::MAX;
        }

        let (position, velocity, _elapsed, _m) = entity_data.position_velocity(tick_now);
        let distance = (position - player_position).magnitude();

        const BASE_SCORE: u64 = 1 << 60;
        const BASE_SCORE_F64: f64 = BASE_SCORE as f64;
        let mut distance_score = if self.control.flags & SOUND_SQUARELAW_ENABLED != 0 {
            let dropoff = distance.max(1.0);
            let score = BASE_SCORE_F64 / dropoff;
            if !score.is_finite() || !score.is_sign_positive() {
                log::warn!(
                    "Nonsensical score for control block {:?}, tick {}, pos {:?}",
                    self,
                    tick_now,
                    player_position
                );
                return 0;
            }
            score as u64
        } else {
            BASE_SCORE
        };

        let mut volume_estimate = 0.0;
        volume_estimate += self.control.turbulence.volume * (velocity.magnitude() / 70.0).powi(4);

        if volume_estimate < 0.001 {
            distance_score /= 10;
        }

        distance_score
    }
}

/// The shared control state, via which sounds can be controlled
pub(crate) struct SharedControl {
    enabled: AtomicBool,
    player_state: SeqLock<PlayerState>,
    simple_sounds: Box<[SeqLock<SimpleSoundControlBlock>; NUM_SIMPLE_SOUND_SLOTS]>,
    entity_data_slots: [AtomicOptionBox<EntityData>; NUM_PROCEDURAL_ENTITY_SLOTS],
    settings: Arc<ArcSwap<GameSettings>>,
}
impl SharedControl {
    fn new(settings: Arc<ArcSwap<GameSettings>>, initial_position: Vector3<f64>) -> SharedControl {
        const SIMPLE_SOUND_CONST_INIT: SeqLock<SimpleSoundControlBlock> =
            SeqLock::new(SimpleSoundControlBlock::const_default());
        SharedControl {
            enabled: AtomicBool::new(false),
            player_state: SeqLock::new(PlayerState {
                position: initial_position,
                velocity: Vector3::zero(),
                acceleration: Vector3::zero(),
                azimuth_radian: 0.0,
                position_timebase_tick: 0,
                entity_filter_state: EntityFilterState {
                    cutoff_hz: 0,
                    degree: 0,
                },
                entity_filter_extra_gain: 1.0,
                attached_entity: 0,
            }),
            simple_sounds: Box::new([SIMPLE_SOUND_CONST_INIT; NUM_SIMPLE_SOUND_SLOTS]),
            entity_data_slots: [const { AtomicOptionBox::none() }; NUM_PROCEDURAL_ENTITY_SLOTS],
            // Start all volumes at 0 until they are set by the game loop
            settings,
        }
    }
}

// Note: This must be small and copiable, since it's accessed via a seqlock
#[derive(Clone, Copy, Debug)]
#[repr(align(64))]
/// The state of a sound that plays at some time and place.
pub(crate) struct SimpleSoundControlBlock {
    /// The flags for the sound. See SOUND_* constants
    ///
    /// Note that the type of this field is subject to change if more than 8 flags are needed.
    pub flags: FlagsType,
    /// The position of the sound in 3D space. The sound is assumed to be stationary,
    /// but the player's motion may be considered for Doppler effect.
    pub position: Vector3<f64>,
    /// The volume of the sound. If this is set to 0.0, the sound is silenced.
    pub volume: f32,
    /// The starting tick of the sound
    pub start_tick: u64,
    /// The sound ID (per the media manager)
    pub id: u32,
    /// The ending tick of the sound. If the span from start_tick to end_tick is greater than
    /// the length of the sound, it will loop. u64::MAX is treated as effectively infinite.
    ///
    /// Skew policy: If the tick clock and codec sample clock go out of skew, the sound will stop
    /// based on TBD
    pub end_tick: u64,
    /// The source to attribute the sound to
    pub source: SoundSource,
}

impl SimpleSoundControlBlock {
    const fn const_default() -> Self {
        SimpleSoundControlBlock {
            flags: 0,
            position: Vector3::new(0.0, 0.0, 0.0),
            volume: 0.0,
            start_tick: 0,
            id: 0,
            end_tick: 0,
            source: SoundSource::SoundsourceUnspecified,
        }
    }

    /// Heuristically identify which sounds are most important. Higher values are considered more
    /// important.
    pub(crate) fn compute_score(&self, tick_now: u64, player_position: Vector3<f64>) -> u64 {
        if self.flags & SOUND_PRESENT == 0 {
            return 0;
        }

        // We assume that the player won't move faster than the speed of sound
        let distance = (self.position - player_position).magnitude();
        let estimated_end_tick = if self.flags & SOUND_MOVESPEED_ENABLED != 0 {
            let delay_ticks =
                ((distance / SPEED_OF_SOUND_METER_PER_SECOND) * 1_000_000_000.0) as u64;
            // The estimated end tick in the *player* reference frame
            self.end_tick.saturating_add(delay_ticks)
        } else {
            self.end_tick
        };
        // Check if the sound is over first; we want sticky sounds to still get cleaned up, just not
        // while they're still running.
        if estimated_end_tick < tick_now {
            return 0;
        }
        if self.flags & SOUND_STICKY != 0 {
            return u64::MAX;
        }
        const BASE_SCORE: u64 = 1 << 60;
        const BASE_SCORE_F64: f64 = BASE_SCORE as f64;
        return if self.flags & SOUND_SQUARELAW_ENABLED != 0 {
            let dropoff = distance.max(1.0);
            let score = BASE_SCORE_F64 / dropoff;
            if !score.is_finite() || !score.is_sign_positive() {
                log::warn!(
                    "Nonsensical score for control block {:?}, tick {}, pos {:?}",
                    self,
                    tick_now,
                    player_position
                );
                return 0;
            }
            score as u64
        } else {
            BASE_SCORE
        };
    }
}

#[repr(align(64))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct ProceduralEntitySoundControlBlock {
    pub(crate) flags: FlagsType,
    pub(crate) entity_id: u64,
    pub(crate) turbulence: TurbulenceSourceControlBlock,
    pub(crate) sound_source: SoundSource,
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct TurbulenceSourceControlBlock {
    pub(crate) volume: f32,
    pub(crate) volume_attached: f32,
    pub(crate) lpf_cutoff_hz: f32,
}

impl ProceduralEntitySoundControlBlock {
    const fn const_default() -> ProceduralEntitySoundControlBlock {
        ProceduralEntitySoundControlBlock {
            flags: 0,
            entity_id: 0,
            turbulence: TurbulenceSourceControlBlock {
                volume: 0.0,
                volume_attached: 0.0,
                lpf_cutoff_hz: 1000.0,
            },
            sound_source: SoundSource::SoundsourceUnspecified,
        }
    }
}

#[derive(Copy, Clone, Debug)]
struct SimpleSoundState {
    start_tick: u64,
    elapsed_samples: u64,
    last_distance: SmoothedVar,
    last_balance: SmoothedVar,
}
impl Default for SimpleSoundState {
    fn default() -> Self {
        SimpleSoundState {
            start_tick: 0,
            elapsed_samples: 0,
            last_distance: SmoothedVar::default(),
            last_balance: SmoothedVar::default(),
        }
    }
}

#[derive(Copy, Clone, Debug)]
struct SmoothedVar {
    val: f64,
}
impl Default for SmoothedVar {
    fn default() -> Self {
        SmoothedVar { val: 0.0 }
    }
}
impl SmoothedVar {
    #[inline]
    fn update(&mut self, val: f64, mix: f64) -> f64 {
        if self.val.is_nan() {
            self.val = val;
        } else {
            self.val = self.val * (1.0 - mix) + val * mix;
        }
        self.val
    }
    #[allow(dead_code)]
    #[inline]
    fn reset(&mut self, val: f64) {
        self.val = val;
    }
    #[allow(dead_code)]
    #[inline]
    fn get(&self) -> f64 {
        self.val
    }
    #[allow(dead_code)]
    #[inline]
    fn clear(&mut self) {
        self.val = f64::NAN;
    }
}

pub type FlagsType = u8;

/// If set, the entry in the control block is valid and enabled
pub const SOUND_PRESENT: FlagsType = 0x1;

/// If set, the sound will be subject to speed-of-sound and doppler effects
/// Applicable for simple sounds
pub const SOUND_MOVESPEED_ENABLED: FlagsType = 0x2;
/// If set, the sound will be subject to square-law effects affecting its amplitude
/// Applicable for simple sounds
pub const SOUND_SQUARELAW_ENABLED: FlagsType = 0x4;
/// If set, the sound undergoes directionality effects
/// Applicable for simple sounds
pub const SOUND_DIRECTIONAL: FlagsType = 0x8;

/// If set, the entity sound has spatial effects
pub const SOUND_ENTITY_SPATIAL: FlagsType = 0x2;

/// If set, the sound is considered sticky, and will never be deallocated
pub const SOUND_STICKY: FlagsType = 0x10;
/// If set, the sound bypasses the attached entity filter. Otherwise, the attached entity filter
/// is applied.
pub const SOUND_BYPASS_ATTACHED_ENTITY_FILTER: FlagsType = 0x20;
/// If set, the segments connecting trailing entities also emit sound. If cleared, the trailing
/// entities act as point sources.
///
/// This should be set for all high-speed entities (i.e. a trailing entity passes in <0.25 sec),
/// otherwise the resulting sound is full of artifacts
pub const SOUND_ENTITY_LINK_TRAILING_ENTITIES: FlagsType = 0x40;

/// The minimum distance considered for square-law effects. By enforcing this, we avoid
/// excessive amplitudes for near-zero distances.
pub const MIN_DISTANCE: f64 = 1.0;
pub const MIN_ENTITY_DISTANCE: f64 = 5.0;

pub const SPEED_OF_SOUND_METER_PER_SECOND: f64 = 343.0;

const OVERSAMPLE_FACTOR: u32 = 1;

struct PrerecordedSampler {
    len_nanos: u64,
    data: Vec<(f32, f32)>,
}
impl PrerecordedSampler {
    fn from_wav<R: std::io::Read>(
        wav: WavReader<R>,
        target_sample_rate: u32,
        resampler_cache: &mut FxHashMap<(u32, u32), FftFixedInOut<f32>>,
    ) -> Result<Self> {
        let len_nanos = wav_len_nanos(&wav);
        let source_rate = wav.spec().sample_rate;
        let source_channels = wav.spec().channels as usize;
        let internal_target_rate = target_sample_rate * OVERSAMPLE_FACTOR;
        if source_channels != 1 && source_channels != 2 {
            bail!(
                "Unsupported number of channels: {}; only 1 or 2 are supported",
                source_channels
            );
        }

        let mut left = Vec::new();
        let mut right = Vec::new();
        for (i, sample) in wav.into_samples::<i32>().enumerate() {
            let sample = sample?;
            if source_channels == 1 {
                left.push(sample as f32);
                right.push(sample as f32);
            } else if source_channels == 2 && i % 2 == 0 {
                left.push(sample as f32);
            } else if source_channels == 2 && i % 2 == 1 {
                right.push(sample as f32);
            }
        }

        // FFT chunk size, has nothing to do with the game map chunk size
        const CHUNK_SIZE: usize = 4096;

        let resampler = match resampler_cache.entry((source_rate, internal_target_rate)) {
            Entry::Occupied(mut v) => {
                v.get_mut().reset();
                v.into_mut()
            }
            Entry::Vacant(v) => v.insert(rubato::FftFixedInOut::new(
                source_rate as usize,
                internal_target_rate as usize,
                CHUNK_SIZE,
                2,
            )?),
        };
        let output_delay = resampler.output_delay();
        let mut left_output = Vec::new();
        let mut right_output = Vec::new();

        let mut pos = 0;

        loop {
            use rubato::Resampler;
            let input_frames = resampler.input_frames_next();
            let remaining = left.len() - pos;
            if remaining < input_frames {
                if remaining > 0 {
                    let outputs =
                        resampler.process_partial(Some(&[&left[pos..], &right[pos..]]), None)?;
                    assert_eq!(outputs.len(), 2);
                    let mut outputs = outputs.into_iter();
                    left_output.extend(outputs.next().unwrap());
                    right_output.extend(outputs.next().unwrap());
                }

                let leftover_outputs = resampler.process_partial::<Vec<f32>>(None, None)?;
                assert_eq!(leftover_outputs.len(), 2);
                let mut leftover_outputs = leftover_outputs.into_iter();
                left_output.extend(leftover_outputs.next().unwrap());
                right_output.extend(leftover_outputs.next().unwrap());
                break;
            } else {
                let outputs = resampler.process(
                    &[
                        &left[pos..pos + input_frames],
                        &right[pos..pos + input_frames],
                    ],
                    None,
                )?;
                assert_eq!(outputs.len(), 2);
                let mut outputs = outputs.into_iter();
                left_output.extend(outputs.next().unwrap());
                right_output.extend(outputs.next().unwrap());
                pos += input_frames;
            }
        }

        let data = left_output
            .into_iter()
            .zip(right_output.into_iter())
            .skip(output_delay)
            .map(|(l, r)| (l, r))
            .collect();
        Ok(Self { len_nanos, data })
    }

    #[inline]
    fn sample(&self, sample: f64) -> (f32, f32) {
        let upscaled = sample * OVERSAMPLE_FACTOR as f64;
        let preceding = upscaled.floor();
        let following = upscaled.ceil();
        let fpart = (upscaled - preceding) as f32;

        let preceding_sample = if preceding < 0.0 {
            (0.0, 0.0)
        } else if preceding as usize >= self.data.len() {
            (0.0, 0.0)
        } else {
            self.data[preceding as usize]
        };

        let following_sample = if following < 0.0 {
            (0.0, 0.0)
        } else if following as usize >= self.data.len() {
            (0.0, 0.0)
        } else {
            self.data[following as usize]
        };

        (
            (1.0 - fpart) * preceding_sample.0 + fpart * following_sample.0,
            (1.0 - fpart) * preceding_sample.1 + fpart * following_sample.1,
        )
    }
}

#[inline]
fn sample_turbulence(scratchpad: &mut EntityScratchpad, rng: &mut SmallRng) -> (f32, f32) {
    let rng_val = rng.gen_range(-1.0..1.0) * 4000.0;
    let filtered = scratchpad.turbulence_iir_state.sample(rng_val);
    (filtered, filtered)
}

#[derive(Copy, Clone, Debug)]
struct EntityScratchpad {
    // Really simple first-order IIR filter, single state
    turbulence_iir_state: IirLpfCascade<1>,
    // The entity ID, used to make sure that we're looking at the same entity
    entity_id: u64,
    last_balance: SmoothedVar,
    // Used to provide an extra burst of sound at edges
    // TODO: Re-add if we start having edge effects
    // edge_cycle: f32,
    // edge_strength: f32,
}
impl Default for EntityScratchpad {
    fn default() -> Self {
        EntityScratchpad {
            turbulence_iir_state: IirLpfCascade::default(),
            entity_id: u64::MAX,
            last_balance: Default::default(),
            // edge_cycle: -1.0,
            // edge_strength: 0.0,
        }
    }
}

// Exposed for benchmarks
#[doc(hidden)]
pub mod generated_eqns {
    #[inline]
    #[doc(hidden)]
    pub fn travel_time_newton_raphson(
        dt: f64,
        rt: f64,
        px: f64,
        py: f64,
        pz: f64,
        vx: f64,
        vy: f64,
        vz: f64,
        ax: f64,
        ay: f64,
        az: f64,
    ) -> f64 {
        /*
        See attached ipynb for symbolic derivation.

        It's tempting to optimize this using Herbie. Sadly, the results are nonsensical.
        e.g. there are results with higher reported accuracy that disregard dt entirely.

        Nevertheless, here's the FPCore form for reference:

        FPCore form:
        (FPCore (dt SPEED_OF_SOUND_METER_PER_SECOND ax rt px vx ay py vy az pz vz)
        :precision binary64
        (let* ((t_0 (- (* 2.0 dt) (* 2.0 rt)))
               (t_1 (+ (- dt) rt))
               (t_2 (pow t_1 2.0))
               (t_3 (+ (+ (* (* 0.5 ay) t_2) py) (* vy t_1)))
               (t_4 (+ (+ (* (* 0.5 ax) t_2) px) (* vx t_1)))
               (t_5 (+ (+ (* (* 0.5 az) t_2) pz) (* vz t_1)))
               (t_6 (sqrt (+ (+ (pow t_4 2.0) (pow t_3 2.0)) (pow t_5 2.0)))))
          (-
           dt
           (/
            (- (* dt SPEED_OF_SOUND_METER_PER_SECOND) t_6)
            (-
             SPEED_OF_SOUND_METER_PER_SECOND
             (/
              (+
               (+
                (* (* (/ 1.0 2.0) (- (* ax t_0) (* 2.0 vx))) t_4)
                (* (* (/ 1.0 2.0) (- (* ay t_0) (* 2.0 vy))) t_3))
               (* (* (/ 1.0 2.0) (- (* az t_0) (* 2.0 vz))) t_5))
              t_6))))))
        */
        //
        // Naive Rust code from sympy:
        dt - (dt * super::SPEED_OF_SOUND_METER_PER_SECOND
            - ((0.5 * ax * (-dt + rt).powi(2) + px + vx * (-dt + rt)).powi(2)
                + (0.5 * ay * (-dt + rt).powi(2) + py + vy * (-dt + rt)).powi(2)
                + (0.5 * az * (-dt + rt).powi(2) + pz + vz * (-dt + rt)).powi(2))
            .sqrt())
            / (super::SPEED_OF_SOUND_METER_PER_SECOND
                - ((1_f64 / 2.0)
                    * (ax * (2.0 * dt - 2.0 * rt) - 2.0 * vx)
                    * (0.5 * ax * (-dt + rt).powi(2) + px + vx * (-dt + rt))
                    + (1_f64 / 2.0)
                        * (ay * (2.0 * dt - 2.0 * rt) - 2.0 * vy)
                        * (0.5 * ay * (-dt + rt).powi(2) + py + vy * (-dt + rt))
                    + (1_f64 / 2.0)
                        * (az * (2.0 * dt - 2.0 * rt) - 2.0 * vz)
                        * (0.5 * az * (-dt + rt).powi(2) + pz + vz * (-dt + rt)))
                    / ((0.5 * ax * (-dt + rt).powi(2) + px + vx * (-dt + rt)).powi(2)
                        + (0.5 * ay * (-dt + rt).powi(2) + py + vy * (-dt + rt)).powi(2)
                        + (0.5 * az * (-dt + rt).powi(2) + pz + vz * (-dt + rt)).powi(2))
                    .sqrt())
    }
}

/// A cascade of *single-pole* filters.
#[derive(Clone, Copy, Debug)]
struct IirLpfCascade<const N: usize> {
    gains: [f32; N],
    states: [f32; N],
}
impl<const N: usize> IirLpfCascade<N> {
    fn new_equal_cascade(cutoff_freq: f32, sample_rate: f32) -> Self {
        let decay = f32::exp(-std::f32::consts::TAU * (cutoff_freq / sample_rate));
        let gain = 1.0 - decay;
        IirLpfCascade {
            gains: [gain; N],
            states: [0.0; N],
        }
    }

    fn new_for_degree(degree: usize, cutoff_freq: f32, sample_rate: f32) -> Self {
        let decay = f32::exp(-std::f32::consts::TAU * (cutoff_freq / sample_rate));
        let gain = 1.0 - decay;
        let mut gains = [1.0; N];
        for i in 0..degree {
            gains[i] = gain;
        }
        IirLpfCascade {
            gains,
            states: [0.0; N],
        }
    }

    fn sample(&mut self, input: f32) -> f32 {
        let mut val = input;
        for i in 0..N {
            self.states[i] += self.gains[i] * (val - self.states[i]);
            val = self.states[i];
        }
        val
    }
}
impl<const N: usize> Default for IirLpfCascade<N> {
    fn default() -> Self {
        IirLpfCascade {
            gains: [1.0; N],
            states: [0.0; N],
        }
    }
}

struct MapSoundEmitter {
    token: Option<SimpleSoundToken>,
    sound_id: u32,
    volume: f32,
}

pub(crate) struct MapSoundState {
    emitters: FxHashMap<BlockCoordinate, MapSoundEmitter>,
    engine: Arc<EngineHandle>,
}
impl MapSoundState {
    pub(crate) fn new(engine: Arc<EngineHandle>) -> Self {
        Self {
            emitters: FxHashMap::default(),
            engine,
        }
    }

    fn split_borrow(
        &mut self,
    ) -> (
        &mut FxHashMap<BlockCoordinate, MapSoundEmitter>,
        &EngineHandle,
    ) {
        (&mut self.emitters, &self.engine)
    }

    fn make_control_block(
        coord: BlockCoordinate,
        tick_now: u64,
        sound_id: u32,
        volume: f32,
    ) -> SimpleSoundControlBlock {
        SimpleSoundControlBlock {
            flags: SOUND_PRESENT
                | SOUND_MOVESPEED_ENABLED
                | SOUND_SQUARELAW_ENABLED
                | SOUND_DIRECTIONAL,
            position: coord.into(),
            volume,
            start_tick: tick_now,
            id: sound_id,
            end_tick: u64::MAX,
            source: SoundSource::SoundsourceWorld,
        }
    }

    pub(crate) fn insert_or_update(
        &mut self,
        tick_now: u64,
        player_pos: Vector3<f64>,
        coord: BlockCoordinate,
        sound_id: u32,
        volume: f32,
    ) {
        let control_block = Self::make_control_block(coord, tick_now, sound_id, volume);
        let entry = self.emitters.entry(coord);
        match entry {
            Entry::Occupied(e) => {
                let v = e.into_mut();
                {
                    v.token = self.engine.insert_or_update_simple_sound(
                        tick_now,
                        player_pos,
                        control_block,
                        v.token,
                    );
                    v.sound_id = sound_id;
                    v.volume = volume;
                }
            }
            Entry::Vacant(v) => {
                v.insert(MapSoundEmitter {
                    token: self.engine.insert_or_update_simple_sound(
                        tick_now,
                        player_pos,
                        control_block,
                        None,
                    ),
                    sound_id,
                    volume,
                });
            }
        };
    }

    pub(crate) fn remove(&mut self, coord: BlockCoordinate) {
        if let Some(v) = self.emitters.remove(&coord) {
            if let Some(tok) = v.token {
                self.engine.remove_simple_sound(tok);
            }
        }
    }

    pub(crate) fn remove_chunk(&mut self, chunk: ChunkCoordinate) {
        let mut to_remove: SmallVec<[BlockCoordinate; 16]> = smallvec::SmallVec::new();
        for k in self.emitters.keys() {
            if k.chunk() == chunk {
                to_remove.push(*k);
            }
        }
        for k in to_remove {
            self.remove(k)
        }
    }

    fn heal(
        engine: &EngineHandle,
        tick_now: u64,
        player_pos: Vector3<f64>,
        k: BlockCoordinate,
        v: &mut MapSoundEmitter,
    ) -> HealResult {
        if let Some(tok) = v.token {
            if !engine.is_token_evicted(tok) {
                return HealResult::NoAction;
            }
        }
        let control = Self::make_control_block(k, tick_now, v.sound_id, v.volume);
        v.token = engine.insert_or_update_simple_sound(tick_now, player_pos, control, v.token);
        if v.token.is_some() {
            HealResult::Healed
        } else {
            HealResult::Unhealed
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum HealResult {
    NoAction,
    Healed,
    Unhealed,
}

pub(crate) struct EvictedAudioHealer {
    client_state: Arc<ClientState>,
    shutdown: CancellationToken,
}
impl EvictedAudioHealer {
    pub(crate) fn new(
        client_state: Arc<ClientState>,
    ) -> (Arc<Self>, tokio::task::JoinHandle<Result<()>>) {
        let worker = Arc::new(Self {
            shutdown: client_state.shutdown.clone(),
            client_state,
        });
        let handle = {
            let worker_clone = worker.clone();
            tokio::task::spawn(worker_clone.run_healing_loop())
        };
        (worker, handle)
    }

    pub(crate) async fn run_healing_loop(self: Arc<Self>) -> Result<()> {
        tracy_client::set_thread_name!("world_audio_healer");
        while !self.shutdown.is_cancelled() {
            let sleep_deadline = std::time::Instant::now() + Duration::from_secs(1);
            tokio::select! {
                _ = tokio::time::sleep_until(tokio::time::Instant::from(sleep_deadline)) => {
                    // pass
                },
                _ = self.shutdown.cancelled() => {
                    break;
                }
            };
            // Consider pre-allocating if malloc becomes a pain
            let mut heal_vec = vec![];
            let pos = self.client_state.weakly_ordered_last_position().position;
            let tick_now = self.client_state.timekeeper.now();
            {
                let mut lock = self.client_state.world_audio.lock().await;

                let (emitters, engine) = lock.split_borrow();

                heal_vec.extend(emitters.iter_mut());
                heal_vec
                    .sort_by_key(|(k, _)| (Vector3::<f64>::from(**k) - pos).magnitude2() as u64);
                for (&k, v) in heal_vec.into_iter() {
                    match MapSoundState::heal(engine, tick_now, pos, k, v) {
                        HealResult::NoAction => continue,
                        HealResult::Healed => continue,
                        HealResult::Unhealed => break,
                    }
                }
            }
        }
        log::info!("Spatial audio heal loop exiting");

        Ok(())
    }
}