Docs / Audio

Audio: volume, tone and time

The volume, three-band equalizer, pitch, delay, fade and time position effects.

Color legend plain property method

These effects change a sound without caring where it is: how loud it is, its tone, its pitch, its echoes, how it starts and ends, and where in the clip it plays. Any number of each can sit in a chain, except the time position effect, of which an emitter has one. Names are colored by what they are: a plain property, a method or an event.

Every setting can change while the sound plays. The audio thread picks the new value up at the start of its next block of samples, a few milliseconds later.

Volume

The volume effect multiplies the sound by Volume, a plain gain: 1 leaves it as it is, 0.5 halves it. The value stays within VolumeRange, from 0 to 1 by default; widen the range to make a sound louder than its clip.

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

var volume = (VolumeAudioEffect)effect;
volume.VolumeRange = (0, 2); // allow boosting up to twice the clip's level
volume.Volume = 1.5f;

For the volume of everything at once, use the manager’s master volume.

VolumeAudioEffect properties

Property Type Description
Volume float The gain, kept within VolumeRange. 1 by default.
VolumeRange (float min, float max) The lowest and highest Volume. Negative ends are taken as 0, and the two ends are put in order. Setting it brings Volume into the new range. (0, 1) by default.

Equalizer

The equalizer has three bands: a low shelf, a peak in the middle and a high shelf. Each band boosts or cuts by its gain in dB, and a band at 0 dB, the default, is switched off and costs nothing.

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

// a voice heard through a wall: no lows, no highs, a honky middle
var eq = (ThreeBandEqualizerAudioEffect)effect;
eq.LowGainDb = -18;
eq.LowFreqHz = 300;
eq.MidGainDb = 6;
eq.MidFreqHz = 1200;
eq.MidQ = 1.5f;
eq.HighGainDb = -18;
eq.HighFreqHz = 3000;
  • Low shelf. Boosts or cuts everything below LowFreqHz. LowSlope sets how steep the step is.
  • Middle peak. Boosts or cuts around MidFreqHz. MidQ sets how narrow: about 0.5 to 1 is a broad, natural change, up to 5 a narrow, pronounced one.
  • High shelf. Boosts or cuts everything above HighFreqHz. HighSlope sets how steep the step is.

Every setting is kept within its limits, which the class gives as static properties for sliders in a settings screen:

Setting Limits Default
LowGainDb, MidGainDb, HighGainDb MinGainDb to MaxGainDb: −24 to 24 dB 0 dB, off
LowFreqHz MinLowFreqHz to MaxLowFreqHz: 20 to 400 Hz 200 Hz
MidFreqHz MinMidFreqHz to MaxMidFreqHz: 200 to 5 000 Hz 1 000 Hz
HighFreqHz MinHighFreqHz to MaxHighFreqHz: 1 000 to 20 000 Hz 6 000 Hz
LowSlope, HighSlope MinSlope to MaxSlope: 0.1 to 2 0.707
MidQ MinQ to MaxQ: 0.3 to 5 1

Changes don’t click: the filters keep their state while their settings move, so a band can follow a slider or an animation.

ThreeBandEqualizerAudioEffect properties

Property Type Description
LowGainDb float The boost or cut of the low shelf, in dB. 0 switches the band off.
LowFreqHz float Where the low shelf starts. 200 Hz by default.
LowSlope float How steep the low shelf’s step is. 0.707 by default.
MidGainDb float The boost or cut of the middle peak, in dB. 0 switches the band off.
MidFreqHz float The center of the middle peak. 1 000 Hz by default.
MidQ float How narrow the middle peak is; higher is narrower. 1 by default.
HighGainDb float The boost or cut of the high shelf, in dB. 0 switches the band off.
HighFreqHz float Where the high shelf starts. 6 000 Hz by default.
HighSlope float How steep the high shelf’s step is. 0.707 by default.
MinGainDb float Static. −24 dB, the lowest band gain.
MaxGainDb float Static. 24 dB, the highest band gain.
MinLowFreqHz float Static. 20 Hz, the lowest LowFreqHz.
MaxLowFreqHz float Static. 400 Hz, the highest LowFreqHz.
MinMidFreqHz float Static. 200 Hz, the lowest MidFreqHz.
MaxMidFreqHz float Static. 5 000 Hz, the highest MidFreqHz.
MinHighFreqHz float Static. 1 000 Hz, the lowest HighFreqHz.
MaxHighFreqHz float Static. 20 000 Hz, the highest HighFreqHz.
MinSlope float Static. 0.1, the gentlest shelf slope.
MaxSlope float Static. 2, the steepest shelf slope.
MinQ float Static. 0.3, the broadest middle peak.
MaxQ float Static. 5, the narrowest middle peak.

