say there are 10,000 sounds on an RPG battlefield and room to mix 64. what happens to the other 9,936?
a footstep might be over before anyone gets near it. a generator is different. if i walk up to it a second later, i want to hear a generator that’s already running, not one that starts because i showed up.
so i built Sonic, a small Rust engine, to see if i could get that behavior without ticking every sound. FMOD and Wwise already do virtual voices. what i wanted to know was how little work i could get away with, and what happened when the work i skipped came back all at once.
i mixed all 10,000 sounds first
at 48 kHz, the mixer needs to make another 256 frames every 5.333 ms. that’s the time between blocks, not necessarily how long the OS gives me to make one. if the thread wakes up late, i’ve already spent some of it.
i didn’t build a convincing battlefield. everything reads from the same precomputed 2,048-frame mono waveform at normal speed, just at different phases. most sounds loop so i can keep the population constant; one test uses sounds that end. no file I/O, decoding, reverb, occlusion, panning or Doppler. i wanted to measure the scheduler, not a codec.
the first version does the obvious thing: work out how loud each sound is and add its samples, even if its gain is zero. that’s 2,560,000 source-samples per full block at 10,000 sounds. with 64 voices it’s at most 16,384. i could skip zero-gain sounds, but what if everyone is standing right next to me? that’s the collapse test later.
64 slots, and a generator that disappeared
i kept the 64 best-ranked sounds and dropped the rest. the score is integer distance attenuation times base priority, with an optional focus boost. ties go to a stable ID. the heap puts the lowest-ranked winner at the top, so most new candidates only need one comparison. if a higher-ranked sound kicks out one i could still hear, that’s a steal. going out of range or ending doesn’t count.
this is fast partly because it forgets things i still need. teleport to the generator and there’s nothing to play. i could start it again, but now it sounds like it just turned on. and that footstep from a second ago? i probably don’t want to start that one again at all.
Try it
Does the generator keep running when you’re too far away to hear it?
Both camps start their generators at the same time. You’re at Camp A. Camp B is too far away to hear.
Sound discarded
Generator B is too far away, so the game drops its sound.
Sound kept for later
The game saves when Generator B started. It doesn’t play its audio yet.
This example shows what you’d hear. It doesn’t play audio.
Show scheduler details
| Scheduler | Mixed | Kept silent | Dropped | Examined |
|---|
Both virtual schedulers choose the same sounds. Eager checks every live source; lazy checks only nearby grid cells. Counts come from a browser model, not audio playback or Rust timings.
keeping the sound without playing it
i split “this sound exists” from “the mixer is reading its samples.” the first is a record with a start, an end and an emitter definition. the second is one of those 64 slots. a sound without a slot is a virtual voice.
my first version of this still went around advancing every playback position on every block. even the generator on the other side of the map. that felt wasteful, but it gave me a version to check against: same ranking, same mixer, just the straightforward way of keeping time.
i sort the selected IDs before mixing. floating-point addition depends on order, and i didn’t want a rounding difference to look like a broken scheduler. the test (tests/observable.rs) checks the actual samples bit for bit across six scenes, compares the voice transitions, and checks that neither renderer calls the allocator. another test hides a loop for 8,192 frames. when it comes back, i compare it to one that was playing all along. the fixed pool gives me silence.
when i say “the same output,” i mean the same as that eager virtual renderer. not all 10,000 sounds mixed together. and a one-shot can expire without ever being heard, but Sonic doesn’t tell the game that it finished. there’s no notification API in this experiment.
why am i advancing a cursor i can subtract?
if the sound runs at normal speed, its playback position is just “now minus when it started,” measured in sample frames. i don’t need to do anything to it while it’s quiet. the mixer (src/lib.rs) uses the first frame that actually overlaps this block:
let first = self.frame.max(v.start);
let stop = end.min(v.end);
let cursor = if self.mode == Mode::Lazy {
first - v.start
} else {
v.cursor
};
The interval is half-open: first, stop). A start at frame 37 inside a block contributes nothing before frame 37, even if the block scheduler has already selected it. If delivery is late, Sonic seeks to the elapsed position rather than turning the late arrival into a new start. Listener position and focus changes are block-rate. One top-K selection governs the entire block: a high-priority start near the end can take a slot before it emits anything, and a short sound that ends mid-block doesn’t free a slot until the next decision. Sample-accurate clip timing is not sample-accurate scheduling.
Two types of wake-up remain:
- Could this sound be heard here? Active emitters occupy a uniform grid of 128-unit cells. The listener queries the cells intersecting a square around its 260-unit radius. The inner score rejects the corners and applies the exact attenuation rule. Inactive definitions never get ranked and active emitters outside the query stay untouched. Intrusive links in the voice records make insertion and unlinking constant time for static emitters.
- Did this sound finish? Finite voices enter a min-heap keyed by end frame. At the next boundary at or past the end, Sonic pops and unlinks one; loops don’t need a heap entry. Only due voices are visited. An end inside the current block still clips the mix at the exact sample, even though removal waits until a boundary.
With N live voices, C candidates, K mixer slots, F finite voices in the heap and E expirations due, eager performs linear expiry and cursor sweeps in addition to ranking all N. Lazy has roughly O(cells + C log K + K log K + E log F) scheduler work in the worst case for those candidates, plus ingress (O(log F) per finite start) and the bounded mix. In either renderer many candidates never modify the selection heap. The grid changes C, not its worst-case bound; it can be N. The memory cost is still linear: this is a reduction in visits, not in world state.
The callback owns mutable engine state. Its [SPSC queue (src/queue.rs) uses acquire/release publication, fixed slots and no callback locks or retry loops. Timestamp order is part of the producer API; Full means the producer must choose what to do, not block the consumer. Ring operations don’t wait for each other, but that’s not a latency guarantee for the scheduler. The worst event batch, page faults, cache misses and OS preemption can still dominate. Buffers, heaps and voice records are reserved up front; the device adapter holds a reusable internal block and also handles backend callbacks of different lengths.
what if everything happens in one block?
i wanted to see what could make a cheap block expensive. not all of these need a lot of mixing:
| Fixture | What changes | What it stresses |
|---|---|---|
spread | Nothing after setup | Steady-state population scaling |
teleport | Listener jumps across the map every 16 blocks | Abrupt candidate growth and rescheduling; half the sources are clustered at the destination |
burst | All sources start at block 16, frame 37 | Queue drain, insertion, and simultaneous ranking in one measured block |
churn | A dense scene flips priority bias every block | Repeated steals with no source arrivals or motion |
collapse | All sources live inside the query, with stable rank | Spatial broad phase offers no pruning |
expiry | All finite voices finish at the same block boundary | A discrete-event scheduler can receive an entire population’s work at once |
The incident that matters to a listener is not necessarily the operation that matters to a profiler. A perfectly timely callback can still click if a stolen waveform is replaced at an unrelated amplitude. Sonic fades the incoming voice over 32 frames (0.67 ms) and puts a decaying copy of each outgoing voice’s last sample into a shared tail. It doesn’t keep old waveforms playing or exceed 64 live waveform reads. This is a de-click, not a true crossfade: it holds the old sample rather than continuing the waveform, isn’t slope-continuous, and doesn’t cover attenuation changes on retained voices or hard ends inside a block.
what the numbers actually said
nobody moves
I ran the release harness (src/main.rs) on a Core i5-1245U laptop, x86-64 Linux 6.8.0-138-generic, using Rust 1.99.0-nightly (26ae60a9e). Each offline row represents 200 consecutive renders after a separately timed setup block; I repeated each configuration three times, rotating mode order. Unless marked otherwise, p50 and p99 below are medians of the three run-level percentiles (nearest rank); maximum is the largest sample across all three runs. The timings include queue ingestion, expiry, selection and mixing, but no device conversion or hardware wait. These were ordinary processes, not isolated real-time threads. The raw results and reproduction commands included in the source archive are included.
| Spread population | Naive p99 | Fixed p99 | Eager p99 | Lazy p99 | Lazy candidates/block | Physical / virtual in lazy |
|---|---|---|---|---|---|---|
| 256 | 85.66 µs | 1.21 µs | 3.08 µs | 1.70 µs | 8 | 4 / 252 |
| 1,000 | 264.33 µs | 6.09 µs | 12.97 µs | 5.63 µs | 43 | 18 / 982 |
| 10,000 | 2,749.97 µs | 26.59 µs | 103.45 µs | 23.61 µs | 346 | 64 / 9,936 |
| 100,000 | 27,645.71 µs | 17.47 µs | 2,285.36 µs | 89.22 µs | 3,490 | 64 / 99,936 |
At 10,000 sources, naive fits this synthetic workload but uses about 45% of a core’s audio-time budget in the measured loop. Eager and lazy both sum 16,384 source-samples per block, yet their p99s differ by over 4×. That second comparison is the useful one: the audible choices and PCM are unchanged, and the savings come from visiting less state, not mixing less sound. The experiment does not isolate the marginal effect of timestamps, the grid and the expiry heap; it compares the full lazy combination with eager tracking.
At 100,000 spread sources:
| Mode | p50 | p99 | Max | Work overruns / 600 | Audio-time CPU budget | Reserved |
|---|---|---|---|---|---|---|
| Naive | 21,521.02 µs | 27,645.71 µs | 32,202.39 µs | 600 | 409.92% | 12.69 MiB |
| Fixed | 15.00 µs | 17.47 µs | 25.37 µs | 0 | 0.28% | 11.17 MiB |
| Eager | 1,153.64 µs | 2,285.36 µs | 5,024.61 µs | 0 | 23.10% | 11.17 MiB |
| Lazy | 64.98 µs | 89.22 µs | 233.19 µs | 0 | 1.26% | 12.70 MiB |
CPU here is calling-thread CPU time divided by the time a 48 kHz stream would allot; an unpaced renderer can consume more than 100% of that budget. Reserved bytes include the source catalog, engine arrays and ring, not process RSS. Even the fixed baseline reserves enough space to stage and rank the incoming population; its steady-state speed comes from forgetting that population, not carrying it at zero cost. The lazy mode reserves another ~1.53 MiB over eager here, mostly for worst-case heap capacity, even when the scene contains only loops.
the grid doesn’t help if everybody is nearby
| 100,000-source fixture | Eager p50 / p99 / max (µs) | Lazy p50 / p99 / max (µs) | Work overruns, eager / lazy (of 600 each) | Max lazy candidates |
|---|---|---|---|---|
| Teleport | 871 / 1,251 / 2,008 | 128 / 642 / 945 | 0 / 0 | 51,748 |
| Burst | 872 / 1,420 / 2,299 | 53 / 60 / 1,069 | 0 / 0 | 3,490 |
| Churn | 953 / 1,794 / 6,439 | 1,212 / 4,471 / 12,127 | 5 / 7 | 100,000 |
| Collapse | 931 / 1,760 / 3,889 | 1,173 / 5,224 / 10,720 | 0 / 9 | 100,000 |
| Expiry | 0.04 / 1,645 / 3,346 | 0.09 / 67 / 8,410 | 0 / 3 | 3,490 |
i thought the grid might stop helping when everyone was nearby. i didn’t expect it to make things slower. both modes checked 100,000 ranks per block, and lazy’s median was worse even when priorities stayed put. maybe following links through the voice array cost more than scanning IDs in order. i didn’t measure cache misses, so that’s a guess.
when priorities flipped, both virtual schedulers made 64 promotions, 64 demotions and 64 steals per block. the fixed pool made none. it had already thrown away the challengers. on teleport, both virtual schedulers made 768 promotions and 768 demotions per run, but no steals: the old sounds just went out of range. the fixed pool lost its 64 voices on the first move and never got them back.
then i made all 100,000 one-shots end on block 16. lazy took 6.11–8.41 ms on that block. eager took 0.59–0.73 ms on the same block; its worst render was somewhere else. lazy’s p99 still looked great at 67 µs. one bad block in 200 doesn’t set p99. the heap had saved me a lot of checking, but it still had to do all the removals when the time came.
what if i only read 2,048 events at a time?
The burst test hands Sonic 100,000 start records in one measured block, with publication preloaded. That’s not a sustainable event rate, and it’s not 100,000 PCM streams once selection is bounded. A separate two-thread ring test transferred one million ordered records in 63.6 ms (~15.7 million/s), with 528,785 full retries. There was no voice state, ranking, mixing, or OS deadline in that test. It measures an aggressively busy transport, not game capacity.
With a 2,048-event dispatch cap, lazy’s worst render across the three burst runs fell from 1,069 µs to 109 µs. The test did not show a deadline problem in the uncapped burst; it showed a cheaper worst callback in exchange for late work. 97,952 starts were processed after their intended block, and the final batch was dispatched 48 blocks after the first (about 256 ms). Later dispatch preserves elapsed phase, not the output that would have been heard before dispatch. A production engine would need an explicit admission policy for timely critical sounds. An ingress cap alone also doesn’t bound dense ranking or a simultaneous expiry wave.
the callback looked fine. ALSA disagreed.
In a paced replay, deadlines were measured against an absolute monotonic schedule, so a late wake-up or an earlier overrun could carry forward. In this one teleport run with 100,000 sources, lazy had no simulated deadline misses in 200 blocks; its max compute time was 4.75 ms, much closer to the budget than the 0.95 ms offline max. Eager had none in that run either, with a 5.12 ms max; naive missed all 200. Sleep-based pacing is not the driver clock or a reproducible model of OS jitter.
A separate 400-block replay pinned lazy to one logical CPU. Without an injected competitor there were two paced misses, including one callback-body overrun. With a hashing process on the same CPU there were three paced misses despite zero callback-body overruns. The contended run actually had a lower callback p99 (1.79 ms vs. 3.55 ms). This is a single pair of runs on an uncontrolled host, not evidence that contention improves computation. It demonstrates something simpler: service time and finishing before a deadline are different measurements.
There was also a five-second muted CPAL/ALSA smoke run at 10,000 sources. It measured callback body times of 69.88 µs p50, 472.17 µs p99 and 697.84 µs max, with no callback-body overruns against the period implied by the supplied buffer lengths. 1,112 of 1,118 callbacks were not 256 frames, despite the request. ALSA logged three underrun recovery notices while CPAL’s error callback did not fire. I can’t attribute those underruns to one cause from these counters. They do rule out using a good callback-body histogram alone as evidence of uninterrupted playback.
what does a voice switch sound like?
In the two-voice fixture (examples/continuity.rs), 53 hard switches produced a maximum adjacent-sample step of 0.743 in normalized PCM (RMS 0.552 at the switching boundaries). Away from boundaries the biggest adjacent step was 0.035. With the tail/ramp enabled, both switching-boundary measurements were exactly zero in that fixture; the largest interior step remained 0.035. That’s a signal-level check, not a count of perceived clicks. Phase, duration, timbre, masking and the listener all matter. The original measurements and the two quieter 48 kHz WAV renders are available: hard switch and ramped.
what i’d try next
The key distinction is between state that changes and state that has to be evaluated right now. A playback cursor under constant-rate motion is more like an OS timer than a task that needs a time slice every tick. The grid is a query on visibility; the heap is a discrete-event simulation of lifecycle boundaries. None of those ideas promises that the next batch of work will be small.
The next experiment I would run is an adaptive dense scan when the queried cells have high occupancy, then a separate study of batched or bucketed expiry. To postpone physical reclamation without changing results, candidate selection would still have to reject logically expired records. To bound candidate work in general, an engine needs either an index that can prove it has the exact top K, or an explicit approximation with an audible error policy. Capping the number of candidates inspected silently changes the experiment’s contract.
Seekable PCM and fixed-rate clocks make the timestamp trick especially clean. Variable-rate playback requires the integral of rate over time, or a piecewise history. Stateful filters, sends that feed audible reverbs, compressed streams without cheap seeking, and random or gameplay-driven generators may need checkpoints, fast-forward rules, or deliberate concessions. Moving emitters would also shift work to index updates. Sonic handles none of these, and it doesn’t support multiple instances of one emitter ID within a block.
i don’t need to advance a generator’s cursor to make it sound like it’s been running. i do need to finish the work when someone finally walks up to it. those turned out to be two very different problems.