concinnity-engine 0.18.65

Runtime engine for Concinnity: ECS schedule, graphics, spawn, streaming
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
// src/audio/system.rs
//
// Audio playback: 3D positional emitters and screen-triggered cues. An internal
// system (not a declarable asset): the engine schedule constructs one whenever
// the world contains any `AudioEmitter` or `AudioCue`, so a world with neither
// never opens an audio device.

use std::collections::{HashMap, HashSet};

use super::occlusion::OcclusionSmoother;
use super::{AudioEngine, AudioVolumes, EmitterId, EmitterParams};
use concinnity_core::components::{
    AudioBus, AudioCommand, AudioCue, AudioEmitter, AudioOcclusionProbe, AudioTarget, Behavior,
    BodyDynamics, Camera3D, ContactEvent, CueKind, PlayCue, ScreenShown, Story, Transform,
};
use concinnity_core::ecs::asset_id::AssetId;
use concinnity_core::ecs::{
    AudioClipHandle, Entity, EntityByName, EventCursor, PayloadLocator, PipelineContext, SimTiming,
    StepResult, System,
};
use concinnity_core::resource::AudioClipTable;

// Audio behavior. Constructed internally by `World::start` when the world
// declares any `AudioEmitter` or `AudioCue`; never a world-declared asset, so
// it carries no config.
pub(crate) struct AudioSystem {
    engine: AudioEngine,
    // The persisted mix volumes (settings menu), applied at init. Resolved by
    // the engine's audio gate, which owns the settings store, and handed in at
    // construction.
    volumes: AudioVolumes,
    // One binding per live `AudioEmitter`, keyed by its entity. Emitters that
    // appear after init (runtime adds) are adopted each step; bindings whose
    // entity died are reaped, mirroring the physics prop-body lifecycle.
    emitters: HashMap<Entity, EmitterBinding>,
    // Screen-triggered cues, keyed by the Screen whose activation fires them.
    cues: HashMap<AssetId, Vec<CueBinding>>,
    // Clip payload locators snapshotted at init, indexed by handle, so a
    // runtime-adopted emitter can still queue its clip.
    clip_locators: Vec<Option<PayloadLocator>>,
    // Clips already handed to the decode worker.
    queued: HashSet<AudioClipHandle>,
    // Last listener position handed to the engine; the occlusion probes ray
    // from it.
    listener_position: [f32; 3],
    // Cursor into the Events<AudioCommand> queue (live volume changes).
    audio_cmd_cursor: EventCursor,
    // Cursor into the Events<ScreenShown> queue (cue triggers).
    view_shown_cursor: EventCursor,
    // Cursor into the Events<PlayCue> queue (direct play requests, e.g. the
    // story system's page audio).
    play_cue_cursor: EventCursor,
    // Cursor into the Events<ContactEvent> queue (impact one-shots).
    contact_cursor: EventCursor,
    // Cues that matched a shown screen so far; observable engine-independent
    // progress for headless tests (playback needs a device and a payload).
    cues_matched: usize,
    // Distinct clips handed to the decode worker; the same kind of
    // engine-independent observable.
    clips_queued: usize,
    // Contacts that resolved to an impact clip; same kind of observable.
    impacts_played: usize,
}

// Links one engine emitter to the world data that positions it.
struct EmitterBinding {
    // `None` when the engine is disabled or kira's track limit was reached;
    // the binding still tracks position so probes stay uniform.
    id: Option<EmitterId>,
    // The Prop this emitter follows each frame, if any.
    follows: Option<AssetId>,
    // Current world position (authored, then updated from the followed prop).
    position: [f32; 3],
    // Smoothed occlusion state fed from this emitter's probe.
    occlusion: OcclusionSmoother,
}

// One AudioCue resolved to its clip and playback style.
struct CueBinding {
    clip: AudioClipHandle,
    kind: CueKind,
    volume: f32,
    bus: AudioBus,
    priority: i32,
}

// The bus a cue routes through when none is authored.
fn default_bus(kind: CueKind) -> AudioBus {
    match kind {
        CueKind::Music => AudioBus::Music,
        CueKind::Sound => AudioBus::Sfx,
    }
}

impl std::fmt::Debug for AudioSystem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AudioSystem")
            .field("engine", &self.engine)
            .field("emitters", &self.emitters.len())
            .field("cue_views", &self.cues.len())
            .finish()
    }
}

