Docs / Audio

Audio: positional sound

Distance attenuation and air absorption, Doppler, HRTF spatialization, HRTF data sets and demo mode.

Color legend plain property method

Three effects place a sound in the world: the distance effect makes it quieter and duller the further it is, the Doppler effect bends its pitch while it moves, and the spatial effect makes it come from its direction. Each one reads the emitter’s Position and the listener, and follows them when they change. An emitter has at most one of each. Names are colored by what they are: a plain property, a method or an event.

Positions are in meters: the speed of sound, air absorption and the distances in HRTF data sets all assume it.

Distance

The distance effect lowers the volume and closes a low-pass filter as the emitter moves away from the listener, until the sound is gone at its audible distance. Set how loud the sound is, and the effect works out how far it carries:

emitter.TryAddAudioEffect(AudioEffectTypes.Distance, out var effect);

var distance = (DistanceAudioEffect)effect;
distance.LoudnessDb = 40;                       // carries about 95 m
Console.WriteLine(distance.MaxAudibleDistanceMeters);

LoudnessDb is the level of the sound one meter away. The audible distance is where that level, falling by 6 dB for every doubling of distance and by the air absorption for every meter, reaches the hearing threshold. With the default environment:

LoudnessDb Audible up to For example
20 10 m A whisper, a ticking clock
30 31 m A campfire, footsteps
40 95 m Voices, a door
50 271 m A shout, an engine
60 677 m A bell, an explosion far off

LoudnessDb is 0 by default, which makes the sound fall silent within a meter, so always set it.

Between the listener and the audible distance, two curves shape the sound. Both take how close the emitter is, from 1 at the listener to 0 at the audible distance, and give a value from 0 to 1:

The curves are shaping functions from BorrowedTorch.ShapingFunctions. A curve that falls faster near the listener sounds closer to real life than the straight line:

using BorrowedTorch.ShapingFunctions;

// quiet soon, then a long faint tail
distance.DistanceVolumeAttenuationFunction =
    ShapingFunctionFactory.Create(ShapingFunctionId.QuadraticEaseIn);

// stays bright longer, then dulls near the edge
distance.DistanceCutoffAttenuationFunction =
    ShapingFunctionFactory.Create(ShapingFunctionId.SineEaseOut);
distance.DistanceMinimumCutoffHz = 400;

The environment

Two static settings of AudioManager, shared by every manager, describe the air and the ears:

  • HearingMinimumDb. The level below which nothing is heard, 0 dB by default. Raise it for a noisy place where quiet sounds get lost sooner.
  • AirSoundAbsorptionCoefficient. How much the air takes away, in dB per meter, 0.005 by default. Raise it for fog or a thick forest.

A distance effect works out its audible distance when its LoudnessDb is set, so change the environment before you set the loudness of your sounds, or set it again afterwards.

DistanceAudioEffect properties

Property Type Description
LoudnessDb float The level of the sound one meter away, in dB. Sets MaxAudibleDistanceMeters. Negative values are taken as 0. 0 by default, so the sound is silent past one meter until you set it.
MaxAudibleDistanceMeters float Read-only. How far the sound carries: where its level falls to HearingMinimumDb, with the air absorption. Worked out when LoudnessDb is set.
DistanceVolumeAttenuationFunction BaseShapingFunction Turns how close the emitter is, 1 at the listener to 0 at the audible distance, into the gain. null sets the default straight line.
DistanceCutoffAttenuationFunction BaseShapingFunction Turns how close the emitter is into the low-pass cutoff, from DistanceMinimumCutoffHz at 0 to DistanceMaximumCutoffHz at 1. null sets the default straight line.
DistanceMinimumCutoffHz float The cutoff at the audible distance, from 0 to 20 000 Hz. 100 Hz by default. Setting it above the maximum raises the maximum too.
DistanceMaximumCutoffHz float The cutoff at the listener, from 0 to 20 000 Hz. 20 000 Hz by default, which leaves a close sound open. Setting it below the minimum lowers the minimum too.
IsDemoMode bool Measures the distance from DemoEmitterPosition to the origin instead of from the emitter to the listener. See demo mode.
DemoEmitterPosition Vector3 Where the sound is in demo mode, around a listener at the origin.

Doppler

The Doppler effect raises the pitch of a sound that comes closer and lowers that of one that moves away, as a passing car does. It needs nothing but to be in the chain:

emitter.TryAddAudioEffect(AudioEffectTypes.Doppler, out var doppler);

