AudioSDL 3.2

SDL_SetAudioStreamGain

Sets a stream's volume: 1.0 unchanged, 0.5 half, 0.0 silent.

Signature

bool SDL_SetAudioStreamGain(SDL_AudioStream *stream, float gain);

How it works

The gain multiplies every sample. Give music and effects separate streams and you get separate volume sliders for free.

Our ears hear loudness roughly logarithmically, so a slider feels more even if it maps to gain as slider² or through decibels (gain = 10^(dB/20)) instead of linearly.

Parameters

streamSDL_AudioStream *

The stream.

gainfloat

Multiplier, 0.0 = silence, 1.0 = default. Above 1.0 is louder (and may clip).

Return value

true on success, false on failure.

Example

example.cpp
1SDL_SetAudioStreamGain(music, settings.musicVolume * settings.musicVolume);
2SDL_SetAudioStreamGain(sfx, 1.0f);
Official SDL wiki