Through the Walls: an audio engine that knows where the doors are
Phase 4 is done: an own mixer on its own thread, 3D sound, occlusion rays, rooms and doorways, a reverb per room, layered gunshots, a flashbang that deafens, and a radio that sounds muffled behind a closed door and clear when it opens.
Phase 3 ended with a picture. Phase 4 ends with a sound: a radio left on in the shop, heard from the forecourt. With the door shut it is a dull mumble that seems to come from the window round the corner. Open the door and it clears, moves to the doorway, and gets louder as the way to it gets shorter. Nothing about that is scripted; it falls out of the rooms, the openings and a few rays.

A mixer on its own thread
The sound card asks for audio in small batches, about five milliseconds at a time, from a thread of its own, and it does not wait. If the batch is late, the speakers click. So the mixer is built around one rule: the audio thread never waits for anything. It never locks, never allocates and never frees. The game talks to it through a lock-free queue of commands (play this, stop that, the listener is here now), and it answers through another queue (this voice finished, here is its sound back), so memory is only ever freed on the game thread.
Sixty-four voices play into six buses (weapons, effects, voice, ambience, music, UI), the buses into the master. When all voices are busy, a new sound steals the least important one, and the oldest among equals. Every gain change is a ramp, so nothing clicks. The whole mix of sixty-four spatial voices with the reverb costs 0.13 ms per five-millisecond batch.
Where a sound is
A sound with a position gets quieter with distance, is panned by its direction with constant power so it does not dip in the middle, and loses its highs when it is behind the listener or far away, as the head and the air take them. The listener is the camera, updated every frame.
Walls
Between every sounding thing and the ears, a small fan of rays tests the level's geometry. The share of rays that hit a wall is the occlusion, and occlusion makes a sound quieter and muffled. The shop door is part of that world: it is a blocker whose transform follows the door, so the rays find it shut and miss it open. That is the first half of the radio.
Rooms and doorways
The second half is a graph. The level is described as rooms (boxes with acoustics) and portals between them (openings with an open fraction and how much a closed one lets through). For every sounding thing the mixer asks the graph for the shortest open way to the listener. The answer says where the sound seems to come from (the first opening on its way), how far it really travelled, and how much the doors let through. A shut door costs as much as a long detour, so the radio behind the closed door comes round through the window, which is why it seems to come from the corner.
A reverb per room
Each room has a decay time, a size and a damping. The reverb is a feedback delay network: four delay lines tuned to the room's size, each losing a little per pass so the tail dies in the room's decay time, with a damping filter so the highs die first, mixed so the energy spreads and the tail stays dense. Every positioned sound sends to it after its distance and walls. Step from the shop to the forecourt and the tail fades out, the lines retune and it fades back in with the forecourt's short, dry decay. A gunshot inside and the same gunshot outside end in different rooms, and you hear it.
Layers, ducking and a flashbang
A gunshot is three sounds: the mechanism, heard only up close; the blast, which carries for hundreds of metres; and a tail chosen for the shooter's room, a dense slap off the walls indoors, slapback echoes off the far buildings outdoors. A suppressor swaps the blast for a cough and drops the tail. Ducking rules make the ambience step aside under gunfire and come back when it stops. A flashbang going off near you plays a ring in your head on the UI bus, and the mixer deafens everything else: it drops and loses its highs, and hearing comes back over a few seconds.
Footsteps fall every stride, one of three takes per surface, asphalt on the forecourt and concrete inside, quieter and shorter when you slow-walk. The forecourt's wind and distant traffic stream from an Ogg Vorbis file, decoded a second ahead on the game thread into a ring the audio thread reads, so long ambiences and music cost a ring buffer rather than their whole length in memory.
All of these sounds are synthesized placeholders. They are the right shape in the right places; the recordings come later.
The debug view
A window lists what the audio thread saw at its last render: every voice with its bus, name, gains, filter cutoff, distance and what the walls and doors did to it, each bus's meter and ducking, the room's reverb, how deaf you are, the streams' buffers and the render time. The audio thread fills it on request and hands it over through a queue, like everything else.
One lesson
The first time I ran the finished mixer on the laptop, the speakers stayed silent while the debug view showed every voice playing. The SDL library that vcpkg builds switches its PipeWire and PulseAudio backends off, so the engine had opened SDL's dummy driver and mixed into nothing. A copy of the SDL port with those backends turned back on fixed it. A test cannot catch that one; the test machines have no speakers either.
The numbers
| Engine code | about 19,900 lines in 12 modules, of which 2,200 are the audio module |
| Tests | 153 unit tests, among them a mixer rendering 64 voices, rays through the door, the route round the window, reverb tails per room, the stream looping and the ducking letting go |
| Mixer | 64 spatial voices and the reverb in 0.13 ms per 5.33 ms of audio (release) |
Next: Stack and Fall
Phase 5 is physics: shapes, queries, a character controller, a rigid-body solver, joints for doors and ragdolls. Its finish line is a stack of twenty boxes standing still for a minute and a ragdoll falling down stairs without exploding.