Skip to content
Work in progress. These docs describe Spawnite at launch, and some parts are still being built.

Sound

<Sound url> plays a sound file inside a Landscape from when it mounts until the file ends or the component unmounts. Where you mount it decides where the player hears it from:

  • Inside an Entity, the sound comes from the entity and grows quieter with distance from the camera.
  • With position={[x, y, z]}, it comes from that point: a place no entity of its own stands at, such as a machine in a view.
  • Anywhere else, the player hears it the same everywhere: an ambience, or a sting at the end of a match.

A placed sound plays at its full volume within minDistance metres of the camera, 1 when left out. Left at that, it fades by the inverse of its distance and never quite stops. Give maxDistance and it fades in a straight line to silence there, so a far machine is not heard at all. Unity’s AudioSource names the two the same, and Godot’s AudioStreamPlayer3D calls them unit_size and max_distance. Set them from where your camera stands: a camera 30 m above the ground hears little from a sound at the default 1 m.

Its props are in SoundProps.

Roblox’s Sound works the same way: parented to a part it plays from there, and parented anywhere else it plays everywhere. Unity’s AudioSource and Godot’s AudioStreamPlayer3D are the placed kind; Godot’s AudioStreamPlayer is the everywhere kind.

A browser, a phone’s included, keeps a page silent until the player presses a key, clicks or touches the screen. A Sound mounted before that input starts playing on it. A one-off sound that a pickup or a footstep plays before that input does not play.

Give fadeInSeconds to bring a loop up from silence, such as rain as a storm opens. The fade counts on the audio clock from when the sound starts, so a file that loads slowly, or a page waiting for that first input, still plays the whole fade. Unreal’s UAudioComponent::FadeIn takes the same duration.

Give fadeOutSeconds to let a loop fall away rather than cut, such as rain as a storm ends. When the Sound unmounts, or its url changes, it falls from its level to silence over those seconds on the audio clock, then stops. The new file plays as soon as it loads, while the old one fades. Unreal’s UAudioComponent::FadeOut is the same; Unity, Godot, Roblox and Phaser leave it to a tween on the volume.

packages/engine/test/outside/RainLoop.tsx
import { Sound } from "@spawnite/engine";
export interface RainLoopProps {
/** The rain's file, a `?url` import or one of `sounds`. */
url: string;
isRaining: boolean;
}
// Rain heard everywhere while it rains: it rises over three seconds as
// the rain starts and falls away over three as it stops.
export function RainLoop({ url, isRaining }: RainLoopProps) {
if (!isRaining) return null;
return (
<Sound
url={url}
loop
volume={0.4}
fadeInSeconds={3}
fadeOutSeconds={3}
/>
);
}

An iPhone with its ring switch set to silent plays no page sound, a game’s included, until the player sets it back to ring.

A browser with no Web Audio, or one that cannot start it, runs the game silent rather than stopping it. Sound mounts nothing, and footsteps and pickups play nothing. The console says so once: Web Audio is unavailable in this browser, so the game runs silent, followed by the browser’s own error. The game needs no change. Other players’ voices still play, through an audio element, up to 100% volume, as voice says.

Two sounds need no Sound of their own:

  • <CharacterSpawn footsteps> takes a list of files and plays one of them at random for each stride the character walks. A stride is 1.67 metres, a third of its top speed of 5 m/s, which matches the three steps a second of its run.
  • <Pickup sound> plays its file once, heard everywhere, as the character takes the pickup. It does not play when the scene ends around a pickup nobody took.

Every sound the page plays goes through one mixer the engine owns: each <Sound>, every footstep and pickup, and the other players’ voices. It has a master and a bus for each of the player’s volume sliders, as Unity’s audio mixer has groups, Godot has buses and Roblox has sound groups:

a sound ─▶ bus input ─▶ gain ─▶ effects ─▶ the player's volume ─▶ master ─▶ speakers
  • <Sound bus> picks the bus a sound plays into: AudioBusName.Effects when left out, Music, Interface or Voice. A JSX prop also takes the string, such as bus="music".
  • findAudioMixer() answers the page’s mixer, { context, master, buses }, or null on a page with no Web Audio and on a room’s process.
  • Each bus, the master among them, has an input a game connects a sound of its own to, and a gain that is the game’s level for the bus, 1 until the game writes it. Its addEffect(effect) puts a Web Audio node at the end of the bus’s effects and returns what takes it out again.
  • The player’s volume slider for each bus comes after its effects, and the game never reaches it. A node connected to a bus’s input follows the player’s volume with no code of its own.