impl AudioSystem {
    // Fresh system with no device, live emitters, or cues. `volumes` holds the
    // persisted settings-menu mix (`None` per stage = unity), applied in
    // `System::init`. The output device is acquired and the emitters / cues are
    // bound from the world's components in `init`, so construction is
    // side-effect-free (required by the `World::system_manifest` gate probe).
    pub(crate) fn new(volumes: AudioVolumes) -> Self {
        Self {
            engine: AudioEngine::disabled(),
            volumes,
            emitters: HashMap::new(),
            cues: HashMap::new(),
            clip_locators: Vec::new(),
            queued: HashSet::new(),
            listener_position: [0.0; 3],
            audio_cmd_cursor: EventCursor::default(),
            view_shown_cursor: EventCursor::default(),
            play_cue_cursor: EventCursor::default(),
            contact_cursor: EventCursor::default(),
            cues_matched: 0,
            clips_queued: 0,
            impacts_played: 0,
        }
    }

    // Number of cue bindings fired since init. Engine-independent progress the
    // schedule tests observe, since playback itself needs an output device.
    #[cfg(test)]
    pub(crate) fn cues_matched(&self) -> usize {
        self.cues_matched
    }

    // Distinct clips handed to the decode worker at init; the same kind of
    // engine-independent observable.
    #[cfg(test)]
    pub(crate) fn clips_queued(&self) -> usize {
        self.clips_queued
    }

    // Contacts that resolved to an impact clip; the same kind of
    // engine-independent observable as `cues_matched`.
    #[cfg(test)]
    pub(crate) fn impacts_played(&self) -> usize {
        self.impacts_played
    }

    // Read a clip's payload from the blob and hand it to the decode worker,
    // once per distinct clip. Returns false when the clip has no compiled
    // payload or the read failed (logged by the caller's context message).
    fn queue_clip(&mut self, ctx: &mut PipelineContext, clip: AudioClipHandle) -> bool {
        if !self.queued.insert(clip) {
            return true;
        }
        let Some(locator) = self.clip_locators.get(clip.index()).cloned().flatten() else {
            self.queued.remove(&clip);
            return false;
        };
        match ctx.read_payload(&locator) {
            Ok(bytes) => {
                self.engine.queue_clip(clip.0 as u64, bytes.to_vec());
                self.clips_queued += 1;
                true
            }
            Err(e) => {
                tracing::warn!("AudioSystem: clip payload read failed: {e}");
                self.queued.remove(&clip);
                false
            }
        }
    }

    // Bind one `AudioEmitter` living on `entity`: create its engine emitter,
    // queue and start its clip, and attach its occlusion probe to the entity
    // (so the probe despawns with it). Shared by init and runtime adoption.
    fn bind_emitter(&mut self, ctx: &mut PipelineContext, entity: Entity, emitter: &AudioEmitter) {
        let params = EmitterParams {
            min_distance: emitter.min_distance,
            max_distance: emitter.max_distance,
            rolloff: emitter.rolloff,
            bus: emitter.bus.unwrap_or(AudioBus::Sfx),
        };
        let id = self.engine.add_emitter(emitter.position, &params);
        match emitter.clip {
            Some(clip) => {
                if self.queue_clip(ctx, clip)
                    && let Some(id) = id
                {
                    // Starts on the tick the decode lands.
                    self.engine.play_emitter_clip(
                        id,
                        clip.0 as u64,
                        emitter.looping,
                        emitter.volume,
                    );
                }
            }
            None => {
                tracing::warn!("AudioSystem: emitter has no clip with a compiled payload, silent")
            }
        }
        // PhysicsSystem answers the probe each frame when the world
        // simulates physics.
        if ctx.get::<AudioOcclusionProbe>(entity).is_none() {
            ctx.insert(
                entity,
                AudioOcclusionProbe {
                    from: [0.0; 3],
                    to: emitter.position,
                    blocked: None,
                },
            );
        }
        self.emitters.insert(
            entity,
            EmitterBinding {
                id,
                follows: emitter.prop,
                position: emitter.position,
                occlusion: OcclusionSmoother::new(),
            },
        );
    }
}

