Spatial sound: a world that sounds from where things are
0.13 CPU ms
Still
The live demo starts when it’s in view.Press Sound on first · click the map or use the arrow keys to walk · O opens the door · the mixer has a fader per bus
The HaTac engine’s spatial audio and its sound recipe kind, lifted out whole on the Web Audio API with no engine.
The HaTac engine’s spatial audio and its sound recipe kind, lifted out whole on the Web Audio API with no engine: each sound is a small JSON recipe (layers of seeded synthesis or decoded samples, a loop with an intro and a release tail, maps from game values such as load, flow or wind to gain and pitch); identical loops in one cell merge into a shared bed, so a hundred motors cost two voices and still come from their middle; the listener ranks every layer by audible level and priority under a voice budget per tier; distance takes level and treble away, a wall or a ridge muffles; five buses with volumes, alerts that duck the rest, one convolution room for outdoors or inside; and loops that wrap without a click. Nothing sounds before a click, and it fades in. Added in the library: heard(), what the listener hears (or would, while sound is still locked) layer by layer for a visual readout, a master fade for a sound switch, walls with doors, and a voice limit that fades rather than clicks. The demo is a workshop by a stream you walk round, with a mixer and a readout of what is heard.
What it costs a frame
CPU
0.13 ms
02468101216.7 ms
GPU
not used
CPU, median
0.13 ms
CPU, 95th pct
0.32 ms
Draw calls
0
Weight
Light
Measured 28 November 2026 on RTX 4060 Laptop, 13th Gen Intel(R) Core(TM) i9-13900H, Chrome 158, 1920×1080 at 1×. 600 frames × 3 rounds, median kept (rounds ranged CPU 0.13–0.14 ms). What ran: Alone: one machine on a plain floor, its drive changing every frame, with the listener circling it; the mix model (bed selection, acoustics) runs at the tier’s mix rate and the readout ten times a second. The bench has no click, so the browser keeps audio locked: no Web Audio graph runs, and what is timed is the main thread (the model, the readout and the drawing). Phones: tiers are being measured next.
How steadily it runs on RTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · reference
Typical frame ?
16.72 ms
1% low ?
53 fps
Varies by ?
±0.52 ms
All metricsRTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · referenceHow long each frame took. Most frames sit in the tall bars; a long tail to the right is hitching.
Frames run on a 60 Hz timer (a page demo), so most frames take about one tick, 16.7 ms: the spread around it is what matters.
Frames
Median (p50) ?
16.72 ms
p95 ?
17.77 ms
p99 ?
18.21 ms
Slowest ?
19.67 ms
Mean ?
16.79 ms
Standard deviation ?
0.52 ms
1% low ?
53 fps
0.1% low ?
51 fps
Hitches ?
0 of 600
Over budget ?
0 of 600
Main thread
Long tasks ?
0
Long frames ?
0
Event-loop delay ?
0.1 / 0.9 / 1.4 ms
Input
No input during this run.
Memory
JavaScript heap ?
31.7 → 30.6 MB
Growth ?
-1.11 MB
Browser
JavaScript a frame ?
0.22 ms
All tasks a frame ?
2.29 ms
Layout / style ?
0.43 / 0.33 ms
Main thread busy ?
14%
Garbage collection
Collections ?
6 (3 minor, 3 major)
Pauses ?
16.7 ms in all, longest 1.8
What it needs
Renders with
DOM and CSSPlain DOM and CSS: every current browser.
Your browser
Runs hereNo WebGPU needed
Depends on
NothingNo other packages
With an agent
install("spatial-sound")Your coding agent reads its pitfalls and installs it through the library’s MCP server. Set it up
What it’s for
A game world whose machines, water, weather and crowds sound from where they are, within a fixed number of voices
A factory, a town or a city builder with thousands of identical noisy things, heard as a few beds that still have a direction
Ambience for a walkable 3D scene or a map: a stream, wind, rain, a room that rings when you step inside
Machine sounds driven by a simulation: hum that rises with load, a saw that bites, a pump that idles
Alerts that dip the rest of the mix, and volume groups for a settings menu
Sound designed as data: recipes an agent or a tool can write, check and change without code
A visual readout or captions of what a place sounds like, for people who are deaf, hard of hearing or have sound off
Pitfalls, in brief
Nothing may sound before the person asks for it.
Sound is never the only way something is said.
Register a machine when it is placed or its drive changes, then call parameters() as its work changes; do not set() it again every frame.
Budget voices per playing layer, release tails and one-shots included: a two-layer motor takes two voices even when a hundred of them share a bed.
Choose a bed cell to match a group of things that should sound as one (a machine hall, a stretch of river).
8 more in brief
Pass positions relative to the listener, in metres, through the sector scheme (worldPos with the same sector size everywhere).
Use the simulation’s clock for work and the audio clock for fades: a paused world stops its machines’ drive, while an alert can still play and fade.
Loops wrap by overlapping their end with their head and resuming after the overlap (loop.blend, 10 to 30 ms for noise): the engine’s offline test measured the largest step at a wrap fall from 1.5 to 0.005 of full scale.
Trigger particle and impact sounds once per burst, at the code that makes the burst.
A missing game value takes the map’s low end, which may be silent (a machine with no level is off).
The engine’s voice limit stopped the voices it dropped on the sample, which clicks (an offline render of a hum: a step of 0.115 of full scale when the limit fell, against 0.0026 in steady playback).
One AudioContext per page, and dispose() closes it.
Obstruction is a callback, so it can be as cheap as it needs: walls as segments (wallObstruction, library), a ridge marched over a heightfield, or a building index’s own segment test.
13 pitfalls in full, the hook-up guide, the module source and its tests come with the part.
Where it comes from
From HaTac. Adapted from published work, named below.
Hannes Andersson: The HaTac engine’s spatial audio, sound recipes and their tests (built for his game and proven there); the library’s additions and demo.
Web Audio API (W3C): AudioBufferSourceNode loop points, PannerNode equal-power panning, BiquadFilterNode, ConvolverNode, DynamicsCompressorNode and AudioParam automation, which the module is built on.
web audiospatial audiopositional audio3d audiopanninglistenerdistance attenuationlow-passocclusionobstructionwallsdoorsmuffledreverbconvolutionroomindoorsoutdoorssound bedsaggregationinstancingvoice budgetvoice limitpriority
An undo/redo history whose steps carry a few words, so the buttons say “Undo Move ‘Dune’”; one drag or one typing session is one step, and so is a drag of a…