Pitch

The pitch effect raises or lowers the pitch by a factor without changing how long the sound lasts: 2 is an octave up, 0.5 an octave down. Small random factors give repeated sounds some variety:

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

var pitch = (PitchAudioEffect)effect;
pitch.Pitch = 0.95f + Random.Shared.NextSingle() * 0.1f; // ±5 %

The shift works on short slices of the sound in the frequency domain, which smears sharp attacks a little, more so the further the factor is from 1. At 1, the effect does nothing and costs nothing.

PitchAudioEffect properties

Property Type Description
Pitch float The pitch factor, from 0.0625 (four octaves down) to 16 (four octaves up). 1 by default, which leaves the sound as it is.

Delay

The delay effect adds an echo: the sound again, DelayMs later. Feedback sends part of each echo back in, for more echoes that fade one after another, and Mix sets how much of the output is echo.

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

// a canyon: a strong echo every 400 ms, fading out
var delay = (DelayAudioEffect)effect;
delay.DelayMs = 400;
delay.Feedback = 0.45f;
delay.Mix = 0.35f;

All three start at 0, so a new delay effect lets the sound through unchanged until you set DelayMs and Mix. A delay of 0 ms switches the effect off. The echoes go on after the clip ends, until they fade out, even when the emitter already reads Finished; Stop cuts them.

DelayAudioEffect properties

Property Type Description
DelayMs float The time between the sound and its echo, from 0 to MaxDelayMs. 0 by default, which switches the effect off.
Feedback float How much of each echo comes back as the next one, from 0 to 1. 0 by default: a single echo.
Mix float How much of the output is echo, from 0 (none) to 1 (only echo). 0 by default.
MaxDelayMs float Static. 3 000 ms, the longest delay.

Fades

The fade effect fades each clip in over its first FadeInDurationMs and out over its last FadeOutDurationMs. It follows the clip’s position, so it fades every clip of the emitter, at every loop, and a clip that starts or jumps into the middle of a fade takes the gain of that point.

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

var fade = (FadeInOutAudioEffect)effect;
fade.FadeInDurationMs = 2000;
fade.FadeOutDurationMs = 3000;
fade.FadeInFunction = ShapingFunctionFactory.Create(ShapingFunctionId.SineEaseInOut);
fade.FadeOutFunction = ShapingFunctionFactory.Create(ShapingFunctionId.SineEaseInOut);

Each fade takes at most half of the clip, so a short clip fades in and straight out again. Both durations are 0 by default: no fade. The shape of a fade is a shaping function from BorrowedTorch.ShapingFunctions, a straight line by default: it gets how far the fade has come, from 0 to 1, and gives the gain. The fade out runs the same shape backwards.

A fade belongs to the clip, not to Pause or Stop, which act at once. To fade a sound out on demand, move a volume effect’s Volume down over a few frames, then stop it.

FadingState tells which part of a fade the clip is in. Its type, FadingStates, is in the BorrowedTorch.Audio.SampleProviders namespace.

FadeInOutAudioEffect properties

Property Type Description
FadeInDurationMs float How long each clip fades in from its start, at most half the clip. Negative values are taken as 0. 0 by default: no fade in.
FadeOutDurationMs float How long each clip fades out before its end, at most half the clip. Negative values are taken as 0. 0 by default: no fade out.
FadeInFunction BaseShapingFunction Turns how far the fade in has come, 0 to 1, into the gain. null sets the default straight line.
FadeOutFunction BaseShapingFunction The same for the fade out, which runs it from 1 back to 0. null sets the default straight line.
FadingState FadingStates Read-only. Whether the last block of sound was fading in, fading out, or neither.

FadingStates

Value Meaning
None The clip plays at full level.
FadingIn The clip is in its fade in.
FadingOut The clip is in its fade out.

Time position

The time position effect reads and moves the playing position of the current clip, for a music player’s progress bar or to start an ambience somewhere in the middle. It works only while it is connected in the chain, with both an input and an output; anywhere in the chain will do.

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

var time = (TimePositionAudioEffect)effect;
time.Input = emitter.EffectLoopSend;
emitter.EffectLoopReturn.Input = time;

emitter.Play();
time.TimePosition = TimeSpan.FromSeconds(30); // jump in

The jump happens at the start of the next block of samples. A position past the end of the clip takes it to its end. IAudioEmitter.TimePosition reads the same position without the effect.

TimePositionAudioEffect properties

Property Type Description
TimePosition TimeSpan How far the current clip has played. Setting it moves the clip there, within its length. Reads zero and ignores sets while the effect isn’t connected on both sides, or the emitter holds no clip.