impl System for AudioSystem {
    fn init(&mut self, ctx: &mut PipelineContext) {
        // Acquire the output device (a disabled no-op engine when none is
        // available), deferred out of `new` so construction stays cheap.
        self.engine = AudioEngine::new();
        // Snapshot the emitters, then the clip payload locators indexed by
        // AudioClipHandle. The `AudioClipTable` resource is built from the blob's
        // resource stream, dense in handle order, so index N is the clip with
        // `AudioClipHandle(N)`. Collecting this owned Vec releases the resource
        // borrow before the `read_payload` calls below.
        let emitter_snaps: Vec<(Entity, AudioEmitter)> = ctx
            .query_with_entity::<AudioEmitter>()
            .map(|(entity, e)| (entity, e.clone()))
            .collect();
        self.clip_locators = ctx
            .resource::<AudioClipTable>()
            .map(|table| table.0.iter().map(|e| e.payload.clone()).collect())
            .unwrap_or_default();

        // The persisted volumes (settings menu) scale the master mix and each
        // bus, so they can be changed live (see `step`). `None` leaves a
        // stage at unity. Clips play at their authored gain; these are
        // separate output-stage multipliers.
        self.engine
            .set_volume(AudioTarget::Master, self.volumes.master.unwrap_or(1.0));
        self.engine
            .set_volume(AudioTarget::Music, self.volumes.music.unwrap_or(1.0));
        self.engine
            .set_volume(AudioTarget::Sfx, self.volumes.sfx.unwrap_or(1.0));
        self.engine
            .set_volume(AudioTarget::Voice, self.volumes.voice.unwrap_or(1.0));

        for (entity, emitter) in emitter_snaps {
            self.bind_emitter(ctx, entity, &emitter);
        }

        // Bind the screen-triggered cues and queue their clips for decode, so
        // firing a cue never touches the blob mid-frame.
        let cue_snaps: Vec<AudioCue> = ctx.query::<AudioCue>().cloned().collect();
        for cue in cue_snaps {
            let (Some(screen), Some(clip)) = (cue.screen, cue.clip) else {
                tracing::warn!("AudioSystem: cue without a screen and a clip, ignored");
                continue;
            };
            if !self.queue_clip(ctx, clip) {
                tracing::warn!("AudioSystem: cue clip has no compiled payload, silent");
            }
            self.cues.entry(screen).or_default().push(CueBinding {
                clip,
                kind: cue.kind,
                volume: cue.volume,
                bus: cue.bus.unwrap_or(default_bus(cue.kind)),
                priority: cue.priority,
            });
        }

        // Stories and behaviors play clips by direct PlayCue request, and
        // physics contacts play whatever impact clip the colliding body
        // carries, so queue every remaining clip up front. Blob payloads are
        // freed after init, so a clip skipped here cannot be queued later.
        let plays_arbitrary_clips = ctx.query::<Story>().next().is_some()
            || ctx.query::<Behavior>().any(Behavior::plays_sound)
            || ctx.query::<BodyDynamics>().any(|d| d.impact_clip.is_some());
        if plays_arbitrary_clips {
            let clips: Vec<AudioClipHandle> = self
                .clip_locators
                .iter()
                .enumerate()
                .filter_map(|(i, loc)| loc.as_ref().map(|_| AudioClipHandle(i as u32)))
                .collect();
            for clip in clips {
                if !self.queue_clip(ctx, clip) {
                    tracing::warn!("AudioSystem: clip payload read failed");
                }
            }
        }

        tracing::info!(
            "AudioSystem: {} emitter(s), {} cue screen(s), {} clip(s) decoding, engine {}",
            self.emitters.len(),
            self.cues.len(),
            self.clips_queued,
            if self.engine.is_enabled() {
                "enabled"
            } else {
                "disabled"
            },
        );
    }