It measures the speeds of the listener and the emitter from how far they moved between two AudioManager.Update calls, so it needs the real frame time in Update. Only the speed along the line between them counts, and both speeds are held below 90 % of the speed of sound, 343 m/s. While neither moves along that line, the effect leaves the sound untouched and costs nothing.

A jump, such as a teleport or a respawn, reads as a very fast move and bends the pitch for one frame.

DopplerAudioEffect properties

Property Type Description
IsDemoMode bool Measures the emitter’s speed from the moves of DemoEmitterPosition, around a listener standing still at the origin. See demo mode.
DemoEmitterPosition Vector3 Where the sound is in demo mode. Move it between updates to hear the effect.

Spatialization

The spatial effect makes a sound come from its direction, in front or behind, left or right, above or below, as headphones would carry it from a real source. It uses an HRTF, a head-related transfer function: for each direction, a pair of short filters, one per ear, measured on a real or modeled head. The effect finds the emitter’s direction in the listener’s frame, blends the filters of the nearest measured directions, and filters the sound for each ear with the right delay between them.

emitter.TryAddAudioEffect(AudioEffectTypes.Spatial, out var effect);

var spatial = (SpatialAudioEffect)effect;
spatial.AudioSpread = 0.3f; // a large source, such as a waterfall

The sound is mixed down to mono first, so a stereo clip loses its own stereo image: give positional sounds in mono. AudioSpread widens the source, from a point at 0, the default, to a sound heard alike from everywhere at 1. An emitter exactly at the listener’s position passes through unchanged.

HRTF is made for headphones. On speakers it still places sounds left and right, but front, back and height are less clear.

HRTF data sets

The filters come from an HRTF data set in OpenAL Soft’s .mhr format, version MinPHR03, as written by OpenAL Soft’s makemhr. The package embeds a default set, which the manager loads unless you give it another one:

// at creation
var audio = new AudioManager("Content/Audio/hrtf/my-head.mhr");

// or later, for every spatial effect at once
if (!audio.TryLoadMhrFile("Content/Audio/hrtf/other-head.mhr"))
    Console.WriteLine("Not an MHR data set: spatial effects pass sound through");

A data set is resampled to 48 kHz when it loads. When the path is null or the file can’t be opened, the manager loads the embedded set and MhrFilePath is null. When the file is there but isn’t a valid data set, TryLoadMhrFile returns false and no set is loaded at all: IsHrtfDataLoaded is false, and spatial effects pass sound through unchanged until a set loads. For a data set measured at several distances, each emitter uses the measurement that fits its own distance.

AudioManager HRTF and environment properties

Property Type Description
IsHrtfDataLoaded bool Read-only. Whether an HRTF data set is loaded. Without one, spatial effects pass sound through.
MhrFilePath string Read-only. The file the loaded data set came from, null when it is the embedded set or none is loaded.
HearingMinimumDb float Static. The level below which nothing is heard, in dB. 0 by default. Distance effects use it when their LoudnessDb is set.
AirSoundAbsorptionCoefficient float Static. How much the air absorbs, in dB per meter. 0.005 by default. Distance effects use it when their LoudnessDb is set.

AudioManager HRTF methods

Method Description
TryLoadMhrFile(string path) Loads an HRTF data set for every spatial effect: the file at the path, or the embedded set when the path is null or can’t be opened. false when the file isn’t a valid data set; no set is loaded then.

SpatialAudioEffect properties

Property Type Description
AudioSpread float How wide the source is, from 0, a point, to 1, heard alike from everywhere. 0 by default.
IsDemoMode bool Places the sound at DemoEmitterPosition around a listener at the origin facing −Z, with +Y up and +X right. See demo mode.
DemoEmitterPosition Vector3 Where the sound is in demo mode.

Demo mode

The distance, Doppler and spatial effects each have a demo mode that ignores the game: the listener stands at the origin facing −Z, with +Y up and +X right, and the sound is at the effect’s DemoEmitterPosition. Use it to preview a sound in an editor, such as moving it around a head with a slider, without a scene or a camera.

var distance = (DistanceAudioEffect)distanceEffect;
var doppler = (DopplerAudioEffect)dopplerEffect;
var spatial = (SpatialAudioEffect)spatialEffect;

distance.IsDemoMode = true;
doppler.IsDemoMode = true;
spatial.IsDemoMode = true;

// two meters to the right and a little ahead, for all three
var position = new Vector3(2, 0, -0.5f);
distance.DemoEmitterPosition = position;
doppler.DemoEmitterPosition = position;
spatial.DemoEmitterPosition = position;

Each effect has its own switch and its own position, so set them together. The Doppler effect still needs AudioManager.Update to measure the moves of its demo position.