The gain comes before the effects, so a limiter on the master holds any boost a game writes to a gain.

A node takes one place in the mixer at a time: adding one the mixer holds already, on any bus, throws and names where it is. An effect made of several nodes, such as a reverb that mixes its wet path with the dry, passes its two ends as { input, output }.

The context is the engine’s. A game never closes or suspends mixer.context, since the loading screen holds the page’s sound through it. As a Game unmounts, every bus’s gain returns to 1 and the effects a game left on the mixer come off, so a hot reload starts from a clean mix.

packages/engine/test/outside/underwater.ts
import {
AudioBusName,
findAudioMixer,
useVoiceSession,
type VoiceSessionState,
} from "@spawnite/engine";
/** Hertz above which the water swallows the sound. */
const waterCutoff = 600;
/** Muffles the effects and the music, as heard under water, until the
* returned call lifts it. Voices and the menu stay clear. */
export function muffleUnderWater(): () => void {
const mixer = findAudioMixer();
if (!mixer) return () => undefined;
const lifts = [AudioBusName.Effects, AudioBusName.Music].map((name) => {
const lowPass = mixer.context.createBiquadFilter();
lowPass.type = "lowpass";
lowPass.frequency.value = waterCutoff;
return mixer.buses[name].addEffect(lowPass);
});
return () => {
for (const lift of lifts) lift();
};
}
/** Keeps the whole mix under full scale, however loud a crowd of hits
* gets: a limiter on the master. */
export function limitMaster() {
const mixer = findAudioMixer();
if (!mixer) return () => undefined;
const limiter = mixer.context.createDynamicsCompressor();
limiter.threshold.value = -3;
limiter.knee.value = 0;
limiter.ratio.value = 20;
return mixer.master.addEffect(limiter);
}
/** The music's level while someone speaks: a quarter, about 12 dB down. */
const duckedLevel = 0.25;
/** Lowers the music while any other player speaks and raises it again
* after, until the returned call stops it and gives the music back. */
export function duckMusicUnderVoice(): () => void {
const mixer = findAudioMixer();
if (!mixer) return () => undefined;
const { gain } = mixer.buses[AudioBusName.Music];
let level = 1;
// The voice store changes many times a second; the music moves only as
// someone starts or stops speaking.
const follow = ({ speakers }: VoiceSessionState) => {
const next = speakers.length > 0 ? duckedLevel : 1;
if (next === level) return;
level = next;
gain.cancelScheduledValues(mixer.context.currentTime);
gain.setTargetAtTime(level, mixer.context.currentTime, 0.1);
};
follow(useVoiceSession.getState());
const stop = useVoiceSession.subscribe(follow);
return () => {
stop();
gain.cancelScheduledValues(mixer.context.currentTime);
gain.value = 1;
};
}

The game decides when the player’s character is under water, such as its camera below the surface, and calls the muffle then: the engine has no water volume that switches a bus. The duck owns the music bus’s gain while it runs, so a game that also fades its music does it on a gain node of its own, added with addEffect. The voice bus keeps a limiter of its own at its tail, after the voice volume, so a game’s limiter on the master is for the whole mix.

A video’s or an audio element’s sound joins the mixer through mixer.context.createMediaElementSource(element), connected to a bus’s input, so it follows the player’s volume too.

The engine ducks nothing on its own: a game that lowers its music while someone speaks ramps the music bus’s gain as the sample does, as Unity’s Duck Volume and Godot’s sidechain compressor are each a creator’s choice there.

sounds names the files in the shared asset library, which ship with the engine, and says what each one is for. Assets says where to find more, and how to check a downloaded sound for hiss.

import {
CharacterSpawn,
Entity,
Pickup,
Sound,
sounds,
Landscape,
} from "@spawnite/engine";
export function Run() {
return (
<Landscape map="meadow">
<CharacterSpawn />
<Sound url="/sounds/rain.mp3" loop volume={0.5} />
<Entity position={[0, 0, -3]}>
<Pickup reward={1} sound={sounds.pickup} />
</Entity>
</Landscape>
);
}

Client. Sound changes nothing in the world; each player hears their own. Removing a game’s frame frees its audio with everything else it held, as the trust boundary says.