    fn step(&mut self, ctx: &mut PipelineContext) -> StepResult {
        // Drain finished decodes (starting deferred plays) and release the
        // voice slots of finished one-shots.
        self.engine.tick();

        // Reap bindings whose entity despawned (the spatial track stops and
        // unloads; the probe died with the entity), then adopt emitters that
        // appeared since init -- the same seen-set lifecycle the physics
        // system runs for prop bodies.
        let dead: Vec<Entity> = self
            .emitters
            .keys()
            .filter(|entity| !ctx.is_alive(**entity))
            .copied()
            .collect();
        for entity in dead {
            if let Some(binding) = self.emitters.remove(&entity)
                && let Some(id) = binding.id
            {
                self.engine.remove_emitter(id);
            }
        }
        let adopted: Vec<(Entity, AudioEmitter)> = ctx
            .query_with_entity::<AudioEmitter>()
            .filter(|(entity, _)| !self.emitters.contains_key(entity))
            .map(|(entity, e)| (entity, e.clone()))
            .collect();
        for (entity, emitter) in adopted {
            self.bind_emitter(ctx, entity, &emitter);
        }

        // Apply any live volume change sent this tick by GraphicsSystem,
        // which runs first. The last one per target this tick wins.
        if let Some(events) = ctx.events::<AudioCommand>() {
            for cmd in events.read(&mut self.audio_cmd_cursor) {
                self.engine.set_volume(cmd.target, cmd.gain);
            }
        }

        // Fire cues for screens shown since the last step. UiInputSystem runs
        // after this system, so a navigation is heard one tick later.
        if !self.cues.is_empty()
            && let Some(events) = ctx.events::<ScreenShown>()
        {
            let shown: Vec<AssetId> = events
                .read(&mut self.view_shown_cursor)
                .map(|e| e.screen)
                .collect();
            for screen in shown {
                let Some(bindings) = self.cues.get(&screen) else {
                    continue;
                };
                self.cues_matched += bindings.len();
                for cue in bindings {
                    let key = cue.clip.0 as u64;
                    match cue.kind {
                        CueKind::Music => {
                            self.engine.play_music(key, cue.volume, cue.bus);
                        }
                        CueKind::Sound => {
                            self.engine
                                .play_sound(key, cue.volume, cue.bus, cue.priority);
                        }
                    }
                }
            }
        }

        // Play direct requests (the story system's page audio). The story
        // system runs earlier in the schedule, so these are heard this tick.
        if let Some(events) = ctx.events::<PlayCue>() {
            let requests: Vec<PlayCue> = events.read(&mut self.play_cue_cursor).copied().collect();
            for cue in requests {
                self.cues_matched += 1;
                let key = cue.clip.0 as u64;
                match cue.kind {
                    CueKind::Music => {
                        self.engine.play_music(key, cue.volume, AudioBus::Music);
                    }
                    CueKind::Sound => {
                        self.engine
                            .play_sound(key, cue.volume, AudioBus::Sfx, cue.priority);
                    }
                }
            }
        }

        // Impact one-shots: a contact plays each colliding body's authored
        // impact clip at the contact point, scaled by the impulse. Physics
        // gates and debounces the events; the voice pool caps bursts. The
        // events copy into frame scratch to release the queue borrow before
        // the component reads below.
        let frame = ctx.frame;
        if let Some(events) = ctx.events::<ContactEvent>() {
            let contacts = frame.collect(events.read(&mut self.contact_cursor).copied());
            for &contact in contacts.iter() {
                for entity in [Some(contact.a), contact.b].into_iter().flatten() {
                    let Some(dynamics) = ctx.get::<BodyDynamics>(entity) else {
                        continue;
                    };
                    let Some(clip) = dynamics.impact_clip else {
                        continue;
                    };
                    let gain = super::impact::gain(contact.impulse) * dynamics.impact_volume;
                    self.impacts_played += 1;
                    self.engine
                        .play_sound_at(contact.point, clip.0 as u64, gain, 0);
                }
            }
        }

        // The listener rides the camera.
        if let Some((pos, yaw, pitch)) = ctx
            .query::<Camera3D>()
            .next()
            .map(|c| (c.position, c.yaw, c.pitch))
        {
            self.listener_position = pos;
            self.engine.set_listener(pos, yaw, pitch);
        }

        // Prop-bound emitters track their followed prop's current position,
        // read from its Transform via the name index.
        if self.emitters.values().any(|b| b.follows.is_some()) {
            for binding in self.emitters.values_mut() {
                if let Some(prop_id) = binding.follows
                    && let Some(entity) =
                        ctx.resource::<EntityByName>().and_then(|n| n.get(prop_id))
                    && let Some(t) = ctx.get::<Transform>(entity)
                {
                    binding.position = t.position;
                    if let Some(id) = binding.id {
                        self.engine.set_emitter_position(id, t.position);
                    }
                }
            }
        }

        // Feed each emitter's occlusion probe (physics answered last frame's
        // ray) through its smoother and into the mixer, and refresh the ray
        // endpoints for the next physics step. An unanswered probe (no
        // physics in the world) reads as clear.
        for (entity, probe) in ctx.query_mut_with_entity::<AudioOcclusionProbe>() {
            let Some(binding) = self.emitters.get_mut(&entity) else {
                continue;
            };
            probe.from = self.listener_position;
            probe.to = binding.position;
            let factor = binding
                .occlusion
                .step(probe.blocked.unwrap_or(false), SimTiming::TICK_DT);
            if let Some(id) = binding.id {
                self.engine.set_emitter_occlusion(id, factor);
            }
        }

        StepResult::Continue
    }
}

#[cfg(test)]
mod tests {
    // These tests drive AudioSystem::init / step against a hand-built
    // PipelineContext and an in-memory blob, so no audio playback happens
    // (init may still probe for a device on a dev machine; every assertion
    // here is engine-independent). They assert on the state the system
    // tracks: cue bindings, queued-clip and match counters, emitter bindings,
    // and the occlusion probes. The gate/schedule tests (which need a whole
    // world) live in the engine's `ecs/schedule.rs`.
    use super::{AudioSystem, EmitterBinding};
    use crate::audio::occlusion::OcclusionSmoother;
    use crate::audio::{AudioVolumes, EmitterId};
    use concinnity_core::components::{
        AudioBus, AudioCommand, AudioCue, AudioEmitter, AudioOcclusionProbe, AudioTarget,
        BodyDynamics, Camera3D, ContactEvent, CueKind, PlayCue, ScreenShown, Story, Transform,
    };
    use concinnity_core::ecs::asset_id::AssetId;
    use concinnity_core::ecs::{
        Arena, AudioClipHandle, ComponentSlot, ComponentStorage, Entity, EntityByName,
        FrameContext, PayloadLocator, PipelineContext, ResourceKind, ResourceRecord, Resources,
        StepResult, System,
    };
    use concinnity_core::gfx::profile::FrameProfile;
    use concinnity_core::resource::AudioClipTable;
    use concinnity_host::store::blob::BlobData;

    // Accumulates audio components + one blob section serving every payload
    // locator handed out, plus the audio-clip resource records, then seals into a
    // context-owning world whose `AudioClipTable` is built from those records --
    // exactly as the runtime loads the blob's resource stream.
    struct AudioWorld {
        components: ComponentStorage,
        section: Vec<u8>,
        // The audio-clip resource records, in handle order (a clip added Nth is
        // handle N). Sealed into the world's `AudioClipTable`.
        clips: Vec<ResourceRecord>,
    }

    struct SealedAudio {
        components: ComponentStorage,
        blob: BlobData,
        profile: FrameProfile,
        resources: Resources,
        scratch: Arena,
    }

    impl AudioWorld {
        fn new() -> Self {
            Self {
                components: ComponentStorage::default(),
                section: Vec::new(),
                clips: Vec::new(),
            }
        }

        fn payload(&mut self, bytes: &[u8]) -> PayloadLocator {
            let offset = self.section.len() as u64;
            self.section.extend_from_slice(bytes);
            PayloadLocator {
                blob_index: 0,
                offset,
                len: bytes.len() as u64,
            }
        }

        fn push<C: ComponentSlot>(&mut self, c: C) -> Entity {
            self.components.push_typed(c)
        }

        // Add an audio clip whose payload is `bytes`, returning its handle (its
        // record order, which the table indexes by).
        fn clip(&mut self, bytes: &[u8]) -> AudioClipHandle {
            let handle = AudioClipHandle(self.clips.len() as u32);
            let locator = self.payload(bytes);
            self.clips.push(ResourceRecord {
                resource_kind: ResourceKind::AudioClip as u8,
                handle: handle.0,
                payload: Some(locator),
                data_bytes: Vec::new(),
            });
            handle
        }

        fn seal(mut self) -> SealedAudio {
            let mut resources = Resources::new();
            resources.insert(AudioClipTable::from_records(&mut self.clips));
            SealedAudio {
                components: self.components,
                blob: BlobData::new(vec![Some(self.section)]),
                profile: FrameProfile::default(),
                resources,
                scratch: Arena::with_capacity(64 * 1024),
            }
        }
    }

    impl SealedAudio {
        fn ctx(&mut self) -> PipelineContext<'_> {
            PipelineContext {
                components: &mut self.components,
                blob: &mut self.blob,
                profile: &mut self.profile,
                resources: &mut self.resources,
                frame: FrameContext::new(&self.scratch),
            }
        }
    }

    // init binds a screen-triggered cue with its routing defaults and hands
    // its clip to the decode worker once, so firing the cue later never
    // touches the blob.
    #[test]
    fn init_binds_cue_and_queues_its_clip() {
        let screen = AssetId(90);

        let mut w = AudioWorld::new();
        let clip = w.clip(b"cue-clip-bytes");
        w.push(AudioCue {
            screen: Some(screen),
            clip: Some(clip),
            kind: CueKind::Music,
            volume: 0.7,
            ..Default::default()
        });
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());

        let bindings = sys.cues.get(&screen).expect("cue bound to its screen");
        assert_eq!(bindings.len(), 1);
        assert_eq!(bindings[0].clip, clip);
        assert_eq!(bindings[0].kind, CueKind::Music);
        assert!((bindings[0].volume - 0.7).abs() < 1.0e-6);
        assert_eq!(
            bindings[0].bus,
            AudioBus::Music,
            "music cue defaults to the music bus"
        );
        assert_eq!(bindings[0].priority, 0);
        assert_eq!(sys.clips_queued(), 1);
    }

    // An authored bus and priority override the kind-derived defaults.
    #[test]
    fn init_respects_authored_cue_bus_and_priority() {
        let screen = AssetId(90);

        let mut w = AudioWorld::new();
        let clip = w.clip(b"line-bytes");
        w.push(AudioCue {
            screen: Some(screen),
            clip: Some(clip),
            kind: CueKind::Sound,
            bus: Some(AudioBus::Voice),
            priority: 4,
            ..Default::default()
        });
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());

        let bindings = sys.cues.get(&screen).expect("cue bound");
        assert_eq!(bindings[0].bus, AudioBus::Voice);
        assert_eq!(bindings[0].priority, 4);
    }

    // A cue missing its screen or its clip is ignored: nothing is bound and no
    // clip is queued.
    #[test]
    fn init_ignores_cue_without_clip() {
        let mut w = AudioWorld::new();
        w.push(AudioCue {
            screen: Some(AssetId(90)),
            clip: None,
            ..Default::default()
        });
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());

        assert!(sys.cues.is_empty());
        assert_eq!(sys.clips_queued(), 0);
    }

    // A story queues every clip up front (it plays them by direct PlayCue
    // request, not through screen-keyed cues), each clip exactly once even
    // when a cue already queued it.
    #[test]
    fn init_queues_story_clips_without_duplicates() {
        let screen = AssetId(90);

        let mut w = AudioWorld::new();
        let cue_clip = w.clip(b"cue-audio");
        let page_clip = w.clip(b"story-page-audio");
        w.push(AudioCue {
            screen: Some(screen),
            clip: Some(cue_clip),
            ..Default::default()
        });
        w.push(Story::default());
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());

        let _ = page_clip;
        assert_eq!(sys.clips_queued(), 2, "both clips queued, neither twice");
    }

    // init binds every emitter (device or not), queues its clip, and
    // attaches one occlusion probe to each emitter's own entity, aimed at it.
    #[test]
    fn init_binds_emitters_and_publishes_occlusion_probes() {
        let mut w = AudioWorld::new();
        let clip = w.clip(b"loop-bytes");
        let entity = w.push(AudioEmitter {
            clip: Some(clip),
            position: [3.0, 1.0, -2.0],
            ..Default::default()
        });
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());

        assert_eq!(sys.emitters.len(), 1);
        let binding = sys.emitters.get(&entity).expect("binding keyed by entity");
        assert_eq!(binding.position, [3.0, 1.0, -2.0]);
        assert_eq!(sys.clips_queued(), 1);

        let ctx = sealed.ctx();
        let probe = ctx
            .get::<AudioOcclusionProbe>(entity)
            .expect("probe rides the emitter's entity");
        assert_eq!(probe.to, [3.0, 1.0, -2.0]);
        assert_eq!(probe.blocked, None, "unanswered until physics steps");
        assert_eq!(ctx.query::<AudioOcclusionProbe>().count(), 1);
    }

    // An AudioEmitter appearing after init is adopted on the next step, and
    // a binding whose entity despawns is reaped (probe and all).
    #[test]
    fn step_adopts_new_emitters_and_reaps_despawned_ones() {
        let mut w = AudioWorld::new();
        let clip = w.clip(b"loop-bytes");
        let first = w.push(AudioEmitter {
            clip: Some(clip),
            position: [1.0, 0.0, 0.0],
            ..Default::default()
        });
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());
        assert_eq!(sys.emitters.len(), 1);

        // A second emitter appears at runtime; the next step binds it and
        // attaches its probe.
        let second = {
            let ctx = sealed.ctx();
            ctx.components.push_typed(AudioEmitter {
                clip: Some(clip),
                position: [7.0, 0.0, 0.0],
                ..Default::default()
            })
        };
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(sys.emitters.len(), 2);
        assert_eq!(
            sys.emitters.get(&second).map(|b| b.position),
            Some([7.0, 0.0, 0.0])
        );
        assert!(
            sealed.ctx().get::<AudioOcclusionProbe>(second).is_some(),
            "adopted emitter got its probe"
        );

        // The first emitter despawns; its binding is reaped and it is not
        // re-adopted.
        sealed.ctx().despawn(first);
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(sys.emitters.len(), 1);
        assert!(sys.emitters.contains_key(&second), "survivor kept");
        assert_eq!(sealed.ctx().query::<AudioOcclusionProbe>().count(), 1);
    }

    // A contact whose body carries an impact clip plays it; bodies without
    // dynamics or without a clip are silent. The counter is the observable.
    #[test]
    fn step_plays_impact_clips_from_contacts() {
        let mut w = AudioWorld::new();
        let clip = w.clip(b"thud-bytes");
        let mut sealed = w.seal();

        let (crate_entity, bare_entity) = {
            let mut ctx = sealed.ctx();
            let crate_entity = ctx.components.spawn();
            ctx.insert(
                crate_entity,
                BodyDynamics {
                    impact_clip: Some(clip),
                    ..Default::default()
                },
            );
            let bare_entity = ctx.components.spawn();
            ctx.insert(bare_entity, BodyDynamics::default());
            (crate_entity, bare_entity)
        };

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());
        assert_eq!(sys.clips_queued(), 1, "impact clip queued up front");

        {
            let mut ctx = sealed.ctx();
            let events = ctx.events_mut::<ContactEvent>();
            // Both sides resolve: only the crate has a clip.
            events.send(ContactEvent {
                a: crate_entity,
                b: Some(bare_entity),
                point: [0.0, 0.5, 0.0],
                normal: [0.0, 1.0, 0.0],
                impulse: 6.0,
            });
            // `a` has dynamics but no clip, and no `b`: silent.
            events.send(ContactEvent {
                a: bare_entity,
                b: None,
                point: [0.0; 3],
                normal: [0.0, 1.0, 0.0],
                impulse: 6.0,
            });
        }
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(sys.impacts_played(), 1, "one clip-carrying side played");
    }

    // A blocked probe answer muffles gradually: the smoothed factor rises
    // over several steps instead of slamming to 1, and the system refreshes
    // the probe's ray endpoints each step.
    #[test]
    fn step_smooths_probe_answers_and_refreshes_endpoints() {
        let mut w = AudioWorld::new();
        let clip = w.clip(b"loop-bytes");
        w.push(AudioEmitter {
            clip: Some(clip),
            position: [5.0, 0.0, 0.0],
            ..Default::default()
        });
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());

        // Physics answered: the segment is blocked.
        {
            let mut ctx = sealed.ctx();
            for probe in ctx.query_mut::<AudioOcclusionProbe>() {
                probe.blocked = Some(true);
            }
        }
        let occlusion_of = |sys: &AudioSystem| {
            sys.emitters
                .values()
                .next()
                .expect("one binding")
                .occlusion
                .current()
        };
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        let after_one = occlusion_of(&sys);
        assert!(
            after_one > 0.0 && after_one < 0.5,
            "one tick moves partway: {after_one}"
        );
        for _ in 0..120 {
            sys.step(&mut sealed.ctx());
        }
        assert!(occlusion_of(&sys) > 0.9, "settles blocked");

        // The ray endpoints track the listener and emitter for next frame.
        let ctx = sealed.ctx();
        let probe = ctx.query::<AudioOcclusionProbe>().next().unwrap();
        assert_eq!(probe.from, [0.0; 3], "no camera: listener at origin");
        assert_eq!(probe.to, [5.0, 0.0, 0.0]);
    }

    // A shown screen fires each of its cues once, across both playback kinds; the
    // engine-independent match counter tracks the progress.
    #[test]
    fn step_fires_cued_view_across_both_kinds() {
        let screen = AssetId(90);

        let mut w = AudioWorld::new();
        let music_clip = w.clip(b"music");
        let sound_clip = w.clip(b"sound");
        w.push(AudioCue {
            screen: Some(screen),
            clip: Some(music_clip),
            kind: CueKind::Music,
            volume: 1.0,
            ..Default::default()
        });
        w.push(AudioCue {
            screen: Some(screen),
            clip: Some(sound_clip),
            kind: CueKind::Sound,
            volume: 1.0,
            ..Default::default()
        });
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());
        assert_eq!(sys.cues.get(&screen).map(Vec::len), Some(2));

        {
            let mut ctx = sealed.ctx();
            ctx.events_mut::<ScreenShown>().send(ScreenShown { screen });
        }
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(sys.cues_matched, 2, "both the screen's cues matched");

        // A screen with no cue leaves the counter untouched.
        {
            let mut ctx = sealed.ctx();
            ctx.events_mut::<ScreenShown>().send(ScreenShown {
                screen: AssetId(999),
            });
        }
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(sys.cues_matched, 2);
    }

    // A direct PlayCue request (the story system's page audio) is played the
    // same tick it is sent.
    #[test]
    fn step_plays_direct_play_cue_requests() {
        let mut w = AudioWorld::new();
        let clip = w.clip(b"page-audio");
        w.push(Story::default());
        let mut sealed = w.seal();

        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());

        {
            let mut ctx = sealed.ctx();
            let events = ctx.events_mut::<PlayCue>();
            events.send(PlayCue {
                clip,
                kind: CueKind::Music,
                volume: 1.0,
                priority: 0,
            });
            events.send(PlayCue {
                clip,
                kind: CueKind::Sound,
                volume: 0.5,
                priority: 2,
            });
        }
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(sys.cues_matched, 2, "both direct requests fired");
    }

    // A prop-bound emitter tracks its prop's Transform each step, and the
    // listener rides the camera. Neither needs a device; the step drives the
    // lookups and returns Continue.
    #[test]
    fn step_follows_prop_emitter_and_moves_listener() {
        let prop = AssetId(200);

        let mut sealed = AudioWorld::new().seal();
        let entity = {
            let mut ctx = sealed.ctx();
            ctx.push(Camera3D::bake(Default::default()));
            let e = ctx.components.spawn();
            ctx.insert(
                e,
                Transform {
                    position: [3.0, 4.0, 5.0],
                    rotation_deg: [0.0; 3],
                    scale: [1.0; 3],
                },
            );
            let mut by_name = std::collections::BTreeMap::new();
            by_name.insert(prop, e);
            ctx.insert_resource(EntityByName(by_name));
            e
        };
        assert!(sealed.ctx().get::<Transform>(entity).is_some());

        let mut sys = AudioSystem::new(AudioVolumes::default());
        // A live emitter that follows the prop, seeded directly (a real
        // emitter needs a device, which the headless test has no access to).
        // Keyed by a spare entity standing in for the emitter's own.
        let emitter_entity = sealed.ctx().components.spawn();
        sys.emitters.insert(
            emitter_entity,
            EmitterBinding {
                id: Some(EmitterId(0)),
                follows: Some(prop),
                position: [0.0; 3],
                occlusion: OcclusionSmoother::new(),
            },
        );

        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(
            sys.emitters[&emitter_entity].position,
            [3.0, 4.0, 5.0],
            "binding tracked the prop"
        );
    }

    // A volume AudioCommand sent mid-tick is read AND applied by the audio
    // system the same tick, so the new gain takes effect without a restart
    // (the settings-menu volume rows). Default volumes at construction mean
    // init leaves every stage at unity.
    #[test]
    fn audio_command_applies_volumes_live_per_target() {
        let mut sealed = AudioWorld::new().seal();
        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());
        // Init applied unity everywhere (no persisted volumes handed in).
        assert_eq!(sys.engine.last_volume(AudioTarget::Master), 1.0);
        assert_eq!(sys.engine.last_volume(AudioTarget::Voice), 1.0);

        // GraphicsSystem would send these when volume rows are cycled; the
        // audio system reads them this same tick.
        {
            let mut ctx = sealed.ctx();
            let events = ctx.events_mut::<AudioCommand>();
            events.send(AudioCommand {
                target: AudioTarget::Master,
                gain: 0.5,
            });
            events.send(AudioCommand {
                target: AudioTarget::Music,
                gain: 0.25,
            });
        }
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(sys.engine.last_volume(AudioTarget::Master), 0.5);
        assert_eq!(sys.engine.last_volume(AudioTarget::Music), 0.25);
        assert_eq!(sys.engine.last_volume(AudioTarget::Sfx), 1.0, "untouched");
    }

    // Several AudioCommands for one target sent in one tick (e.g. a rapid
    // double-cycle) are all read in order; the last one sent wins.
    #[test]
    fn audio_command_last_write_wins_per_tick() {
        let mut sealed = AudioWorld::new().seal();
        let mut sys = AudioSystem::new(AudioVolumes::default());
        sys.init(&mut sealed.ctx());

        {
            let mut ctx = sealed.ctx();
            let events = ctx.events_mut::<AudioCommand>();
            events.send(AudioCommand {
                target: AudioTarget::Master,
                gain: 0.5,
            });
            events.send(AudioCommand {
                target: AudioTarget::Master,
                gain: 0.25,
            });
        }
        assert_eq!(sys.step(&mut sealed.ctx()), StepResult::Continue);
        assert_eq!(sys.engine.last_volume(AudioTarget::Master), 0.25);
    }

    // Persisted volumes handed in at construction are applied to every stage
    // at init (the settings-menu volumes, resolved by the engine's gate).
    #[test]
    fn init_applies_persisted_volumes() {
        let mut sealed = AudioWorld::new().seal();
        let mut sys = AudioSystem::new(AudioVolumes {
            master: Some(0.5),
            music: Some(0.75),
            sfx: None,
            voice: Some(0.25),
        });
        sys.init(&mut sealed.ctx());
        assert_eq!(sys.engine.last_volume(AudioTarget::Master), 0.5);
        assert_eq!(sys.engine.last_volume(AudioTarget::Music), 0.75);
        assert_eq!(
            sys.engine.last_volume(AudioTarget::Sfx),
            1.0,
            "unset = unity"
        );
        assert_eq!(sys.engine.last_volume(AudioTarget::Voice), 0.25);
    }
}